Advertisement
Sk8erPeter

OpenGL - Window, Camera, Scene (bme2dsysr.ppt)

Oct 14th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // http://cg.iit.bme.hu/portal/oktatott-targyak/szamitogepes-grafika-es-kepfeldolgozas/2d-grafikus-rendszerek
  2. // http://cg.iit.bme.hu/portal/sites/default/files/oktatott-targyak/szamitogepes-grafika-es-kepfeldolgozas/2d-grafikus-rendszerek/bme2dsysr.ppt
  3. // 12. diától 21. diáig részletek
  4.  
  5. Window window;
  6. int appWinHeight, appWinWidth;
  7.  
  8. // ...
  9.  
  10. void MouseDown(int button, int state, int x, int y) {
  11.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  12.     window.MouseLD(x, appWinHeight - y); // Windows kr.
  13.   glutPostRedisplay();
  14. }
  15.  
  16. // ...
  17.  
  18. struct Window {
  19.   Scene scene;
  20.   // ...
  21.  
  22.   void MouseLD(int X, int Y) {
  23.     // ...
  24.     scene.InputPipe(X, Y);
  25.     // ...
  26.   }
  27. }
  28.  
  29. class Camera {
  30.   Rect viewport, window;
  31.  
  32.   void SetGL() {
  33.     glViewport(viewport.left, viewport.bottom,
  34.         viewport.width, viewport.height);
  35.  
  36.     glMatrixMode(GL_PROJECTION);
  37.     glLoadIdentity();
  38.     gluOrtho2D(window.left, window.left + window.width,
  39.         window.bottom, window.bottom + window.height);
  40.   }
  41.  
  42.   Vector Inverse(Vector scrPos) {
  43.     float sx = window.width / viewport.width;
  44.     float sy = window.height / viewport.height;
  45.     return Vector(
  46.         (scrPos.x – viewport.left) * sx + window.left,
  47.         (scrPos.y – viewport.bottom) * sy + window.bottom
  48.         );
  49.   }
  50. };
  51.  
  52. class Scene {
  53.   // ...
  54.   Camera camera;
  55.   // ...
  56.  
  57.   void InputPipe(int X, int Y) {
  58.     Vector wPos = camera.Inverse(Vector(X, Y));
  59.     // ...
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement