Guest User

Untitled

a guest
Oct 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #ifdef __APPLE__
  2. #include <OpenGL/OpenGL.h>
  3. #include <GLUT/glut.h>
  4. #else
  5. #include <glut.h>
  6. #endif
  7.  
  8. using namespace std;
  9.  
  10. //Called when a key is pressed
  11. void handleKeypress(unsigned char key, //The key that was pressed
  12.                     int x, int y) {    //The current mouse coordinates
  13.     switch (key) {
  14.         case 27: //Escape key
  15.             exit(0); //Exit the program
  16.     }
  17. }
  18.  
  19. a
  20. //Initializes 3D rendering
  21. void initRendering() {
  22.     //Makes 3D drawing work when something is in front of something else
  23.     glEnable(GL_DEPTH_TEST);
  24.     glEnable(GL_COLOR_MATERIAL);
  25.     glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
  26. }
  27.  
  28. //Called when the window is resized
  29. void handleResize(int w, int h) {
  30.     //Tell OpenGL how to convert from coordinates to pixel values
  31.     glViewport(0, 0, w, h);
  32.    
  33.     glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  34.     glLoadIdentity(); //Reset the camera
  35.     gluPerspective(45.0,                  //The camera angle
  36.                    (double)w / (double)h, //The width-to-height ratio
  37.                    1.0,                   //The near z clipping coordinate
  38.                    200.0);                //The far z clipping coordinate
  39. }
  40.  
  41. float _angle = 30.0f;
  42. float _cameraAngle = 0.0f;
  43.  
  44.  
  45. //FIS Size Variables
  46. //Should I just use an array with all of the FIS size and action variables instead of these? If the list gets too long, then I will
  47. float _radius = 0.5f;
  48. float _height = 2.0f;
  49.  
  50. //FIS Action Variables
  51. float walk_speed = 0.0f;
  52. float max_speed = 1.0f;
  53. float stance = 2.0f; //2 is Standing, 1 is crouching, .5 is prone (Prone may not be included)
  54. bool can_jump = false;
  55.  
  56. //Character FIS Spacial Variables
  57. float _origin_x = 1.0f;
  58. float _origin_y = 0.0f;
  59. float _origin_z = 0.0f;
  60. float _xangle = 0.0f;
  61. float _yangle = 0.0f;
  62. float _zangle = 0.0f;
  63.  
  64. //Character FIS Spacial and Phsyics Arrays (instead of individual variadbles)
  65. //Going to use thses arrays to phase out the individual variables. Tidier and faster.
  66. float loc[3] = {1.0f, 0.0f, 0.0f};
  67. float rot[3] = {0.0f, 0.0f, 0.0f};
  68. float loc_speed[3] = {0.0f, 0.0f, 0.0f};//Added to loc array in update.
  69. float rot_speed[3] = {0.0f, 0.0f, 0.0f};//Added to rot array in update.
  70.  
  71. //FIS Physics Variables
  72. float _xspeed = 0.0f;//speeds are added to origins in update every frame: [_origin_x = _origin_x + _xspeed] so on and so forth.
  73. float _yspeed = 0.0f;
  74. float _zspeed = 0.0f;
  75. float _xrotspeed = 0.0f;//Similar to speeds, these are added to respective angles in update.
  76. float _yrotspeed = 0.0f;
  77. float _zrotspeed = 0.0f;
  78.  
  79.  
  80. //Draws the 3D scene
  81. void drawScene() {
  82.     //Clear information from last draw
  83.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  84.    
  85.     glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  86.     glLoadIdentity(); //Reset the drawing perspective
  87.     glRotatef(-_cameraAngle, 0.0f, 0.0f, 0.0f);
  88.     glTranslatef(0.0f, 0.0f, -5.0f);
  89.  
  90.     glPushMatrix();
  91.  
  92.     glBegin(GL_QUADS);
  93.    
  94.     // These placeholders will eventually be replaced with an array that automatically draws all called quads based upon their parameters.
  95.  
  96.     //Draw a 1x1 Placeholder Facing Image Shape (FIS)
  97.     glVertex3f(-1.5f, 0.0f, 0.0f);
  98.     glVertex3f(-0.5f, 0.0f, 0.0f);
  99.     glVertex3f(-0.5f, 1.0f, 0.0f);
  100.     glVertex3f(-1.5f, 1.0f, 0.0f);
  101.  
  102.     //Draw a 1x2 Placeholder FIS
  103.     glVertex3f(loc[0] - _radius, loc[1], loc[2]);
  104.     glVertex3f(loc[0] + _radius, loc[1], loc[2]);
  105.     glVertex3f(loc[0] + _radius, loc[1] + _height, loc[2]);
  106.     glVertex3f(loc[0] - _radius, loc[1] + _height, loc[2]);
  107.    
  108.     glEnd(); //End quadrilateral coordinates
  109.     glPopMatrix();
  110.  
  111.     /*
  112.  
  113.     glPushMatrix();
  114.     glTranslatef(1.0f, 1.0f, 0.0f);
  115.     glTranslatef(0.5f, 0.0f, 0.0f);
  116.     glRotatef(_angle, 0.0f, 1.0f, 0.0f);
  117.     glScalef(0.7f, 0.7f, 0.7f);
  118.  
  119.     glBegin(GL_TRIANGLES); //Begin triangle coordinates
  120.  
  121.     //Pentagon
  122.     glVertex3f(-0.5f, -0.5f, 0.0f);
  123.     glVertex3f(0.5f, -0.5f, 0.0f);
  124.     glVertex3f(-0.5f, 0.0f, 0.0f);
  125.    
  126.     glVertex3f(-0.5f, 0.0f, 0.0f);
  127.     glVertex3f(0.5f, -0.5f, 0.0f);
  128.     glVertex3f(0.5f, 0.0f, 0.0f);
  129.    
  130.     glVertex3f(-0.5f, 0.0f, 0.0f);
  131.     glVertex3f(0.5f, 0.0f, 0.0f);
  132.     glVertex3f(0.0f, 0.5f, 0.0f);
  133.    
  134.     glEnd();
  135.     glPopMatrix();
  136.  
  137.     glTranslatef(-1.0f, 1.0f, 0.0f);
  138.     glTranslatef(0.5f, 0.0f, 0.0f);
  139.     glRotatef(_angle, 1.0f, 2.0f, 3.0f);
  140.     glBegin(GL_TRIANGLES);
  141.  
  142.     //Triangle
  143.     glColor3f(1.0f, 0.7f, 0.0f);
  144.     glVertex3f(0.5f, -0.5f, 0.0f);
  145.     glVertex3f(0.0f, 0.5f, 0.0f);
  146.     glColor3f(0.0f, 0.0f, 1.0f);
  147.     glVertex3f(-0.5f, -0.5f, 0.0f);
  148.  
  149.     glEnd(); //End triangle coordinates
  150.     */
  151.    
  152.     glutSwapBuffers(); //Send the 3D scene to the screen
  153. }
  154.  
  155. void update(int value) {
  156.     if (loc[0] != loc[0] + loc_speed[0])
  157.         loc[0] = loc[0] + loc_speed[0];
  158.  
  159.     if (loc[1] != loc[1] + loc_speed[1])
  160.         loc[1] = loc[1] + loc_speed[1];
  161.  
  162.     if (loc[2] != loc[2] + loc_speed[2])
  163.         loc[2] = loc[2] + loc_speed[2];
  164.  
  165.     if (can_jump && loc_speed[2] != 0.0f)
  166.         loc_speed[2] = 0.0f;
  167.  
  168.  
  169.     glutPostRedisplay();
  170.     glutTimerFunc(25, update, 0);
  171. }
  172.  
  173. int main(int argc, char** argv) {
  174.     //Initialize GLUT
  175.     glutInit(&argc, argv);
  176.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  177.     glutInitWindowSize(400, 400); //Set the window size
  178.    
  179.     //Create the window
  180.     glutCreateWindow("Basic Shapes - videotutorialsrock.com");
  181.     initRendering(); //Initialize rendering
  182.    
  183.     //Set handler functions for drawing, keypresses, and window resizes
  184.     glutDisplayFunc(drawScene);
  185.     glutKeyboardFunc(handleKeypress);
  186.     glutReshapeFunc(handleResize);
  187.    
  188.     glutTimerFunc(25, update, 0);
  189.     glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
  190.     return 0; //This line is never reached
  191. }
Add Comment
Please, Sign In to add comment