📄 opengl.cpp
字号:
// OpenGL.cpp: implementation of the OpenGL class.
// 学程序编游戏系列丛书
// 唐明理 E_mail: cqtmL@163.com
//====================================================================
#include "stdafx.h"
#include "OpenGL.h"
//////////////////////////////////////////////////////////////////////
GLfloat r;
CString test;
anmobj* m_anmobj;
mdlobj m_mdlobj;
//////////////////////////////////////////////////////////////////////
OpenGL::OpenGL()
{ hFont =CreateFont(-12,0,0,0,400,0,0,0,GB2312_CHARSET,0,0,0,FF_MODERN,"Arial");
hFont0 =CreateFont(-48,0,0,0,800,0,0,0,GB2312_CHARSET,0,0,0,FF_MODERN,"黑体");
m_Fram=0;//
m_Time = timeGetTime();
tim=0;
Font=new CGLFont() ;
}
OpenGL::~OpenGL()
{ CleanUp();
}
BOOL OpenGL::SetupPixelFormat(HDC hDC0)
{ int nPixelFormat;
hDC=hDC0;
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
16,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd)))
{ MessageBox(NULL,"没找到合适的显示模式","Error",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
SetPixelFormat(hDC,nPixelFormat,&pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
m_anmobj =new anmobj();
m_baiscobj=new baiscobj();
m_baiscobj->light0();
return TRUE;
}
void OpenGL::init(int Width, int Height)
{ glViewport(0,0,Width,Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective
( 54.0f,
(GLfloat)Width/(GLfloat)Height,
0.1f,
3000.0f
);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//
//====================================================
}
void OpenGL::Render()
{ if(m_baiscobj==NULL) return;
if(m_anmobj==NULL) return;
glClearColor(0.0f, 0.0f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//////////////////////////////////////////////////////////////////////////
m_baiscobj->DisplayScene();
m_baiscobj->DrawSand();
m_baiscobj->CreateSkyBox(3,6,3,6);
srand(100);
for(int i=0;i<TREESL;i++)
{m_baiscobj->ShowTree(i);
}
m_baiscobj->Scene(0,MAP+30,13.6f,-MAP-20, 0, 0,0.35f);
m_baiscobj->Scene(1,MAP+30,15.0f,-MAP, 70, r,0.2f);
m_baiscobj->Scene(2,MAP+30,12.0f,-MAP, 65,r+90,0.5f);
for(i=0;i<RNSHU;i++)
{float y;
y= m_baiscobj->GetHeight(m_anmobj->man[i].qd[0],-m_anmobj->man[i].qd[1]);
m_anmobj->setman(i,y+1.4f);
}
///////////////////////////////////////////////////////
float xx0=m_baiscobj->g_eye [0]+0.6f*cos(m_baiscobj->rad_xz);//g_eye [0]
float zz0=m_baiscobj->g_eye [2]+0.6f*sin(m_baiscobj->rad_xz);
m_mdlobj.DrawModels(xx0,m_baiscobj->GetHeight(xx0,zz0)+1.7
,zz0,0,0.1f,m_baiscobj->g_Angle,m_baiscobj->g_elev);
//////////////////////////////////////////////////////////////////////////
m_baiscobj->fire();
m_baiscobj->plosion();
text();
SwapBuffers(hDC);
}
void OpenGL::CleanUp()
{ delete[] m_baiscobj;
delete[] m_anmobj;
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
}
///////////////////////////////////////////////////////////////
void OpenGL::text()
{ DWORD Dura =(timeGetTime()-m_Time)/1000;
if(Dura>0) tim=m_Fram/Dura;
m_Fram++;
char str[128];
sprintf(str, "刷屏: %2d 帧/秒 %s",tim,test);
Font->settext(300,550,str,hFont,1,1,1.0f);
Font->settext(418,265,"+",hFont,1,0,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -