📄 main.cpp
字号:
// ping3D.cpp : Defines the entry point for the console application.
//
//c header files
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "math.h"
#include "time.h"
//include GLUT header file
#include "glew.h"
#include "glut.h"
#include "FreeImage.h"
const double PI = 3.14159265358979323846;
int g_frameCount = 0;
GLuint g_displayList = 0;
GLuint tableID;
GLuint courtID;
GLuint lightID;
GLuint shadowID;
//global variables for drawing area size
int g_width = 1000;
int g_height = 1000;
//global camera parameters
double g_eye[3] = {0, 90, 170};
double g_center[3] = {0, 25, 0};
double g_up[3] = {0, 1, 0};
//global color vectors
float g_red[3] = {1.0f, 0.0f, 0.0f};
float g_green[3] = {0.0f, 1.0f, 0.0f};
float g_blue[3] = {0.0f, 0.0f, 1.0f};
float g_white[3] = {1.0f, 1.0f, 1.0f};
//point light
float ambient0[] = {0.2f, 0.2f, 0.6f, 1.0f};
float diffuse0[] = {0.3f, 0.3f, 0.3f, 1.0f};
float specular0[] = {1.0f, 0.0f, 0.0f, 1.0f};
float position0[] = {0.0f, 50.0f, 0.0f, 1.0f};
//directional light
float ambient1[] = {0.5f, 0.75f, 0.75f, 1.0f};
float diffuse1[] = {0.0f, 0.75f, 1.0f, 1.0f};
float specular1[] = {0.0f, 1.0f, 1.0f, 1.0f};
float position1[] = {20.0f, 10.0f, 30.0f, 0.0f};
//spot light
float ambient2[] = {0.5f, 0.0f, 0.0f, 1.0f};
float diffuse2[] = {0.25f, 0.75f, 1.0f, 1.0f};
float specular2[] = {1.0f, 0.0f, 0.0f, 1.0f};
float position2[] = {0.0f, 50.0f, 300.0f, 1.0f};
float spotDirection[]={0.0f, 0.0f, -1.0f};
float spotCutOff = 20.0f;
float spotExp = 0.2f;
//global parameters for walls
float g_wallHalfThickness = 2.0f;
float g_blockSize[3] = {100.0f, 100.0f, 100.0f};
float g_wallCenter[6][3];
float g_wallSize[6][3];
float g_wallNormal[6][3];
//global ball parameters
float g_ballPosition[3] = {0.0f, 50.0f, 0.0f};
float g_lastBallPosition[3] = {0.0f, 50.0f, 0.0f};
float g_ballVector[3];
float g_trajectory[3] = {1.5f, 0.6f, -0.7f};
//global ball movement parameters
float g_step = 0.08f;
float g_lastStep = 0.06f;
float g_damper = 0.99645f;
float g_reflect = 0.25f;
float g_maxStep = 0.12f;
float g_minStep = 0.055f;
//global behavior control
bool g_speedControl = false;
bool g_changeColorOnHit = true;
bool g_rotateCamera = false;
//which wall we just hit?
int g_wallHit = -1;
int g_gameOver = 1;
int g_currentGame = 0;
int g_misses = 0; /* to keep track of the g_misses */
int g_leftButtonState = GLUT_UP;
int g_leftButtonLastX;
int g_leftButtonLastY;
int threshold = 5;
float g_wallHitColor[6][3] = {
{1.0f, 0.25f, 0.75f},
{0.0f, 1.0f, 0.0f},
{0.0f, 0.0f, 1.0f},
{1.0f, 1.0f, 0.0f},
{1.0f, 0.0f, 1.0f},
{0.0f, 1.0f, 1.0f}
};
void LoadTexture(char *pFileName, GLuint texID)
{
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(pFileName, 0);
FIBITMAP* image = FreeImage_Load(format, pFileName);
FIBITMAP* temp = image;
image = FreeImage_ConvertTo32Bits(image);
FreeImage_Unload(temp);
int w = FreeImage_GetWidth(image);
int h = FreeImage_GetHeight(image);
GLubyte* texture = new GLubyte[4*w*h];
char* pixels = (char*)FreeImage_GetBits(image);
for(int j= 0; j<w*h; j++)
{
texture[j*4+0]= pixels[j*4+2];
texture[j*4+1]= pixels[j*4+1];
texture[j*4+2]= pixels[j*4+0];
texture[j*4+3]= pixels[j*4+3];
}
FreeImage_Unload(image);
//Now generate the OpenGL texture object
glBindTexture(GL_TEXTURE_2D, texID);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_RGBA,GL_UNSIGNED_BYTE,(GLvoid*)texture );
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
delete [ ]texture;
}
//GLuint tableID;GLuint courtID;GLuint lightID;GLuint shadowID;
void InitTextures()
{
glGenTextures(1, &tableID);
glGenTextures(1, &courtID);
glGenTextures(1, &lightID);
glGenTextures(1, &shadowID);
LoadTexture("table.bmp", tableID);
LoadTexture("court.bmp", courtID);
LoadTexture("lightmap.bmp", lightID);
LoadTexture("shadowmap.bmp", shadowID);
}
void drawString(char* pText, int x, int y)
{
glRasterPos2i(x, y);
for (unsigned int i = 0; i < strlen(pText); i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, pText[i]);
}
//normalize a vector
void normalize(float* v)
{
float length = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
length= sqrtf(length);
if (length > 0.0f)
{
float inv = 1.0f/length;
v[0] *= inv;
v[1] *= inv;
v[2] *= inv;
}
}
//setup random initial ball movement vector
void initBallVector()
{
g_ballVector[0] = g_trajectory[0];
g_ballVector[1] = g_trajectory[1];
g_ballVector[2] = g_trajectory[2];
normalize(g_ballVector);
}
//prepare wall coordinates, size and normals
void initWall()
{
//front
g_wallCenter[0][0] = 0.0f;
g_wallCenter[0][1] = g_blockSize[1]*0.5f;
g_wallCenter[0][2] = g_blockSize[2]*0.5f;
g_wallSize[0][0] = g_blockSize[0]*0.15f;
g_wallSize[0][1] = g_blockSize[1]*0.15f;
g_wallSize[0][2] = g_wallHalfThickness;
g_wallNormal[0][0] = 0.0f;
g_wallNormal[0][1] = 0.0f;
g_wallNormal[0][2] = -1.0f;
//back
g_wallCenter[1][0] = 0.0f;
g_wallCenter[1][1] = g_blockSize[1]*0.5f;
g_wallCenter[1][2] = -g_blockSize[2]*0.5f;
g_wallSize[1][0] = g_blockSize[0]*0.5f;
g_wallSize[1][1] = g_blockSize[1]*0.5f;
g_wallSize[1][2] = g_wallHalfThickness;
g_wallNormal[1][0] = 0.0f;
g_wallNormal[1][1] = 0.0f;
g_wallNormal[1][2] = 1.0f;
//left
g_wallCenter[2][0] = -g_blockSize[0]*0.5f;
g_wallCenter[2][1] = g_blockSize[1]*0.5f;
g_wallCenter[2][2] = 0.0f;
g_wallSize[2][2] = g_blockSize[2]*0.5f;
g_wallSize[2][1] = g_blockSize[1]*0.5f;
g_wallSize[2][0] = g_wallHalfThickness;
g_wallNormal[2][0] = 1.0f;
g_wallNormal[2][1] = 0.0f;
g_wallNormal[2][2] = 0.0f;
//right
g_wallCenter[3][0] = g_blockSize[0]*0.5f;
g_wallCenter[3][1] = g_blockSize[1]*0.5f;
g_wallCenter[3][2] = 0.0f;
g_wallSize[3][2] = g_blockSize[2]*0.5f;
g_wallSize[3][1] = g_blockSize[1]*0.5f;
g_wallSize[3][0] = g_wallHalfThickness;
g_wallNormal[3][0] = -1.0f;
g_wallNormal[3][1] = 0.0f;
g_wallNormal[3][2] = 0.0f;
//top
g_wallCenter[4][0] = 0.0f;
g_wallCenter[4][1] = g_blockSize[1];
g_wallCenter[4][2] = 0.0f;
g_wallSize[4][2] = g_blockSize[2]*0.5f;
g_wallSize[4][0] = g_blockSize[0]*0.5f;
g_wallSize[4][1] = g_wallHalfThickness;
g_wallNormal[4][0] = 0.0f;
g_wallNormal[4][1] = -1.0f;
g_wallNormal[4][2] = 0.0f;
//bottom
g_wallCenter[5][0] = 0.0f;
g_wallCenter[5][1] = 0.0f;
g_wallCenter[5][2] = 0.0f;
g_wallSize[5][2] = g_blockSize[2]*0.5f;
g_wallSize[5][0] = g_blockSize[0]*0.5f;
g_wallSize[5][1] = g_wallHalfThickness;
g_wallNormal[5][0] = 0.0f;
g_wallNormal[5][1] = 1.0f;
g_wallNormal[5][2] = 0.0f;
}
void drawBlock3D(float* pCenter, float* pHalfLength)
{
float vertices[8][3];
//normals for six quads of each block
float normals[6][3]={
{0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f},
{0.0f, -1.0f, 0.0f},
{1.0f, 0.0f, 0.0f},
{-1.0f, 0.0f, 0.0f},
{0.0f, 1.0f, 0.0f}
};
int flags[8][3] = {
{-1, -1, -1},
{1, -1, -1},
{1, 1, -1},
{-1, 1, -1},
{-1, -1, 1},
{1, -1, 1},
{1, 1, 1},
{-1, 1, 1}
};
//generate 8 vertices based on center position, half length in x, y, z direction, and flags
for (int i = 0; i < 8; i++)
{
vertices[i][0] = pCenter[0] + pHalfLength[0]*flags[i][0];
vertices[i][1] = pCenter[1] + pHalfLength[1]*flags[i][1];
vertices[i][2] = pCenter[2] + pHalfLength[2]*flags[i][2];
}
//indices of 4 vertices for one quad
int indices[6][4] = {
{3, 2, 1, 0},
{4, 5, 6, 7},
{0, 1, 5, 4},
{6, 5, 1, 2},
{0, 4, 7, 3},
{7, 6, 2, 3}
};
glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
float base = sin(g_frameCount*PI/18000.0f) * 0.95;
//draw 3D block using quads
glBegin(GL_QUADS);
for (int i = 0; i < 6; i++)
{
glNormal3f(normals[i][0], normals[i][1], normals[i][2]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vertices[indices[i][0]]);
glTexCoord2f(1.0f + base, 0.0f);
glVertex3fv(vertices[indices[i][1]]);
glTexCoord2f(1.0f + base, 1.0f + base);
glVertex3fv(vertices[indices[i][2]]);
glTexCoord2f(0.0f, 1.0f + base);
glVertex3fv(vertices[indices[i][3]]);
}
glEnd();
}
//draw walls
void drawWall()
{
glColor3fv(g_blue);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
drawBlock3D(g_wallCenter[0], g_wallSize[0]);
for (int i = 1; i < 6; i++)
{
glColor3fv(g_white);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
if (g_wallHit == i && g_changeColorOnHit)
{
glColor3fv(g_wallHitColor[g_wallHit]);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
drawBlock3D(g_wallCenter[i], g_wallSize[i]);
}
}
//calculate reflection vector r based on incoming vector v and plane normal n
void reflection(float* v, float* n, float* r)
{
float vDOTn = v[0]*n[0] + v[1]*n[1] + v[2]*n[2];
r[0] = v[0] - 2*vDOTn*n[0];
r[1] = v[1] - 2*vDOTn*n[1];
r[2] = v[2] - 2*vDOTn*n[2];
normalize(r);
}
//calculate point to plane distance
float pointPlaneDistance(float* point, float* planeOrigin, float* planeNormal)
{
float dist = 0.0f;
float a, b, c, d;//plane equation: a*x + b*y + c*z + d = 0
a = planeNormal[0];
b = planeNormal[1];
c = planeNormal[2];
d = -planeNormal[0]*planeOrigin[0] - planeNormal[1]*planeOrigin[1] - planeNormal[2]*planeOrigin[2];
dist = a*point[0] + b*point[1] + c*point[2] + d;
return dist;
}
float pointPlaneIntersection(float* p1, float* p2, float* po, float* pn)
{
float a, b, c, d;//plane equation
a = pn[0];
b = pn[1];
c = pn[2];
d = -pn[0]*po[0] - pn[1]*po[1] - pn[2]*po[2];
float u, denominator;
u = (a*p1[0] + b*p1[1] + c*p1[2] + d);
denominator = a*(p1[0] - p2[0]) + b*(p1[1] - p2[1]) + c*(p1[2] - p2[2]);
if (fabs(denominator) > 0.00001f)
return u/denominator;
else
return -1.0f;
//1. P = P1 + u (P2 - P1) line equation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -