void InitGL() { // Setting Camera Moving Sensitivity.. hCamera.SetMouseSensitivity(0.1f, 0.02f, 0.1f); // Generating bottom plate... GLfloat gray[4] = {0.5f, 0.5f, 0.5f, 1.0f}; GLfloat black[4] = {0.0f, 0.0f, 0.0f, 1.0f}; btmPlate = hCamera.makeBottomPlate(gray, black, 80.0f, 80.0f, 10.0f, 0.0f); // Generate an axis g_glLtAxis = GenerateAxis(5.0f); }
void OnMouseClick(int button, int state, int x, int y) { mouseButton = button; if ( state == GLUT_DOWN ) { bMousePressed = true; hCamera.mousePress(x, y); } else if ( state == GLUT_UP ) { bMousePressed = false; hCamera.mouseRelease(x, y); } }
//=================== void Initialize() //=================== { /*if (glewInit() != GLEW_OK) { printf("GLEW initialization failed\n"); return; }*/ GLfloat ambientLight[4] = {0.2f, 0.2f, 0.2f, 1.0f}; GLfloat diffuseLight[4] = {0.9f, 0.9f, 0.9f, 1.0f}; GLfloat diffuseLight1[4] = {0.2f, 0.2f, 0.2f, 1.0f}; GLfloat specularLight[4] = {0.8f, 0.8f, 0.8f, 1.0f}; GLfloat lightPosition[4] = {30.0f, 30.0f, 30.0f, 0.0f}; GLfloat lightPosition1[4] = {-30.0f, 30.0f, -30.0f, 0.0f}; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); //glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight1); glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1); glEnable(GL_LIGHT1); // Setting Camera Moving Sensitivity.. hCamera.SetMouseSensitivity(0.05f, 0.3f, 0.3f); // Generating bottom plate... btmPlate = hCamera.makeBottomPlate(gray, black, 200.0f, 200.0f, 10.0f, 0.0f); // Generate an axis g_glLtAxis = GenerateAxis(5.0f); // Create a rigidbody simulation g_BulletSim.Create(); }
void OnMouseMove(int x, int y) { if ( bMousePressed ) { if( mouseButton == GLUT_LEFT_BUTTON ) { hCamera.mouseMove(x, y, Left_button); } else if( mouseButton == GLUT_RIGHT_BUTTON ) { hCamera.mouseMove(x, y, Right_button); } else if( mouseButton == (GLUT_LEFT_BUTTON | GLUT_RIGHT_BUTTON) ) { hCamera.mouseMove(x, y, Middle_button); } else if( mouseButton == GLUT_MIDDLE_BUTTON ) { hCamera.mouseMove(x, y, Middle_button); } glutPostRedisplay(); } }
void OnRender() { SetWindowTitle(); std::string sTitle = g_sWindowTitle; glutSetWindowTitle(sTitle.c_str()); static GLfloat RedSurface[] = { 1.0f, 0.0f, 0.0f, 1.0f}; static GLfloat GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat BlueSurface[] = { 0.0f, 0.0f, 1.0f, 1.0f}; static GLfloat YellowSurface[] = { 1.0f, 1.0f, 0.0f, 1.0f}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer glLoadIdentity(); // reset modelview matrix //--------------- //Draw background //--------------- glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glBegin(GL_QUADS); glColor3f(0.9F, 0.9F, 0.9F); glVertex2f( 1.0F, 1.0F); glColor3f(0.9F, 0.9F, 0.9F); glVertex2f(-1.0F, 1.0F); glColor3f(0.5F, 0.5F, 0.5F); glVertex2f(-1.0F, -1.0F); glColor3f(0.5F, 0.5F, 0.5F); glVertex2f( 1.0F, -1.0F); glEnd(); glEnable(GL_DEPTH_TEST); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); //---------------- // Applying camera //---------------- hCamera.ApplyCamera(); glCallList(btmPlate); glPushMatrix(); glCallList(g_glLtAxis); glPopMatrix(); // Lights GLfloat ambientLight[4] = {0.1f, 0.1f, 0.1f, 1.0f}; GLfloat diffuseLight[4] = {0.7f, 0.7f, 0.7f, 1.0f}; GLfloat lightPosition[4] = {200.0f, 200.0f, 100.0f, 0.0f}; glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glEnable(GL_LIGHT0); if ( !g_bPause ) { if ( g_bOneStep ) { g_bOneStep = false; g_bPause = true; } g_WorldSim.Update(g_dt); g_CurFrame++; } glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); g_WorldSim.Render(g_bWireframe); //------------ // Draw texts //------------ /* We are going to do some 2-D orthographic drawing. */ glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); int w = g_Width; int h = g_Height; GLdouble size = (GLdouble)((w >= h) ? w : h) / 2.0; GLdouble aspect; if (w <= h) { aspect = (GLdouble)h/(GLdouble)w; glOrtho(-size, size, -size*aspect, size*aspect, -1000000.0, 1000000.0); } else { aspect = (GLdouble)w/(GLdouble)h; glOrtho(-size*aspect, size*aspect, -size, size, -1000000.0, 1000000.0); } /* Make the world and window coordinates coincide so that 1.0 in */ /* model space equals one pixel in window space. */ glScaled(aspect, aspect, 1.0); glDisable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); std::string sInfo = "Current Algorithm(press 'a' to change): "; sInfo.append(g_sWindowTitleInfo); int linePos = 0; DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20)); // frame char frame[10]; itoa(g_CurFrame, frame, 10); sInfo = "Frame: "; sInfo.append(frame); DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20)); //// batch //char batch[5]; //itoa(g_WorldSim.m_RenderBatchIndex, batch, 10); //sInfo = "Batch: "; //sInfo.append(batch); //DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20)); //// toggle CPU/GPU //if ( g_WorldSim.m_bGPU ) // sInfo = "GPU solver"; //else // sInfo = "CPU solver"; //DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20)); // help for keys DrawTextGlut("space: advance one step", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); DrawTextGlut("'s': start or stop", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); DrawTextGlut("'r': toggle rotating", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); //DrawTextGlut("'c': reset", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); //DrawTextGlut("'b' : next batch", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); //DrawTextGlut("'g' : toggle CPU/GPU solver", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); DrawTextGlut("===========================================", -g_Width/2 + 10, g_Height/2 - (linePos += 20)); char str[100]; sprintf(str, "Penetration depth:%11.9f", g_WorldSim.m_pNarrowPhase->GetPairs()[0].penetrationDepth); DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20)); { const CTransform& trans = g_WorldSim.m_pNarrowPhase->GetPairs()[0].pObjA->GetTransform(); sprintf(str, "ObjectA translation(%11.9f, %11.9f, %11.9f)", trans.GetTranslation().m_X, trans.GetTranslation().m_Y, trans.GetTranslation().m_Z); DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20)); sprintf(str, "ObjectA rotation(%11.9f, %11.9f, %11.9f, %11.9f)", trans.GetRotation().m_X, trans.GetRotation().m_Y, trans.GetRotation().m_Z, trans.GetRotation().m_W); DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20)); } { const CTransform& trans = g_WorldSim.m_pNarrowPhase->GetPairs()[0].pObjB->GetTransform(); sprintf(str, "ObjectB translation(%11.9f, %11.9f, %11.9f)", trans.GetTranslation().m_X, trans.GetTranslation().m_Y, trans.GetTranslation().m_Z); DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20)); sprintf(str, "ObjectB rotation(%11.9f, %11.9f, %11.9f, %11.9f)", trans.GetRotation().m_X, trans.GetRotation().m_Y, trans.GetRotation().m_Z, trans.GetRotation().m_W); DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20)); } glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glutSwapBuffers(); }
//=================== void RenderFunc() //=================== { static GLfloat RedSurface[] = { 1.0f, 0.0f, 0.0f, 1.0f}; static GLfloat GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat BlueSurface[] = { 0.0f, 0.0f, 1.0f, 1.0f}; static GLfloat YellowSurface[] = { 1.0f, 1.0f, 0.0f, 1.0f}; hCamera.Update(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer glLoadIdentity(); // reset modelview matrix //--------------- //Draw background //--------------- glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glBegin(GL_QUADS); glColor3f(0.9F, 0.9F, 0.9F); glVertex2f( 1.0F, 1.0F); glColor3f(0.9F, 0.9F, 0.9F); glVertex2f(-1.0F, 1.0F); glColor3f(0.2F, 0.2F, 0.2F); glVertex2f(-1.0F, -1.0F); glColor3f(0.2F, 0.2F, 0.2F); glVertex2f( 1.0F, -1.0F); glEnd(); glEnable(GL_DEPTH_TEST); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); //---------------- // Applying camera //---------------- hCamera.ApplyCamera(); glCallList(btmPlate); //------------------------------------------- // Rotate GL coordinate so that Z axis is up. //------------------------------------------- /* Y Z | | | | | ===> | |________ X |________ Y / / / GL coordinate / Modeler coordinate / / Z X */ //glRotatef(-90.0f, 0, 0, 1.0f); //glRotatef(-90.0f, 0, 1.0f, 0); //------------- // Draw an axis //------------- glPushMatrix(); glCallList(g_glLtAxis); glPopMatrix(); glEnable(GL_LIGHTING); if ( !g_bPause ) g_BulletSim.Update(); g_BulletSim.Render(2); glDisable(GL_LIGHTING); }
//====================================================================================== // the Windows Procedure event handler LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) //====================================================================================== { static HGLRC hRC; // rendering context static HDC hDC; // device context char string[] = "Hello, world!"; // text to be displayed //int width, height; // window width and height int cx, cy; GLfloat h; int mx, my; switch(message) { case WM_CREATE: // window is being created return 0; break; case WM_ACTIVATE: // Watch For Window Activate Message { if (!HIWORD(wParam)) // Check Minimization State { g_active=TRUE; // Program Is Active } else { g_active=FALSE; // Program Is No Longer Active } return 0; // Return To The Message Loop } case WM_CLOSE: // windows is closing // send WM_QUIT to message queue PostQuitMessage(0); return 0; case WM_KEYDOWN: if(wParam == VK_ESCAPE) g_bEnd = TRUE; else if ( wParam == VK_SPACE ) { g_bPause = !g_bPause; } else if ( wParam == 79 ) // 'o' { bool bPause = g_bPause; g_bPause = true; TCHAR lpstrFile[MAX_PATH]=""; // MAX_PATH = 260 OPENFILENAME OFN = { sizeof(OPENFILENAME), g_hWnd, NULL, "Bullet Physics Files (*.bullet)\0*.bullet\0ALL Files (*.*)\0*.*\0\0", NULL, 0, 1, lpstrFile, MAX_PATH, NULL, 0, NULL, "Load Bullet Physics File Dialog", OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, 0, 0, "bullet", 0, NULL, NULL }; if ( GetOpenFileName(&OFN) ) { if ( !g_BulletSim.LoadBulletFile(lpstrFile) ) { MessageBox(g_hWnd, "Failed to load the bullet file", "Error", MB_OK | MB_ICONERROR); } } g_bPause = bPause; } else if ( wParam == 82 ) // 'r' { g_BulletSim.ReloadBulletFile(); } break; case WM_SIZE: cy = HIWORD(lParam); // retrieve width and height cx = LOWORD(lParam); GLfloat w; h = 1.0; if(cy == 0) w = (GLfloat) cx; else w = (GLfloat) cx / (GLfloat) cy; glViewport(0, 0, cx, cy); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-w, w, -h, h, 3.0, 3000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; break; case WM_LBUTTONDOWN: mx = LOWORD(lParam); my = HIWORD(lParam); //SetCapture(g_hWnd); hCamera.mousePress(mx, my); return 0; break; case WM_LBUTTONUP: mx = LOWORD(lParam); my = HIWORD(lParam); //ReleaseCapture(); hCamera.mouseRelease(mx, my); return 0; break; case WM_RBUTTONDOWN: mx = LOWORD(lParam); my = HIWORD(lParam); //SetCapture(g_hWnd); hCamera.mousePress(mx, my); return 0; break; case WM_RBUTTONUP: mx = LOWORD(lParam); my = HIWORD(lParam); //ReleaseCapture(); hCamera.mouseRelease(mx, my); return 0; break; case WM_MOUSEMOVE: mx = LOWORD(lParam); my = HIWORD(lParam); gmx = mx; gmy = my; if( wParam == MK_LBUTTON ) { hCamera.mouseMove(mx, my, Left_button); } else if( wParam == MK_RBUTTON ) { hCamera.mouseMove(mx, my, Right_button); } else if( wParam == (MK_RBUTTON | MK_LBUTTON) ) { hCamera.mouseMove(mx, my, (Right_button | Left_button) ); } return 0; break; default: break; } return (DefWindowProc(hwnd, message, wParam, lParam)); }