Advertisement
Guest User

A2

a guest
Oct 31st, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <GLUT/glut.h>
  4. #include <OpenGL/gl.h>
  5.  
  6.  
  7. int indices[6][4] = { {1, 2, 6, 5}, {1, 4, 5, 0}, {5, 6, 7, 4}, {2, 6, 7, 3}, {0, 4, 7, 3}, {1, 0, 3, 2}};
  8. float verts[8][3] = { {-0.5,-0.5,0.5}, {-0.5,0.5,0.5}, {0.5,0.5,0.5}, {0.5,-0.5,0.5}, {-0.5,-0.5,-0.5}, {-0.5,0.5,-0.5}, {0.5,0.5,-0.5}, {0.5,-0.5,-0.5} };
  9. float cols[6][3] = { {1,0,0}, {0,1,1}, {1,1,0}, {0,1,0}, {0,0,1}, {1,0,1} };
  10.  
  11. float pos[] = {0,1,0};
  12.  
  13. float camPos[] = {5, 5, 10};
  14. float angle = 0.0f;
  15.  
  16. //draw one face of a cube
  17. void drawFaceIndex(int index)
  18. {
  19.     glBegin(GL_POLYGON);
  20.         for(int i=0; i<4; i++)
  21.         {
  22.             int vIndex = indices[index][i];
  23.             glVertex3fv(verts[vIndex]);
  24.         }
  25.     glEnd();
  26. }
  27.  
  28. //draw a cube
  29. void drawCubeIndex(void)
  30. {
  31.     for(int idx=0; idx<6; idx++)
  32.     {
  33.         glColor3fv(cols[idx]);
  34.         drawFaceIndex(idx);
  35.     }
  36. }
  37.  
  38. void init(void)
  39. {
  40.     glClearColor(0, 0, 0, 0);
  41.     glColor3f(1, 1, 1);
  42.  
  43.     glMatrixMode(GL_PROJECTION);
  44.     gluPerspective(45, 1, 1, 100);
  45. }
  46.  
  47. //display function - GLUT display callback function
  48. void display(void)
  49. {
  50.     float origin[3] = {0,0,0};
  51.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears the screen
  52.  
  53.     glMatrixMode(GL_MODELVIEW);
  54.     glLoadIdentity();
  55.     gluLookAt(camPos[0], camPos[1], camPos[2], 0,0,0, 0,1,0);
  56.  
  57.     glPushMatrix();
  58.  
  59.     drawCubeIndex();
  60.  
  61.     glTranslatef(2, 0, 0);
  62.  
  63.     glPopMatrix();
  64.  
  65.     glutSwapBuffers();
  66. }
  67.  
  68. void kbd(unsigned char key, int x, int y)
  69. {
  70.     //if the "q" key is pressed, quit the program
  71.     if(key == 'q' || key == 'Q')
  72.     {
  73.         exit(0);
  74.     }
  75. }
  76.  
  77. void special(int key, int x, int y)
  78. {
  79.     /* arrow key presses move the camera */
  80.     switch(key)
  81.     {
  82.         case GLUT_KEY_LEFT:
  83.             camPos[0]-=0.1;
  84.             break;
  85.  
  86.         case GLUT_KEY_RIGHT:
  87.             camPos[0]+=0.1;
  88.             break;
  89.  
  90.         case GLUT_KEY_UP:
  91.             camPos[2] -= 0.1;
  92.             break;
  93.  
  94.         case GLUT_KEY_DOWN:
  95.             camPos[2] += 0.1;
  96.             break;
  97.        
  98.         case GLUT_KEY_HOME:
  99.             camPos[1] += 0.1;
  100.             break;
  101.  
  102.         case GLUT_KEY_END:
  103.             camPos[1] -= 0.1;
  104.             break;
  105.  
  106.     }
  107.     glutPostRedisplay();
  108. }
  109.  
  110. //main function - program entry point
  111. int main(int argc, char** argv)
  112. {
  113.     glutInit(&argc, argv);      //starts up GLUT
  114.     glutInitWindowSize(600, 600);
  115.  
  116.    
  117.     glutCreateWindow("Window"); //creates the window
  118.  
  119.  
  120.     glutDisplayFunc(display);   //registers "display" as the display callback function
  121.     glutKeyboardFunc(kbd);
  122.     glutSpecialFunc(special);
  123.     glutMainLoop();             //starts the event loop
  124.  
  125.     return(0);                  //return may not be necessary on all compilers
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement