Advertisement
Guest User

Grafika pelda program

a guest
Jan 19th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdlib.h>
  3.  
  4. #if defined(__APPLE__)
  5.   #include <OpenGL/gl.h>
  6.   #include <OpenGL/glu.h>
  7.   #include <GLUT/glut.h>
  8. #else
  9.   #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
  10.     #include <windows.h>
  11.   #endif
  12.   #include <GL/gl.h>
  13.   #include <GL/glu.h>
  14.   #include <GL/glut.h>
  15. #endif
  16.  
  17. // Globalis valtozo, hogy eppen rajzolni akarunk-e
  18. bool drawing = false;
  19.  
  20. const int kScreenWidth = 600, kScreenHeight = 600;
  21.  
  22. struct Vector {
  23.   float x, y;
  24.   Vector(float x = 0, float y = 0) : x(x), y(y) {}
  25. } last_mouse_pos;
  26.  
  27. void onInitialization() {
  28.   glClearColor(0.1, 0.2, 0.3, 1); // A hatterszin beallitasa.
  29.   glClear(GL_COLOR_BUFFER_BIT); // A kepernyo torlese, az uj hatterszinnel.
  30. }
  31.  
  32. void onDisplay() {
  33.   glutSwapBuffers();
  34. }
  35.  
  36. // A esemeny kezelesekor az operacios rendszer koordinatarendszereben kapunk ertekeket,
  37. // azaz azt, hogy a bal felso saroktol hany pixel tavolsagra volt a kattintas. De nekunk
  38. // normalizalt eszkoz koordinatak (NDC - Normalized Device Coordinates) kellenek.
  39. // pl: ha az x = 147 -> az a kepernyo kozepetol(x = 300) balra van, ezert negativ lesz,
  40. // es absz erteke pedig az, hogy milyen tavolvan az origotol, ami 153 / 300.
  41. // Az oprendszer koordinata rendszerebe az y lefele no, mig NDC-ben felfele.
  42. Vector convertToNdc(float x, float y) {
  43.   Vector ret;
  44.   ret.x = (x - kScreenWidth/2) / (kScreenWidth/2);
  45.   ret.y = (kScreenHeight/2 - y) / (kScreenHeight/2);
  46.   return ret;
  47. }
  48.  
  49. void onMouse(int button, int state, int x, int y) {
  50.   if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
  51.     glClear(GL_COLOR_BUFFER_BIT); // Jobb klikkre toroljuk a kepernyot.
  52.     glutPostRedisplay(); // Szolunk, hogy az ablak tartalma megvaltozott, kerjuk a GLUT-ot, hogy hívja meg az onDisplay-t.
  53.   } else if(button == GLUT_LEFT_BUTTON) { // Ha a bal gomb allapota megvaltozott.
  54.     if(state == GLUT_DOWN) {
  55.       drawing = true; // Ha lenyomtuk akkor rajzolni akarunk.
  56.       Vector pos = convertToNdc(x, y); // Atvaltjuk a pontot.
  57.       glBegin(GL_POINTS); { // Kirajzoljuk.
  58.         glVertex2f(pos.x, pos.y);
  59.       } glEnd();
  60.       last_mouse_pos = pos; // Elmentjuk, hogy az elso szakasz, majd ebbol a pontbol indul.
  61.       glutPostRedisplay(); // Szolunk, hogy az ablak megvaltozott, kerjuk az ujrarajzolasat.
  62.     } else if(state == GLUT_UP) {
  63.       drawing = false; // Ha most engedtuk fel, akkor mar nem akarunk rajzolni.
  64.     }
  65.   }
  66. }
  67.  
  68. void onMouseMotion(int x, int y) {
  69.   if(drawing) {
  70.     Vector pos = convertToNdc(x, y); // Kiszamoljuk az eger jelenlegi helyzetet NDC-ben.
  71.     glBegin(GL_LINES); { // Kirajzolunk egy vonalat az elozo es a mostani helyzete koze.
  72.       glVertex2f(last_mouse_pos.x, last_mouse_pos.y);
  73.       glVertex2f(pos.x, pos.y);
  74.     } glEnd();
  75.     glutPostRedisplay(); // Szolunk, hogy az ablak megvaltozott, kerjuk az ujrarajzolasat.
  76.     last_mouse_pos = pos; // Frissitjuk a elozo helyzetet.
  77.   }
  78. }
  79.  
  80. void onKeyboard(unsigned char, int, int) {
  81. }
  82.  
  83. void onKeyboardUp(unsigned char, int, int) {
  84. }
  85.  
  86. void onIdle() {
  87.   // Erre azert van szukseg mert nehany ablakkezelo (foleg linux-os)
  88.   // magatol nem hivja meg az onDislay()-t egyszer se, ha csak kulon
  89.   // meg nem kerjuk erre. Windows-on erre a pár sorra nincs szükség.
  90.   static bool first_call = true;
  91.   if(first_call) {
  92.     glutPostRedisplay();
  93.     first_call = false;
  94.   }
  95. }
  96.  
  97. int main(int argc, char **argv) {
  98.   glutInit(&argc, argv);
  99.   glutInitWindowSize(600, 600);
  100.   glutInitWindowPosition(100, 100);
  101.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  102.  
  103.   glutCreateWindow("Grafika pelda program");
  104.  
  105.   glMatrixMode(GL_MODELVIEW);
  106.   glLoadIdentity();
  107.   glMatrixMode(GL_PROJECTION);
  108.   glLoadIdentity();
  109.  
  110.   onInitialization();
  111.  
  112.   glutDisplayFunc(onDisplay);
  113.   glutMouseFunc(onMouse);
  114.   glutIdleFunc(onIdle);
  115.   glutKeyboardFunc(onKeyboard);
  116.   glutKeyboardUpFunc(onKeyboardUp);
  117.   glutMotionFunc(onMouseMotion);
  118.  
  119.   glutMainLoop();
  120.  
  121.   return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement