setMat4x4f uniform function

This commit is contained in:
Argore 2024-11-05 17:28:05 +01:00
parent 261b2540e0
commit 9582453dd1
5 changed files with 39 additions and 35 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

@ -106,8 +106,10 @@ 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())
{ {
chunkMutex.lock();
delete it->second; delete it->second;
it = chunkData.erase( it ); it = chunkData.erase( it );
chunkMutex.unlock();
} }
else else
{ {

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:
}; };