Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. int xpos = 0;   // current x position
  2. int ypos = 0;   // current y position
  3. int orig_xpos = 0; //where the user first clicked x
  4. int orig_ypos = 0; //where the use first clicked y
  5. GLdouble newx, newy, newz; //the new object space coordinates
  6. GLdouble mx, my, mz; // move with this vector
  7.  
  8. GLdouble model_matrix[16];
  9. GLdouble proj_matrix[16];
  10. GLint view_matrix[4];
  11.  
  12. void setMatrixes()
  13. {
  14.     glGetDoublev(GL_MODELVIEW_MATRIX, model_matrix);
  15.     glGetDoublev(GL_PROJECTION_MATRIX, proj_matrix);
  16.     glGetIntegerv(GL_VIEWPORT, view_matrix);
  17. }
  18.  
  19. void GLFWCALL onMouseButton( int button, int action )
  20. {
  21.     if( button == GLFW_MOUSE_BUTTON_LEFT )
  22.     {
  23.         dragging = action == GLFW_PRESS;
  24.         glfwGetMousePos(&xpos, &ypos);
  25.  
  26.         if(orig_xpos == 0 && orig_ypos == 0)
  27.         {
  28.             orig_xpos = xpos;
  29.             orig_ypos = ypos;
  30.         }
  31.         gluUnProject(xpos, ypos, 0, model_matrix, proj_matrix, view_matrix, &newx, &newy, &newz);
  32.     }
  33. }
  34.  
  35. void moveObject(int dx, int dy)
  36. {
  37.     GLdouble nx, ny, nz;
  38.     gluUnProject(dx, dy, 0, model_matrix, proj_matrix, view_matrix, &nx, &ny, &nz);
  39.  
  40.     mx = newx - nx;
  41.     my = newy - ny;
  42.     mz = newz - nz;
  43. }
  44.  
  45. void GLFWCALL onMouseMove(int x, int y)
  46. {
  47.     if(dragging)
  48.     {
  49.         moveObject(orig_xpos - x, orig_ypos - y);
  50.     }
  51. }
  52.  
  53. int main()
  54. {
  55.     init();
  56.  
  57.     glfwSetMouseButtonCallback(onMouseButton);
  58.     glfwSetMousePosCallback(onMouseMove);
  59.     glEnable(GL_DEPTH_TEST);
  60.  
  61.     glClearColor( 1,1,1,1 );
  62.  
  63.     while(running())
  64.     {
  65.         glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT);
  66.         glLoadIdentity();
  67.         gluLookAt(15, 0, 5, 0, 0, 0, 0, 0, 1 );
  68.         setMatrixes();
  69.  
  70.         /* A simple sphere, but should work with more simple objects */
  71.         glColor3ub(0, 0, 0);
  72.         glPushMatrix();
  73.         glTranslated(mx, my, mz);
  74.         drawSmoothUnitySphere();
  75.         glPopMatrix();
  76.  
  77.         glfwSwapBuffers();
  78.     }
  79.  
  80.     glfwTerminate();
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement