Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. // GLParty3D.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include "glut.h"
  7.  
  8. // Function prototypes
  9. void initOpenGL();
  10.  
  11. void keyboard(unsigned char key, int x, int y);
  12.  
  13. void idle();
  14. void draw();
  15. void drawAxisLines();
  16.  
  17. // Global variables
  18. float xpos = 0.f;
  19. float ypos = 2.f;
  20. float zpos = -35.f;
  21. float xrot = 0.f;
  22. float yrot = 0.f;
  23. float zrot = 0.f;
  24.  
  25. // Constant tri-force vertex array
  26. const GLfloat triforce[48*3] = {
  27.     // The closest front faces of the triforce
  28.     -57.5f,     0.f,        0.f,
  29.      57.5f,     0.f,        0.f,
  30.      0.f,       100.f,      0.f,
  31.  
  32.      -115.f,    -100.f,     0.f,
  33.       0.f,      -100.f,     0.f,
  34.      -57.f,      0.f,       0.f,
  35.  
  36.       0.f,      -100.f,     0.f,
  37.       115.f,    -100.f,     0.f,
  38.       57.f,      0.f,       0.f,
  39.  
  40.      // The "left" side of the triforce
  41.      -115.f,    -100.f,     0.f,
  42.      -115.f,    -100.f,     20.f,
  43.       0.f,       100.f,     0.f,
  44.  
  45.      -115.f,    -100.f,     20.f,
  46.       0.f,       100.f,     0.f,
  47.       0.f,       100.f,     20.f,
  48.  
  49.      // The "right" side of the triforce
  50.      115.f,     -100.f,     0.f,
  51.      115.f,     -100.f,     20.f,
  52.      0.f,        100.f,     0.f,
  53.  
  54.      115.f,     -100.f,     20.f,
  55.      0.f,        100.f,     0.f,
  56.      0.f,        100.f,     20.f,
  57.  
  58.      // The "bottom" of the triforce
  59.      -115.f,    -100.f,     0.f,
  60.      -115.f,    -100.f,     20.f,
  61.       115.f,    -100.f,     0.f,
  62.  
  63.       -115.f,   -100.f,     20.f,
  64.       115.f,    -100.f,     0.f,
  65.       115.f,    -100.f,     20.f,
  66.  
  67.       // The "back" face of the triforce
  68.       -57.5f,   0.f,        20.f,
  69.       57.5f,    0.f,        20.f,
  70.       0.f,      100.f,      20.f,
  71.  
  72.      -115.f,    -100.f,     20.f,
  73.       0.f,      -100.f,     20.f,
  74.      -57.5f,     0.f,       20.f,
  75.  
  76.       0.f,      -100.f,     20.f,
  77.       115.f,    -100.f,     20.f,
  78.       57.5f,     0.f,       20.f,
  79.  
  80.       // The inside, lower left
  81.       0.f,      -100.f,     0.f,
  82.       0.f,      -100.f,     20.f,
  83.       57.5f,    0.f,        0.f,
  84.  
  85.       0.f,      -100.f,     20.f,
  86.       57.5f,    0.f,        0.f,
  87.       57.5f,    0.f,        20.f,
  88.  
  89.       // The inside, lower right
  90.       0.f,      -100.f,     0.f,
  91.       0.f,      -100.f,     20.f,
  92.       -57.5f,   0.f,        0.f,
  93.  
  94.       0.f,      -100.f,     20.f,
  95.       -57.5f,   0.f,        0.f,
  96.       -57.5f,   0.f,        20.f,
  97. };
  98.  
  99. // HOVEDPROGRAMMET //
  100. int main(int argc, char **argv)
  101. {
  102.     glutInit(&argc, argv);
  103.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  104.     glutInitWindowSize(600,400);
  105.     glutCreateWindow("CV: movY  FR: rotX  GT: rotY  HY: rotz");
  106.  
  107.     glutIdleFunc(idle);
  108.     glutDisplayFunc(draw);
  109.     glutKeyboardFunc(keyboard);
  110.  
  111.     initOpenGL();
  112.  
  113.     glutMainLoop();
  114.  
  115.     return 0;
  116. }
  117.  
  118. void initOpenGL()
  119. {
  120.     gluPerspective(
  121.         50,
  122.         600.f/400.f,
  123.         0.1,
  124.         1000
  125.         );
  126.     glViewport(0,0,600,400);
  127.  
  128.     glEnable(GL_DEPTH_TEST);
  129.     glEnable(GL_BLEND);
  130.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  131.  
  132.     glDisable(GL_TEXTURE_2D);
  133.     glDisable(GL_VERTEX_ARRAY);
  134.     glDisable(GL_COLOR_ARRAY);
  135.  
  136.     glClearColor(0.f, 1.f, 0.5f, 1.f);
  137.  
  138.     glMatrixMode(GL_MODELVIEW);
  139. }
  140.  
  141. void keyboard(unsigned char key, int x, int y)
  142. {
  143.     key = towlower(key);
  144.  
  145.     if (key == 'p')
  146.         exit(0);
  147.  
  148.     else if (key == 'w')
  149.         zpos += 1.f;
  150.     else if (key == 's')
  151.         zpos -= 1.f;
  152.  
  153.     else if (key == 'a')
  154.         xpos += 1.f;
  155.     else if (key == 'd')
  156.         xpos -= 1.f;
  157.  
  158.     else if (key == 'q')
  159.         ypos -= 1.f;
  160.     else if (key == 'z')
  161.         ypos += 1.f;
  162. }
  163.  
  164. void idle()
  165. {
  166.     //xrot += 0.5f;
  167.     yrot += 0.5f;
  168.  
  169.     glutPostRedisplay();
  170. }
  171. void draw()
  172. {
  173.     glPushMatrix();
  174.    
  175.     glTranslatef(xpos, ypos, zpos);
  176.     glRotatef(xrot, 1.f, 0.f, 0.f);
  177.     glRotatef(yrot, 0.f, 1.f, 0.f);
  178.     glRotatef(zrot, 0.f, 0.f, 1.f);
  179.  
  180.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  181.  
  182.     //glColor3f(1.f, 0.f, 0.f);
  183.     //glutWireTeapot(1);
  184.  
  185.     glEnable(GL_VERTEX_ARRAY);
  186.     glEnable(GL_COLOR_ARRAY);
  187.  
  188.     //drawAxisLines();
  189.  
  190.     glDisable(GL_COLOR_ARRAY);
  191.  
  192.     glColor3f(1.f, 1.f, 0.f);
  193.     glScalef(0.1f, 0.1f, 0.1f);
  194.  
  195.     glVertexPointer(3, GL_FLOAT, 0, &triforce);
  196.     glDrawArrays(GL_TRIANGLES, 0, 48);
  197.  
  198.     glDisable(GL_VERTEX_ARRAY);
  199.  
  200.     glPopMatrix();
  201.  
  202.     glutSwapBuffers();
  203. }
  204. void drawAxisLines()
  205. {
  206.     // This function expects GL_VERTEX_ARRAY to be enabled.
  207.     glLineWidth(5.f);
  208.  
  209.     GLfloat verts[] = {
  210.         -100.f, 0.f, 0.f,
  211.          100.f, 0.f, 0.f,
  212.  
  213.          0.f, -100.f, 0.f,
  214.          0.f,  100.f, 0.f,
  215.  
  216.          0.f, 0.f, -100.f,
  217.          0.f, 0.f,  100.f,
  218.     };
  219.    
  220.     GLfloat color[] = {
  221.         0.f, 0.f, 1.f,
  222.         0.f, 0.f, 1.f,
  223.  
  224.         0.f, 1.f, 0.f,
  225.         0.f, 1.f, 0.f,
  226.        
  227.         1.f, 0.f, 0.f,
  228.         1.f, 0.f, 0.f,
  229.     };
  230.  
  231.                     // Point to the color array
  232.     glColorPointer(3, GL_FLOAT, 0, &color);
  233.  
  234.                     // Point to the vertex array
  235.     glVertexPointer(3, GL_FLOAT, 0, &verts);
  236.     //OpenGL now knows that "verts" is: X * (3 * sizeof(GL_FLOAT))
  237.                     // Draw the array
  238.     glDrawArrays(GL_LINES, 0, 6);
  239.     // OpenGL now knows that the size of "verts" is 6*3*sizeof(GL_FLOAT)
  240.  
  241.     glLineWidth(1.f);
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement