Compare commits

..

2 Commits

Author SHA1 Message Date
Argore
9582453dd1 setMat4x4f uniform function 2024-11-05 19:28:30 +01:00
Argore
261b2540e0 iterator erase fix and improved readability 2024-11-05 17:00:17 +01:00
5 changed files with 60 additions and 49 deletions

View File

@ -380,28 +380,20 @@ int main(int argc, char *argv[])
projection = glm::perspective(glm::radians(camera.Zoom), windowX / windowY, 0.1f, 1000.0f); projection = glm::perspective(glm::radians(camera.Zoom), windowX / windowY, 0.1f, 1000.0f);
shader.use(); shader.use();
unsigned int viewLoc = glGetUniformLocation(shader.ID, "view"); shader.setMat4x4f( "view", view );
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); shader.setMat4x4f( "projection", projection );
unsigned int projectionLoc = glGetUniformLocation(shader.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
waterShader.use(); waterShader.use();
viewLoc = glGetUniformLocation(waterShader.ID, "view"); waterShader.setMat4x4f( "view", view );
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); waterShader.setMat4x4f( "projection", projection );
projectionLoc = glGetUniformLocation(waterShader.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
billboardShader.use(); billboardShader.use();
viewLoc = glGetUniformLocation(billboardShader.ID, "view"); billboardShader.setMat4x4f( "view", view );
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); billboardShader.setMat4x4f( "projection", projection );
projectionLoc = glGetUniformLocation(billboardShader.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
outlineShader.use(); outlineShader.use();
viewLoc = glGetUniformLocation(outlineShader.ID, "view"); outlineShader.setMat4x4f( "view", view );
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); outlineShader.setMat4x4f( "projection", projection );
projectionLoc = glGetUniformLocation(outlineShader.ID, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
Planet::planet->Update(camera.Position); Planet::planet->Update(camera.Position);
@ -492,8 +484,7 @@ int main(int argc, char *argv[])
glEnable(GL_BLEND); glEnable(GL_BLEND);
glEnable(GL_COLOR_LOGIC_OP); glEnable(GL_COLOR_LOGIC_OP);
unsigned int crosshairProjLoc = glGetUniformLocation(crosshairShader.ID, "projection"); crosshairShader.setMat4x4f( "projection", ortho );
glUniformMatrix4fv(crosshairProjLoc, 1, GL_FALSE, glm::value_ptr(ortho));
glBindVertexArray(crosshairVAO); glBindVertexArray(crosshairVAO);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);

View File

@ -519,18 +519,15 @@ void Chunk::Render(Shader* mainShader, Shader* billboardShader)
// Render main mesh // Render main mesh
mainShader->use(); mainShader->use();
mainShader->setMat4x4f( "model", model );
modelLoc = glGetUniformLocation(mainShader->ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glBindVertexArray(mainVAO); glBindVertexArray(mainVAO);
glDrawElements(GL_TRIANGLES, numTrianglesMain, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, numTrianglesMain, GL_UNSIGNED_INT, 0);
// Render billboard mesh // Render billboard mesh
billboardShader->use(); billboardShader->use();
billboardShader->setMat4x4f( "model", model );
modelLoc = glGetUniformLocation(billboardShader->ID, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
glBindVertexArray(billboardVAO); glBindVertexArray(billboardVAO);
@ -546,12 +543,11 @@ void Chunk::RenderWater(Shader* shader)
//std::cout << "Rendering chunk " << chunkPos.x << ", " << chunkPos.y << ", " << chunkPos.z << '\n' //std::cout << "Rendering chunk " << chunkPos.x << ", " << chunkPos.y << ", " << chunkPos.z << '\n'
// << "Chunk VAO: " << vertexArrayObject << '\n' << "Triangles: " << numTriangles << '\n'; // << "Chunk VAO: " << vertexArrayObject << '\n' << "Triangles: " << numTriangles << '\n';
modelLoc = glGetUniformLocation(shader->ID, "model");
glBindVertexArray(waterVAO); glBindVertexArray(waterVAO);
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, worldPos); 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); glDrawElements(GL_TRIANGLES, numTrianglesWater, GL_UNSIGNED_INT, 0);
} }

View File

@ -39,13 +39,15 @@ void Planet::Update(glm::vec3 cameraPos)
{ {
numChunks++; numChunks++;
if (!(*it->second).ready) Chunk& chunk = *it->second;
if (!chunk.ready)
chunksLoading++; chunksLoading++;
int chunkX = (*it->second).chunkPos.x; int chunkX = chunk.chunkPos.x;
int chunkY = (*it->second).chunkPos.y; int chunkY = chunk.chunkPos.y;
int chunkZ = (*it->second).chunkPos.z; int chunkZ = chunk.chunkPos.z;
if ((*it->second).ready && (abs(chunkX - camChunkX) > renderDistance || if ( chunk.ready && (abs(chunkX - camChunkX) > renderDistance ||
abs(chunkY - camChunkY) > renderDistance || abs(chunkY - camChunkY) > renderDistance ||
abs(chunkZ - camChunkZ) > renderDistance)) abs(chunkZ - camChunkZ) > renderDistance))
{ {
@ -60,13 +62,13 @@ void Planet::Update(glm::vec3 cameraPos)
chunkDataDeleteQueue.push({ chunkX, chunkY, chunkZ - 1 }); chunkDataDeleteQueue.push({ chunkX, chunkY, chunkZ - 1 });
// Delete chunk // Delete chunk
delete it->second; delete it->second; // &chunk
it = chunks.erase(it); it = chunks.erase(it);
} }
else else
{ {
numChunksRendered++; numChunksRendered++;
(*it->second).Render(solidShader, billboardShader); chunk.Render(solidShader, billboardShader);
++it; ++it;
} }
} }
@ -75,11 +77,13 @@ void Planet::Update(glm::vec3 cameraPos)
waterShader->use(); waterShader->use();
for (auto it = chunks.begin(); it != chunks.end(); ) for (auto it = chunks.begin(); it != chunks.end(); )
{ {
int chunkX = (*it->second).chunkPos.x; Chunk& chunk = *it->second;
int chunkY = (*it->second).chunkPos.y;
int chunkZ = (*it->second).chunkPos.z;
(*it->second).RenderWater(waterShader); int chunkX = chunk.chunkPos.x;
int chunkY = chunk.chunkPos.y;
int chunkZ = chunk.chunkPos.z;
chunk.RenderWater(waterShader);
++it; ++it;
} }
@ -102,11 +106,16 @@ 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() &&
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); chunkMutex.lock();
chunkData.erase(pos); delete it->second;
it = chunkData.erase( it );
chunkMutex.unlock();
} }
else
{
++it; ++it;
} }
}
// Check if camera moved to new chunk // Check if camera moved to new chunk
if (camChunkX != lastCamX || camChunkY != lastCamY || camChunkZ != lastCamZ) if (camChunkX != lastCamX || camChunkY != lastCamY || camChunkZ != lastCamZ)

View File

@ -2,6 +2,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/gtc/type_ptr.hpp>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
@ -91,15 +93,22 @@ void Shader::use()
glUseProgram(ID); 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 ) );
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <glm/fwd.hpp>
class Shader class Shader
{ {
@ -13,7 +14,12 @@ public:
// use/activate the shader // use/activate the shader
void use(); void use();
// utility uniform functions // utility uniform functions
void setBool(const std::string& name, bool value) const; void setBool(const std::string& name, bool value);
void setInt(const std::string& name, int value) const; void setInt(const std::string& name, int value);
void setFloat(const std::string& name, float value) const; void setFloat(const std::string& name, float value);
void setMat4x4f( const std::string& name, const glm::mat4& matrix );
private:
}; };