void DrawCube(void) { float CubeAngle; clock_t Now = clock(); if (LastTime == 0) LastTime = Now; CubeRotation += 45.0f * ((float)(Now - LastTime) / CLOCKS_PER_SEC); CubeAngle = DegreesToRadians(CubeRotation); LastTime = Now; ModelMatrix = IDENTITY_MATRIX; RotateAboutY(&ModelMatrix, CubeAngle); RotateAboutX(&ModelMatrix, CubeAngle); glUseProgram(ShaderIds[0]); ExitOnGLError("ERROR: Could not use the shader program"); glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, ModelMatrix.m); glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, ViewMatrix.m); ExitOnGLError("ERROR: Could not set the shader uniforms"); glBindVertexArray(BufferIds[0]); ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes"); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0); ExitOnGLError("ERROR: Could not draw the cube"); glBindVertexArray(0); glUseProgram(0); }
// This is a relatively expensive call as it uses the string stream, etc. // Only call this if the shader has changed void SetLightingUniformIDs(GLuint shaderID) // USES shadermanager { CShaderProgramDescription theShader; theShader.ID = shaderID; ::g_pShaderManager->GetShaderProgramInfo( theShader ); ExitOnGLError("Error getting uniform light variables"); for ( unsigned int index = 0; index != ::g_p_LightManager->getMaxLights(); index++ ) { std::stringstream ssLight; ssLight << "Lights[" << index << "]."; ::g_p_LightManager->GetLightPointer(index)->ID_bIsEnabled = theShader.getUniformVarIDFromName( ssLight.str().append("isEnabled") ); ::g_p_LightManager->GetLightPointer(index)->ID_bIsLocal = theShader.getUniformVarIDFromName( ssLight.str().append("isLocal") ); ::g_p_LightManager->GetLightPointer(index)->ID_bIsSpot = theShader.getUniformVarIDFromName( ssLight.str().append("isSpot") ); ::g_p_LightManager->GetLightPointer(index)->ID_ambientColour = theShader.getUniformVarIDFromName( ssLight.str().append("ambient") ); ::g_p_LightManager->GetLightPointer(index)->ID_diffuseColour = theShader.getUniformVarIDFromName( ssLight.str().append("color") ); ::g_p_LightManager->GetLightPointer(index)->ID_position = theShader.getUniformVarIDFromName( ssLight.str().append("position") ); ::g_p_LightManager->GetLightPointer(index)->ID_halfVector = theShader.getUniformVarIDFromName( ssLight.str().append("halfVector") ); ::g_p_LightManager->GetLightPointer(index)->ID_spotConeDirection = theShader.getUniformVarIDFromName( ssLight.str().append("coneDirection") ); ::g_p_LightManager->GetLightPointer(index)->ID_spotCosCutoff = theShader.getUniformVarIDFromName( ssLight.str().append("spotCosCutoff") ); ::g_p_LightManager->GetLightPointer(index)->ID_spotExponent = theShader.getUniformVarIDFromName( ssLight.str().append("spotExponent") ); ::g_p_LightManager->GetLightPointer(index)->ID_constantAttenuation = theShader.getUniformVarIDFromName( ssLight.str().append("constantAttenuation") ); ::g_p_LightManager->GetLightPointer(index)->ID_linearAttenuation = theShader.getUniformVarIDFromName( ssLight.str().append("linearAttenuation") ); ::g_p_LightManager->GetLightPointer(index)->ID_quadraticAttenuation = theShader.getUniformVarIDFromName( ssLight.str().append("quadraticAttenuation") ); ::g_p_LightManager->GetLightPointer(index)->ID_range = theShader.getUniformVarIDFromName( ssLight.str().append("range") ); } ExitOnGLError("Error getting uniform light variables"); return; }
void Initialize (int argc, char *argv[]) { GLenum GlewInitResult; TempString = (char *) malloc (512 + strlen(WINDOW_TITLE_PREFIX)); InitWindow (argc, argv); glewExperimental = GL_TRUE; GlewInitResult = glewInit(); if (GLEW_OK != GlewInitResult) { fprintf (stderr, "ERROR: %s\n", glewGetErrorString (GlewInitResult) ); exit (EXIT_FAILURE); } fprintf (stdout, "INFO: OpengGL Vertion: %s\n", glGetString (GL_VERSION)); glGetError (); glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LESS); ExitOnGLError ("ERROR: Could not set OpenGL depth testing options"); glEnable (GL_CULL_FACE); glCullFace (GL_BACK); glFrontFace (GL_CCW); ExitOnGLError ("ERROR: Could not set OpenGL culling options"); ModelMatrix = IDENTITY_MATRIX; ProjectionMatrix = IDENTITY_MATRIX; ViewMatrix = IDENTITY_MATRIX; TranslateMatrix (&ViewMatrix, 0, 0, -2); CreateCube (); }
void ArcCanvas::draw() { int n = 0; std::list<ArcVertexData>::iterator i; for (i = mVertexData.begin(); i != mVertexData.end(); ++i) { // std::cout << n << std::endl; glBindVertexArray(mVertexArrayIDs[n++]); ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes"); //ArcMatrix modelMatrix = i->getModelMatrix(); //printMatrix(modelMatrix); ArcApp::getInstance().getEngine().getShaderProgram(). setModelMatrix(i->getModelMatrix()); glDrawElements(GL_TRIANGLES, i->getNumIndices(), GL_UNSIGNED_INT, (GLvoid*)0); //starts at beginning each time //n++; //std::cout << n << std::endl; ExitOnGLError("ERROR: Couldn't draw"); glBindVertexArray(0); } }
void ArcCanvas::draw() { glBindVertexArray(mBufferIDs[0]); ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes"); glDrawElements(GL_TRIANGLES, mIndexSize, GL_UNSIGNED_INT, (GLvoid*)0); ExitOnGLError("ERROR: Couldn't draw"); glBindVertexArray(0); }
void DestroyCube() { glDetachShader(ShaderIds[0], ShaderIds[1]); glDetachShader(ShaderIds[0], ShaderIds[2]); glDeleteShader(ShaderIds[1]); glDeleteShader(ShaderIds[2]); glDeleteProgram(ShaderIds[0]); ExitOnGLError("ERROR: Could not destroy the shaders"); glDeleteBuffers(2, &BufferIds[1]); glDeleteVertexArrays(1, &BufferIds[0]); ExitOnGLError("ERROR: Could not destroy the buffer objects"); }
void ArcCanvas::destroy() { for (int i = 0; i < mVertexData.size(); i++) glDeleteBuffers(2, &mBufferIDs[i][0]); for (int i = 0; i < mVertexData.size(); i++) glDeleteVertexArrays(mVertexSizes[i], &mVertexArrayIDs[i]); ExitOnGLError("ERROR: Could not destroy the buffer objects"); for (int i = 0; i < mVertexData.size(); i++) { delete mVertexArrays[i]; delete mIndexArrays[i]; delete mBufferIDs[i]; } delete mVertexArrays; delete mIndexArrays; delete mVertexArrayIDs; delete mBufferIDs; std::list<ArcVertexData>::iterator i; for (i = mVertexData.begin(); i != mVertexData.end(); ++i) i->destroy(); }
void setUpShadowTexture(void) { glGenFramebuffers(1, &g_shadow_depthTexture_FrameBufferObjectID); glBindFramebuffer(GL_FRAMEBUFFER, g_shadow_depthTexture_FrameBufferObjectID); glGenTextures(1, &g_shadow_depthTexture_ID); glBindTexture(GL_TEXTURE_2D, g_shadow_depthTexture_ID); glTexStorage2D(GL_TEXTURE_2D, 11, GL_DEPTH_COMPONENT32F, SHADOW_DEPTH_TEXTURE_SIZE, SHADOW_DEPTH_TEXTURE_SIZE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, g_shadow_depthTexture_ID, 0); glDrawBuffer(GL_NONE); // No color buffer is drawn to. // Always check that our framebuffer is ok if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { return; } ExitOnGLError("There was a problem setting up the shadow texture."); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); return; }
void CGraficObject::draw(CCreature* creature) { glm::mat4 modelMatrix = glm::mat4() ; CPos* pos = &creature->getPos(); //model translate and rotate modelMatrix = glm::translate(modelMatrix, glm::vec3((float)pos->x/1000,(float)pos->y/1000,(float)pos->z/1000)); modelMatrix = glm::rotate(modelMatrix,((float)pos->d)/100,glm::vec3(0,0,1)); _model->getShader()->enable(); //enable object shader _model->bind(); //bind this model for rendering //set object specific variables for drawing _model->getShader()->setModelMatrix(&modelMatrix); _model->getShader()->setViewMatrix(masterScreen->getCamera()->getViewMatrix()); //draw glDrawElements(GL_TRIANGLES, _model->getIndicesCount(), GL_UNSIGNED_INT, (void*)0); ExitOnGLError("ERROR: Could not draw the cube"); _model->unbind(); //draw all assosiated particalsystems for(map<uint32_t, ParticalSystem*>::iterator it = _particalSystems.begin(); it != _particalSystems.end(); it++) { if(it->second) it->second->draw(masterScreen->getCamera()); } }
void ArcCanvas::destroy() { glDeleteBuffers(2, &mBufferIDs[1]); glDeleteVertexArrays(1, &mBufferIDs[0]); ExitOnGLError("ERROR: Could not destroy the buffer objects"); delete mVertexArray; delete mIndexArray; }
void ArcShaderProgram::setMatrix(ArcMatrix projectionMatrix) { glUseProgram(mProgram); ExitOnGLError("ERROR: Could not use the shader program"); glUniformMatrix4fv(mProjectionMatrixUniformLocation, 1, GL_FALSE, projectionMatrix.m); glUseProgram(0); }
void ArcShaderProgram::run(ArcCanvas &canvas, ArcMatrix view) { glUseProgram(mProgram); ExitOnGLError("ERROR: Could not use the shader program"); setViewMatrix(view); canvas.draw(); glUseProgram(0); }
void ArcShaderProgram::setViewMatrix(ArcMatrix viewMatrix) { //glUseProgram(mProgram); ExitOnGLError("ERROR: Could not use the shader program"); //std::cout << "View Matrix"; //printMatrix(viewMatrix); glUniformMatrix4fv(mViewMatrixUniformLocation, 1, GL_FALSE, viewMatrix.m); //glUseProgram(0); }
void ArcShaderProgram::setMatrices(ArcMatrix modelMatrix, ArcMatrix viewMatrix) { glUniformMatrix4fv(mModelMatrixUniformLocation, 1, GL_FALSE, modelMatrix.m); glUniformMatrix4fv(mViewMatrixUniformLocation, 1, GL_FALSE, viewMatrix.m); ExitOnGLError("ERROR: Could not set the shader uniforms"); }
void ArcShaderProgram::setUniforms() { mModelMatrixUniformLocation = glGetUniformLocation(mProgram, "ModelMatrix"); mViewMatrixUniformLocation = glGetUniformLocation(mProgram, "ViewMatrix"); mProjectionMatrixUniformLocation = glGetUniformLocation(mProgram, "ProjectionMatrix"); ExitOnGLError("ERROR: Could not get the shader uniform locations"); }
void ArcShaderProgram::destroy() { std::list<ArcShader>::iterator i; for (i = mShaders.begin(); i != mShaders.end(); ++i) { glDetachShader(mProgram, i->getHandle()); glDeleteShader(i->getHandle()); } mShaders.clear(); glDeleteProgram(mProgram); ExitOnGLError("ERROR: Could not destroy the shaders"); }
GLuint LoadShader(const char* filename, GLenum shaderType) { GLuint shaderID = 0; FILE* file; long fileSize = -1; char* glslSource; if (NULL != (file = fopen(filename, "rb")) && 0 == fseek(file, 0, SEEK_END) && -1 != (fileSize = ftell(file))) { rewind(file); if (NULL != (glslSource = (char*)malloc(fileSize + 1))) { if (fileSize == (long)fread(glslSource, sizeof(char), fileSize, file)) { glslSource[fileSize] = '\0'; if (0 != (shaderID = glCreateShader(shaderType))) { // Must be cast glShaderSource(shaderID, 1, (const char **)&glslSource, NULL); glCompileShader(shaderID); ExitOnGLError("Could not compile a shader"); } else fprintf(stderr, "ERROR: Could not create a shader.\n"); } else fprintf(stderr, "ERROR: Could not read file %s\n", filename); free(glslSource); } else fprintf(stderr, "ERROR: Could not allocate %i bytes.\n", (int)fileSize); fclose(file); } else { if (NULL != file) fclose(file); fprintf(stderr, "ERROR: Could not open file %s\n", filename); } return shaderID; }
void CopyLigtDataToShader(GLuint shaderID) // USES shadermanager { for ( int index = 0; index != ::g_p_LightManager->getMaxLights(); index++ ) { CLight* pLight = ::g_p_LightManager->GetLightPointer(index); // Got a valid pointer? if ( pLight != 0 ) { if ( pLight->bIsEnabled ) { glUniform1i( static_cast<GLuint>(pLight->ID_bIsEnabled), GL_TRUE ); } else { glUniform1i( static_cast<GLuint>(pLight->ID_bIsEnabled), GL_FALSE ); } ExitOnGLError("Error setting light values"); if ( pLight->bIsLocal ) { glUniform1i( static_cast<GLuint>(pLight->ID_bIsLocal), GL_TRUE); } else { glUniform1i( static_cast<GLuint>(pLight->ID_bIsLocal), GL_FALSE); } if ( pLight->bIsSpot ) { glUniform1i( static_cast<GLuint>(pLight->ID_bIsSpot), GL_TRUE); } else { glUniform1i( static_cast<GLuint>(pLight->ID_bIsSpot), GL_FALSE); } glUniform3f( static_cast<GLuint>(pLight->ID_ambientColour), pLight->ambientColour.x, pLight->ambientColour.y, pLight->ambientColour.z ); glUniform3f( static_cast<GLuint>(pLight->ID_diffuseColour), pLight->diffuseColour.x, pLight->diffuseColour.y, pLight->diffuseColour.z ); glUniform3f( static_cast<GLuint>(pLight->ID_position), pLight->position.x, pLight->position.y, pLight->position.z ); glUniform3f( static_cast<GLuint>(pLight->ID_halfVector), pLight->halfVector.x, pLight->halfVector.y, pLight->halfVector.z ); glUniform3f( static_cast<GLuint>(pLight->ID_spotConeDirection), pLight->spotConeDirection.x, pLight->spotConeDirection.y, pLight->spotConeDirection.z ); glUniform1f( static_cast<GLuint>(pLight->ID_spotCosCutoff), pLight->spotCosCutoff ); glUniform1f( static_cast<GLuint>(pLight->ID_spotExponent), pLight->spotExponent ); glUniform1f( static_cast<GLuint>(pLight->ID_constantAttenuation), pLight->constantAttenuation ); glUniform1f( static_cast<GLuint>(pLight->ID_linearAttenuation), pLight->linearAttenuation ); glUniform1f( static_cast<GLuint>(pLight->ID_quadraticAttenuation), pLight->quadraticAttenuation ); glUniform1f( static_cast<GLuint>(pLight->ID_range), pLight->range ); } }// for ( int index = 0... return; }
void ArcCanvas::generate() { formArrays(); mBufferIDs = new GLuint*[mVertexData.size()]; for (int i = 0; i < mVertexData.size(); i++) mBufferIDs[i] = new GLuint[2]; mVertexArrayIDs = new GLuint[mVertexData.size()]; glGenVertexArrays(mVertexData.size(), &mVertexArrayIDs[0]); ExitOnGLError("ERROR: Could not generate the VAOs"); for (int i = 0; i < mVertexData.size(); i++) { glGenBuffers(2, &mBufferIDs[i][0]); ExitOnGLError("ERROR: Could not generate the buffer objects"); glBindVertexArray(mVertexArrayIDs[i]); ExitOnGLError("ERROR: Could not bind the VAOs"); glEnableVertexAttribArray(0); //xyzw glEnableVertexAttribArray(1); //rgba ExitOnGLError("ERROR: Could not enable vertex attributes"); // check sizeof occasions glBindBuffer(GL_ARRAY_BUFFER, mBufferIDs[i][0]); glBufferData(GL_ARRAY_BUFFER, sizeof(ArcVertex) * mVertexSizes[i], mVertexArrays[i], GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the BVO to the VAO"); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(mVertexArrays[i][0]), (GLvoid*)0); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(mVertexArrays[i][0]), (GLvoid*) sizeof(mVertexArrays[i][0].Position)); ExitOnGLError("ERROR: Could not set VAO attributes"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferIDs[i][1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * mIndexSizes[i], mIndexArrays[i], GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the IBO to the VAO"); glBindVertexArray(0); } }
void SetUpTextures(void) { g_p_TextureManager = new CTextureManager(); unsigned int totalTextures = 0; for (unsigned i = 0; i < vecModelsInfo.size(); i++) { if (vecModelsInfo[i].isSkybox) { ::g_p_TextureManager->setBasePath("assets/textures/SkyBoxes_by_Michael"); if (!g_p_TextureManager->CreateCubeTextureFromBMPFiles(vecModelsInfo[i].tex[0][0], vecModelsInfo[i].tex[0][1], vecModelsInfo[i].tex[0][2], vecModelsInfo[i].tex[0][3], vecModelsInfo[i].tex[0][4], vecModelsInfo[i].tex[0][5], vecModelsInfo[i].tex[0][6], true, true)) { std::cout << "Didn't load the sky box texture(s):" << std::endl; std::cout << ::g_p_TextureManager->getLastError() << std::endl; } totalTextures++; ExitOnGLError("Skymap didn't load."); } else if (vecModelsInfo[i].tex.size() > 0) { g_p_TextureManager->setBasePath("assets/textures"); for (unsigned j = 0; j < vecModelsInfo[i].tex[0].size(); j++) { if (!g_p_TextureManager->Create2DTextureFromBMPFile(vecModelsInfo[i].tex[0][j], true)) { std::cout << ::g_p_TextureManager->getLastError() << std::endl; } if (j == 0) vecModelsInfo[i].firstTex = totalTextures; totalTextures++; } } } return; }
void ReadInModelsInfo(std::string fileLocation) { std::ifstream myFile((fileLocation).c_str()); //Check if file is open if (!myFile.is_open()) { ExitOnGLError("ERROR: Could not open text file"); return; } //Model tempModel; CVector3f tempLight; const Model blankModel; const CVector3f blankLight; std::string tempString; bool bKeepReading = true; myFile >> tempString; //Read in the file while (bKeepReading) { if (tempString == "<Model>") { Model tempModel; while (true) { myFile >> tempString; if (tempString == "<Type>") { myFile >> tempModel.type; myFile >> tempString; } else if (tempString == "<PlyFile>") { myFile >> tempModel.file; myFile >> tempString; }
void ArcCanvas::generate() { formArrays(); glGenBuffers(2, &mBufferIDs[1]); ExitOnGLError("ERROR: Could not generate the buffer objects"); glGenVertexArrays(1, &mBufferIDs[0]); ExitOnGLError("ERROR: Could not generate the VAO"); glBindVertexArray(mBufferIDs[0]); ExitOnGLError("ERROR: Could not bind the VAO"); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); ExitOnGLError("ERROR: Could not enable vertex attributes"); // check sizeof occasions glBindBuffer(GL_ARRAY_BUFFER, mBufferIDs[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(ArcVertex) * mVertexSize, mVertexArray, GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the BVO to the VAO"); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(mVertexArray[0]), (GLvoid*)0); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(mVertexArray[0]), (GLvoid*) sizeof(mVertexArray[0].Position)); ExitOnGLError("ERROR: Could not set VAO attributes"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferIDs[2]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * mIndexSize, mIndexArray, GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the IBO to the VAO"); glBindVertexArray(0); }
void Camera::init(GLuint programId){ matrixUniformLocation = glGetUniformLocation(programId, "ViewMatrix"); ExitOnGLError("ERROR: Could not get the camera uniform locations"); }
void DrawMesh( CMeshDesc curMesh, glm::mat4 &matView ) { glm::mat4 matWorld(1.0f); matWorld = glm::translate( matWorld, glm::vec3( curMesh.relPosition.x, curMesh.relPosition.y, curMesh.relPosition.z) ); // // Added on October 16th, 2014 // "Imposters" (usually 2D "flat" quads) often "face" the camera, so you only see one side. // This code will face the camera, but only aligned with the x-z plane (so ignoring y). // In other words, if the camera goes "above" the imposter, it WON'T face it. if ( curMesh.bIsImposter ) { // http://www.rastertek.com/dx11tut34.html // // Calculate the rotation that needs to be applied to the billboard model to face the current camera position using the arc tangent function. // angle = atan2(modelPosition.x - cameraPosition.x, // modelPosition.z - cameraPosition.z) // * (180.0 / D3DX_PI); // Convert rotation into radians. // rotation = (float)angle * 0.0174532925f float angle = atan2( curMesh.relPosition.x - ::g_pCamera->eye.x, curMesh.relPosition.z - ::g_pCamera->eye.z ); //* ( 180.0 / PI ); // Since we aren't paying any attention to the x and z rotations. //pObjectToDraw->rotation.y = angle; curMesh.orientation = glm::fquat( glm::vec3( 0.0f, angle, 0.0f ) ); } // UPDATE************************************************* glm::mat4 matRotation = glm::toMat4( curMesh.orientation ); matWorld = matWorld * matRotation; matWorld = glm::scale( matWorld, glm::vec3( curMesh.scale.x, curMesh.scale.y, curMesh.scale.z) ); if ( curMesh.bIsWireframe ) { glPolygonMode( GL_FRONT_AND_BACK , GL_LINE ); } else { glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); } ExitOnGLError("What the what?"); glCullFace( GL_BACK ); GLuint uberShaderID = ::g_pShaderManager->GetShaderIDFromName("UberShader"); if ( !::g_pShaderManager->UseShaderProgram( uberShaderID ) ) { std::cout << "Can't switch to shader " << uberShaderID << std::endl; return; } SetLightingUniformIDs( uberShaderID ); CopyLigtDataToShader( uberShaderID ); // Set the "EyeDirection" (aka the camera) ::g_pShaderManager->SetUniformVar3f( uberShaderID, "EyeDirection", ::g_pCamera->eye.x, ::g_pCamera->eye.y, ::g_pCamera->eye.z ); // Shininess and Strength... ::g_pShaderManager->SetUniformVar1f( uberShaderID, "Shininess", curMesh.specularShininess ); ::g_pShaderManager->SetUniformVar1f( uberShaderID, "Strength", curMesh.specularStrength ); // ___ _ _ __ __ /// __|| |_ __ _ __| | ___ __ __ __| \/ | __ _ _ __ //\__ \| ' \ / _` |/ _` |/ _ \\ V V /| |\/| |/ _` || '_ \ //|___/|_||_|\__,_|\__,_|\___/ \_/\_/ |_| |_|\__,_|| .__/ // |_| // Render normally ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bGenShadowFromLight", GL_FALSE ); if (curMesh.bIsSkybox) { CTextureFromBMP* pTexture = ::g_p_TextureManager->getTextureFromTextureName(curMesh.textureName_0); glActiveTexture(pTexture->getTextureUnit()); GLuint textureID = pTexture->getTextureNumber(); glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); } else if (curMesh.bHasTexture) { CTextureFromBMP* pTexture = ::g_p_TextureManager->getTextureFromTextureName(curMesh.textureName_0); glActiveTexture(pTexture->getTextureUnit()); GLuint textureID = pTexture->getTextureNumber(); glBindTexture(GL_TEXTURE_2D, textureID); } //unsigned int texCount = 0; //for (std::map<unsigned int, std::vector<std::string> >::iterator itr = g_mapTextures.begin(); itr != g_mapTextures.end(); ++itr) //{ // if (itr->second[0] == "Skybox") // { // //::g_pShaderManager->SetUniformVar1i(uberShaderID, "skyMapTexture", 0); // CTextureFromBMP* pTexture = ::g_p_TextureManager->getTextureFromTextureName(itr->second[0]); // glActiveTexture(pTexture->getTextureUnit()); // GLuint textureID = pTexture->getTextureNumber(); // glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); // } // else if (curMesh.bHasTexture) // { // CTextureFromBMP* pTexture = ::g_p_TextureManager->getTextureFromTextureName(itr->second[0]); // glActiveTexture(pTexture->getTextureUnit()); // GLuint textureID = pTexture->getTextureNumber(); // glBindTexture(GL_TEXTURE_2D, textureID); // } // ExitOnGLError("ERROR: Could not use the shader program"); //} ::g_pShaderManager->SetUniformVar1i(uberShaderID, "textureID", curMesh.firstTex - 1); ::g_pShaderManager->SetUniformVar1i(uberShaderID, "hasTexture", curMesh.bHasTexture); ::g_pShaderManager->SetUniformVar1i(uberShaderID, "blend", curMesh.blend); ::g_pShaderManager->SetUniformVar1i(uberShaderID, "bIsParticle", curMesh.bIsParticle); ::g_pShaderManager->SetUniformVar1i(uberShaderID, "isTransparent", curMesh.bIsTransparent); ::g_pShaderManager->SetUniformVar1f(uberShaderID, "transparency", curMesh.tranparency); if ( curMesh.bIsSkybox ) { ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bIsSkyMap", GL_TRUE ); glDisable( GL_CULL_FACE ); // draw ALL faces } else { ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bIsSkyMap", GL_FALSE ); glEnable( GL_CULL_FACE ); // Turn on culling } ExitOnGLError("ERROR: Could not use the shader program"); if ( curMesh.bUseDebugColour ) { ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bUseDebugColour", GL_TRUE ); ::g_pShaderManager->SetUniformVar4f( uberShaderID, "debugObjectColour", curMesh.debugColour.x, curMesh.debugColour.y, curMesh.debugColour.z, curMesh.debugColour.a ); } else { ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bUseDebugColour", GL_FALSE ); } ExitOnGLError("ERROR: Could not use the shader program"); ::g_pShaderManager->SetUniformMatrix4fv( uberShaderID, "WorldMatrix", 1, GL_FALSE, glm::value_ptr(matWorld) ); ExitOnGLError("ERROR: Could not use the shader program"); ::g_pShaderManager->SetUniformMatrix4fv( uberShaderID, "ViewMatrix", 1, GL_FALSE, glm::value_ptr(matView) ); ExitOnGLError("ERROR: Could not set the shader uniforms"); // Based on the object "ply file" name, point to the appropriate buffer int numberOfIndicesToRender = 0; CPlyInfo tempPlyInfo; std::string plyFileNameDEBUG = curMesh.modelName; if ( !::g_pModelLoader->GetRenderingInfoByModelFileName( curMesh.modelName, tempPlyInfo ) ) { // Model isn't present, which is a Very Bad Thing return; // in a for loop, this will go to the next count, skipping everyint } // At this point, we have found a valid model (that was loaded) glBindVertexArray( tempPlyInfo.vertexBufferObjectID ); // g_VertexPositionBufferID[1] ); // "Connects" to a vertex buffer glBindBuffer( GL_ARRAY_BUFFER, tempPlyInfo.vertexAttribBufferID ); // g_VertexColourBufferID[1] ); // Also buffer (with colours) numberOfIndicesToRender = tempPlyInfo.numberOfElements * 3; // g_numberOfTriangles[1] * 3; glEnableVertexAttribArray(0); // Position glEnableVertexAttribArray(1); // Colour glEnableVertexAttribArray(2); // Normal glEnableVertexAttribArray(3); // Texture ExitOnGLError("ERROR: Could not enable vertex attributes"); ExitOnGLError("ERROR: Could not set VAO attributes"); glDrawElements(GL_TRIANGLES, numberOfIndicesToRender, GL_UNSIGNED_INT, (GLvoid*)0 ); ExitOnGLError("ERROR: Could not draw the cube"); // *** END of DRAW THE OBJECT FROM THE BUFFER **** glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); glDisableVertexAttribArray(3); return; }
int main(int argc, char* argv[]) { bool bIsbowlingMode = false; std::wstring input2 = L"Hello! Please enter 1 if you wish to try the bowling mode. Enter anything else to play checkpoint mode"; std::wcout << input2 << std::endl; std::wstring input; std::getline(std::wcin, input); if (input == L"1") { bIsbowlingMode = true; } bool bLoadTires = false; std::wstring input3 = L"Would you like to load the tire models? (loading tires may cause slowdown) y/n"; std::wcout << input3 << std::endl; std::wstring input4; std::getline(std::wcin, input4); if (input4 == L"y") { bLoadTires = true; } printTheWhatsThisProgramAboutBlurb(); ::g_gameState.currentGameMode = CGameState::GAME_LOADING; ::OpenGL_Initialize( argc, argv, 1200, 800 ); // Initialize(argc, argv); //::OpenGL_Initialize( argc, argv, 640, 480 ); // Initialize(argc, argv); // CModelLoaderManager g_pModelLoader = new CModelLoaderManager(); g_pModelLoader->SetRootDirectory( "assets/models" ); std::vector< CModelLoaderManager::CLoadInfo > vecModelsToLoad; if (bIsbowlingMode) ReadInModelsInfo("assets/BowlingScene.txt"); else ReadInModelsInfo("assets/Scene.txt"); for (unsigned int i = 0; i < vecModelsInfo.size(); i++) { ::g_mapModelTypes[vecModelsInfo[i].type] = vecModelsInfo[i].file; if (vecModelsInfo[i].tex.size() > 0) ::g_mapTextures[i] = vecModelsInfo[i].tex[0]; //vecModelsToLoad.push_back(CModelLoaderManager::CLoadInfo(vecModelsInfo[i].file, 1.0f, true)); } for (unsigned int i = 0; i < vecModelsInfo.size(); i++) { for (std::map<std::string, std::string>::iterator jIterator = g_mapModelTypes.begin(); jIterator != ::g_mapModelTypes.end(); jIterator++) { if (vecModelsInfo[i].type == jIterator->first){ if (vecModelsInfo[i].isEnvironment){ vecModelsToLoad.push_back(CModelLoaderManager::CLoadInfo(jIterator->second)); } else vecModelsToLoad.push_back(CModelLoaderManager::CLoadInfo(jIterator->second, 1.0f, true)); } } } vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("sphere_UV_xyz.ply", 1.0f, true) ); vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("Cube.ply", 1.0f, true ) ); vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("Cube2.ply", 1.0f, true)); vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("1x1_6_Star_2_Sided.ply", 1.0f, true ) ); vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("truck.ply", 1.0f, true)); //vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("truck2.ply", 1.0f, true)); if (bLoadTires) vecModelsToLoad.push_back( CModelLoaderManager::CLoadInfo("tire.ply", 1.0f, true)); if ( ! g_pModelLoader->LoadModels( vecModelsToLoad ) ) { std::cout << "Can't load one or more models. Sorry it didn't work out." << std::endl; return -1; } g_pShaderManager = new CGLShaderManager(); //LoadShaders(); // Moved from CreateCube CShaderDescription uberVertex; uberVertex.filename = "assets/shaders/OpenGL.LightTexSkyUber.vertex.glsl"; uberVertex.name = "UberVertex"; CShaderDescription uberFragment; uberFragment.filename = "assets/shaders/OpenGL.LightTexSkyUber.fragment_texture.glsl"; uberFragment.name = "UberFragment"; CShaderProgramDescription uberShader("UberShader", uberVertex, uberFragment ); if ( !g_pShaderManager->CreateShaderProgramFromFile( uberShader ) ) { std::cout << "Error compiling one or more shaders..." << std::endl; std::cout << g_pShaderManager->getLastError() << std::endl; std::cout << uberShader.getErrorString() << std::endl; return -1; } ::SetUpTextures(); std::cout << "Starting Havok" << std::endl; g_pFactoryMediator = new CFactoryMediator(); // ****************************************************** // Set up the Havok physics thing // Create a Havok Physic thingie: // Passing "Havok" gives us a physics manager using Havok. // Anything else is an error... // if crashing on release, right click on Desktop->NVIDIA Contol Panel->Set PhysX configuration. Select a PhysX dropdown menu->CPU //::g_pPhysicsManager = CPhysicsManagerFactory::CreatePhysicsMananger( L"Havok" ); if ((::g_pPhysicsManager = CPhysicsManagerFactory::CreatePhysicsMananger(L"Havok")) == 0){ std::cout << "Failed Creating Havok" << std::endl; std::cout.flush(); } // std::cout << "Havok Created" << std::endl; std::cout.flush(); std::vector< CNameValuePair2 > vecParams; vecParams.push_back( CNameValuePair2( L"VisualDebugger", true ) ); ::g_pPhysicsManager->Init(vecParams); // ****************************************************************** std::cout << "Done with physics." << std::endl; std::cout.flush(); static const float oneDegreeInRadians = static_cast<float>(PI) / 180.0f; for (unsigned i = 0; i < vecModelsInfo.size(); i++) { CPhysicalProp physicalProp; physicalProp.position = vecModelsInfo[i].pos; physicalProp.setOrientationEulerAngles(CVector3f(oneDegreeInRadians * vecModelsInfo[i].rot.x, oneDegreeInRadians * vecModelsInfo[i].rot.y, oneDegreeInRadians * vecModelsInfo[i].rot.z)); physicalProp.rotStep = vecModelsInfo[i].rot; CMeshDesc mesh(vecModelsInfo[i].file); mesh.scale = vecModelsInfo[i].scale; mesh.bIsSkybox = vecModelsInfo[i].isSkybox; mesh.bIsPlayer = vecModelsInfo[i].isPlayer; mesh.modelID = i; mesh.blend = vecModelsInfo[i].blend; mesh.debugColour = CVector4f(vecModelsInfo[i].col.x, vecModelsInfo[i].col.y, vecModelsInfo[i].col.z, 1.0f); mesh.bIsParticle = vecModelsInfo[i].isParticle; if (vecModelsInfo[i].isTransparent){ mesh.bIsTransparent = true; mesh.tranparency = vecModelsInfo[i].transparency; } else{ mesh.bIsTransparent = false; mesh.tranparency = 1.0f; } if (vecModelsInfo[i].tex.size() > 0) { mesh.bHasTexture = true; mesh.firstTex = vecModelsInfo[i].firstTex; } mesh.ObjectType = vecModelsInfo[i].type; if (vecModelsInfo[i].isSkybox || vecModelsInfo[i].isParticle) mesh.bIsHavok = false; vecModelsInfo[i].ID = g_pFactoryMediator->CreateObjectByType(vecModelsInfo[i].type, physicalProp, mesh); makeHavokObject(vecModelsInfo[i].ID); if (vecModelsInfo[i].isPlayer) ::g_FloorHavokID = vecModelsInfo[i].ID; if (vecModelsInfo[i].isSkybox) ::g_skyBoxID = vecModelsInfo[i].ID; //if (vecModelsInfo[i].isLightBall) // ::g_lightModelID = vecModelsInfo[i].ID; if (vecModelsInfo[i].isParticle) ::g_vecParticleID.push_back(vecModelsInfo[i].ID); } CMeshDesc sphereMesh("sphere_UV_xyz.ply"); sphereMesh.debugColour = CVector4f(0.75f, 0.5f, 0.3f, 0.0f); sphereMesh.bIsTransparent = true; sphereMesh.tranparency = 0.4f; CPhysicalProp sphereProps( CVector3f( 0.0f, 5.0f, 4.0f ) ); sphereProps.position = (CVector3f(0.0f, 5.0f, 0.0f)); ::g_lightModelID = g_pFactoryMediator->CreateObjectByType( "Sphere UV", sphereProps, sphereMesh ); CMeshDesc sphereHavokMesh("sphere_UV_xyz.ply"); sphereHavokMesh.debugColour = CVector4f(0.75f, 0.5f, 0.3f, 0.0f); sphereHavokMesh.ObjectType = "Star"; sphereHavokMesh.bIsHavok = false; CPhysicalProp sphereHavokProps( CVector3f( 0.0f, 5.0f, -10.0f ) ); ::g_StarHavokID = g_pFactoryMediator->CreateObjectByType( "Sphere UV", sphereHavokProps, sphereHavokMesh ); //makeHavokObject(::g_StarHavokID); //for (int i = 1; i < 4; i++){ // for (int j = 1; j < 4; j++){ // CMeshDesc floorHavokMesh("Cube2.ply"); // floorHavokMesh.debugColour = CVector4f(0.75f, 0.5f, 0.3f, 0.0f); // floorHavokMesh.bHasTexture = true; // if (i == 2 && j == 2){ // floorHavokMesh.ObjectType = "IceFloor"; // } // else { // floorHavokMesh.ObjectType = "GroundFloor"; // } // floorHavokMesh.scale = floorSize; // //CPhysicalProp floorHavokProps(CVector3f(-(floorSize / 2.0f), -floorSize, -(floorSize / 2.0f))); // CVector3f floorPosition; // floorPosition.x = i*(-(floorSize / 2.0f)); // floorPosition.z = j*(-(floorSize / 2.0f)); // floorPosition.y = -floorSize; // CPhysicalProp floorHavokProps(floorPosition); // unsigned int floorID = g_pFactoryMediator->CreateObjectByType("Cube UV", floorHavokProps, floorHavokMesh); // makeHavokObject(floorID); // } //} CMeshDesc truckMesh("truck.ply"); truckMesh.ObjectType = "Vehicle"; truckMesh.scale = 5.0f; CPhysicalProp truckProps(CVector3f(0.0f, 5.0f, 4.0f)); truckProps.position = (CVector3f(0.0f, 5.0f, 0.0f)); ::g_Player_ID = g_pFactoryMediator->CreateObjectByType("Player", truckProps, truckMesh); makeHavokObject(::g_Player_ID); if (bLoadTires){ CMeshDesc tireMesh("tire.ply"); tireMesh.bIsHavok = false; tireMesh.scale = 1.0f; g_pFactoryMediator->addMeshDescription(::g_Player_ID, tireMesh); g_pFactoryMediator->addMeshDescription(::g_Player_ID, tireMesh); g_pFactoryMediator->addMeshDescription(::g_Player_ID, tireMesh); g_pFactoryMediator->addMeshDescription(::g_Player_ID, tireMesh); } /*CMeshDesc checkpointHavokMesh("Cube2.ply"); checkpointHavokMesh.debugColour = CVector4f(0.75f, 0.5f, 0.3f, 0.0f); checkpointHavokMesh.ObjectType = "CheckPoint"; CPhysicalProp checkpointHavokProps(CVector3f(0.0f, 0.0f, 0.0f)); unsigned int checkpoint_ID = g_pFactoryMediator->CreateObjectByType("Sphere UV", checkpointHavokProps, checkpointHavokMesh); makeHavokObject(checkpoint_ID);*/ g_pPhysicsManager->SetCheckPoints(vecCheckPointsInfo); //g_pPhysicsManager //GenerateAABBWorld(); // Added October 3, 2014 g_pCamera = new CCamera(); // Camera expects an IMediator*, so cast it as that g_pCamera->SetMediator( (IMediator*)g_pFactoryMediator ); g_pCamera->eye.x = 0.0f; // Centred (left and right) g_pCamera->eye.y = 5.0f; // 2.0 units "above" the "ground" g_pCamera->eye.z = -20.0f; // .0funits0 from "back" the origin . 1, 2.5, 1.8 g_pCamera->target.x = 50.0f; // Centred (left and right) g_pCamera->target.y = 350.0f; // 2.0 units "above" the "ground" g_pCamera->target.z = 50.0f; // 0.0 units "back" from the origin g_pCamera->cameraAngleYaw = 2.8f; g_pCamera->cameraAnglePitch = -0.2f; g_pCamera->up.x = 0.0f; g_pCamera->up.y = 1.0f; // The Y axis is "up and down" g_pCamera->m_LEFPLookupMode = CCamera::LERP; g_pCamera->setMode_FollowAtDistance( ::g_Player_ID ); float followSpeed = 30.0f; // 1.0f whatever per second float followMinDistance = 3.0f; float followMaxSpeedDisance = 100.0f; g_pCamera->setFollowDistances( followMinDistance, followMaxSpeedDisance ); g_pCamera->setFollowMaxSpeed( followSpeed ); // A "fly through" camera // These numbers are sort of in the direction of the original camera g_pCamera->orientation = glm::fquat( 0.0960671529f, 0.972246766f, -0.0900072306f, -0.193443686f ); glm::normalize( g_pCamera->orientation ); //g_pCamera->setMode_IndependentFreeLook(); g_pMouseState = new CMouseState(); // Set up the basic lighting ExitOnGLError("Error setting light values"); SetupLights(); // if ( !::g_p_LightManager->initShadowMaps( 1, CLightManager::DEFAULT_SHADOW_DEPTH_TEXTURE_SIZE ) ) { std::cout << "Error setting up the shadow textures: " << std::endl; std::cout << ::g_p_LightManager->getLastError(); } setUpShadowTexture(); g_p_GameControllerManager = CGameControllerManager::getGameControllerManager(); g_p_GameController_0 = g_p_GameControllerManager->getController(0); if ( ( g_p_GameController_0 != 0 ) && ( g_p_GameController_0->bIsConnected() ) ) { g_p_GameController_0->AddVibrationSequence( IGameController::CVibStep::LEFT, 0.5f, 2.0f ); g_p_GameController_0->AddVibrationSequence( IGameController::CVibStep::RIGHT, 0.5f, 1.0f ); std::cout << "Game controller 0 found!" << std::endl; } else { std::cout << "Didn't get an ID for the game controller; is there one plugged in?" << std::endl; } //*************************************** g_p_checkpointEmitter = new CParticleEmitter(); g_p_timerEmitter = new CParticleEmitter(); //************************************** CPhysicalProp starProps; ::g_pFactoryMediator->getPhysicalPropertiesByID(::g_StarHavokID, starProps); g_p_checkpointEmitter->SetLocation(starProps.position); ::g_p_checkpointEmitter->GenerateParticles(1, /*number of particles*/ CVector3f(0.0f, 0.0f, 0.0f), /*Init veloc.*/ 0.1f, /*dist from source */ 5.0f, /*seconds*/ true); ::g_p_checkpointEmitter->SetAcceleration(CVector3f(0.0f, 0.0f, 0.0f)); g_p_timerEmitter->SetLocation(truckProps.position); ::g_p_timerEmitter->GenerateParticles(1, /*number of particles*/ CVector3f(0.0f, 1.0f, 0.0f), /*Init veloc.*/ 0.01f, /*dist from source */ 1.0f, /*seconds*/ true); ::g_p_timerEmitter->SetAcceleration(CVector3f(0.0f, 1.0f, 0.0f)); ::g_p_PhysicsThingy = new CPhysicsCalculatron(); // Added in animation on Sept 19 ::g_simTimer.Reset(); ::g_simTimer.Start(); // Start "counting" if (bIsbowlingMode) { ::g_gameState.currentGameMode = CGameState::GAME_BOWLING; } else ::g_gameState.currentGameMode = CGameState::GAME_RUNNING; // FULL SCREEN, Mr. Data! //::glutFullScreen(); ExitOnGLError("Error getting uniform light variables"); glutMainLoop(); std::cout << "Shutting down..." << std::endl; ShutDown(); exit(EXIT_SUCCESS); }
void DrawMeshForShadow( CMeshDesc curMesh, glm::mat4 &matView, GLuint lightNumber ) { glm::mat4 matWorld(1.0f); matWorld = glm::translate( matWorld, glm::vec3( curMesh.relPosition.x, curMesh.relPosition.y, curMesh.relPosition.z) ); // // Added on October 16th, 2014 // "Imposters" (usually 2D "flat" quads) often "face" the camera, so you only see one side. // This code will face the camera, but only aligned with the x-z plane (so ignoring y). // In other words, if the camera goes "above" the imposter, it WON'T face it. if ( curMesh.bIsImposter ) { // http://www.rastertek.com/dx11tut34.html // // Calculate the rotation that needs to be applied to the billboard model to face the current camera position using the arc tangent function. // angle = atan2(modelPosition.x - cameraPosition.x, // modelPosition.z - cameraPosition.z) // * (180.0 / D3DX_PI); // Convert rotation into radians. // rotation = (float)angle * 0.0174532925f float angle = atan2( curMesh.relPosition.x - ::g_pCamera->eye.x, curMesh.relPosition.z - ::g_pCamera->eye.z ); //* ( 180.0 / PI ); // Since we aren't paying any attention to the x and z rotations. //pObjectToDraw->rotation.y = angle; curMesh.orientation = glm::fquat( glm::vec3( 0.0f, angle, 0.0f ) ); } glm::mat4 matRotation = glm::toMat4( curMesh.orientation ); matWorld = matWorld * matRotation; matWorld = glm::scale( matWorld, glm::vec3( curMesh.scale.x, curMesh.scale.y, curMesh.scale.z) ); glCullFace( GL_BACK ); GLuint uberShaderID = ::g_pShaderManager->GetShaderIDFromName("UberShader"); if ( !::g_pShaderManager->UseShaderProgram( uberShaderID ) ) { std::cout << "Can't switch to shader " << uberShaderID << std::endl; return; } // ___ _ _ __ __ /// __|| |_ __ _ __| | ___ __ __ __| \/ | __ _ _ __ //\__ \| ' \ / _` |/ _` |/ _ \\ V V /| |\/| |/ _` || '_ \ //|___/|_||_|\__,_|\__,_|\___/ \_/\_/ |_| |_|\__,_|| .__/ // |_| ::g_pShaderManager->SetUniformVar1i( uberShaderID, "bGenShadowFromLight", GL_TRUE ); ::g_pShaderManager->SetUniformMatrix4fv( uberShaderID, "WorldMatrix", 1, GL_FALSE, glm::value_ptr(matWorld) ); ExitOnGLError("ERROR: Could not use the shader program"); ::g_pShaderManager->SetUniformMatrix4fv( uberShaderID, "ViewMatrix", 1, GL_FALSE, glm::value_ptr(matView) ); ExitOnGLError("ERROR: Could not set the shader uniforms"); // Based on the object "ply file" name, point to the appropriate buffer int numberOfIndicesToRender = 0; CPlyInfo tempPlyInfo; std::string plyFileNameDEBUG = curMesh.modelName; if ( !::g_pModelLoader->GetRenderingInfoByModelFileName( curMesh.modelName, tempPlyInfo ) ) { // Model isn't present, which is a Very Bad Thing return; // in a for loop, this will go to the next count, skipping everyint } // At this point, we have found a valid model (that was loaded) glBindVertexArray( tempPlyInfo.vertexBufferObjectID ); // g_VertexPositionBufferID[1] ); // "Connects" to a vertex buffer glBindBuffer( GL_ARRAY_BUFFER, tempPlyInfo.vertexAttribBufferID ); // g_VertexColourBufferID[1] ); // Also buffer (with colours) numberOfIndicesToRender = tempPlyInfo.numberOfElements * 3; // g_numberOfTriangles[1] * 3; glEnableVertexAttribArray(0); // Position glEnableVertexAttribArray(1); // Colour glEnableVertexAttribArray(2); // Normal glEnableVertexAttribArray(3); // Texture ExitOnGLError("ERROR: Could not enable vertex attributes"); ExitOnGLError("ERROR: Could not set VAO attributes"); glDrawElements(GL_TRIANGLES, numberOfIndicesToRender, GL_UNSIGNED_INT, (GLvoid*)0 ); ExitOnGLError("ERROR: Could not draw the cube"); // *** END of DRAW THE OBJECT FROM THE BUFFER **** glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); glDisableVertexAttribArray(3); return; }
void glutDisplay_callback(void) { ::g_FrameCount++; clock_t Now = clock(); if ( ::g_LastTime == 0 ) // if (LastTime == 0) { ::g_LastTime = Now; // LastTime = Now; } // ___ _ _ _ //| _ \ ___ _ _ __| | ___ _ _ ___| |_ __ _ __| | ___ __ __ __ _ __ __ _ _ __ //| // -_)| ' \ / _` |/ -_)| '_| (_-<| ' \ / _` |/ _` |/ _ \\ V V / | ' \ / _` || '_ \ //|_|_\\___||_||_|\__,_|\___||_| /__/|_||_|\__,_|\__,_|\___/ \_/\_/ |_|_|_|\__,_|| .__/ // |_| // Draw depth buffer from light perspective glm::mat4 matViewFromLight(1.0f); glm::mat4 light_proj_matrix = glm::frustum( -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 10000.0f ); std::vector< IGameObjectRender* > vec_pRenderedObjects; g_pFactoryMediator->getRenderedObjects( vec_pRenderedObjects, IFactory::ONLY_REGULAR_MESH_OBJECTS ); SortGameObjects(vec_pRenderedObjects); for ( unsigned int lightID = 0; lightID != g_p_LightManager->getMaxLights(); lightID++ ) { CLight light_props; ::g_p_LightManager->GetLightInformation(lightID, light_props); if ( light_props.bIsEnabled && light_props.get_bHasShadow() ) { // AKA The "view or camera" matrix, but from the lights perspective glm::mat4 light_view_matrix = glm::lookAt( glm::vec3(light_props.position.x, light_props.position.y, light_props.position.z ), glm::vec3( 0.0f, 0.0f, 0.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ) ); for (std::vector< IGameObjectRender* >::iterator itGO = vec_pRenderedObjects.begin(); itGO != vec_pRenderedObjects.end(); itGO++) { std::vector<CMeshDesc> vecMeshes; (*itGO)->getMeshDescriptions( vecMeshes ); for ( std::vector<CMeshDesc>::iterator itCurMesh = vecMeshes.begin(); itCurMesh != vecMeshes.end(); itCurMesh++ ) { DrawMeshForShadow( *itCurMesh, matViewFromLight, lightID ); } } } }// for ( unsigned int lightID... // ___ _ __ _ _ __ //| _ \ ___ _ _ __| | ___ _ _ ___ __ ___ _ _ ___ / /_ _ ___ _ _ _ __ __ _ | || | _ _\ \ //| // -_)| ' \ / _` |/ -_)| '_| (_-</ _|/ -_)| ' \ / -_) | || ' \ / _ \| '_|| ' \ / _` || || || || || | //|_|_\\___||_||_|\__,_|\___||_| /__/\__|\___||_||_|\___| | ||_||_|\___/|_| |_|_|_|\__,_||_||_| \_, || | // \_\ |__//_/ // ********************************* glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glm::mat4 matView(1.0f); if (::g_pCamera->getCameraState() == CCamera::INDEPENDENT_FREE_LOOK) { if (g_pCamera->deltaAngleYaw != 0.0f){ g_pCamera->cameraAngleYaw += g_pCamera->deltaAngleYaw; g_pCamera->deltaAngleYaw = 0.0f; } if (g_pCamera->deltaAnglePitch != 0.0f){ g_pCamera->cameraAnglePitch += ::g_pCamera->deltaAnglePitch; g_pCamera->deltaAnglePitch = 0.0f; } if (g_pCamera->deltaMove != 0.0f){ g_pCamera->eye.x += g_pCamera->deltaMove*sin(g_pCamera->cameraAngleYaw + g_pCamera->cameraMove); g_pCamera->eye.y += g_pCamera->deltaMove*tan(g_pCamera->cameraAnglePitch); g_pCamera->eye.z += g_pCamera->deltaMove*(-cos(g_pCamera->cameraAngleYaw + g_pCamera->cameraMove)); g_pCamera->deltaMove = 0.0f; g_pCamera->cameraMove = 0.0f; } g_pCamera->target.x = g_pCamera->eye.x + sin(g_pCamera->cameraAngleYaw + g_pCamera->cameraMove); g_pCamera->target.y = g_pCamera->eye.y + tan(g_pCamera->cameraAnglePitch); g_pCamera->target.z = g_pCamera->eye.z + (-cos(g_pCamera->cameraAngleYaw + g_pCamera->cameraMove)); } matView = glm::lookAt( glm::vec3(g_pCamera->eye.x, g_pCamera->eye.y, g_pCamera->eye.z), // Camera (aka "Eye") glm::vec3(g_pCamera->target.x, g_pCamera->target.y, g_pCamera->target.z), // At (aka "target") glm::vec3(g_pCamera->up.x, g_pCamera->up.y, g_pCamera->up.z) ); // Up ExitOnGLError("Unknown error"); // ___ ___ _ _ __ __ //| \ _ _ __ _ __ __ __ / __|| |__ _ _ | |__ ___ __ __ / /___ ___\ \ //| |) || '_|/ _` |\ V V / \__ \| / /| || || '_ \/ _ \\ \ /| |/ -_)(_-< | | //|___/ |_| \__,_| \_/\_/ |___/|_\_\ \_, ||_.__/\___//_\_\| |\___|/__/ | | // |__/ \_\ /_/ // Get the objects we are to render from the factory g_pFactoryMediator->getRenderedObjects( vec_pRenderedObjects, IFactory::ONLY_SKYBOXES ); SortGameObjects(vec_pRenderedObjects); glDisable(GL_DEPTH_TEST); for (std::vector< IGameObjectRender* >::iterator itGO = vec_pRenderedObjects.begin(); itGO != vec_pRenderedObjects.end(); itGO++) { DrawObject( *itGO, matView ); } glEnable(GL_DEPTH_TEST); // __ __ _ ___ _ //| \/ | __ _ (_) _ _ | \ _ _ __ _ __ __ __ | | ___ ___ _ __ //| |\/| |/ _` || || ' \ | |) || '_|/ _` |\ V V / | |__ / _ \/ _ \| '_ \ //|_| |_|\__,_||_||_||_| |___/ |_| \__,_| \_/\_/ |____|\___/\___/| .__/ // |_| g_pFactoryMediator->getRenderedObjects(vec_pRenderedObjects, IFactory::ONLY_REGULAR_MESH_OBJECTS); SortGameObjects(vec_pRenderedObjects); for (std::vector< IGameObjectRender* >::iterator itGO = vec_pRenderedObjects.begin(); itGO != vec_pRenderedObjects.end(); itGO++) { DrawObject( *itGO, matView ); } for (std::vector< CGameObject* >::iterator itGO = g_vecPhysDebugObjects.begin(); itGO != g_vecPhysDebugObjects.end(); itGO++) { DrawObject(*itGO, matView); } // Debug object draw loop // Get the debug objects... std::vector< CMeshDesc > vecDebugMeshes; for (std::vector< IGameObjectRender* >::iterator itGO = vec_pRenderedObjects.begin(); itGO != vec_pRenderedObjects.end(); itGO++) { vecDebugMeshes.clear(); (*itGO)->getDebugMeshObjects( vecDebugMeshes, IGameObjectRender::AABB_CENTRED ); // Then draw them for ( std::vector< CMeshDesc >::iterator itDO = vecDebugMeshes.begin(); itDO != vecDebugMeshes.end(); itDO++ ) { DrawMesh( *itDO, matView ); } } // for ( std::vector< IGameObjectRender* >::iterator itGO ... // vecDebugMeshes is stack based, so debug objects get deleted if (g_SavingData == true) { std::ofstream myFile; myFile.open("assets/Scene2.txt"); for (unsigned index = 0; index < vecModelsInfo.size(); index++) { myFile << "<Model> " << std::endl; myFile << "\t<Type> " << vecModelsInfo[index].type << " </Type>" << std::endl; myFile << "\t<PlyFile> " << vecModelsInfo[index].file << " </PlyFile>" << std::endl; if (vecModelsInfo[index].isSkybox) { myFile << "\t<SkyboxTexture>" << std::endl; myFile << "\t\t<Name> " << vecModelsInfo[index].tex[0][0] << " </Name>" << std::endl; for (unsigned j = 1; j <= 6; j++) myFile << "\t\t<Texture> " << vecModelsInfo[index].tex[0][j] << " <Texture>" << std::endl; myFile << "\t</SkyboxTexture> " << std::endl; myFile << "\t<IsSkybox> True </IsSkybox> " << std::endl; } else { for (unsigned j = 0; j < vecModelsInfo[index].tex[0].size(); j++) myFile << "\t<Texture> " << vecModelsInfo[index].tex[0][j] << " </Texture>" << std::endl; } static CPhysicalProp physProps; ::g_pFactoryMediator->getPhysicalPropertiesByID(vecModelsInfo[index].ID, physProps); myFile << "\t<Position> " << physProps.position.x << " " << physProps.position.y << " " << physProps.position.z << " </Position>" << std::endl; myFile << "\t<Colour> " << vecModelsInfo[index].col.x << " " << vecModelsInfo[index].col.y << " " << vecModelsInfo[index].col.z << " </Colour>" << std::endl; myFile << "\t<Rotation> " << physProps.rotStep.x << " " << physProps.rotStep.y << " " << physProps.rotStep.z << " </Rotation>" << std::endl; myFile << "\t<Blend> " << vecModelsInfo[index].blend << " </Blend>" << std::endl; if (vecModelsInfo[index].isTransparent) myFile << "\t<Transparency> " << vecModelsInfo[index].transparency << " </Transparency>" << std::endl; if (vecModelsInfo[index].isPlayer) myFile << "\t<IsPlayer> True </IsPlayer>" << std::endl; if (vecModelsInfo[index].isLightBall) myFile << "\t<IsLightBall> True </IsLightBall>" << std::endl; if (vecModelsInfo[index].isEnvironment) myFile << "\t<IsEnviro> True </IsEnviro>" << std::endl; else myFile << "\t<Scale> " << vecModelsInfo[index].scale << " </Scale>" << std::endl; myFile << "</Model> " << std::endl; } g_SavingData = false; } // Detatch from the vertex buffer (0 is reserved in OpenGL, so setting to "0" means "set to nothing", sort of) glBindVertexArray(0); // Detatch from the current shader program //glUseProgram(0); // Now that everything is drawn, show the back buffer // (i.e. switch the 'back' framebuffer with the 'front' glutSwapBuffers(); return; }
void ArcShaderProgram::initialize() { mProgram = glCreateProgram(); ExitOnGLError("ERROR: Could not create the shader program"); }
void ArcShaderProgram::link() { glLinkProgram(mProgram); ExitOnGLError("ERROR: Could not link the shader program"); }
void CreateCube() { const Vertex VERTICES[8] = { { { -.5f, -.5f, .5f, 1 }, { 0, 0, 1, 1 } }, { { -.5f, .5f, .5f, 1 }, { 1, 0, 0, 1 } }, { { .5f, .5f, .5f, 1 }, { 0, 1, 0, 1 } }, { { .5f, -.5f, .5f, 1 }, { 1, 1, 0, 1 } }, { { -.5f, -.5f, -.5f, 1 }, { 1, 1, 1, 1 } }, { { -.5f, .5f, -.5f, 1 }, { 1, 0, 0, 1 } }, { { .5f, .5f, -.5f, 1 }, { 1, 0, 1, 1 } }, { { .5f, -.5f, -.5f, 1 }, { 0, 0, 1, 1 } } }; const GLuint INDICES[36] = { 0,2,1, 0,3,2, 4,3,0, 4,7,3, 4,1,5, 4,0,1, 3,6,2, 3,7,6, 1,6,5, 1,2,6, 7,5,6, 7,4,5 }; ShaderIds[0] = glCreateProgram(); ExitOnGLError("ERROR: Could not create the shader program"); { ShaderIds[1] = LoadShader("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER); ShaderIds[2] = LoadShader("SimpleShader.vertex.glsl", GL_VERTEX_SHADER); glAttachShader(ShaderIds[0], ShaderIds[1]); glAttachShader(ShaderIds[0], ShaderIds[2]); } glLinkProgram(ShaderIds[0]); ExitOnGLError("ERROR: Could not link the shader program"); ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix"); ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix"); ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix"); ExitOnGLError("ERROR: Could not get shader uniform locations"); glGenVertexArrays(1, &BufferIds[0]); ExitOnGLError("ERROR: Could not generate the VAO"); glBindVertexArray(BufferIds[0]); ExitOnGLError("ERROR: Could not bind the VAO"); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); ExitOnGLError("ERROR: Could not enable vertex attributes"); glGenBuffers(2, &BufferIds[1]); ExitOnGLError("ERROR: Could not generate the buffer objects"); glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the VBO to the VAO"); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position)); ExitOnGLError("ERROR: Could not set VAO attributes"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), INDICES, GL_STATIC_DRAW); ExitOnGLError("ERROR: Could not bind the IBO to the VAO"); glBindVertexArray(0); }