Guest User

Grafika példa program

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