void Update (DWORD milliseconds) // Perform Motion Updates Here { if (g_keys->keyDown ['W']) trans.z += 1.0f; if (g_keys->keyDown ['S']) trans.z -= 1.0f; if (g_keys->keyDown ['A']) trans.x += 1.0f; if (g_keys->keyDown ['D']) trans.x -= 1.0f; if (g_keys->keyDown ['B']) { splineFunction = &bezier; BuildPath(); } if (g_keys->keyDown ['C']) { splineFunction = &catmullrom; BuildPath(); } if (g_keys->keyDown [VK_ESCAPE] == TRUE) // Is ESC Being Pressed? TerminateApplication (g_window); // Terminate The Program if (g_keys->keyDown [VK_F1] == TRUE) // Is F1 Being Pressed? ToggleFullscreen (g_window); // Toggle Fullscreen Mode if (isRClicked) // If Right Mouse Clicked, Reset All Rotations { Matrix3fSetIdentity(&LastRot); // Reset Rotation Matrix3fSetIdentity(&ThisRot); // Reset Rotation Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation } if (!isDragging) // Not Dragging { if (isClicked) // First Click { isDragging = true; // Prepare For Dragging LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One ArcBall.click(&MousePt); // Update Start Vector And Prepare For Dragging } } else { if (isClicked) // Still Clicked, So Still Dragging { Quat4fT ThisQuat; ArcBall.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One } else // No Longer Dragging isDragging = false; } //rotY+=1.0f; }
void motion(int x, int y) { MousePt.s.X = x; MousePt.s.Y = y; if(!isDragging && isClicked){ isDragging = true; LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One ArcBall.click(&MousePt); // Update Start Vector And Prepare For Dragging } else{ if (isClicked) // Still Clicked, So Still Dragging { Quat4fT ThisQuat; ArcBall.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One } else // No Longer Dragging isDragging = false; } if(buttonState == 4){ Matrix3fSetIdentity(&LastRot); // Reset Rotation Matrix3fSetIdentity(&ThisRot); // Reset Rotation Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation } //////////////////////////////////////////// float dx, dy; dx = (float)(x - ox); dy = (float)(y - oy); if (buttonState == 1) { //Copy Transform Rotation to Model Matrix for(int i=0; i<4; ++i) for(int j=0; j<4; ++j){ Model[i][j] = Transform.M[i*4+j]; } } if (buttonState == 2) { // middle = translate View = glm::translate(View, glm::vec3(dx * translateSpeed, -dy*translateSpeed, 0.f)); } if (buttonState == 3) { // left+middle = zoom View = glm::translate(View, glm::vec3(0.f, 0.f, dy*translateSpeed)); } MVP = Projection * View * Model; ox = x; oy = y; glutPostRedisplay(); }
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { if (isRClicked) // If Right Mouse Clicked, Reset All Rotations { Matrix3fSetIdentity(&LastRot); // Reset Rotation Matrix3fSetIdentity(&ThisRot); // Reset Rotation Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation } if (!isDragging) // Not Dragging { if (isClicked) // First Click { isDragging = true; // Prepare For Dragging LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One ArcBall.click(&MousePt); // Update Start Vector And Prepare For Dragging } } else { if (isClicked) // Still Clicked, So Still Dragging { Quat4fT ThisQuat; ArcBall.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One } else // No Longer Dragging isDragging = false; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glTranslatef(0, 0, -1.0f); glPushMatrix(); // NEW: Prepare Dynamic Transform glMultMatrixf(Transform.M); glCallList(dl); glPopMatrix(); glLoadIdentity(); // Reset The Current Modelview Matrix return TRUE; // Everything Went OK }
void ReshapeGL (int width, int height) // Reshape The Window When It's Moved Or Resized { glViewport (0, 0, (GLsizei)(width), (GLsizei)(height)); // Reset The Current Viewport glMatrixMode (GL_PROJECTION); // Select The Projection Matrix glLoadIdentity (); // Reset The Projection Matrix gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height), // Calculate The Aspect Ratio Of The Window 1.0f, 100.0f); glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity (); // Reset The Modelview Matrix ArcBall.setBounds((GLfloat)width, (GLfloat)height); //*NEW* Update mouse bounds for arcball }
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { double ymax, ymin, xmax, xmin = 0; 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(); ymax = 0.1f * tan(45.0f * M_PI / 360.0); ymin = -ymax; xmin = ymin * (GLfloat)width/(GLfloat)height; xmax = ymax * (GLfloat)width/(GLfloat)height; glFrustum(xmin, xmax, ymin, ymax, 0.1f, 5000.0f); //glFrontFace(GL_CW); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix ArcBall.setBounds(width, height); }
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { //go and get the contents ImportREFile(); //start the windows loop MSG msg; // Windows message structure bool done = false; // bool variable to exit loop fullscreen = false; // Windowed mode // Create our OpenGL window if (!CreateGLWindow("Roadworks Estimator OpenGL Renderer",1024,768,16,fullscreen)) { return 0; // Quit if window was not created } while(!done) // Loop that runs while done = false { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is there a message waiting? { if (msg.message == WM_QUIT) // Have we received a quit message? { done = true; // If so done = true } else // If not, deal with window messages { TranslateMessage(&msg); // Translate the message DispatchMessage(&msg); // Dispatch the message } } else // If there are no messages { // Draw the scene. Watch for ESC key and quit messages from DrawGLScene() if (active) // Program active? { if (keys['W'] ) { translatez+=0.01f; } if (keys['S'] ) { translatez-=0.01f; } if (keys['A'] ) { rotatey+=0.01f; } if (keys['D'] ) { rotatey-=0.01f; } if (keys[VK_UP] ) { rotatex+=0.025f; } if (keys[VK_DOWN] ) { rotatex-=0.025f; } if (keys[VK_LEFT] ) { rotatez+=0.1f; } if (keys[VK_RIGHT] ) { rotatez-=0.1f; } if (keys[VK_F1] ) { wireframemode=!wireframemode; if(wireframemode) { WireframeMode(); Sleep(100); } else { TextureMode(); Sleep(100); } } if (keys[VK_ESCAPE]) // Was ESC pressed? { done = true; // ESC signalled a quit } // Not time to quit, Update screen if (isRClicked) // If Right Mouse Clicked, Reset All Rotations { Matrix3fSetIdentity(&LastRot); // Reset Rotation Matrix3fSetIdentity(&ThisRot); // Reset Rotation Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Reset Rotation } if (!isDragging) // Not Dragging { if (isClicked) // First Click { isDragging = true; // Prepare For Dragging LastRot = ThisRot; // Set Last Static Rotation To Last Dynamic One ArcBall.click(&MousePt); // Update Start Vector And Prepare For Dragging } } if (isClicked) // Still Clicked, So Still Dragging { Quat4fT ThisQuat; ArcBall.drag(&MousePt, &ThisQuat); // Update End Vector And Get Rotation As Quaternion Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT Matrix3fMulMatrix3f(&ThisRot, &LastRot); // Accumulate Last Rotation Into This One Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One } else // No Longer Dragging isDragging = false; DrawGLScene(); // Draw the scene SwapBuffers(hDC); // Swap buffers (Double buffering) } } } // Shutdown KillGLWindow(); // Kill the window return (msg.wParam); // Exit the program }