Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////
- // Locations //
- ///////////////
- GLint ambientLightLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "AmbientLight" );
- assert ( ambientLightLocation != -1 );
- GLint lightPositionLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "LightPositionWorld" );
- assert ( lightPositionLocation != -1 );
- GLint specularPowerLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "SpecularPower" );
- GLint diffuseColorLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "DiffuseColor" );
- assert ( lightPositionLocation != -1 );
- GLint specularColorLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "SpecularColor" );
- assert ( lightPositionLocation != -1 );
- GLint positionLocation = 0;
- GLint normalLocation = 1;
- GLint offsetLocation = 2;
- GLint fullTransformMatrixLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "MVP" );
- assert ( fullTransformMatrixLocation != -1 );
- GLint modelToWorldMatrixLocation = glGetProgramResourceLocation ( program, GL_UNIFORM, "ModelViewMatrix" );
- assert ( modelToWorldMatrixLocation != -1 );
- ////////////////////
- // Vertex Buffers //
- ////////////////////
- // Object Description
- int objectVertexSize = ms->GetVertices ( ms->Lookup ( object ) ).size ();
- int objectIndexSize = ms->GetIndices ( ms->Lookup ( object ) ).size ();
- int objectNormalSize = ms->GetNormals ( ms->Lookup ( object ) ).size ();
- int objectVertexBytes = objectVertexSize * sizeof ( GLfloat );
- int objectIndexBytes = objectIndexSize * sizeof ( GLuint );
- int objectNormalBytes = objectNormalSize * sizeof ( GLfloat );
- int objectSizeBytes = objectVertexBytes + objectIndexBytes + objectNormalBytes;
- // Plane Description
- int planeVertexSize = ms->GetVertices ( ms->Lookup ( plane ) ).size ();
- int planeIndexSize = ms->GetIndices ( ms->Lookup ( plane ) ).size ();
- int planeNormalSize = ms->GetNormals ( ms->Lookup ( plane ) ).size ();
- int planeVertexBytes = planeVertexSize * sizeof ( GLfloat );
- int planeIndexBytes = planeIndexSize * sizeof ( GLuint );
- int planeNormalBytes = planeNormalSize * sizeof ( GLfloat );
- int planeSizeBytes = planeVertexBytes + planeIndexBytes + planeNormalBytes;
- // Buffer Base
- GLuint buffer;
- glCreateBuffers ( 1, &buffer ); // Create a new VBO and use the variable to store the VBO id
- glBindBuffer ( GL_ARRAY_BUFFER, buffer ); // Make the new VBO active
- glBufferData ( GL_ARRAY_BUFFER, objectSizeBytes + planeSizeBytes, nullptr, GL_STATIC_DRAW ); // Upload the vertex data to the video device
- // Object Subdata
- glBufferSubData ( GL_ARRAY_BUFFER, 0, objectVertexBytes, ms->GetVertices ( ms->Lookup ( object ) ).data () );
- glBufferSubData ( GL_ARRAY_BUFFER, objectVertexBytes, objectIndexBytes, ms->GetIndices ( ms->Lookup ( object ) ).data () );
- glBufferSubData ( GL_ARRAY_BUFFER, objectVertexBytes + objectIndexBytes, objectNormalBytes, ms->GetNormals ( ms->Lookup ( object ) ).data () );
- // Plane Subdata
- glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes, planeVertexBytes, ms->GetVertices ( ms->Lookup ( plane ) ).data () );
- glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes + planeVertexBytes, planeIndexBytes, ms->GetIndices ( ms->Lookup ( plane ) ).data () );
- glBufferSubData ( GL_ARRAY_BUFFER, objectSizeBytes + planeVertexBytes + planeIndexBytes, objectNormalBytes, ms->GetNormals ( ms->Lookup ( plane ) ).data () );
- //////////////////
- // Vertex Array //
- //////////////////
- // Create a new VAO and use the variable to store the VAO id
- GLuint vertexArray;
- glCreateVertexArrays ( 1, &vertexArray );
- // Setup the formats
- 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
- glVertexArrayAttribFormat ( vertexArray, normalLocation, 3, GL_FLOAT, GL_FALSE, 0 );
- // Setup the buffer sources
- glVertexArrayElementBuffer ( vertexArray, buffer );
- glVertexArrayVertexBuffer ( vertexArray, positionLocation, buffer, 0, 3 * sizeof ( float ) );
- glVertexArrayVertexBuffer ( vertexArray, normalLocation, buffer, (objectVertexBytes + objectIndexBytes), 3 * sizeof ( float ) ); // Issue 2
- // Link
- glVertexArrayAttribBinding ( vertexArray, positionLocation, 0 );
- glVertexArrayAttribBinding ( vertexArray, normalLocation, 1 );
- // Enable
- glEnableVertexArrayAttrib ( vertexArray, positionLocation ); // Enable attribute
- glEnableVertexArrayAttrib ( vertexArray, normalLocation );
- ////////////
- // LIGHTS //
- ////////////
- glm::vec3 lightPosition = glm::vec3( 0.0f, 2.0f, 0.0f );
- glm::vec4 lightAmbient = glm::vec4 ( 0.05f, 0.05f, 0.05f, 1.0f );
- glm::vec3 ambientColor = glm::vec3 ( 0.1f, 0.1f, 0.1f );
- //glm::vec3 lightDiffuse = glm::vec3 ( 1.0f, 1.0f, 1.0f );
- glProgramUniform3fv ( program, lightPositionLocation, 1, &lightPosition[0] );
- glProgramUniform4fv ( program, ambientLightLocation, 1, &lightAmbient[0] );
- //glProgramUniform3fv ( program, ambientColorLocation, 1, &ambientColor[0] );
- //////////
- // LOOP //
- //////////
- double x, y;
- glm::mat4 FullTransformMatrix;
- glEnable ( GL_DEPTH_TEST );
- glm::mat4 translation;
- glm::mat4 ModelMatrix;
- glm::mat4 ModelViewMatrix;
- glm::mat3 NormalMatrix;
- glm::mat4 mvp;
- glm::vec3 specularPower;
- glm::vec3 color;
- glm::vec3 specularColor;
- while ( !glfwWindowShouldClose ( m_pWindow ) )
- {
- glfwPollEvents ();
- glfwGetCursorPos ( m_pWindow, &x, &y );
- mainCamera->MouseUpdate ( glm::vec2 ( x, y ) );
- glClearColor ( 0.3f, 0.45f, 0.65f, 1.0f );
- glClear ( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
- glUseProgram ( program );
- glEnable ( GL_CULL_FACE );
- glCullFace ( GL_BACK );
- glFrontFace ( GL_CCW );
- // Camera //
- glm::mat4 ProjectionMatrix = mainCamera->GetProjectionMatrix ();
- glm::mat4 ViewMatrix = mainCamera->GetViewMatrix ();
- // Shaders //
- specularColor = glm::vec3( 0.3f, 0.45f, 0.65f );
- glProgramUniform3fv ( program, specularColorLocation, 1, &specularColor[0] );
- specularPower = glm::vec3 ( 2.0f, 2.0f, 2.0f );
- glProgramUniform3fv ( program, specularPowerLocation, 1, &specularPower[0] );
- color = glm::vec3 ( 0.7f, 0.7f, 0.7f );
- glProgramUniform3fv ( program, diffuseColorLocation, 1, &color[0] );
- // Light //
- specularPower = glm::vec3 ( 70.0f, 70.7f, 70.7f );
- glProgramUniform3fv ( program, specularPowerLocation, 1, &specularPower[0] );
- glBindVertexArray ( vertexArray ); // if I want to draw, I have to bind
- ///////////
- // Plane //
- ///////////
- ModelMatrix = ts->GetModelMatrix ( ts->Lookup ( plane ) );
- mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;
- glProgramUniformMatrix4fv ( program, modelToWorldMatrixLocation, 1, GL_FALSE, &ModelMatrix[0][0] );
- glProgramUniformMatrix4fv ( program, fullTransformMatrixLocation, 1, GL_FALSE, &mvp[0][0] );
- //glDrawElements ( GL_TRIANGLES, planeIndexSize, GL_UNSIGNED_INT, ( void * ) planeVertexBytes );
- glDrawElementsBaseVertex ( GL_TRIANGLES, planeIndexSize, GL_UNSIGNED_INT, ( void * ) ( objectSizeBytes + planeVertexBytes ), 0 );// ISSUE 1
- ////////////
- // Object //
- ////////////
- // Update data and send to shaders
- color = glm::vec3 ( 0.67f, 0.56f, 0.2f );
- ModelMatrix = ts->GetModelMatrix ( ts->Lookup ( object ) );
- mvp = ProjectionMatrix * ViewMatrix * ModelMatrix;
- glProgramUniform3fv ( program, diffuseColorLocation, 1, &color[0] ); // Use to go below bindvertexarray
- glProgramUniformMatrix4fv ( program, fullTransformMatrixLocation, 1, GL_FALSE, &mvp[0][0] );
- glProgramUniformMatrix4fv ( program, modelToWorldMatrixLocation, 1, GL_FALSE, &ModelMatrix[0][0] );
- // Render
- //glDrawElements ( GL_TRIANGLES, ms->GetIndices ( ms->Lookup ( object ) ).size (), GL_UNSIGNED_INT, (void *) (ms->GetVertices ( ms->Lookup ( object ) ).size () * sizeof ( GLfloat )));
- glDrawElementsBaseVertex ( GL_TRIANGLES, objectIndexSize, GL_UNSIGNED_INT, ( void * ) ( objectVertexBytes ), 0 );
- glfwSwapBuffers ( m_pWindow );
- }
Advertisement
Add Comment
Please, Sign In to add comment