📄 main.cpp
字号:
// 确定是否有UVW纹理坐标
if(pObject->pTexVerts) {
glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
}
} else {
if(Model.pMaterials.size() && pObject->materialID >= 0)
{
BYTE *pColor = Model.pMaterials[pObject->materialID].color;
glColor3ub(pColor[0], pColor[1], pColor[2]);
}
}
glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
}
}
glEnd(); // 绘制结束
}
}
////////////////////////// 绘制模型结束 ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// Font Begin /////////////////////////////////////////////
GLuint base; // Base Display List For The Font Set
GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
HFONT oldfont; // Used For Good House Keeping
base = glGenLists(96); // Storage For 96 Characters
font = CreateFont( -10, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Arial"); // Font Name
oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want
wglUseFontBitmaps(hDC, 32, 96, base); // Builds 96 Characters Starting At Character 32
SelectObject(hDC, oldfont); // Selects The Font We Want
DeleteObject(font); // Delete The Font
}
GLvoid KillFont(GLvoid) // Delete The Font List
{
glDeleteLists(base, 96); // Delete All 96 Characters
}
GLvoid glPrint(const char *fmt, ...) // Custom GL "Print" Routine
{
char text[256]; // Holds Our String
va_list ap; // Pointer To List Of Arguments
if (fmt == NULL) // If There's No Text
return; // Do Nothing
va_start(ap, fmt); // Parses The String For Variables
vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
va_end(ap); // Results Are Stored In Text
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(base - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Font End ////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// 精准的计时器 ////////////////////////////////////////
float GetTime()
{
static bool init = false;
static bool hires = false;
static __int64 freq = 1;
if(!init)
{
hires = !QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
if(!hires)
freq = 1000;
init = true;
}
__int64 now;
if(hires)
QueryPerformanceCounter((LARGE_INTEGER *)&now);
else
now = GetTickCount();
return (float)((double)now / (double)freq);
}
float last_time(0.0f);
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// 精准的计时器结束 ////////////////////////////////////
/////////// 鼠标调整视野 /////////////////
float angle_x(45.0f);
float angle_y(45.0f);
void AnjustViewByMouse()
{
static int mouse_x=0;
static int mouse_y=0;
if(fullscreen == true)
{
POINT newpos;
GetCursorPos(&newpos);
angle_x+=(newpos.y - mouse_y)/360.0f;
angle_y+=(newpos.x - mouse_x)/360.0f;
//confine the movement
if(angle_x>3.14159f/2.0f) angle_x=3.14159f/2.0f;
if(angle_x<-3.14159f/2.0f) angle_x=-3.14159f/2.0f;
//newpos.x=320;
//newpos.y=240;
SetCursorPos(320, 240);
}
if(GetAsyncKeyState(VK_LBUTTON))
{
POINT newpos;
GetCursorPos(&newpos);
angle_x+=(newpos.y - mouse_y)/360.0f;
angle_y+=(newpos.x - mouse_x)/360.0f;
//confine the movement
if(angle_x>3.14159f/2.0f) angle_x=3.14159f/2.0f;
if(angle_x<-3.14159f/2.0f) angle_x=-3.14159f/2.0f;
}
POINT pos;
GetCursorPos(&pos);
mouse_x=pos.x;
mouse_y=pos.y;
}
/////////// 鼠标调整视野结束 /////////////
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,3000.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
float lightrate(0.0f);
void CubeBox()
{
glColor3f(lightrate, lightrate, lightrate);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, cubeface[0]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(1000, 1000, 1000);
glTexCoord2i(0, 0); glVertex3i(1000, -1000, 1000);
glTexCoord2i(1, 0); glVertex3i(1000, -1000, -1000);
glTexCoord2i(1, 1); glVertex3i(1000, 1000, -1000);
glEnd();
glBindTexture(GL_TEXTURE_2D, cubeface[1]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(-1000, 1000, -1000);
glTexCoord2i(0, 0); glVertex3i(-1000, -1000, -1000);
glTexCoord2i(1, 0); glVertex3i(-1000, -1000, 1000);
glTexCoord2i(1, 1); glVertex3i(-1000, 1000, 1000);
glEnd();
glBindTexture(GL_TEXTURE_2D, cubeface[2]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(-1000, 1000, -1000);
glTexCoord2i(0, 0); glVertex3i(-1000, 1000, 1000);
glTexCoord2i(1, 0); glVertex3i(1000, 1000,1000);
glTexCoord2i(1, 1); glVertex3i(1000, 1000, -1000);
glEnd();
glBindTexture(GL_TEXTURE_2D, cubeface[3]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(-1000, -1000, 1000);
glTexCoord2i(0, 0); glVertex3i(-1000, -1000, -1000);
glTexCoord2i(1, 0); glVertex3i(1000, -1000,-1000);
glTexCoord2i(1, 1); glVertex3i(1000, -1000, 1000);
glEnd();
glBindTexture(GL_TEXTURE_2D, cubeface[4]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(-1000, 1000, 1000);
glTexCoord2i(0, 0); glVertex3i(-1000,-1000, 1000);
glTexCoord2i(1, 0); glVertex3i(1000, -1000, 1000);
glTexCoord2i(1, 1); glVertex3i(1000, 1000, 1000);
glEnd();
glBindTexture(GL_TEXTURE_2D, cubeface[5]);
glBegin(GL_QUADS);
glTexCoord2i(0, 1); glVertex3i(1000, 1000, -1000);
glTexCoord2i(0, 0); glVertex3i(1000, -1000, -1000);
glTexCoord2i(1, 0); glVertex3i(-1000, -1000, -1000);
glTexCoord2i(1, 1); glVertex3i(-1000, 1000, -1000);
glEnd();
glDisable(GL_TEXTURE_2D);
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
//得到显卡信息
brand=(char*)glGetString(GL_RENDERER);
vendor=(char*)glGetString(GL_VENDOR);
version=(char*)glGetString(GL_VERSION);
BuildFont(); //建立字体
//读环境的纹理
BuildTexture("lobbyxpos.jpg", cubeface[0]);
BuildTexture("lobbyxneg.jpg", cubeface[1]);
BuildTexture("lobbyypos.jpg", cubeface[2]);
BuildTexture("lobbyyneg.jpg", cubeface[3]);
BuildTexture("lobbyzpos.jpg", cubeface[4]);
BuildTexture("lobbyzneg.jpg", cubeface[5]);
BuildTexture("Particle.bmp", particle);
//设置灯光
return TRUE; // Initialization Went OK
}
void ViewOrtho() // Set Up An Ortho View
{
glMatrixMode(GL_PROJECTION); // Select Projection
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
glOrtho( 0, 1024 , 768 , 0, -3, 3 ); // Select Ortho Mode (640x480)
glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
}
void ViewPerspective() // Set Up A Perspective View
{
glMatrixMode( GL_PROJECTION ); // Select Projection
glPopMatrix(); // Pop The Matrix
glMatrixMode( GL_MODELVIEW ); // Select Modelview
glPopMatrix(); // Pop The Matrix
}
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
//计算 FPS
static int counter=0;
if(GetTime()-last_time>=1.0f)
{
fps=counter;
counter=0;
last_time=GetTime();
}
counter++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
//////// Draw Scene /////////////////////
AnjustViewByMouse();
eyePosition[0] = 900.0f*cos(angle_y);
eyePosition[1] = 900.0f*sin(angle_x);
eyePosition[2] = 900.0f*sin(angle_y);
gluLookAt( eyePosition[0],
eyePosition[1],
eyePosition[2],
0.0f, 20.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// glEnable(GL_LIGHTING);
// glDisable(GL_LIGHTING);
CubeBox();
DrawParticle();
lightrate = (ParticleCount/10000.0f)*0.2f;
//////// Draw Text //////////////////////
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
glColor3f(1.0f,1.0f,0.0f);
glRasterPos2f(-0.52f, 0.36f);
glPrint("Particle Effect by Azure (www.azure.com.cn)");
glRasterPos2f(-0.52f, 0.33f);
glPrint("SPACE - Stop Particle Source");
glRasterPos2f(0.32f, 0.36f);
glPrint("Particle Count:%d", ParticleCount);
ParticleCount=0;
glRasterPos2f(-0.52f, -0.30f);
glPrint("Renderer:%s", brand);
glRasterPos2f(-0.52f, -0.33f);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -