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

Untitled

By: a guest on May 14th, 2012  |  syntax: C++  |  size: 5.32 KB  |  hits: 20  |  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. #include "pgr.h"   // includes all PGR libraries, like shader, glm, assimp ...
  2. #include "PyramidNode.h"
  3.  
  4. GLuint PyramidNode::m_vertexArrayObject  = 0;
  5. GLuint PyramidNode::m_vertexBufferObject = 0;
  6. GLuint PyramidNode::m_program            = 0;
  7.  
  8. GLint  PyramidNode::m_PVMmatrixLoc  = -1;
  9. GLint  PyramidNode::m_posLoc        = -1;
  10. GLint  PyramidNode::m_colLoc        = -1;
  11. GLint  PyramidNode::m_norLoc        = -1;
  12.  
  13. // For all tasks - multiple colors and matrix multiplication
  14. /**
  15. static const std::string strVertexShader(
  16.   "#version 130\n"
  17.   "uniform mat4 PVMmatrix;\n"
  18.   "in vec4 position;\n"
  19.   "in vec4 color;\n"
  20.   "smooth out vec4 theColor;\n"
  21.   "void main()\n"
  22.   "{\n"
  23.   "     gl_Position = PVMmatrix * position;\n"
  24.   "     theColor = color;\n"
  25.   "}\n"
  26. );
  27.  
  28. static const std::string strFragmentShader(
  29.   "#version 130\n"
  30.   "smooth in vec4 theColor;\n"
  31.   "out vec4 outputColor;\n"
  32.   "void main()\n"
  33.   "{\n"
  34.   "     outputColor = theColor;\n"
  35.   "}\n"
  36. );
  37. */
  38. static const float vertexData[] = {
  39.    // vertices for the base of the pyramid
  40.   -1.0f,    1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  41.   -1.0f,   -1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  42.    1.0f,   -1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  43.   -1.0f,    1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  44.    1.0f,   -1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  45.    1.0f,    1.0f,   0.0f,  1.0f,  1.0f,  1.0f,
  46.  
  47.    // vertices for the sides of the pyramid
  48.    0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  49.    1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  50.   -1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  51.    //1
  52.   -1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  53.   -0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  54.    0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  55.  
  56.    1.0f,    1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  57.    1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  58.    0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  59.    //3
  60.    1.0f,    1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  61.    0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  62.    0.3f,    0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  63.    //4
  64.   -1.0f,    1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  65.    1.0f,    1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  66.    0.3f,    0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  67.    //5
  68.   -1.0f,    1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  69.   -0.3f,    0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  70.   -1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  71.   //6
  72.  
  73.   -0.3f,   -0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  74.   -1.0f,   -1.0f,   0.0f, 1.0f,  1.0f,  1.0f,
  75.   -0.3f,    0.3f,  -1.0f, 1.0f,  1.0f,  1.0f,
  76.   //7
  77.  
  78.   -1.0f,    1.0f,   0.0f, 1.0f, 1.0f,  1.0f,
  79.    0.3f,    0.3f,  -1.0f, 1.0f, 1.0f,  1.0f,
  80.   -0.3f,    0.3f,  -1.0f, 1.0f, 1.0f,  1.0f,
  81.   //8
  82. };
  83.  
  84.  
  85. PyramidNode::PyramidNode(const char * name, SceneNode * parent):
  86.   SceneNode(name, parent)
  87. {
  88.   if(m_program == 0)
  89.   {
  90.     std::vector<GLuint> shaderList;
  91.  
  92.     // Push vertex shader and fragment shader
  93.         //shaderList.push_back(pgr::createShader(GL_VERTEX_SHADER,    strVertexShader  ));
  94.     //shaderList.push_back(pgr::createShader(GL_FRAGMENT_SHADER,  strFragmentShader));
  95.  
  96.         shaderList.push_back(pgr::createShader(GL_VERTEX_SHADER,    "MeshNode.vp"));
  97.     shaderList.push_back(pgr::createShader(GL_FRAGMENT_SHADER,  "MeshNode.fp"));
  98.  
  99.     // Create the program with two shaders
  100.     m_program       = pgr::createProgram(shaderList);
  101.     m_PVMmatrixLoc  = glGetUniformLocation( m_program, "PVMmatrix");
  102.     m_posLoc        = glGetAttribLocation(  m_program, "position");
  103.     m_colLoc        = glGetAttribLocation(  m_program, "color");
  104.         m_norLoc                = glGetAttribLocation(  m_program, "normal");
  105.   }
  106.  
  107.   if(m_vertexArrayObject == 0)
  108.   {
  109.     glGenBuffers(1, &m_vertexBufferObject);
  110.     glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
  111.  
  112.           ///this buffers vertexData
  113.       glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
  114.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  115.  
  116.     glGenVertexArrays(1, &m_vertexArrayObject );
  117.     glBindVertexArray( m_vertexArrayObject );
  118.       glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferObject);
  119.       // vertices of triangles
  120.       glEnableVertexAttribArray(m_posLoc);
  121.       glVertexAttribPointer(m_posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
  122.       // 8 = 4 + 4 floats per vertex - color
  123.       //glEnableVertexAttribArray(m_colLoc);
  124.           glVertexAttribPointer(m_norLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
  125.  
  126.       //glVertexAttribPointer(m_colLoc, 6, GL_FLOAT, GL_FALSE, 0, (void*)(6*sizeof(vertexData)/(12)));
  127.     glBindVertexArray( 0 );
  128.   }
  129.  
  130. }
  131.  
  132. void PyramidNode::draw(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix)
  133. {
  134.   // inherited draw - draws all children
  135.   SceneNode::draw(view_matrix, projection_matrix);
  136.  
  137.   glm::mat4 matrix = projection_matrix * view_matrix * globalMatrix();
  138.  
  139.   glUseProgram(m_program);
  140.   glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) );
  141.  
  142.   glBindVertexArray( m_vertexArrayObject );
  143.     glDrawArrays(GL_TRIANGLES, 0, sizeof(vertexData)/(3*sizeof(float)));
  144.   glBindVertexArray( 0 );
  145. }
  146.  
  147. void PyramidNode::drawPick(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix)
  148. {
  149.   PyramidNode::draw(view_matrix, projection_matrix);
  150.  
  151.   glm::mat4 matrix = projection_matrix * view_matrix * globalMatrix();
  152.  
  153.   glUseProgram(m_program);
  154.   glUniform1i(glGetUniformLocation(m_program, "pick_id"), 42);
  155.   glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) );
  156.  
  157.   glBindVertexArray( m_vertexArrayObject );
  158.     glDrawArrays(GL_LINES, 0, 6);
  159.   glBindVertexArray( 0 );
  160. }