Advertisement
Guest User

Grafika példa program

a guest
Jan 22nd, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 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. struct Vector {
  18.   float x, y;
  19.   Vector(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
  20. };
  21.  
  22. Vector trans(0, 0), scale(1, 1);
  23. float angle;
  24.  
  25. void onInitialization() {
  26.   glMatrixMode(GL_PROJECTION);
  27.   gluOrtho2D(-5, 5, -5, 5);
  28. }
  29.  
  30. void onDisplay() {
  31.   glClear(GL_COLOR_BUFFER_BIT);
  32.  
  33.   glMatrixMode(GL_MODELVIEW);
  34.   glLoadIdentity();
  35.  
  36.   // Eltolás a megadott vektorral
  37.   glTranslatef(trans.x, trans.y, 0);
  38.   // Z tengely azaz (0, 0, 1) vektor körüli forgatás
  39.   // A szöget fokban várja nem radiánban.
  40.   glRotatef(angle, 0, 0, 1);
  41.   // Skálázás (nagyítás), koordinátánként.
  42.   glScalef(scale.x, scale.y, 1);
  43.  
  44.   glLineWidth(1);
  45.   glBegin(GL_LINES); {
  46.     // Négyzetrács
  47.     for(int i = -100; i <= 100; i++) {
  48.       glColor3f(1.0f, 1.0f, 1.0f);
  49.       glVertex2f(i, -100);
  50.       glVertex2f(i, +100);
  51.  
  52.       glVertex2f(-100, i);
  53.       glVertex2f(+100, i);
  54.     }
  55.   } glEnd();
  56.  
  57.   glLineWidth(4);
  58.   glBegin(GL_LINES); {
  59.     // X tengely
  60.     glColor3f(1.0f, 0.0f, 0.0f);
  61.     glVertex2f(-5.0f, 0.0f);
  62.     glVertex2f(5.0f, 0.0f);
  63.     glVertex2f(4.5f, 0.5f);
  64.     glVertex2f(5.0f, 0.0f);
  65.     glVertex2f(4.5f, -0.5f);
  66.     glVertex2f(5.0f, 0.0f);
  67.  
  68.     // Y tengely
  69.     glColor3f(0.0f, 1.0f, 0.0f);
  70.     glVertex2f(0.0f, -5.0f);
  71.     glVertex2f(0.0f, 5.0f);
  72.     glVertex2f(0.5f, 4.5f);
  73.     glVertex2f(0.0f, 5.0f);
  74.     glVertex2f(-0.5f, 4.5f);
  75.     glVertex2f(0.0f, 5.0f);
  76.   } glEnd();
  77.  
  78.   glutSwapBuffers();
  79. }
  80.  
  81. void onIdle() {
  82.   float time = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  83.   if(2.0f < time && time < 4.0f) {
  84.     scale.x = (time-2)/2 + 1;
  85.     scale.y = (time-2)/2 * 1.5 + 1;
  86.   } else if(4.0f < time && time < 6.0f) {
  87.     angle = (time-4)/2 * 67;
  88.   } else if(6.0f < time && time < 8.0f) {
  89.     trans.x = (time-6)/2 * 2.7;
  90.     trans.y = (time-6)/2 * -3.1;
  91.   }
  92.  
  93.   glutPostRedisplay();
  94. }
  95.  
  96. void onMouse(int, int, int, int) {}
  97.  
  98. void onMouseMotion(int, int) {}
  99.  
  100. void onKeyboard(unsigned char, int, int) {}
  101.  
  102. void onKeyboardUp(unsigned char, int, int) {}
  103.  
  104. int main(int argc, char **argv) {
  105.   glutInit(&argc, argv);
  106.   glutInitWindowSize(600, 600);
  107.   glutInitWindowPosition(100, 100);
  108.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  109.  
  110.   glutCreateWindow("Grafika pelda program");
  111.  
  112.   glMatrixMode(GL_MODELVIEW);
  113.   glLoadIdentity();
  114.   glMatrixMode(GL_PROJECTION);
  115.   glLoadIdentity();
  116.  
  117.   onInitialization();
  118.  
  119.   glutDisplayFunc(onDisplay);
  120.   glutMouseFunc(onMouse);
  121.   glutIdleFunc(onIdle);
  122.   glutKeyboardFunc(onKeyboard);
  123.   glutKeyboardUpFunc(onKeyboardUp);
  124.   glutMotionFunc(onMouseMotion);
  125.  
  126.   glutMainLoop();
  127.  
  128.   return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement