Stradigos

glDrawElementsBaseVertex

Jul 12th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 KB | None | 0 0
  1.         ///////////////
  2.     // Locations //
  3.     ///////////////
  4.  
  5.     GLint ambientLightLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "AmbientLight" );
  6.     assert ( ambientLightLocation != -1 );
  7.     GLint lightPositionLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "LightPositionWorld" );
  8.     assert ( lightPositionLocation != -1 );
  9.     GLint specularPowerLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "SpecularPower" );
  10.    
  11.     GLint diffuseColorLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "DiffuseColor" );
  12.     assert ( lightPositionLocation != -1 );
  13.     GLint specularColorLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "SpecularColor" );
  14.     assert ( lightPositionLocation != -1 );
  15.  
  16.     GLint positionLocation = 0;
  17.     GLint normalLocation = 1;
  18.     GLint offsetLocation = 2;
  19.  
  20.     GLint fullTransformMatrixLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "MVP" );
  21.     assert ( fullTransformMatrixLocation != -1 );
  22.  
  23.     GLint modelToWorldMatrixLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "ModelViewMatrix" );
  24.     assert ( modelToWorldMatrixLocation != -1 );
  25.  
  26.     ////////////////////
  27.     // Vertex Buffers //
  28.     ////////////////////
  29.  
  30.     // Object Description
  31.     int objectVertexSize = ms->GetVertices ( ms->Lookup ( object ) ).size ();
  32.     int objectIndexSize = ms->GetIndices ( ms->Lookup ( object ) ).size ();
  33.     int objectNormalSize = ms->GetNormals ( ms->Lookup ( object ) ).size ();
  34.  
  35.     int objectVertexBytes = objectVertexSize * sizeof ( GLfloat );
  36.     int objectIndexBytes = objectIndexSize * sizeof ( GLuint );
  37.     int objectNormalBytes = objectNormalSize * sizeof ( GLfloat );
  38.  
  39.     int objectSizeBytes = objectVertexBytes + objectIndexBytes + objectNormalBytes;
  40.  
  41.     // Plane Description
  42.     int planeVertexSize = ms->GetVertices ( ms->Lookup ( plane ) ).size ();
  43.     int planeIndexSize = ms->GetIndices ( ms->Lookup ( plane ) ).size ();
  44.     int planeNormalSize = ms->GetNormals ( ms->Lookup ( plane ) ).size ();
  45.  
  46.     int planeVertexBytes = planeVertexSize * sizeof ( GLfloat );
  47.     int planeIndexBytes = planeIndexSize * sizeof ( GLuint );
  48.     int planeNormalBytes = planeNormalSize * sizeof ( GLfloat );
  49.  
  50.     int planeSizeBytes = planeVertexBytes + planeIndexBytes + planeNormalBytes;
  51.    
  52.     // Buffer Base
  53.     GLuint buffer;
  54.     glCreateBuffers ( 1, &buffer ); // Create a new VBO and use the variable to store the VBO id
  55.     glBindBuffer ( GL_ARRAY_BUFFER, buffer ); // Make the new VBO active
  56.     glBufferData ( GL_ARRAY_BUFFER, objectSizeBytes + planeSizeBytes, nullptr, GL_STATIC_DRAW ); // Upload the vertex data to the video device
  57.    
  58.     // Object Subdata
  59.     glBufferSubData ( GL_ARRAY_BUFFER, 0, objectVertexBytes, ms->GetVertices ( ms->Lookup ( object ) ).data () );
  60.     glBufferSubData ( GL_ARRAY_BUFFER, objectVertexBytes, objectIndexBytes, ms->GetIndices ( ms->Lookup ( object ) ).data () );
  61.     glBufferSubData ( GL_ARRAY_BUFFER, objectVertexBytes + objectIndexBytes, objectNormalBytes, ms->GetNormals ( ms->Lookup ( object ) ).data () );
  62.  
  63.     // Plane Subdata
  64.     glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes, planeVertexBytes, ms->GetVertices ( ms->Lookup ( plane ) ).data () );
  65.     glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes + planeVertexBytes, planeIndexBytes, ms->GetIndices ( ms->Lookup ( plane ) ).data () );
  66.     glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes + planeVertexBytes + planeIndexBytes, objectNormalBytes, ms->GetNormals ( ms->Lookup ( plane ) ).data () );
  67.  
  68.     //////////////////
  69.     // Vertex Array //
  70.     //////////////////
  71.  
  72.     // Create a new VAO and use the variable to store the VAO id
  73.     GLuint vertexArray;
  74.     glCreateVertexArrays ( 1, &vertexArray );
  75.  
  76.     // Setup the formats
  77.     glVertexArrayAttribFormat ( vertexArray, positionLocation, 3, GL_FLOAT, GL_FALSE, 0 ); // Specify that our position data is going into attribute index 0 and contains 3 floats per vertex
  78.     glVertexArrayAttribFormat ( vertexArray, normalLocation, 3, GL_FLOAT, GL_FALSE, 0 );
  79.  
  80.     // Setup the buffer sources
  81.     glVertexArrayElementBuffer ( vertexArray, buffer );
  82.     glVertexArrayVertexBuffer ( vertexArray, positionLocation, buffer, 0, 3 * sizeof ( float ) );
  83.     glVertexArrayVertexBuffer ( vertexArray, normalLocation, buffer, (objectVertexBytes + objectIndexBytes), 3 * sizeof ( float ) ); // Issue 2
  84.    
  85.     // Link
  86.     glVertexArrayAttribBinding ( vertexArray, positionLocation, 0 );
  87.     glVertexArrayAttribBinding ( vertexArray, normalLocation, 1 );
  88.  
  89.     // Enable
  90.     glEnableVertexArrayAttrib ( vertexArray, positionLocation ); // Enable attribute
  91.     glEnableVertexArrayAttrib ( vertexArray, normalLocation ); 
  92.  
  93.     ////////////
  94.     // LIGHTS //
  95.     ////////////
  96.  
  97.     glm::vec3 lightPosition = glm::vec3( 0.0f, 2.0f, 0.0f );
  98.     glm::vec4 lightAmbient = glm::vec4 ( 0.05f, 0.05f, 0.05f, 1.0f );
  99.     glm::vec3 ambientColor = glm::vec3 ( 0.1f, 0.1f, 0.1f );
  100.     //glm::vec3 lightDiffuse = glm::vec3 ( 1.0f, 1.0f, 1.0f );
  101.    
  102.     glProgramUniform3fv ( program, lightPositionLocation, 1, &lightPosition[0] );
  103.     glProgramUniform4fv ( program, ambientLightLocation, 1, &lightAmbient[0] );
  104.     //glProgramUniform3fv ( program, ambientColorLocation, 1, &ambientColor[0] );
  105.  
  106.     //////////
  107.     // LOOP //
  108.     //////////
  109.  
  110.     double x, y;
  111.     glm::mat4 FullTransformMatrix;
  112.     glEnable ( GL_DEPTH_TEST );
  113.     glm::mat4 translation;
  114.  
  115.     glm::mat4 ModelMatrix;
  116.     glm::mat4 ModelViewMatrix;
  117.     glm::mat3 NormalMatrix;
  118.     glm::mat4 mvp;
  119.     glm::vec3 specularPower;
  120.     glm::vec3 color;
  121.     glm::vec3 specularColor;
  122.  
  123.     while ( !glfwWindowShouldClose ( m_pWindow ) )
  124.     {
  125.         glfwPollEvents ();
  126.  
  127.         glfwGetCursorPos ( m_pWindow, &x, &y );
  128.         mainCamera->MouseUpdate ( glm::vec2 ( x, y ) );
  129.  
  130.         glClearColor ( 0.3f, 0.45f, 0.65f, 1.0f );
  131.         glClear ( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
  132.  
  133.         glUseProgram ( program );
  134.  
  135.         glEnable ( GL_CULL_FACE );
  136.         glCullFace ( GL_BACK );
  137.         glFrontFace ( GL_CCW );
  138.  
  139.         // Camera //
  140.         glm::mat4 ProjectionMatrix = mainCamera->GetProjectionMatrix ();
  141.         glm::mat4 ViewMatrix = mainCamera->GetViewMatrix ();
  142.        
  143.         // Shaders //
  144.         specularColor = glm::vec3( 0.3f, 0.45f, 0.65f );
  145.         glProgramUniform3fv ( program, specularColorLocation, 1, &specularColor[0] );
  146.        
  147.         specularPower = glm::vec3 ( 2.0f, 2.0f, 2.0f );
  148.         glProgramUniform3fv ( program, specularPowerLocation, 1, &specularPower[0] );
  149.  
  150.         color = glm::vec3 ( 0.7f, 0.7f, 0.7f );
  151.         glProgramUniform3fv ( program, diffuseColorLocation, 1, &color[0] );
  152.        
  153.         // Light //
  154.         specularPower = glm::vec3 ( 70.0f, 70.7f, 70.7f );
  155.         glProgramUniform3fv ( program, specularPowerLocation, 1, &specularPower[0] );
  156.  
  157.         glBindVertexArray ( vertexArray ); // if I want to draw, I have to bind
  158.  
  159.         ///////////
  160.         // Plane //
  161.         ///////////
  162.                
  163.         ModelMatrix = ts->GetModelMatrix ( ts->Lookup ( plane ) );
  164.         mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;
  165.         glProgramUniformMatrix4fv ( program, modelToWorldMatrixLocation, 1, GL_FALSE, &ModelMatrix[0][0] );
  166.         glProgramUniformMatrix4fv ( program, fullTransformMatrixLocation, 1, GL_FALSE, &mvp[0][0] );
  167.         //glDrawElements ( GL_TRIANGLES, planeIndexSize, GL_UNSIGNED_INT, ( void * ) planeVertexBytes );
  168.         glDrawElementsBaseVertex ( GL_TRIANGLES, planeIndexSize, GL_UNSIGNED_INT, ( void * ) ( objectSizeBytes + planeVertexBytes ), 0 );// ISSUE 1
  169.        
  170.         ////////////
  171.         // Object //
  172.         ////////////
  173.        
  174.         // Update data and send to shaders
  175.         color = glm::vec3 ( 0.67f, 0.56f, 0.2f );
  176.         ModelMatrix = ts->GetModelMatrix ( ts->Lookup ( object ) );
  177.         mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;
  178.         glProgramUniform3fv ( program, diffuseColorLocation, 1, &color[0] ); // Use to go below bindvertexarray
  179.         glProgramUniformMatrix4fv ( program, fullTransformMatrixLocation, 1, GL_FALSE, &mvp[0][0] );
  180.         glProgramUniformMatrix4fv ( program, modelToWorldMatrixLocation, 1, GL_FALSE, &ModelMatrix[0][0] );
  181.        
  182.         // Render
  183.         //glDrawElements ( GL_TRIANGLES, ms->GetIndices ( ms->Lookup ( object ) ).size (), GL_UNSIGNED_INT, (void *) (ms->GetVertices ( ms->Lookup ( object ) ).size () * sizeof ( GLfloat )));
  184.         glDrawElementsBaseVertex ( GL_TRIANGLES, objectIndexSize, GL_UNSIGNED_INT, ( void * ) ( objectVertexBytes ), 0 );
  185.        
  186.         glfwSwapBuffers ( m_pWindow );
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment