diff --git a/ScuffedMinecraft/src/Application.cpp b/ScuffedMinecraft/src/Application.cpp index eeec24a..28fd296 100644 --- a/ScuffedMinecraft/src/Application.cpp +++ b/ScuffedMinecraft/src/Application.cpp @@ -380,28 +380,20 @@ int main(int argc, char *argv[]) projection = glm::perspective(glm::radians(camera.Zoom), windowX / windowY, 0.1f, 1000.0f); shader.use(); - unsigned int viewLoc = glGetUniformLocation(shader.ID, "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - unsigned int projectionLoc = glGetUniformLocation(shader.ID, "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + shader.setMat4x4f( "view", view ); + shader.setMat4x4f( "projection", projection ); waterShader.use(); - viewLoc = glGetUniformLocation(waterShader.ID, "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - projectionLoc = glGetUniformLocation(waterShader.ID, "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + waterShader.setMat4x4f( "view", view ); + waterShader.setMat4x4f( "projection", projection ); billboardShader.use(); - viewLoc = glGetUniformLocation(billboardShader.ID, "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - projectionLoc = glGetUniformLocation(billboardShader.ID, "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + billboardShader.setMat4x4f( "view", view ); + billboardShader.setMat4x4f( "projection", projection ); outlineShader.use(); - viewLoc = glGetUniformLocation(outlineShader.ID, "view"); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - projectionLoc = glGetUniformLocation(outlineShader.ID, "projection"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + outlineShader.setMat4x4f( "view", view ); + outlineShader.setMat4x4f( "projection", projection ); Planet::planet->Update(camera.Position); @@ -492,8 +484,7 @@ int main(int argc, char *argv[]) glEnable(GL_BLEND); glEnable(GL_COLOR_LOGIC_OP); - unsigned int crosshairProjLoc = glGetUniformLocation(crosshairShader.ID, "projection"); - glUniformMatrix4fv(crosshairProjLoc, 1, GL_FALSE, glm::value_ptr(ortho)); + crosshairShader.setMat4x4f( "projection", ortho ); glBindVertexArray(crosshairVAO); glDrawArrays(GL_TRIANGLES, 0, 6); diff --git a/ScuffedMinecraft/src/Chunk.cpp b/ScuffedMinecraft/src/Chunk.cpp index f40bfe5..f08a6ff 100644 --- a/ScuffedMinecraft/src/Chunk.cpp +++ b/ScuffedMinecraft/src/Chunk.cpp @@ -519,18 +519,15 @@ void Chunk::Render(Shader* mainShader, Shader* billboardShader) // Render main mesh mainShader->use(); + mainShader->setMat4x4f( "model", model ); - modelLoc = glGetUniformLocation(mainShader->ID, "model"); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glBindVertexArray(mainVAO); glDrawElements(GL_TRIANGLES, numTrianglesMain, GL_UNSIGNED_INT, 0); // Render billboard mesh billboardShader->use(); - - modelLoc = glGetUniformLocation(billboardShader->ID, "model"); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + billboardShader->setMat4x4f( "model", model ); glDisable(GL_CULL_FACE); glBindVertexArray(billboardVAO); @@ -546,13 +543,12 @@ void Chunk::RenderWater(Shader* shader) //std::cout << "Rendering chunk " << chunkPos.x << ", " << chunkPos.y << ", " << chunkPos.z << '\n' // << "Chunk VAO: " << vertexArrayObject << '\n' << "Triangles: " << numTriangles << '\n'; - modelLoc = glGetUniformLocation(shader->ID, "model"); glBindVertexArray(waterVAO); glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, worldPos); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - + shader->setMat4x4f( "model", model ); + glDrawElements(GL_TRIANGLES, numTrianglesWater, GL_UNSIGNED_INT, 0); } diff --git a/ScuffedMinecraft/src/Planet.cpp b/ScuffedMinecraft/src/Planet.cpp index 1e40bd4..b2c6ee4 100644 --- a/ScuffedMinecraft/src/Planet.cpp +++ b/ScuffedMinecraft/src/Planet.cpp @@ -39,13 +39,15 @@ void Planet::Update(glm::vec3 cameraPos) { numChunks++; - if (!(*it->second).ready) + Chunk& chunk = *it->second; + + if (!chunk.ready) chunksLoading++; - int chunkX = (*it->second).chunkPos.x; - int chunkY = (*it->second).chunkPos.y; - int chunkZ = (*it->second).chunkPos.z; - if ((*it->second).ready && (abs(chunkX - camChunkX) > renderDistance || + int chunkX = chunk.chunkPos.x; + int chunkY = chunk.chunkPos.y; + int chunkZ = chunk.chunkPos.z; + if ( chunk.ready && (abs(chunkX - camChunkX) > renderDistance || abs(chunkY - camChunkY) > renderDistance || abs(chunkZ - camChunkZ) > renderDistance)) { @@ -60,13 +62,13 @@ void Planet::Update(glm::vec3 cameraPos) chunkDataDeleteQueue.push({ chunkX, chunkY, chunkZ - 1 }); // Delete chunk - delete it->second; + delete it->second; // &chunk it = chunks.erase(it); } else { numChunksRendered++; - (*it->second).Render(solidShader, billboardShader); + chunk.Render(solidShader, billboardShader); ++it; } } @@ -75,11 +77,13 @@ void Planet::Update(glm::vec3 cameraPos) waterShader->use(); for (auto it = chunks.begin(); it != chunks.end(); ) { - int chunkX = (*it->second).chunkPos.x; - int chunkY = (*it->second).chunkPos.y; - int chunkZ = (*it->second).chunkPos.z; + Chunk& chunk = *it->second; - (*it->second).RenderWater(waterShader); + int chunkX = chunk.chunkPos.x; + int chunkY = chunk.chunkPos.y; + int chunkZ = chunk.chunkPos.z; + + chunk.RenderWater(waterShader); ++it; } @@ -102,10 +106,15 @@ void Planet::ChunkThreadUpdate() chunks.find({ pos.x, pos.y, pos.z + 1 }) == chunks.end() && chunks.find({ pos.x, pos.y, pos.z - 1 }) == chunks.end()) { - delete chunkData.at(pos); - chunkData.erase(pos); + chunkMutex.lock(); + delete it->second; + it = chunkData.erase( it ); + chunkMutex.unlock(); + } + else + { + ++it; } - ++it; } // Check if camera moved to new chunk diff --git a/ScuffedMinecraft/src/Shader.cpp b/ScuffedMinecraft/src/Shader.cpp index 98c80cf..d61335e 100644 --- a/ScuffedMinecraft/src/Shader.cpp +++ b/ScuffedMinecraft/src/Shader.cpp @@ -2,6 +2,8 @@ #include +#include + #include #include #include @@ -91,15 +93,22 @@ void Shader::use() glUseProgram(ID); } -void Shader::setBool(const std::string& name, bool value) const +void Shader::setBool(const std::string& name, bool value) { - glUniform1i(glGetUniformLocation(ID, name.c_str()), (int)value); + glUniform1i( glGetUniformLocation( ID, name.c_str() ), (int)value); } -void Shader::setInt(const std::string& name, int value) const + +void Shader::setInt(const std::string& name, int value) { - glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + glUniform1i( glGetUniformLocation( ID, name.c_str() ), value); } -void Shader::setFloat(const std::string& name, float value) const + +void Shader::setFloat(const std::string& name, float value) { - glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + glUniform1f( glGetUniformLocation( ID, name.c_str() ), value); +} + +void Shader::setMat4x4f( const std::string& name, const glm::mat4& matrix ) +{ + glUniformMatrix4fv( glGetUniformLocation( ID, name.c_str() ), 1, GL_FALSE, glm::value_ptr( matrix ) ); } \ No newline at end of file diff --git a/ScuffedMinecraft/src/Shader.h b/ScuffedMinecraft/src/Shader.h index e8f9ddb..c59a114 100644 --- a/ScuffedMinecraft/src/Shader.h +++ b/ScuffedMinecraft/src/Shader.h @@ -1,6 +1,7 @@ #pragma once #include +#include class Shader { @@ -13,7 +14,12 @@ public: // use/activate the shader void use(); // utility uniform functions - void setBool(const std::string& name, bool value) const; - void setInt(const std::string& name, int value) const; - void setFloat(const std::string& name, float value) const; + void setBool(const std::string& name, bool value); + void setInt(const std::string& name, int value); + void setFloat(const std::string& name, float value); + + void setMat4x4f( const std::string& name, const glm::mat4& matrix ); + +private: + }; \ No newline at end of file