void SharedProjectionAndViewing::setViewMatrix( glm::mat4 viewMatrix) { if (projViewBlockSize > 0 ) { // Bind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, projViewBuffer); viewMatrix = viewMatrix; glBufferSubData(GL_UNIFORM_BUFFER, viewLocation, sizeof(glm::mat4), glm::value_ptr(viewMatrix)); } if (worldEyeBlockSize > 0 ) { // Bind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, worldEyeBuffer); glm::vec3 viewPoint = (glm::inverse(viewMatrix)[3]).xyz; glBufferSubData(GL_UNIFORM_BUFFER, eyePositionLocation, sizeof(glm::vec3), glm::value_ptr(viewPoint)); } // Unbind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, 0); checkOpenGLErrors("setViewMatrix"); } // end setViewMatrix
bool VisibleObject::update(float deltaTime) { for (Behavior* behavior : behaviors) { behavior->update(deltaTime); } size_t i = 0; while (i < children.size()) { if (!children[i]->update(deltaTime)) { delete children[i]; children.erase(children.begin() + i); } else { i++; } } checkOpenGLErrors("VisibleObject::update"); return true; }
void SharedProjectionAndViewing::determineBlockSizeSetBindingPoint( GLuint shaderProgram ) { // Get the index of the "projectionViewBlock" projViewBlockIndex = glGetUniformBlockIndex(shaderProgram, transformBlockName.c_str()); cout << transformBlockName.c_str() << " index is " << projViewBlockIndex << endl; if (checkLocationFound(transformBlockName.c_str(), projViewBlockIndex)) { // Determine the size in bytes of the uniform block. glGetActiveUniformBlockiv(shaderProgram, projViewBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &projViewBlockSize); cout << transformBlockName.c_str() << " size is " << projViewBlockSize << endl; // Assign the block to a binding point. In this case 2. glUniformBlockBinding(shaderProgram, projViewBlockIndex, projectionViewBlockBindingPoint); } // Get the index of the "EyeBlock" worldEyeBlockIndex = glGetUniformBlockIndex(shaderProgram, eyeBlockName.c_str() ); cout << eyeBlockName.c_str() << " index is " << worldEyeBlockIndex << endl; if ( checkLocationFound( "worldEyeBlock", worldEyeBlockIndex ) ) { // Determine the size in bytes of the uniform block. glGetActiveUniformBlockiv(shaderProgram, worldEyeBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &worldEyeBlockSize); cout << eyeBlockName.c_str() << " size is " << worldEyeBlockSize << endl; // Assign the block to a binding point. In this case 3. glUniformBlockBinding(shaderProgram, worldEyeBlockIndex, worldEyeBlockBindingPoint); } checkOpenGLErrors("findBlockSizeSetBindingPoint"); } // end determineBlockSizeSetBindingPoint
void VisibleObject::draw() { for (unsigned int i = 0; i < children.size(); i++) { children[i]->draw(); } checkOpenGLErrors("VisibleObject::draw"); }
void SharedMaterialProperties::setUniformBlockForShader(GLuint shaderProgram) { // Determine the size of the block and set the binding point for the block(s) determineBlockSizeSetBindingPoint(shaderProgram); // Has the buffer been created and have the byte offset been found? if (blockSizeAndOffetsSet == false) { // Set up the buffers and bind to binding points allocateBuffers(shaderProgram); // Find the byte offsets of the uniform block variables findOffsets(shaderProgram); } checkOpenGLErrors("setUniformBlockForShader"); } // end setUniformBlockForShader
void SharedMaterialProperties::findOffsets(GLuint shaderProgram) { const int numberOfNames = 6; GLuint uniformIndices[numberOfNames] = { 0 }; GLint uniformOffsets[numberOfNames] = { 0 }; const GLchar * charStringNames[] = { "object.ambientMat", "object.diffuseMat", "object.specularMat", "object.emmissiveMat", "object.specularExp", "object.textureMode" /*, "object.gSampler"*/ }; glGetUniformIndices(shaderProgram, numberOfNames, (const GLchar **)charStringNames, uniformIndices); if (uniformIndices[0] != GL_INVALID_INDEX && uniformIndices[1] != GL_INVALID_INDEX) { for (int i = 0; i < numberOfNames; i++) { checkLocFound(charStringNames[i], uniformIndices[i]); } //Get the offsets of the uniforms. The offsets in the buffer will be the same. glGetActiveUniformsiv(shaderProgram, numberOfNames, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets); for (int i = 0; i < numberOfNames; i++) { cout << '\t' << charStringNames[i] << " offset is " << uniformOffsets[i] << endl; } // Save locations ambientMatLocation = uniformOffsets[0]; diffuseMatLocation = uniformOffsets[1]; specularMatLocation = uniformOffsets[2]; emmissiveMatLocation = uniformOffsets[3]; specularExpLocation = uniformOffsets[4]; textureModeLocation = uniformOffsets[5]; //gSamplerLocation = uniformOffsets[6]; } else { cout << " Did not find names in " << materialBlockName.c_str() << endl; } checkOpenGLErrors("findOffets"); } // findOffsets
void SharedProjectionAndViewing::setProjectionMatrix( glm::mat4 projectionMatrix) { if (projViewBlockSize > 0 ) { // Bind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, projViewBuffer); projectionMatrix = projectionMatrix; glBufferSubData(GL_UNIFORM_BUFFER, projectionLocation, sizeof(glm::mat4), glm::value_ptr(projectionMatrix)); // Unbind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, 0); } checkOpenGLErrors("setProjectionMatrix"); } // end setProjectionMatrix
void SharedMaterialProperties::determineBlockSizeSetBindingPoint(GLuint shaderProgram) { // Get the index of the "materialBlock" materialBlockIndex = glGetUniformBlockIndex(shaderProgram, materialBlockName.c_str()); cout << materialBlockName.c_str() << " index is " << materialBlockIndex << endl; if (checkLocFound(materialBlockName.c_str(), materialBlockIndex)) { // Determine the size in bytes of the uniform block. glGetActiveUniformBlockiv(shaderProgram, materialBlockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &materialBlockSize); cout << materialBlockName.c_str() << " size is " << materialBlockSize << endl; // Assign the block to a binding point. glUniformBlockBinding(shaderProgram, materialBlockIndex, materialBlockBindingPoint); } checkOpenGLErrors("findBlockSizeSetBindingPoint"); } // end determineBlockSizeSetBindingPoint
void SharedProjectionAndViewing::allocateBuffers(GLuint shaderProgram) { if ( projViewBlockSize > 0 ) { // Get an identifier for a buffer glGenBuffers(1, &projViewBuffer); // Bind the buffer glBindBuffer(GL_UNIFORM_BUFFER, projViewBuffer); // Allocate the buffer. Does not load data. Note the use of NULL where the data would normally be. // Data is not loaded because the above struct will not be byte alined with the uniform block. glBufferData(GL_UNIFORM_BUFFER, projViewBlockSize, NULL, GL_DYNAMIC_DRAW); // Assign the buffer to a binding point to be the same as the uniform in the shader(s). In this case 1. glBindBufferBase(GL_UNIFORM_BUFFER, projectionViewBlockBindingPoint, projViewBuffer); blockSizeAndOffetsSet = true; } if ( worldEyeBlockSize > 0 ) { // Get an identifier for a buffer glGenBuffers(1, &worldEyeBuffer); // Bind the buffer glBindBuffer(GL_UNIFORM_BUFFER, worldEyeBuffer); // Allocate the buffer. Does not load data. Note the use of NULL where the data would normally be. // Data is not loaded because the above struct will not be byte alined with the uniform block. glBufferData(GL_UNIFORM_BUFFER, worldEyeBlockSize, NULL, GL_DYNAMIC_DRAW); // Assign the buffer to a binding point to be the same as the uniform in the shader(s). In this case 1. glBindBufferBase(GL_UNIFORM_BUFFER, worldEyeBlockBindingPoint, worldEyeBuffer); blockSizeAndOffetsSet = true; } checkOpenGLErrors("findOffets"); } // end allocateBuffers
void SharedProjectionAndViewing::setModelingMatrix(glm::mat4 modelingMatrix) { if (projViewBlockSize > 0) { // Bind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, projViewBuffer); modelMatrix = modelingMatrix; glBufferSubData(GL_UNIFORM_BUFFER, modelLocation, sizeof(glm::mat4), glm::value_ptr(modelMatrix)); glBufferSubData(GL_UNIFORM_BUFFER, normalModelLocation, sizeof(glm::mat4), glm::value_ptr(glm::transpose(glm::inverse(modelMatrix)))); // Unbind the buffer. glBindBuffer(GL_UNIFORM_BUFFER, 0); } checkOpenGLErrors("setModelingMatrix"); } // end setModelingMatrix
/* * Render and update the scene */ void OpenGLApplicationBase::RenderSceneCB() { // time in milliseconds of last frame render static GLint lastRenderTime = 0; int currentTime = glutGet( GLUT_ELAPSED_TIME ); // Get current time int elapsedTime = currentTime - lastRenderTime; // Calc time since last frame // Check if enough time has elapsed since the last render. if ( elapsedTime >= FRAME_INTERVAL) { // Save time for this frame render lastRenderTime = currentTime; // Clear the color and depth buffers glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Draw the scene objects draw(); // Flush all drawing commands and swapbuffers glutSwapBuffers(); // Update the scene for the next frame update( (float)elapsedTime / 1000.0f ); // Query OpenGL for errors. checkOpenGLErrors("RenderSceneCB"); } } // end RenderSceneCB
void MyScene::initialize() { checkOpenGLErrors("MyScene::initialize0"); // Initialize OpenGL glEnable(GL_DEPTH_TEST); // Turn depth testing glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // Set the window clear color // Build shader proram ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "vertexShaderPerPixel.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShaderPerPixel.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; perPixelShaderProgram = BuildShaderProgram(shaders); checkOpenGLErrors("MyScene::initialize1"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(perPixelShaderProgram); SharedMaterialProperties::setUniformBlockForShader(perPixelShaderProgram); SharedGeneralLighting::setUniformBlockForShader(perPixelShaderProgram); // Build shader proram ShaderInfo shadersPointFive[] = { { GL_VERTEX_SHADER, "vertexShaderPerPixel.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShaderPerPixelMultiTexture.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; GLuint modelShaderProgram = BuildShaderProgram(shadersPointFive); checkOpenGLErrors("MyScene::initialize1"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(modelShaderProgram); SharedMaterialProperties::setUniformBlockForShader(modelShaderProgram); SharedGeneralLighting::setUniformBlockForShader(modelShaderProgram); // Build shader proram ShaderInfo shaders2[] = { { GL_VERTEX_SHADER, "vertexShader.vs.glsl" }, { GL_FRAGMENT_SHADER, "fragmentShader.fs.glsl" }, { GL_NONE, NULL } // signals that there are no more shaders }; shaderProgram = BuildShaderProgram(shaders2); checkOpenGLErrors("MyScene::initialize2"); // Set up the uniform blocks for this shader SharedProjectionAndViewing::setUniformBlockForShader(shaderProgram); SharedMaterialProperties::setUniformBlockForShader(shaderProgram); SharedGeneralLighting::setUniformBlockForShader(shaderProgram); Cube* cube = new Cube(); //cube->initialize(); cube->material.setAmbientAndDiffuseMat(glm::vec4(0.1f, 0.1f, 1.0f, 1.0f)); cube->material.setupTexture("Brick.bmp", DECAL); //cube->addBehavior(new Behavior()); vector<glm::vec3> p; p.push_back(glm::vec3(0.0f, 0.0f, 0.0f)); p.push_back(glm::vec3(0.0f, 0.0f, 20.0f)); p.push_back(glm::vec3(0.0f, 10.0f, 20.0f)); p.push_back(glm::vec3(0.0f, 10.0f, -20.0f)); //points.push_back(glm::vec3(0.0f, 0.0f, 0.0f)); //points.push_back(glm::vec3(0.0f, 0.0f, 5.0f)); cube->addBehavior(new WaypointBehavior(p, 1)); addChild(cube); Sphere* sun = new Sphere(1, 64, 64); //sphere->initialize(); sun->material.setAmbientAndDiffuseMat(glm::vec4(1.0f, 1.0f, 0.1f, 1.0f)); sun->material.setupTexture("preview_sun.jpg", REPLACE_AMBIENT_DIFFUSE); sun->addBehavior(new Behavior()); sun->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 2.0f, 1.0f)); addChild(sun); Sphere* earth = new Sphere(0.5); //earth->initialize(); earth->material.setAmbientAndDiffuseMat(glm::vec4(0.0f, 0.5f, 0.0f, 1.0f)); earth->material.setupTexture("earth.bmp", REPLACE_AMBIENT_DIFFUSE); earth->addBehavior(new Behavior()); earth->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 2.5f, 1.5f)); sun->addChild(earth); Sphere* moon = new Sphere(0.25); //moon->initialize(); moon->material.setupTexture("moon.bmp", REPLACE_AMBIENT_DIFFUSE); moon->addBehavior(new Behavior()); moon->addBehavior(new OrbitBehavior(glm::vec3(0.0f, 1.0f, 0.0f), 1.0f, 2.0f)); earth->addChild(moon); //Model =================================================== AssimpModel* model = new AssimpModel("model/nanosuit.obj"); //model->initialize(); model->setShader(modelShaderProgram); model->material.setTextureMapped(REPLACE_AMBIENT_DIFFUSE); //model->material.setSpecularExponentMat(16.0f); model->addBehavior(new Behavior()); model->scale = glm::scale(glm::vec3(0.2f, 0.2f, 0.2f)); model->addBehavior(new SpinBehavior(glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::radians(30.0f))); addChild(model); //========================================================= // Initialize the shader for all the obects selectShader(0); checkOpenGLErrors("MyScene::initialize3"); Camera* cam1 = new Camera(); cam1->setViewPort(0.5, 0, 0.5, 1); cam1->enable(true); cam1 = new Camera(); cam1->setViewPort(0, 0, 0.5, 1); cam1->enable(true); // Viewing transformation //glm::mat4 viewingTransformation = glm::lookAt(glm::vec3(0.0f, 0.0f, 30.0f), // glm::vec3(0.0f, 0.0f, 0.0f), // glm::vec3(0.0f, 1.0f, 0.0f)); // Set viewing transformation //SharedProjectionAndViewing::setViewMatrix(viewingTransformation); checkOpenGLErrors("MyScene::initialize4"); // Light 1 glm::vec4 light1AmbColor(0.15f, 0.15f, 0.15f, 1.0f); glm::vec4 light1DifColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light1SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light1PositionOfDirection(5.0f, 5.0f, 5.0f, 1.0f); bool light1IsSpot = false; bool light1Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_ZERO, light1AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_ZERO, light1DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_ZERO, light1SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_ZERO, light1PositionOfDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_ZERO, light1IsSpot); SharedGeneralLighting::setEnabled(GL_LIGHT_ZERO, light1Enabled); checkOpenGLErrors("MyScene::initialize5"); // Light 2 glm::vec4 light2AmbColor(0.15f, 0.15f, 0.15, 1.0f); glm::vec4 light2DifColor(0.75f, 0.75f, 0.75f, 1.0f); glm::vec4 light2SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light2PositionOfDirection(1.0f, 1.0f, 1.0f, 0.0f); bool light2IsSpot = false; bool light2Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_ONE, light2AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_ONE, light2DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_ONE, light2SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_ONE, light2PositionOfDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_ONE, light2IsSpot); SharedGeneralLighting::setEnabled(GL_LIGHT_ONE, light2Enabled); checkOpenGLErrors("MyScene::initialize6"); // Light 3 glm::vec4 light3AmbColor(0.15f, 0.15f, 0.15, 1.0f); glm::vec4 light3DifColor(0.9f, 0.9f, 0.9f, 1.0f); glm::vec4 light3SpecColor(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 light3PositionOfDirection(0.0f, 0.0f, 12.0f, 1.0f); glm::vec3 light3SpotDirection(0.0f, 0.0f, -1.0f); bool light3IsSpot = true; GLfloat light3SpotCutOff = glm::cos(glm::radians(10.0f)); GLfloat light3SpotExponent = 0.9f; bool light3Enabled = true; SharedGeneralLighting::setAmbientColor(GL_LIGHT_TWO, light3AmbColor); SharedGeneralLighting::setDiffuseColor(GL_LIGHT_TWO, light3DifColor); SharedGeneralLighting::setSpecularColor(GL_LIGHT_TWO, light3SpecColor); SharedGeneralLighting::setPositionOrDirection(GL_LIGHT_TWO, light3PositionOfDirection); SharedGeneralLighting::setSpotDirection(GL_LIGHT_TWO, light3SpotDirection); SharedGeneralLighting::setIsSpot(GL_LIGHT_TWO, light3IsSpot); SharedGeneralLighting::setSpotCutoffCos(GL_LIGHT_TWO, light3SpotCutOff); SharedGeneralLighting::setSpotExponent(GL_LIGHT_TWO, light3SpotExponent); SharedGeneralLighting::setEnabled(GL_LIGHT_TWO, light3Enabled); checkOpenGLErrors("MyScene::initialize7"); VisibleObject::initialize(); // Initialize the shader for all the obects selectShader(0); }
void SharedProjectionAndViewing::findOffsets(GLuint shaderProgram) { const int numberOfNames = 4; GLuint uniformIndices[numberOfNames] = { 0 }; GLint uniformOffsets[numberOfNames] = { 0 }; const GLchar * charStringNames[] = { "modelMatrix", "viewingMatrix", "projectionMatrix", "normalModelMatrix" }; glGetUniformIndices(shaderProgram, numberOfNames, (const GLchar **)charStringNames, uniformIndices); if (uniformIndices[0] != GL_INVALID_INDEX && uniformIndices[1] != GL_INVALID_INDEX) { for (int i = 0; i < numberOfNames; i++) { checkLocationFound(charStringNames[i], uniformIndices[i]); } //Get the offsets of the uniforms. The offsets in the buffer will be the same. glGetActiveUniformsiv(shaderProgram, numberOfNames, uniformIndices, GL_UNIFORM_OFFSET, uniformOffsets); for (int i = 0; i < numberOfNames; i++) { cout << '\t' << charStringNames[i] << " offset is " << uniformOffsets[i] << endl; } // Save locations modelLocation = uniformOffsets[0]; viewLocation = uniformOffsets[1]; projectionLocation = uniformOffsets[2]; normalModelLocation = uniformOffsets[3]; } else { cout << " Did not find names in " << transformBlockName.c_str() << endl; } GLuint uniformEyeIndice = 0; GLint uniformEyeOffset = 0; const GLchar * eyeName[] = { "worldEyePosition" }; glGetUniformIndices(shaderProgram, 1, eyeName, &uniformEyeIndice); if (uniformEyeIndice != GL_INVALID_INDEX) { //Get the offsets of the uniforms. The offsets in the buffer will be the same. glGetActiveUniformsiv(shaderProgram, 1, &uniformEyeIndice, GL_UNIFORM_OFFSET, &uniformEyeOffset); cout << '\t' << eyeName[0] << " offset is " << uniformEyeOffset << endl; // Save location eyePositionLocation = uniformEyeOffset; } else { cout << " Did not find names in " << eyeBlockName.c_str() << endl; } checkOpenGLErrors("findOffets"); } // findOffsets