Don't like ads? PRO users don't see any ads ;-)
Guest

Main code for cube

By: Shadowfury333 on Jun 27th, 2012  |  syntax: C++  |  size: 7.72 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* square using vertex buffer objects */
  2.  
  3. #include "include/Angel.h"
  4. #include <string>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <streambuf>
  8. #define WINDOW_TITLE "Tutorial"
  9. using namespace std;
  10.  
  11. void createVBO( void );
  12. void createCube( void );
  13. void destroyCube( void );
  14. void drawCube( void );
  15.  
  16. struct vertex {
  17.         vec4 XYZW;
  18.         vec4 RGBA;
  19. };
  20.  
  21. int current_width = 800, current_height = 600, window_id = 0;
  22. uint frame_count = 0;
  23. GLuint proj_mat_uniform_loc, view_mat_uniform_loc, model_mat_uniform_loc, buffer_ids[3], program;
  24. GLfloat cube_angle, cam_tz=-2.0f, cam_ty, cam_tx, cam_thetax, cam_thetay, cam_thetaz;
  25. mat4 proj_matrix, view_matrix, model_matrix;
  26. bool displayedRound = false;
  27. clock_t last_time = 0;
  28.         vertex vertices[] = {
  29.     { vec4(-.5f, -.5f,  .5f, 1.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f) },
  30.     { vec4(-.5f,  .5f,  .5f, 1.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f) },
  31.     { vec4( .5f,  .5f,  .5f, 1.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f) },
  32.     { vec4( .5f, -.5f,  .5f, 1.0f), vec4(1.0f, 1.0f, 0.0f, 1.0f) },
  33.     { vec4(-.5f, -.5f, -.5f, 1.0f), vec4(1.0f, 1.0f, 1.0f, 1.0f) },
  34.     { vec4(-.5f,  .5f, -.5f, 1.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f) },
  35.     { vec4( .5f,  .5f, -.5f, 1.0f), vec4(1.0f, 0.0f, 1.0f, 1.0f) },
  36.     { vec4( .5f, -.5f, -.5f, 1.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f) }
  37.         };
  38. // GLuint vao_id, vbo_id, program, colour_buffer_id, index_buffer_id[2], active_index_buffer;
  39.  
  40. //----------------------------------------------------------------------------
  41.  
  42. void
  43.         init( void )
  44. {
  45.  
  46.         program = InitShader("vshader.glsl", "fshader.glsl");  
  47.         glUseProgram(program);
  48.  
  49.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  50.  
  51.         glEnable(GL_DEPTH_TEST);
  52.         glDepthFunc(GL_LESS);
  53.  
  54.         glEnable(GL_CULL_FACE);
  55.         glCullFace(GL_BACK);
  56.         glFrontFace(GL_CCW);
  57.         view_matrix = Translate(cam_tx,cam_ty,cam_tz);
  58.         // cout << view_matrix << "\n";
  59.  
  60.         proj_matrix = Perspective(90, (float) current_width/current_height, 0.1f, 100.0f);
  61.         createCube();
  62. }
  63.  
  64. //----------------------------------------------------------------------------
  65.  
  66. void reshape (int width, int height){
  67.         current_width = width;
  68.         current_height = height;
  69.         glViewport(0,0,current_width, current_height);
  70.  
  71.         proj_matrix = Perspective(90, (float) current_width/current_height, 0.1f, 100.0f);
  72.  
  73.         glUseProgram(program);
  74.         glUniformMatrix4fv(proj_mat_uniform_loc, 1, GL_FALSE, proj_matrix);
  75.         glUseProgram(0);
  76.         // cout << "Current Height: " << height << ", Current Width: " << width << "\n";
  77. }
  78.  
  79. void
  80.         display( void )
  81. {
  82.         ++frame_count;
  83.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );    
  84.  
  85.         drawCube();
  86.  
  87.         // Force OpenGL to render
  88.         glutSwapBuffers();
  89.         glutPostRedisplay();
  90. }
  91.  
  92. //----------------------------------------------------------------------------
  93.  
  94. void
  95.         keyboard( unsigned char key, int x, int y )
  96. {
  97.         switch ( key ) {
  98.         case 033:       //ESC
  99.                 exit( EXIT_SUCCESS );
  100.                 break;
  101.         case 'w':
  102.                 cam_tz -= 0.01f;
  103.                 break;
  104.         case 's':
  105.                 cam_tz += 0.01f;
  106.                 break;
  107.         case 'a':
  108.                 cam_tx += 0.01f;
  109.                 break;
  110.         case 'd':
  111.                 cam_tx -= 0.01f;
  112.                 break;
  113.         case 'r':
  114.                 cam_ty -= 0.01f;
  115.                 break;
  116.         case 'f':
  117.                 cam_ty += 0.01f;
  118.                 break;
  119.         case 'i':
  120.                 cam_thetax -= 1;
  121.                 break;
  122.         case 'k':
  123.                 cam_thetax += 1;
  124.                 break;
  125.         case 'j':
  126.                 cam_thetay -= 1;
  127.                 break;
  128.         case 'l':
  129.                 cam_thetay += 1;
  130.                 break;
  131.         case 'u':
  132.                 cam_thetaz += 1;
  133.                 break;
  134.         case 'o':
  135.                 cam_thetaz -= 1;
  136.                 break;
  137.         default:
  138.                 break;
  139.         }
  140. }
  141.  
  142. void timer( int value ){
  143.         if (value != 0){
  144.                 string fps;
  145.                 stringstream fps_stream(fps);
  146.                 fps_stream << WINDOW_TITLE << ": " << frame_count * 4 << " Frames per Second @ " << current_width << " x " << current_height;
  147.                 glutSetWindowTitle(fps_stream.str().c_str());
  148.         }
  149.  
  150.         frame_count = 0;
  151.         glutTimerFunc(250, timer, 1);
  152. }
  153.  
  154. void computeCubeRotation(int speed) {
  155.         if(speed != 0){
  156.                 cube_angle += 45.0f/speed;
  157.                 glutTimerFunc(10, computeCubeRotation, 128);
  158.         }
  159. }
  160.  
  161. void idle( void ){
  162.         glutPostRedisplay();
  163. }
  164.  
  165. void createCube(){
  166.          GLuint indices[] =
  167.         {
  168.             0,2,1,  0,3,2,
  169.             4,3,0,  4,7,3,
  170.             4,1,5,  4,0,1,
  171.             3,6,2,  3,7,6,
  172.             1,6,5,  1,2,6,
  173.             7,5,6,  7,4,5
  174.         };
  175.  
  176.         program = InitShader("vshader_3d.glsl","fshader_3d.glsl");
  177.         model_mat_uniform_loc = glGetUniformLocation(program, "model_matrix");
  178.         view_mat_uniform_loc = glGetUniformLocation(program, "view_matrix");
  179.         proj_mat_uniform_loc = glGetUniformLocation(program, "proj_matrix");
  180.  
  181.         glGenVertexArrays(1, &buffer_ids[0]);
  182.         glBindVertexArray(buffer_ids[0]);
  183.  
  184.         glGenBuffers(2, &buffer_ids[1]);
  185.  
  186.         glBindBuffer(GL_ARRAY_BUFFER, buffer_ids[1]);
  187.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  188.  
  189.         GLuint in_Position = glGetAttribLocation(program, "in_Position");
  190.         GLuint in_Colour = glGetAttribLocation(program, "in_Colour");
  191.  
  192.         glEnableVertexAttribArray(in_Position);
  193.         glEnableVertexAttribArray(in_Colour);
  194.  
  195.         glVertexAttribPointer(in_Position, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid*)0);
  196.         glVertexAttribPointer(in_Colour, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (GLvoid*)sizeof(vertices[0].XYZW));
  197.  
  198.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_ids[2]);
  199.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
  200.  
  201.         glBindVertexArray(0);
  202.         glutTimerFunc(10, computeCubeRotation, 128);
  203. }
  204.  
  205. void destroyCube(){
  206.         glDeleteProgram(program);
  207.         glDeleteBuffers(2, &buffer_ids[1]);
  208.     glDeleteVertexArrays(1, &buffer_ids[0]);
  209. }
  210.  
  211. void drawCube(){
  212.  
  213.         view_matrix = LookAt(vec4(cam_tx, cam_ty, cam_tz-2, 1), RotateY(cam_thetay) * RotateX(cam_thetax) * vec4(cam_tx,cam_ty,cam_tz,1), RotateZ(cam_thetaz)*vec4(0,1,0,1));
  214.         // model_matrix = RotateX(cube_angle/2) * RotateY(cube_angle);
  215.  
  216.         // cout << model_matrix << "\n";
  217.  
  218.         glUseProgram(program);
  219.         glUniformMatrix4fv(model_mat_uniform_loc, 1, GL_FALSE, model_matrix);
  220.         glUniformMatrix4fv(view_mat_uniform_loc, 1, GL_FALSE, view_matrix);
  221.  
  222.         if ((int)round(cube_angle) % 15 == 0){
  223.                 if(!displayedRound){
  224.                         cout << "Cube angle: " << (int)round(cube_angle) % 360 << " degrees\n";
  225.                         cout << "MVP matrix: " << proj_matrix * view_matrix * model_matrix << "\n";
  226.                         cout << "Projection matrix: " << proj_matrix << "\n";
  227.                         cout << "MV matrix: " << view_matrix * model_matrix << "\n";
  228.                         cout << "View matrix: " << view_matrix << "\n";
  229.                         cout << "Model matrix: " << model_matrix << "\n";
  230.                         for (int i=0; i<8; i++){
  231.                                 vec4 vertex = vertices[i].XYZW;
  232.                                 cout << (proj_matrix * view_matrix * model_matrix * vertex)/(proj_matrix * view_matrix * model_matrix * vertex).w << "\n";
  233.                         }
  234.                         cout << "\n";
  235.                         displayedRound = true;
  236.                 }
  237.         }
  238.         else {
  239.                 displayedRound = false;
  240.         }
  241.  
  242.  
  243.         glBindVertexArray(buffer_ids[0]);
  244.         glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);
  245.         glBindVertexArray(0);
  246.         glUseProgram(0);
  247. }
  248.  
  249. //----------------------------------------------------------------------------
  250.  
  251. int
  252.         initWindow(int argc, char **argv)
  253. {
  254.         // Initialize glut library
  255.         glutInit( &argc, argv );
  256.         glutInitContextVersion(3,3);
  257.         glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  258.         glutInitContextProfile(GLUT_CORE_PROFILE);
  259.         glutSetOption(
  260.                 GLUT_ACTION_ON_WINDOW_CLOSE,
  261.                 GLUT_ACTION_GLUTMAINLOOP_RETURNS
  262.         );
  263.         // Create the window
  264.         glutInitWindowSize( current_width, current_height );
  265.         glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
  266.         window_id = glutCreateWindow( WINDOW_TITLE );
  267.  
  268.         // Initialize glew library
  269.         glewExperimental = GL_TRUE;
  270.         glewInit();
  271.  
  272.         // Your own initialization
  273.         init();
  274.  
  275.         // Set callback functions
  276.         glutReshapeFunc( reshape );
  277.         glutDisplayFunc( display );
  278.         glutKeyboardFunc( keyboard );
  279.         glutTimerFunc(0, timer, 0);
  280.         glutIdleFunc( idle );
  281.         glutCloseFunc( destroyCube );
  282.  
  283.         return 0;
  284. }
  285.  
  286. int
  287.         main( int argc, char **argv ) {
  288.                 initWindow(argc, argv);
  289.  
  290.                 cout << "OpenGL Version " << glGetString(GL_VERSION) <<"\n";
  291.                 // Start the main loop
  292.                 glutMainLoop();
  293.  
  294.                 return 0;
  295.         }