Guest User

Grafika példa program

a guest
Jan 23rd, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. #ifndef M_PI
  18.   #define M_PI 3.14159265359
  19. #endif
  20.  
  21. struct Vector {
  22.   float x, y;
  23.   Vector(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
  24. };
  25.  
  26. #define CIRCLE_RESOLUTION 16
  27.  
  28. struct Stickman {
  29.   const float period_time, walk_speed;
  30.   bool left_leg_is_front;
  31.   Vector pos;
  32.  
  33.   Stickman(float period_time)
  34.     : period_time(period_time)
  35.     , walk_speed(0.5f*sin(1.2f*period_time) + 0.5f*sin(1.2f*period_time-0.3f))
  36.     , left_leg_is_front(false)
  37.   { }
  38.  
  39.   void updatePos(float time) {
  40.     pos.x = time*walk_speed;
  41.   }
  42.  
  43.   void draw(float time) {
  44.     float modulo_time = fmod(time, period_time) / period_time, mod = modulo_time - 0.5f;
  45.     left_leg_is_front = fmod(time, 2*period_time) > period_time;
  46.  
  47.     const Vector head_pos(pos.x, 1.75f), neck_pos(pos.x, 1.5f), hip_pos(pos.x, 1.0f);
  48.  
  49.     // Egy kis trigonometria, a végtagok mozogjanak egy-egy körív mentén
  50.     // (valóságban nagyjából parabola pályán mozognak, de egy egyszerű
  51.     // közelítésnek a kör is megteszi). Az egyszerűség kedvéért a teljes
  52.     // járás periódusnak csak a felét animáljuk le.
  53.     Vector f_elbow_pos( // front_elblow
  54.       neck_pos.x + 0.4f*sin(1.2f*mod),
  55.       neck_pos.y - 0.4f*cos(1.2f*mod)
  56.     );
  57.     Vector f_hand_pos(
  58.       f_elbow_pos.x + 0.4f*sin(M_PI/6 + fabs(mod)),
  59.       f_elbow_pos.y - 0.4f*cos(M_PI/6 + fabs(mod))
  60.     );
  61.  
  62.     Vector b_elbow_pos(
  63.       neck_pos.x - 0.4f*sin(mod),
  64.       neck_pos.y - 0.4f*cos(mod)
  65.     );
  66.     Vector b_hand_pos(
  67.       b_elbow_pos.x + 0.4f*sin(M_PI/6 + fabs(mod)),
  68.       b_elbow_pos.y - 0.4f*cos(M_PI/6 + fabs(mod))
  69.     );
  70.  
  71.     Vector f_knee_pos(
  72.       hip_pos.x - 0.5f*sin(1.2f*mod),
  73.       hip_pos.y - 0.5f*cos(1.2f*mod)
  74.     );
  75.     Vector f_leg_pos(
  76.       f_knee_pos.x - 0.5f*sin(1.2f*mod + 0.3f),
  77.       f_knee_pos.y - 0.5f*cos(1.2f*mod)
  78.     );
  79.  
  80.     Vector b_knee_pos(
  81.       hip_pos.x + 0.5f*sin(1.2f*mod),
  82.       hip_pos.y - 0.5f*cos(1.2f*mod)
  83.     );
  84.     Vector b_leg_pos(
  85.       b_knee_pos.x + 0.5f*sin(1.2f*mod - 0.3f),
  86.       b_knee_pos.y - 0.5f*cos(1.2f*mod)
  87.     );
  88.  
  89.     glLineWidth(4.0f); // Ez a GL_LINE* primitívek vastagságát állítja
  90.     glColor3f(1.0f, 1.0f, 1.0f);
  91.  
  92.     // Fej kirajzolása
  93.     glBegin(GL_LINE_STRIP); {
  94.       float radius = head_pos.y - neck_pos.y;
  95.  
  96.       for(int i = 0; i <= CIRCLE_RESOLUTION; i++) {
  97.         float angle = float(i) / CIRCLE_RESOLUTION * 2.0f * M_PI;
  98.         glVertex2f(head_pos.x + radius*cos(angle), head_pos.y + radius*sin(angle));
  99.       }
  100.     } glEnd();
  101.  
  102.     // Test kirajzolása
  103.     glBegin(GL_LINES); {
  104.       glVertex2f(neck_pos.x, neck_pos.y);
  105.       glVertex2f(hip_pos.x, hip_pos.y);
  106.     } glEnd();
  107.  
  108.     // A bal és jobb láb mérete különbözzön egy kicsit.
  109.     if(left_leg_is_front) {
  110.       glLineWidth(4.0f);
  111.     } else {
  112.       glLineWidth(3.0f);
  113.     }
  114.  
  115.     glBegin(GL_LINES); {
  116.       glVertex2f(neck_pos.x, neck_pos.y);
  117.       glVertex2f(f_elbow_pos.x, f_elbow_pos.y);
  118.  
  119.       glVertex2f(f_elbow_pos.x, f_elbow_pos.y);
  120.       glVertex2f(f_hand_pos.x, f_hand_pos.y);
  121.  
  122.       glVertex2f(hip_pos.x, hip_pos.y);
  123.       glVertex2f(f_knee_pos.x, f_knee_pos.y);
  124.  
  125.       glVertex2f(f_knee_pos.x, f_knee_pos.y);
  126.       glVertex2f(f_leg_pos.x, f_leg_pos.y);
  127.     } glEnd();
  128.  
  129.     if(!left_leg_is_front) {
  130.       glLineWidth(4.0f);
  131.     } else {
  132.       glLineWidth(3.0f);
  133.     }
  134.  
  135.     glBegin(GL_LINES); {
  136.       glVertex2f(neck_pos.x, neck_pos.y);
  137.       glVertex2f(b_elbow_pos.x, b_elbow_pos.y);
  138.  
  139.       glVertex2f(b_elbow_pos.x, b_elbow_pos.y);
  140.       glVertex2f(b_hand_pos.x, b_hand_pos.y);
  141.  
  142.       glVertex2f(hip_pos.x, hip_pos.y);
  143.       glVertex2f(b_knee_pos.x, b_knee_pos.y);
  144.  
  145.       glVertex2f(b_knee_pos.x, b_knee_pos.y);
  146.       glVertex2f(b_leg_pos.x, b_leg_pos.y);
  147.     } glEnd();
  148.  
  149.   }
  150. } stickman(0.9f);
  151.  
  152. struct Ground {
  153.   static void Draw() {
  154.  
  155.     glLineWidth(5.0f);
  156.     glColor3f(0.0f, 1.0f, 0.0f);
  157.  
  158.     // Talaj
  159.     glBegin(GL_LINES); {
  160.       glVertex2f(-50, 0);
  161.       glVertex2f(50, 0);
  162.     } glEnd();
  163.  
  164.     glColor3f(1.0f, 1.0f, 0.0f);
  165.  
  166.     // Házikó
  167.     glBegin(GL_LINE_STRIP); {
  168.       glVertex2f(2, 0);
  169.       glVertex2f(2, 4);
  170.       glVertex2f(6, 4);
  171.       glVertex2f(6, 0);
  172.     } glEnd();
  173.  
  174.     glColor3f(1.0f, 0.0f, 0.0f);
  175.  
  176.     // Tető
  177.     glBegin(GL_LINE_STRIP); {
  178.       glVertex2f(6, 4);
  179.       glVertex2f(4, 6);
  180.       glVertex2f(2, 4);
  181.     } glEnd();
  182.  
  183.     glColor3f(1.0f, 1.0f, 1.0f);
  184.  
  185.     // Jobb Ablak
  186.     glBegin(GL_LINE_STRIP); {
  187.       glVertex2f(4.5f, 3.0f);
  188.       glVertex2f(4.5f, 3.5f);
  189.       glVertex2f(5.0f, 3.5f);
  190.       glVertex2f(5.0f, 3.0f);
  191.       glVertex2f(4.5f, 3.0f);
  192.     } glEnd();
  193.  
  194.     // Bal ablak
  195.     glBegin(GL_LINE_STRIP); {
  196.       glVertex2f(3.5f, 3.0f);
  197.       glVertex2f(3.5f, 3.5f);
  198.       glVertex2f(3.0f, 3.5f);
  199.       glVertex2f(3.0f, 3.0f);
  200.       glVertex2f(3.5f, 3.0f);
  201.     } glEnd();
  202.  
  203.     glColor3f(168.0f/255.0f, 84.0f/255.0f, 0.0f);
  204.  
  205.     // Ajtó
  206.     glBegin(GL_LINE_STRIP); {
  207.       glVertex2f(4.5f, 0);
  208.       glVertex2f(4.5f, 2);
  209.       glVertex2f(3.5f, 2);
  210.       glVertex2f(3.5f, 0);
  211.     } glEnd();
  212.  
  213.     // Fa törzs
  214.     glBegin(GL_LINE_STRIP); {
  215.       glVertex2f(10.0f, 0.0f);
  216.       glVertex2f(10.2f, 2.0f);
  217.       glVertex2f(10.0f, 4.0f);
  218.       glVertex2f(12.0f, 4.0f);
  219.       glVertex2f(11.8f, 2.0f);
  220.       glVertex2f(12.0f, 0.0f);
  221.     } glEnd();
  222.  
  223.     glColor3f(0.0f, 1.0f, 0.0f);
  224.  
  225.     // Fa lombkorona
  226.     glBegin(GL_LINE_STRIP); {
  227.      float radius = 2.0f;
  228.  
  229.       for(int i = 0; i <= CIRCLE_RESOLUTION; i++) {
  230.         float angle = float(i) / CIRCLE_RESOLUTION * 2.0f * M_PI;
  231.         glVertex2f(11.0f + radius*cos(angle), 6.0f + radius*sin(angle));
  232.       }
  233.     } glEnd();
  234.   }
  235. };
  236.  
  237.  
  238. void onDisplay() {
  239.   glClear(GL_COLOR_BUFFER_BIT);
  240.  
  241.   float time = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  242.  
  243.   stickman.updatePos(time);
  244.  
  245.   // Megmondjuk a OpenGL-nek, hogy ezután a Projekciósmátrixot
  246.   // akarjuk módosítani a transzformációs mátrix műveletekkel.
  247.   glMatrixMode(GL_PROJECTION);
  248.   // Egység mátrixot töltük be a jelenlegi vetítési mátrix helyére.
  249.   glLoadIdentity();
  250.   // Ez egy olyan merőleges (Orthogonális) vetítés, aminek eredménye képpen a karakter
  251.   // az x tengely mentén középen lesz, és 10 egység (méter) széles részt látunk a világból
  252.   // míg az y tengely mentén a képernyő alsó egy ötödében lesz, és itt is 10 egység magas
  253.   // részt látunk.
  254.   gluOrtho2D(
  255.     stickman.pos.x - 5, stickman.pos.x + 5,
  256.     stickman.pos.y - 2, stickman.pos.y + 8
  257.   );
  258.  
  259.   Ground::Draw();
  260.   stickman.draw(time);
  261.  
  262.   glutSwapBuffers();
  263. }
  264.  
  265. void onIdle() {
  266.   glutPostRedisplay();
  267. }
  268.  
  269. void onInitialization() {}
  270.  
  271. void onMouse(int, int, int, int) {}
  272.  
  273. void onMouseMotion(int, int) {}
  274.  
  275. void onKeyboard(unsigned char, int, int) {}
  276.  
  277. void onKeyboardUp(unsigned char, int, int) {}
  278.  
  279. int main(int argc, char **argv) {
  280.   glutInit(&argc, argv);
  281.   glutInitWindowSize(600, 600);
  282.   glutInitWindowPosition(100, 100);
  283.   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  284.  
  285.   glutCreateWindow("Grafika pelda program");
  286.  
  287.   glMatrixMode(GL_MODELVIEW);
  288.   glLoadIdentity();
  289.   glMatrixMode(GL_PROJECTION);
  290.   glLoadIdentity();
  291.  
  292.   onInitialization();
  293.  
  294.   glutDisplayFunc(onDisplay);
  295.   glutMouseFunc(onMouse);
  296.   glutIdleFunc(onIdle);
  297.   glutKeyboardFunc(onKeyboard);
  298.   glutKeyboardUpFunc(onKeyboardUp);
  299.   glutMotionFunc(onMouseMotion);
  300.  
  301.   glutMainLoop();
  302.  
  303.   return 0;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment