📄 demo.cpp
字号:
// update the current model if(!m_bPaused) { m_CoreModel->onUpdate(elapsedSeconds); } double stop = Tick::getTime(); stop -= start; static double cumul = 0; cumul += stop; if (!bFirst) { m_averageCPUTime = cumul / float(lastTime - firstTime) * 100; } bFirst = false; // current tick will be last tick next round m_lastTick = tick; // update the screen glutPostRedisplay();}//----------------------------------------------------------------------------//// Initialize the demo ////----------------------------------------------------------------------------//bool Demo::onInit(){ // load the cursor texture std::string strFilename; strFilename = m_strDatapath + "cursor.raw"; if(!loadTexture(strFilename, m_cursorTextureId)) return false; // load the logo texture strFilename = m_strDatapath + "logo.raw"; if(!loadTexture(strFilename, m_logoTextureId)) return false; // load the fps texture strFilename = m_strDatapath + "fps.raw"; if(!loadTexture(strFilename, m_fpsTextureId)) return false;
// 加载地面纹理
strFilename = m_strDatapath + "ground.tga";
if(!loadTexture(strFilename, m_groundTextureId)) return false;
// load 'cally' model std::cout << "Loading 'cally' model ..." << std::endl; m_CoreModel = new CoreModel();/* if (m_strCal3D_Datapath != "") m_CoreModel->setPath( m_strCal3D_Datapath + "/" + "cal3d/" ); if(!m_CoreModel->onInit(m_strDatapath + "cal3d.cfg")) { std::cerr << "Model initialization failed! (cally)" << std::endl; return false; }
*/ if (m_strCal3D_Datapath != "")
m_CoreModel->setPath( m_strCal3D_Datapath + "/" + "cally/" );
if(!m_CoreModel->onInit(m_strDatapath + "cally.cfg"))
{
std::cerr << "Model initialization failed! (cally)" << std::endl;
return false;
}
std::cout << std::endl; // we're done std::cout << "Initialization done." << std::endl; std::cout << std::endl; std::cout << "Quit the demo by pressing ESC" << std::endl; std::cout << std::endl; return true;}//----------------------------------------------------------------------------//// Handle a key event ////----------------------------------------------------------------------------//void Demo::onKey(unsigned char key, int x, int y){
// test for quit event if(key == 27) exit(0); // test for pause event if(key == ' ') m_bPaused = !m_bPaused;
//////////////////////////////////////////摄像机运动/////////////////////////////////////////
if(key == 't')//UP
{
YY=-2;
camera_x=camera_x+(YY*camera_speed)*sin(camera_psi/57.3);
camera_y=camera_y+(-YY*camera_speed)*cos(camera_psi/57.3);
}
if(key == 'g')//DOWN
{
YY=2;
camera_x=camera_x+(YY*camera_speed)*sin(camera_psi/57.3);
camera_y=camera_y+(-YY*camera_speed)*cos(camera_psi/57.3);
}
if(key == 'a')//LEFT
{
XX=-1;
camera_psi=camera_psi-XX;
}
if(key == 'd')//RIGHT
{
XX=1;
camera_psi=camera_psi-XX;
}
if(key == 's')//HOME
{
ZZ=2;
camera_z=camera_z+ZZ;
}
if(key == 'w')//END
{
ZZ=-2;
camera_z=camera_z+ZZ;
}
if(key == 'r')//INSERT
{
LL=-1;
camera_gama=camera_gama+LL;
}
if(key == 'f')//DELETE
{
LL=1;
camera_gama=camera_gama+LL;
}
if(key == 'y')//PAGEUP
{
RR=1;
camera_theta=camera_theta+RR;
}
if(key == 'h')//PAGEDOWN
{
RR=-1;
camera_theta=camera_theta+RR;
}
//键盘控制动作
if(key == 'z')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(0, 0.3f, 0.3f);//F1 if(key == 'x')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(1, 0.3f, 0.3f);//F2
if(key == 'c')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(2, 0.3f, 0.3f);//F3
if(key == 'v')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(3, 0.3f, 0.3f);//F4
if(key == 'b')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(4, 0.3f, 0.3f);//F5
if(key == 'n')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(5, 0.3f, 0.3f);//F6
if(key == 'm')m_CoreModel->m_ModelVector[0]->m_calModel->getMixer()->executeAction(6, 0.3f, 0.3f);//F7
if(key == 'j')m_CoreModel->m_ModelVector[0]->setState(0,2.0);//F8
if(key == 'k')m_CoreModel->m_ModelVector[0]->setState(1,2.0);//F9
if(key == 'l')m_CoreModel->m_ModelVector[0]->setState(2,2.0);//F10
}//----------------------------------------------------------------------------//// Handle a mouse button down event ////----------------------------------------------------------------------------//void Demo::onMouseButtonDown(int button, int x, int y){ // update mouse button states if(button == GLUT_LEFT_BUTTON) { m_bLeftMouseButtonDown = true; } if(button == GLUT_RIGHT_BUTTON) { m_bRightMouseButtonDown = true; }
// update internal mouse position m_mouseX = x; m_mouseY = y;}//----------------------------------------------------------------------------//// Handle a mouse button up event ////----------------------------------------------------------------------------//void Demo::onMouseButtonUp(int button, int x, int y){ // update mouse button states if(button == GLUT_LEFT_BUTTON) { m_bLeftMouseButtonDown = false; } if(button == GLUT_RIGHT_BUTTON) { m_bRightMouseButtonDown = false; } // update internal mouse position m_mouseX = x; m_mouseY = y;}//----------------------------------------------------------------------------//// Handle a mouse move event ////----------------------------------------------------------------------------//void Demo::onMouseMove(int x, int y){ // update internal mouse position m_mouseX = x; m_mouseY = y;}//----------------------------------------------------------------------------//// Render the current scene ////----------------------------------------------------------------------------//void Demo::onRender(){
//设置摄像机参数
UpdateInputState();
// clear all the buffers glClearColor(0.0f, 0.0f, 0.2f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // get the render scale of the model float renderScale; renderScale = m_CoreModel->getRenderScale();
// set the projection transformation glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLdouble)m_width / (GLdouble)m_height, renderScale * 50.0, renderScale * 5000.0); // set the model transformation glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // set the light position and attributes const GLfloat lightPosition[] = { 1.0f, -1.0f, 1.0f, 1.0f }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); const GLfloat lightColorAmbient[] = { 0.3f, 0.3f, 0.3f, 1.0f }; glLightfv(GL_LIGHT0, GL_AMBIENT, lightColorAmbient); const GLfloat lightColorDiffuse[] = { 0.52f, 0.5f, 0.5f, 1.0f }; glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColorDiffuse); const GLfloat lightColorSpecular[] = { 0.1f, 0.1f, 0.1f, 1.0f }; glLightfv(GL_LIGHT0, GL_SPECULAR, lightColorSpecular); //设置摄像机
cam.x=camera_x;
cam.y=camera_y;
cam.z=camera_z;
cam.theta=camera_theta;
cam.psi=camera_psi;
cam.gama=camera_gama;
cam.width=m_width;
cam.height=m_height;
cam.aspect=m_width/(1.0*m_height);
cam.Setup(cam.fovy,cam.aspect,cam.znear,cam.zfar,cam.vx,cam.vy,cam.width,cam.height);
cam.Place(cam.x,cam.y,cam.z,cam.theta,cam.psi,cam.gama);
glEnable(GL_DEPTH_TEST);
//画地面
glBindTexture(GL_TEXTURE_2D, m_groundTextureId);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3d(-500, 0.0, 500);
glTexCoord2f(1.0, 1.0); glVertex3d( 500, 0.0, 500);
glTexCoord2f(1.0, 0.0); glVertex3d( 500, 0.0, -500);
glTexCoord2f(0.0, 0.0); glVertex3d(-500, 0.0, -500);
glEnd();
glDisable(GL_TEXTURE_2D);
// render model m_CoreModel->onRender(); glDisable(GL_DEPTH_TEST);
// switch to orthogonal projection for 2d stuff glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, (GLdouble)m_width, 0, (GLdouble)m_height, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // we will render some alpha-blended textures glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_TEXTURE_2D); // render the logo glBindTexture(GL_TEXTURE_2D, m_logoTextureId); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex2i(0, 0); glTexCoord2f(1.0f, 1.0f); glVertex2i(128, 0); glTexCoord2f(1.0f, 0.0f); glVertex2i(128, 128); glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 128); glEnd(); // render the fps counter glBindTexture(GL_TEXTURE_2D, m_fpsTextureId); glBegin(GL_QUADS); int digit; digit = m_fps; int digitId; for(digitId = 2; digitId >= 0; digitId--) { int x; x = 29 + digitId * 16; float tx; tx = (float)(digit % 10) * 0.0625f; glTexCoord2f(tx, 1.0f); glVertex2i(x, 94); glTexCoord2f(tx + 0.0625f, 1.0f); glVertex2i(x + 16, 94); glTexCoord2f(tx + 0.0625f, 0.0f); glVertex2i(x + 16, 110); glTexCoord2f(tx, 0.0f); glVertex2i(x, 110); digit /= 10; } glEnd(); // render the cursor glBindTexture(GL_TEXTURE_2D, m_cursorTextureId); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex2i(m_mouseX, m_mouseY - 32); glTexCoord2f(1.0f, 1.0f); glVertex2i(m_mouseX + 32, m_mouseY - 32); glTexCoord2f(1.0f, 0.0f); glVertex2i(m_mouseX + 32, m_mouseY); glTexCoord2f(0.0f, 0.0f); glVertex2i(m_mouseX, m_mouseY); glEnd(); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); // swap the front- and back-buffer glutSwapBuffers(); // increase frame counter m_fpsFrames++;}//----------------------------------------------------------------------------//// Shut the demo down ////----------------------------------------------------------------------------//void Demo::onShutdown(){ // shut the menu down// theMenu.onShutdown();
FreeDirectInput();//操纵杆退出
// shut all models down m_CoreModel->onShutdown(); delete m_CoreModel; if (m_bOutputAverageCPUTimeAtExit) std::cout << m_averageCPUTime;}//----------------------------------------------------------------------------//// Set the dimension ////----------------------------------------------------------------------------//void Demo::setDimension(int width, int height){ // store new width and height values m_width = width; m_height = height; // set new viewport dimension glViewport(0, 0, m_width, m_height); // adjust menu// theMenu.onResize(width, height);}//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -