Advertisement
Lucassim

MyWindow.cpp

Apr 5th, 2013
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.49 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/freeglut.h>
  3.  
  4. #include <glm/glm.hpp>
  5. #include <glm/gtc/matrix_transform.hpp>
  6. #include <glm/gtx/transform2.hpp>
  7. #include <glm/gtx/projection.hpp>
  8.  
  9. #include <myWindow.h>
  10.    
  11. #include <iostream>
  12. #include <fstream>
  13. #include <vector>
  14.  
  15. void myWindow::createCube()
  16. {
  17.     if (this->cube_vertex_buffer != NULL)
  18.     {
  19.         delete this->cube_vertex_buffer;
  20.     }
  21.     this->cube_vertex_buffer_size = 3*8;
  22.     this->cube_vertex_buffer = new GLfloat[this->cube_vertex_buffer_size];
  23.  
  24.     if (this->cube_normal_buffer != NULL)
  25.     {
  26.         delete this->cube_normal_buffer;
  27.     }
  28.     this->cube_normal_buffer_size = 3*8;
  29.     this->cube_normal_buffer = new GLfloat[this->cube_normal_buffer_size];
  30.    
  31.     if (this->cube_index_buffer != NULL)
  32.     {
  33.         delete this->cube_index_buffer;
  34.     }
  35.     this->cube_index_buffer_size = 4 * 6;
  36.     this->cube_index_buffer = new GLuint[this->cube_index_buffer_size];
  37.  
  38.     this->cube_vertex_buffer[0] = 0.5f;
  39.     this->cube_vertex_buffer[1] = 0.5f;
  40.     this->cube_vertex_buffer[2] = -0.5f;
  41.  
  42.     this->cube_vertex_buffer[3] = 0.5f;
  43.     this->cube_vertex_buffer[4] = -0.5f;
  44.     this->cube_vertex_buffer[5] = -0.5f;
  45.  
  46.     this->cube_vertex_buffer[6] = -0.5f;
  47.     this->cube_vertex_buffer[7] = -0.5f;
  48.     this->cube_vertex_buffer[8] = -0.5f;
  49.  
  50.     this->cube_vertex_buffer[9] = -0.5f;
  51.     this->cube_vertex_buffer[10] = 0.5f;
  52.     this->cube_vertex_buffer[11] = -0.5f;
  53.  
  54.     this->cube_vertex_buffer[12] = 0.5f;
  55.     this->cube_vertex_buffer[13] = 0.5f;
  56.     this->cube_vertex_buffer[14] = 0.5f;
  57.  
  58.     this->cube_vertex_buffer[15] = 0.5f;
  59.     this->cube_vertex_buffer[16] = -0.5f;
  60.     this->cube_vertex_buffer[17] = 0.5f;
  61.  
  62.     this->cube_vertex_buffer[18] = -0.5f;
  63.     this->cube_vertex_buffer[19] = -0.5f;
  64.     this->cube_vertex_buffer[20] = 0.5f;
  65.  
  66.     this->cube_vertex_buffer[21] = -0.5f;
  67.     this->cube_vertex_buffer[22] = 0.5f;
  68.     this->cube_vertex_buffer[23] = 0.5f;
  69.  
  70.     this->cube_index_buffer[0] = 0;
  71.     this->cube_index_buffer[1] = 1;
  72.     this->cube_index_buffer[2] = 2;
  73.     this->cube_index_buffer[3] = 3;
  74.  
  75.     this->cube_index_buffer[4] = 4;
  76.     this->cube_index_buffer[5] = 5;
  77.     this->cube_index_buffer[6] = 6;
  78.     this->cube_index_buffer[7] = 7;
  79.    
  80.     this->cube_index_buffer[8] = 0;
  81.     this->cube_index_buffer[9] = 4;
  82.     this->cube_index_buffer[10] = 5;
  83.     this->cube_index_buffer[11] = 1;
  84.  
  85.     this->cube_index_buffer[12] = 1;
  86.     this->cube_index_buffer[13] = 5;
  87.     this->cube_index_buffer[14] = 6;
  88.     this->cube_index_buffer[15] = 2;
  89.  
  90.     this->cube_index_buffer[16] = 2;
  91.     this->cube_index_buffer[17] = 6;
  92.     this->cube_index_buffer[18] = 7;
  93.     this->cube_index_buffer[19] = 3;
  94.  
  95.     this->cube_index_buffer[20] = 3;
  96.     this->cube_index_buffer[21] = 7;
  97.     this->cube_index_buffer[22] = 4;
  98.     this->cube_index_buffer[23] = 0;
  99.  
  100.     this->cube_normal_buffer[0] = 0.5f;
  101.     this->cube_normal_buffer[1] = 0.5f;
  102.     this->cube_normal_buffer[2] = 0.0f;
  103.  
  104.     this->cube_normal_buffer[3] = 0.5f;
  105.     this->cube_normal_buffer[4] = -0.5f;
  106.     this->cube_normal_buffer[5] = 0.0f;
  107.  
  108.     this->cube_normal_buffer[6] = -0.5f;
  109.     this->cube_normal_buffer[7] = -0.5f;
  110.     this->cube_normal_buffer[8] = 0.0f;
  111.  
  112.     this->cube_normal_buffer[9] = -0.5f;
  113.     this->cube_normal_buffer[10] = 0.5f;
  114.     this->cube_normal_buffer[11] = 0.0f;
  115.  
  116.     this->cube_normal_buffer[12] = 0.5f;
  117.     this->cube_normal_buffer[13] = 0.5f;
  118.     this->cube_normal_buffer[14] = 0.0f;
  119.  
  120.     this->cube_normal_buffer[15] = 0.5f;
  121.     this->cube_normal_buffer[16] = -0.5f;
  122.     this->cube_normal_buffer[17] = 0.0f;
  123.  
  124.     this->cube_normal_buffer[18] = -0.5f;
  125.     this->cube_normal_buffer[19] = -0.5f;
  126.     this->cube_normal_buffer[20] = 0.0f;
  127.  
  128.     this->cube_normal_buffer[21] = -0.5f;
  129.     this->cube_normal_buffer[22] = 0.5f;
  130.     this->cube_normal_buffer[23] = 0.0f;
  131. }
  132.  
  133. void myWindow::createGrid(int size)
  134. {
  135.     if (this->grid_vertex_buffer != NULL)
  136.     {
  137.         delete this->grid_vertex_buffer;
  138.     }
  139.     this->grid_vertex_buffer_size = 12*(2*size +1);
  140.     this->grid_vertex_buffer = new GLfloat[this->grid_vertex_buffer_size];
  141.    
  142.    
  143.     if (this->grid_index_buffer != NULL)
  144.     {
  145.         delete this->grid_index_buffer;
  146.     }
  147.     this->grid_index_buffer_size = 4*(2*size +1);
  148.     this->grid_index_buffer = new GLuint[this->grid_index_buffer_size];
  149.  
  150.     int offset;
  151.     for (int i=0; i<size; i++)
  152.     {
  153.         offset = 24*i;
  154.         this->grid_vertex_buffer[offset++] = -size;
  155.         this->grid_vertex_buffer[offset++] = i+1;
  156.         this->grid_vertex_buffer[offset++] = 0;
  157.  
  158.         this->grid_vertex_buffer[offset++] = size;
  159.         this->grid_vertex_buffer[offset++] = i+1;
  160.         this->grid_vertex_buffer[offset++] = 0;
  161.  
  162.         this->grid_vertex_buffer[offset++] = -size;
  163.         this->grid_vertex_buffer[offset++] = -(i+1);
  164.         this->grid_vertex_buffer[offset++] = 0;
  165.  
  166.         this->grid_vertex_buffer[offset++] = size;
  167.         this->grid_vertex_buffer[offset++] = -(i+1);
  168.         this->grid_vertex_buffer[offset++] = 0;
  169.  
  170.  
  171.         this->grid_vertex_buffer[offset++] = i+1;
  172.         this->grid_vertex_buffer[offset++] = -size;
  173.         this->grid_vertex_buffer[offset++] = 0;
  174.  
  175.         this->grid_vertex_buffer[offset++] = i+1;
  176.         this->grid_vertex_buffer[offset++] = size;
  177.         this->grid_vertex_buffer[offset++] = 0;
  178.  
  179.         this->grid_vertex_buffer[offset++] = -(i+1);
  180.         this->grid_vertex_buffer[offset++] = -size;
  181.         this->grid_vertex_buffer[offset++] = 0;
  182.  
  183.         this->grid_vertex_buffer[offset++] = -(i+1);
  184.         this->grid_vertex_buffer[offset++] = size;
  185.         this->grid_vertex_buffer[offset++] = 0;
  186.     }
  187.  
  188.     offset = 24 * size;
  189.     this->grid_vertex_buffer[offset++]   = -size;
  190.     this->grid_vertex_buffer[offset++] = 0;
  191.     this->grid_vertex_buffer[offset++] = 0;
  192.  
  193.     this->grid_vertex_buffer[offset++] = size;
  194.     this->grid_vertex_buffer[offset++] = 0;
  195.     this->grid_vertex_buffer[offset++] = 0;
  196.  
  197.     this->grid_vertex_buffer[offset++] = 0;
  198.     this->grid_vertex_buffer[offset++] = -size;
  199.     this->grid_vertex_buffer[offset++] = 0;
  200.  
  201.     this->grid_vertex_buffer[offset++] = 0;
  202.     this->grid_vertex_buffer[offset++] = size;
  203.     this->grid_vertex_buffer[offset++] = 0;
  204.  
  205.     for (int i=0; i< this->grid_index_buffer_size; i++)
  206.     {
  207.         this->grid_index_buffer[i] = i;
  208.     }
  209. }
  210.  
  211. void myWindow::createSpiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop)
  212. {
  213.  
  214.     if (this->vertex_buffer.size() >0)
  215.     {
  216.         this->vertex_buffer.clear();
  217.     }
  218.  
  219.     if (this->index_buffer.size() >0)
  220.     {
  221.         this->index_buffer.clear();
  222.     }
  223.  
  224.     float PI = 3.1415f;
  225.  
  226.     for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
  227.     {
  228.         float theta = 0;
  229.         float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
  230.         float sinTheta = std::sin(theta);
  231.         float sinPhi = std::sin(phi);
  232.         float cosTheta = std::cos(theta);
  233.         float cosPhi = std::cos(phi);
  234.  
  235.         //
  236.         this->vertex_buffer.push_back(radius * cosPhi * sinTheta); // Vx
  237.         this->vertex_buffer.push_back(radius * sinPhi * sinTheta); // Vy
  238.         this->vertex_buffer.push_back(radius * cosTheta);          // Vz
  239.     }
  240.     for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber)
  241.     {
  242.         for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
  243.         {
  244.             float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops));
  245.             if (loopNumber == loops)
  246.             {
  247.                 theta = PI;
  248.             }
  249.             float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
  250.             float sinTheta = std::sin(theta);
  251.             float sinPhi = std::sin(phi);
  252.             float cosTheta = std::cos(theta);
  253.             float cosPhi = std::cos(phi);
  254.  
  255.             this->vertex_buffer.push_back(radius * cosPhi * sinTheta);
  256.             this->vertex_buffer.push_back(radius * sinPhi * sinTheta);
  257.             this->vertex_buffer.push_back(radius * cosTheta);
  258.         }
  259.     }
  260.     for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
  261.     {
  262.         this->index_buffer.push_back(loopSegmentNumber);
  263.         this->index_buffer.push_back(segmentsPerLoop + loopSegmentNumber);
  264.     }
  265.     for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber)
  266.     {
  267.         for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
  268.         {
  269.             this->index_buffer.push_back(((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber);
  270.             this->index_buffer.push_back(((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber);
  271.         }
  272.     }
  273.  
  274.     //
  275.     if (this->sphere_vertex_buffer != NULL)
  276.     {
  277.         delete[] this->sphere_vertex_buffer;
  278.     }
  279.     this->sphere_vertex_buffer = new float[this->vertex_buffer.size()];
  280.    
  281.     if ( this->sphere_index_buffer != NULL)
  282.     {
  283.         delete[] this->sphere_index_buffer;
  284.     }
  285.     this->sphere_index_buffer = new unsigned int[this->index_buffer.size()];
  286.  
  287.     std::vector<float>::iterator vertex_it;
  288.     unsigned int v = 0;
  289.     for (vertex_it = this->vertex_buffer.begin(); vertex_it != this->vertex_buffer.end(); vertex_it++)
  290.     {
  291.         this->sphere_vertex_buffer[v] = *vertex_it;
  292.         v++;
  293.     }
  294.  
  295.     std::vector<unsigned int>::iterator index_it;
  296.     unsigned int i = 0;
  297.     for (index_it = this->index_buffer.begin(); index_it != this->index_buffer.end(); index_it++)
  298.     {
  299.         this->sphere_index_buffer[i] = *index_it;
  300.         i++;
  301.     }
  302. }
  303.  
  304. void myWindow::renderGrid(glm::mat4 model_matrix)
  305. {
  306.     // Normal Matrix
  307.     glm::mat3 normal_matrix = glm::mat3 ( 1.0f );
  308.    
  309.     // Bind Normal MAtrix
  310.     GLuint location_normal_matrix = glGetUniformLocation( this->programHandle, "NormalMatrix");
  311.     if( location_normal_matrix >= 0 )
  312.     {
  313.         glUniformMatrix3fv( location_normal_matrix, 1, GL_FALSE, &normal_matrix[0][0]);
  314.     }
  315.  
  316.     // Bind Model Matrix
  317.     GLuint location_model_matrix = glGetUniformLocation( this->programHandle, "ModelMatrix");
  318.     if( location_model_matrix >= 0 )
  319.     {
  320.         glUniformMatrix4fv( location_model_matrix, 1, GL_FALSE, &model_matrix[0][0]);
  321.     }
  322.  
  323.     glEnableClientState(GL_VERTEX_ARRAY);
  324.    
  325.     glVertexPointer(3, GL_FLOAT, 0, this->grid_vertex_buffer);
  326.    
  327.     glDrawElements(GL_LINES, this->grid_index_buffer_size, GL_UNSIGNED_INT, this->grid_index_buffer);
  328.        
  329.     glDisableClientState(GL_VERTEX_ARRAY);
  330. }
  331.  
  332. void myWindow::renderSpiralSphere(glm::mat4 model_matrix)
  333. {
  334.     // Normal Matrix
  335.     glm::mat3 normal_matrix = glm::mat3 ( 1.0f );
  336.     glm::mat4 aux = this->view_matrix * model_matrix;
  337.     for (int i=0; i<3; i++)
  338.         for (int j=0; j<3; j++)
  339.             normal_matrix[i][j] = aux[i][j];
  340.  
  341.     // Bind Normal MAtrix
  342.     GLuint location_normal_matrix = glGetUniformLocation( this->programHandle, "NormalMatrix");
  343.     if( location_normal_matrix >= 0 )
  344.     {
  345.         glUniformMatrix3fv( location_normal_matrix, 1, GL_FALSE, &normal_matrix[0][0]);
  346.     }
  347.  
  348.     // Bind Model Matrix
  349.     GLuint location_model_matrix = glGetUniformLocation( this->programHandle, "ModelMatrix");
  350.     if( location_model_matrix >= 0 )
  351.     {
  352.         glUniformMatrix4fv( location_model_matrix, 1, GL_FALSE, &model_matrix[0][0]);
  353.     }
  354.  
  355.     glEnableClientState(GL_VERTEX_ARRAY);
  356.     glEnableClientState(GL_NORMAL_ARRAY);
  357.  
  358.     glVertexPointer(3, GL_FLOAT, 0, sphere_vertex_buffer);
  359.     glNormalPointer(GL_FLOAT, 0, sphere_vertex_buffer);
  360.  
  361.     glDrawElements(GL_TRIANGLE_STRIP, index_buffer.size(), GL_UNSIGNED_INT, sphere_index_buffer);
  362.  
  363.     glDisableClientState(GL_VERTEX_ARRAY);
  364.     glDisableClientState(GL_NORMAL_ARRAY);
  365. }
  366.  
  367. void myWindow::renderCube(glm::mat4 model_matrix)
  368. {
  369.     // Normal Matrix
  370.     glm::mat3 normal_matrix = glm::mat3 ( 1.0f );
  371.        
  372.     // Bind Normal MAtrix
  373.     GLuint location_normal_matrix = glGetUniformLocation( this->programHandle, "NormalMatrix");
  374.     if( location_normal_matrix >= 0 )
  375.     {
  376.         glUniformMatrix3fv( location_normal_matrix, 1, GL_FALSE, &normal_matrix[0][0]);
  377.     }
  378.  
  379.     // Bind Model Matrix
  380.     GLuint location_model_matrix = glGetUniformLocation( this->programHandle, "ModelMatrix");
  381.     if( location_model_matrix >= 0 )
  382.     {
  383.         glUniformMatrix4fv( location_model_matrix, 1, GL_FALSE, &model_matrix[0][0]);
  384.     }
  385.  
  386.     glEnableClientState(GL_VERTEX_ARRAY);
  387.     glEnableClientState(GL_NORMAL_ARRAY);
  388.  
  389.     glVertexPointer(3, GL_FLOAT, 0, cube_vertex_buffer);
  390.     glNormalPointer(GL_FLOAT, 0, cube_normal_buffer);
  391.  
  392.     glDrawElements(GL_QUADS, this->cube_index_buffer_size, GL_UNSIGNED_INT, this->cube_index_buffer);
  393.  
  394.     glDisableClientState(GL_VERTEX_ARRAY);
  395.     glDisableClientState(GL_NORMAL_ARRAY);
  396. }
  397.  
  398. void myWindow::renderArm(glm::mat4 model_matrix)
  399. {
  400.     this->renderSpiralSphere(model_matrix);
  401.     glm::mat4 m = glm::mat4 ( 1.0f );
  402.     m = glm::scale(model_matrix, glm::vec3 (0.6f, 0.6f, 3.0f) );
  403.     m = glm::translate(m , glm::vec3(0.0, 0.0, 0.5) );
  404.  
  405.     this->renderCube(m);
  406. }
  407.  
  408. void myWindow::renderMechanicArm(glm::mat4 model_matrix)
  409. {
  410.     glm::mat4 model_matrix2 = glm::rotate (model_matrix, -90.0f, glm::vec3( 1.0, 0.0, 0.0));
  411.     this->renderArm(model_matrix2);
  412.    
  413.     model_matrix = glm::translate(model_matrix , glm::vec3(0.0, 3.5, 0) );
  414.  
  415.     //Movimiento del codo
  416.     elbow_angle+=elbow_angular_speed;
  417.     model_matrix = glm::rotate (model_matrix, elbow_angle, glm::vec3( 1.0, 0.0, 0.0));
  418.    
  419.     this->renderHandAndForearm(model_matrix);
  420.    
  421.        
  422. }
  423.  
  424.  
  425. //crea mano y antebrazo en el origen centrado en el codo
  426. void myWindow::renderHandAndForearm(glm::mat4 model_matrix){
  427.     glm::mat4 model_matrix2 = glm::rotate (model_matrix, -90.0f, glm::vec3( 1.0, 0.0, 0.0));
  428.     this->renderArm(model_matrix2);
  429.  
  430.  
  431.     model_matrix = glm::translate(model_matrix , glm::vec3(0.0, 3.5, 0.0) );
  432.    
  433.     hand_x_rotation_angle+=hand_x_rotation_speed;
  434.     model_matrix = glm::rotate (model_matrix, hand_x_rotation_angle, glm::vec3( 1.0, 0.0, 0.0));
  435.    
  436.     hand_y_rotation_angle+=hand_y_rotation_speed;
  437.     model_matrix = glm::rotate (model_matrix, hand_y_rotation_angle, glm::vec3( 0.0, 1.0, 0.0));
  438.  
  439.     this->renderHand(model_matrix);
  440.  
  441. }
  442. //Crea mano en el origen apuntando en -y
  443. void myWindow::renderHand(glm::mat4 model_matrix){
  444.     changeObjectColor(1.0, 1.0, 1.0);
  445.     hand_angle+=hand_angular_speed;
  446.     if (hand_angle<0) hand_angle=0;
  447.     if (hand_angle>63) hand_angle=63;
  448.  
  449.     glm::mat4 model_matrix2 = glm::rotate (model_matrix,  -45.0f - hand_angle/2 , glm::vec3( 1.0, 0.0, 0.0));
  450.     this->renderArm(model_matrix2);
  451.  
  452.     model_matrix = glm::rotate (model_matrix, -135.0f + hand_angle/2 , glm::vec3( 1.0, 0.0, 0.0));
  453.     this->renderArm(model_matrix);
  454.    
  455. }
  456.  
  457.  
  458.  
  459. //Riel
  460. void myWindow::renderRail(glm::mat4 model_matrix){
  461.     changeObjectColor(0.0, 0.0, 0.0);
  462.     model_matrix = glm::scale(model_matrix, glm::vec3 (0.3f, 40.0f, 0.3f) );
  463.     model_matrix = glm::rotate (model_matrix, 90.0f , glm::vec3( 1.0, 0.0, 0.0));
  464.     this->renderCube(model_matrix);
  465.    
  466. }
  467.  
  468. const char* loadShaderAsString(const char* file)
  469. {
  470.     std::ifstream shader_file(file, std::ifstream::in);
  471.     std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>());
  472.     return str.c_str();
  473. }
  474.  
  475. myWindow::myWindow()
  476. {
  477.     this->sphere_vertex_buffer = NULL;
  478.     this->sphere_index_buffer = NULL;
  479.     this->grid_index_buffer = NULL;
  480.     this->grid_vertex_buffer = NULL;
  481.     this->cube_index_buffer = NULL;
  482.     this->cube_vertex_buffer = NULL;
  483.     this->cube_normal_buffer = NULL;
  484. }
  485.  
  486. void myWindow::changeObjectColor(float r, float g, float b)
  487. {
  488.     glm::vec3 diffuse_reflectivity = glm::vec3( r, g, b );
  489.  
  490.     GLuint location_diffuse_reflectivity = glGetUniformLocation( this->programHandle, "Kd");
  491.     if( location_diffuse_reflectivity >= 0 )
  492.     {
  493.         glUniform3fv( location_diffuse_reflectivity, 1, &diffuse_reflectivity[0]);
  494.     }
  495. }
  496.  
  497. void myWindow::OnRender(void)
  498. {
  499.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  500.    
  501.     //////////////////////////////////////
  502.     // View and Projection Matrices Setting
  503.     //
  504.     view_y_angle+=view_y_angular_speed;
  505.     if (view_y_angle == 360) view_y_angle = 0;
  506.     float y_rad_angle = view_y_angle*3.141592/180;
  507.     float flag = 1.0;
  508.     if (view_y_angle >90 && view_y_angle <270 )  flag = -1.0;
  509.     this->view_matrix = glm::lookAt ( glm::vec3 ( 10.0*cos(y_rad_angle) , 0.0, 10.0*sin(y_rad_angle) ),
  510.                                       glm::vec3 ( 0.0, 0.0, 0.0 ),
  511.                                       glm::vec3 ( 0.0, 0.0, flag ) );
  512.  
  513.     // Projection Matrix
  514.     glm::mat4 projection_matrix = glm::mat4 ( 1.0f );
  515.     projection_matrix = glm::infinitePerspective( 52.0f , (float)this->width / (float)this->height, 0.1f);
  516.  
  517.     glUseProgram( this->programHandle );
  518.  
  519.     // Bind View MAtrix
  520.     GLuint location_view_matrix = glGetUniformLocation( this->programHandle, "ViewMatrix");
  521.     if( location_view_matrix >= 0 )
  522.     {
  523.         glUniformMatrix4fv( location_view_matrix, 1, GL_FALSE, &view_matrix[0][0]);
  524.     }
  525.  
  526.     // Bind View Matrix
  527.     GLuint location_projection_matrix = glGetUniformLocation( this->programHandle, "ProjectionMatrix");
  528.     if( location_projection_matrix >= 0 )
  529.     {
  530.         glUniformMatrix4fv( location_projection_matrix, 1, GL_FALSE, &projection_matrix[0][0]);
  531.     }
  532.     //
  533.     ///////////////////////////////////////////
  534.    
  535.  
  536.     //////////////////////////////////////
  537.     // Bind Light Settings
  538.     glm::vec4 light_position = glm::vec4( 8.0f, 8.0f, 2.0f, 1.0f );
  539.     glm::vec3 light_intensity = glm::vec3( 1.0f, 1.0f, 1.0f );
  540.        
  541.     GLuint location_light_position = glGetUniformLocation( this->programHandle, "LightPosition");
  542.     if( location_light_position >= 0 )
  543.     {
  544.         glUniform4fv( location_light_position, 1, &light_position[0]);
  545.     }
  546.  
  547.     GLuint location_light_intensity = glGetUniformLocation( this->programHandle, "Ld");
  548.     if( location_light_intensity >= 0 )
  549.     {
  550.         glUniform3fv( location_light_intensity, 1, &light_intensity[0]);
  551.     }
  552.     //
  553.     ///////////////////////////////////////////
  554.  
  555.  
  556.     // Drawwing Grid
  557.     changeObjectColor(0.5, 0.5, 0.5);
  558.     glm::mat4 model_matrix_grid = glm::mat4 ( 1.0f );
  559.     this->renderGrid(model_matrix_grid);
  560.  
  561.     // ARM
  562.     changeObjectColor(0.0, 1.0, 1.0);
  563.    
  564.    
  565.     //Movimientos
  566.     arm_angle_from_y_axis +=arm_angle_x;
  567.     float angle_in_rads = arm_angle_from_y_axis*3.141592/180.0;
  568.     arm_location_matrix = glm::translate(arm_location_matrix , glm::vec3(0.0,arm_speed_y*cos(angle_in_rads), -arm_speed_y*sin(angle_in_rads)) );
  569.     arm_location_matrix = glm::rotate (arm_location_matrix, arm_angle_x, glm::vec3( 1.0, 0.0, 0.0));
  570.  
  571.     this->renderMechanicArm(arm_location_matrix);
  572.     this->renderRail(glm::mat4 ( 1.0f ));
  573.     glutSwapBuffers();
  574. }
  575.  
  576. void  myWindow::OnIdle()
  577. {
  578.     this->OnRender();
  579. }
  580.  
  581. // When OnInit is called, a render context (in this case GLUT-Window)
  582. // is already available!
  583. void  myWindow::OnInit(){
  584.  
  585.      // View (camera) Matrix
  586.     this->view_matrix = glm::lookAt ( glm::vec3 ( 8.0, 0.0, 3.0 ),
  587.                                       glm::vec3 ( 0.0, 0.0, 0.0 ),
  588.                                       glm::vec3 ( 0.0, 0.0, 1.0 ) );
  589.  
  590.     this->createSpiralSphere(1.0, 32, 32);
  591.     this->createGrid(10);
  592.     this->createCube();
  593.  
  594.     //Initial position of arm
  595.     this->arm_location_matrix = glm::mat4 ( 1.0f );
  596.     arm_location_matrix = glm::scale(arm_location_matrix, glm::vec3 (0.5f, 0.5f, 0.5f) );
  597.     this->arm_speed_y = 0;
  598.     this->arm_angle_x = 0;
  599.     this->elbow_angle = 0;
  600.     this->elbow_angular_speed = 0;
  601.     this->hand_angle= 30;
  602.     this->hand_angular_speed = 0;
  603.     this->hand_x_rotation_angle = 0;
  604.     this->hand_x_rotation_speed = 0;
  605.  
  606.     this->hand_y_rotation_angle = 0;
  607.     this->hand_y_rotation_speed = 0;
  608.  
  609.     arm_angle_from_y_axis=0;
  610.    
  611.     //view
  612.     view_y_angular_speed = 0;
  613.     view_y_angle = 0;
  614.  
  615.     glClearColor(0.3f, 0.3f, 0.4f, 0.0f);
  616.     glShadeModel(GL_SMOOTH);
  617.     glEnable(GL_DEPTH_TEST);
  618.  
  619.    
  620.  
  621.     // ********************************************
  622.     // Compiling the shader programms
  623.     //*********************************************
  624.  
  625.     // Do your GLEW experiments here:
  626.     if (GLEW_ARB_shading_language_100)
  627.     {
  628.         std::cout << "GLEW_ARB_shading_language_100" << std::endl;
  629.         int major, minor, revision;
  630.         const GLubyte* sVersion = glGetString(GL_SHADING_LANGUAGE_VERSION_ARB);
  631.         if (glGetError() == GL_INVALID_ENUM)
  632.         {
  633.             major = 1; minor = 0; revision=51;
  634.         }
  635.         else
  636.         {
  637.             std::string version((char*)sVersion);
  638.             std::cout << version.c_str() << std::endl;
  639.         }
  640.  
  641.         // Load vertex Shader
  642.         this->vertShader = glCreateShader (GL_VERTEX_SHADER);
  643.         if ( 0 == this->vertShader )
  644.         {
  645.             std::cout << "Error creating vertex shader" << std::endl;
  646.         }
  647.  
  648.         std::ifstream v_shader_file("DiffuseShadingVShader.vert", std::ifstream::in);
  649.         std::string v_str((std::istreambuf_iterator<char>(v_shader_file)), std::istreambuf_iterator<char>());
  650.         const char* vs_code_array[] = {v_str.c_str()};
  651.        
  652.         glShaderSource( this->vertShader, 1, vs_code_array, NULL);
  653.  
  654.         // Compilar el shader
  655.         glCompileShader( this->vertShader );
  656.  
  657.         // verificar resultado de la compilacion
  658.         GLint vs_compilation_result;
  659.         glGetShaderiv( this->vertShader, GL_COMPILE_STATUS, &vs_compilation_result );
  660.         if( GL_FALSE == vs_compilation_result )
  661.         {
  662.             std::cout << "Vertex shader compilation failed!\n" << std::endl;
  663.             GLint logLen;
  664.             glGetShaderiv( this->vertShader, GL_INFO_LOG_LENGTH, &logLen );
  665.             if( logLen > 0 )
  666.             {
  667.                 char * log = (char *)malloc(logLen);
  668.                 GLsizei written;
  669.                 glGetShaderInfoLog(vertShader, logLen, &written, log);
  670.                 std::cout << "Shader log: " << log << std::endl;
  671.                 free(log);
  672.             }
  673.         }
  674.  
  675.          // Load fragment Shader
  676.         this->fragShader = glCreateShader (GL_FRAGMENT_SHADER);
  677.         if ( 0 == this->fragShader )
  678.         {
  679.             std::cout << "Error creating fragment shader" << std::endl;
  680.         }
  681.  
  682.         std::ifstream f_shader_file("DiffuseShadingFShader.frag", std::ifstream::in);
  683.         std::string f_str((std::istreambuf_iterator<char>(f_shader_file)), std::istreambuf_iterator<char>());
  684.         const char* fs_code_array[] = {f_str.c_str()};
  685.        
  686.         glShaderSource( this->fragShader, 1, fs_code_array, NULL);
  687.  
  688.         // Compilar el shader
  689.         glCompileShader( this->fragShader );
  690.  
  691.         // verificar resultado de la compilacion
  692.         GLint fs_compilation_result;
  693.         glGetShaderiv( this->fragShader, GL_COMPILE_STATUS, &fs_compilation_result );
  694.         if( GL_FALSE == fs_compilation_result )
  695.         {
  696.             std::cout << "Fragment shader compilation failed!\n" << std::endl;
  697.             GLint logLen;
  698.             glGetShaderiv( this->fragShader, GL_INFO_LOG_LENGTH, &logLen );
  699.             if( logLen > 0 )
  700.             {
  701.                 char * log = (char *)malloc(logLen);
  702.                 GLsizei written;
  703.                 glGetShaderInfoLog( this->fragShader, logLen, &written, log);
  704.                 std::cout << "Shader log: " << log << std::endl;
  705.                 free(log);
  706.             }
  707.         }
  708.     // *******************************************
  709.  
  710.     // *******************************************
  711.     // Linking the shader programms
  712.     // *******************************************
  713.         this->programHandle = glCreateProgram();
  714.         if ( 0 == this->programHandle )
  715.         {
  716.             std::cout << "Error creating program object" << std::endl;
  717.         }
  718.         else
  719.         {
  720.             glAttachShader( this->programHandle, this->vertShader );
  721.             glAttachShader( this->programHandle, this->fragShader );
  722.  
  723.             glLinkProgram( this->programHandle );
  724.  
  725.             GLint status;
  726.             glGetProgramiv( this->programHandle, GL_LINK_STATUS, &status );
  727.             if( GL_FALSE == status )
  728.             {
  729.                 std::cout << "Failed to link shader program!\n" << std::endl;
  730.                 GLint logLen;
  731.                 glGetProgramiv( this->programHandle, GL_INFO_LOG_LENGTH, &logLen);
  732.                 if( logLen > 0 )
  733.                 {
  734.                     char * log = (char *)malloc(logLen);
  735.                     GLsizei written;
  736.                     glGetProgramInfoLog(programHandle, logLen, &written, log);
  737.                     std::cout << "Program log: \n%s" << std::endl;
  738.                     free(log);
  739.                 }
  740.             }
  741.             else
  742.             {
  743.                 glUseProgram( this->programHandle );
  744.             }
  745.         }
  746.     }
  747. }
  748.  
  749. void myWindow::OnResize(int w, int h)
  750. {
  751.     glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  752.     this->width = w;
  753.     this->height = h;
  754. }
  755.  
  756. void  myWindow::OnClose(void)
  757. {
  758. }
  759.  
  760.  
  761. void myWindow::OnMouseDown(int button, int x, int y)
  762. {
  763.     float velocidad_mano = 1;
  764.     if (button == 0){
  765.         hand_angular_speed = velocidad_mano ;
  766.     }
  767.  
  768.     if (button == 2){
  769.         hand_angular_speed = -velocidad_mano ;
  770.     }
  771.    
  772.  
  773. }
  774.  
  775. void myWindow::OnMouseUp(int button, int x, int y)
  776. {
  777.     if (button == 0 || button == 2){
  778.         hand_angular_speed = 0 ;
  779.     }
  780.  
  781. }
  782.  
  783. void myWindow::OnMouseWheel(int nWheelNumber, int nDirection, int x, int y)
  784. {
  785. }
  786.  
  787. void myWindow::OnKeyDown(int nKey, char cAscii)
  788. {      
  789.     float velocidad_y = 0.09;
  790.     float velocidad_angular_x = 1;
  791.     float velocidad_codo = 1;
  792.     float velocidad_giro_mano = 1;
  793.  
  794.     if (cAscii == 27) // 0x1b = ESC
  795.     {
  796.         this->Close(); // Close Window!
  797.     }
  798.     if (cAscii == 'd'){
  799.         arm_speed_y =velocidad_y;
  800.     }
  801.     if (cAscii == 'a'){
  802.         arm_speed_y = -velocidad_y;
  803.     }
  804.     if (cAscii == 'w'){
  805.         arm_angle_x = velocidad_angular_x;
  806.     }
  807.     if (cAscii == 's'){
  808.         arm_angle_x = -velocidad_angular_x;
  809.     }
  810.  
  811.     if (cAscii == 'r'){
  812.         elbow_angular_speed = velocidad_codo;
  813.     }
  814.     if (cAscii == 'f'){
  815.         elbow_angular_speed = -velocidad_codo;
  816.     }
  817.  
  818.     if (cAscii == 'x'){
  819.         hand_x_rotation_speed = velocidad_giro_mano;
  820.     }
  821.     if (cAscii == 'c'){
  822.         hand_x_rotation_speed = -velocidad_giro_mano;
  823.     }
  824.  
  825.     if (cAscii == 'v'){
  826.         hand_y_rotation_speed = velocidad_giro_mano;
  827.     }
  828.     if (cAscii == 'b'){
  829.         hand_y_rotation_speed = -velocidad_giro_mano;
  830.     }
  831.     if (cAscii == '2'){
  832.         view_y_angular_speed = 1;
  833.     }
  834.    
  835. };
  836.  
  837. void myWindow::OnKeyUp(int nKey, char cAscii)
  838. {
  839.     float velocidad_codo = 1;
  840.     if (cAscii == '1')
  841.         SetFullscreen(true);
  842.     else if (cAscii == 'q')
  843.         SetFullscreen(false);
  844.  
  845.     if (cAscii == 'd'|| cAscii=='a'){
  846.         arm_speed_y = 0;
  847.     }
  848.  
  849.     if (cAscii == 'w'|| cAscii=='s'){
  850.         arm_angle_x= 0;
  851.     }
  852.  
  853.     if (cAscii == 'r'|| cAscii=='f'){
  854.         elbow_angular_speed = 0;
  855.     }
  856.    
  857.     if (cAscii == 'x'|| cAscii=='c'){
  858.         hand_x_rotation_speed = 0;
  859.     }
  860.  
  861.     if (cAscii == 'v'|| cAscii=='b'){
  862.         hand_y_rotation_speed = 0;
  863.     }
  864.     if (cAscii == '2'){
  865.         view_y_angular_speed = 0;
  866.     }
  867. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement