Skip to content

Commit

Permalink
Added uniform vector function
Browse files Browse the repository at this point in the history
  • Loading branch information
dorosch committed Sep 15, 2022
1 parent c5b197e commit 8a659f5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 96 deletions.
11 changes: 4 additions & 7 deletions include/core/render/shader/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ namespace Engine {

public:
static ShaderProgram *GetInstance();
virtual void Build(std::filesystem::path, std::filesystem::path) = 0;

virtual std::optional<uint32_t> Compile(Shader, std::string) = 0;
virtual void Linking() = 0;
virtual void Use() = 0;

// TODO: Change to Uniform3f, Uniform4m
virtual void Linking() = 0;
virtual std::optional<uint32_t> Compile(Shader, std::string) = 0;
virtual void Build(std::filesystem::path, std::filesystem::path) = 0;
virtual void UniformFloat(const char *, float) = 0;
virtual void UniformInt(const char *, int) = 0;
virtual void UniformColor(const char *, float, float, float) = 0;
virtual void UniformPosition(const char *, float, float, float) = 0;
virtual void UniformVector(const char *, glm::vec3) = 0;
virtual void UniformMatrix(const char *, glm::mat4) = 0;
};
}
Expand Down
9 changes: 2 additions & 7 deletions include/core/render/shader/opengl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ namespace Engine {
~OpenglShaderProgram();

std::optional<uint32_t> Compile(Shader, std::string);
void Use();
void Linking();

void Build(std::filesystem::path, std::filesystem::path);

void Use();

// TODO: Change to Uniform3f, Uniform4m
void UniformInt(const char *, int);
void UniformFloat(const char *, float);
void UniformColor(const char *, float, float, float);
void UniformPosition(const char *, float, float, float);
void UniformVector(const char *, glm::vec3);
void UniformMatrix(const char *, glm::mat4);
};
}
Expand Down
86 changes: 13 additions & 73 deletions src/core/render/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,53 +105,18 @@ void Render::RenderObject(Object *object, glm::mat4 projection, glm::mat4 view,
shader->UniformMatrix("view", view);
shader->UniformMatrix("projection", projection);

shader->UniformPosition(
"viewPosition",
cameraPosition.x,
cameraPosition.y,
cameraPosition.z
);

shader->UniformPosition(
"light.position",
lighting->transform->position.x,
lighting->transform->position.y,
lighting->transform->position.z
);
shader->UniformPosition(
"light.color",
lighting->light->color.x,
lighting->light->color.y,
lighting->light->color.z
);
shader->UniformPosition(
"light.ambient",
lighting->light->ambient.x,
lighting->light->ambient.y,
lighting->light->ambient.z
);
shader->UniformPosition(
"light.diffuse",
lighting->light->diffuse.x,
lighting->light->diffuse.y,
lighting->light->diffuse.z
);
shader->UniformPosition(
"light.specular",
lighting->light->specular.x,
lighting->light->specular.y,
lighting->light->specular.z
);
shader->UniformVector("viewPosition", cameraPosition);

shader->UniformVector("light.position", lighting->transform->position);
shader->UniformVector("light.color", lighting->light->color);
shader->UniformVector("light.ambient", lighting->light->ambient);
shader->UniformVector("light.diffuse", lighting->light->diffuse);
shader->UniformVector("light.specular", lighting->light->specular);
shader->UniformFloat("light.intensity", lighting->light->intensity);

if (lighting->light->lightType == Graphics::Lighting::Type::DIRECTIONAL) {
shader->UniformInt("light.type", Graphics::Lighting::Type::DIRECTIONAL);
shader->UniformPosition(
"light.direction",
lighting->light->direction.x,
lighting->light->direction.y,
lighting->light->direction.z
);
shader->UniformVector("light.direction", lighting->light->direction);
}
else if (lighting->light->lightType == Graphics::Lighting::Type::POINT) {
shader->UniformInt("light.type", Graphics::Lighting::Type::POINT);
Expand All @@ -161,12 +126,7 @@ void Render::RenderObject(Object *object, glm::mat4 projection, glm::mat4 view,
}
else if (lighting->light->lightType == Graphics::Lighting::Type::SPOT) {
shader->UniformInt("light.type", Graphics::Lighting::Type::SPOT);
shader->UniformPosition(
"light.direction",
lighting->light->direction.x,
lighting->light->direction.y,
lighting->light->direction.z
);
shader->UniformVector("light.direction", lighting->light->direction);
shader->UniformFloat("light.constant", lighting->light->constant);
shader->UniformFloat("light.linear", lighting->light->linear);
shader->UniformFloat("light.quadratic", lighting->light->quadratic);
Expand All @@ -175,30 +135,10 @@ void Render::RenderObject(Object *object, glm::mat4 projection, glm::mat4 view,
}

if (object->HasComponent(Ecs::Component::Type::MATERIAL)) {
shader->UniformPosition(
"material.ambient",
object->material->ambient.x,
object->material->ambient.y,
object->material->ambient.z
);
shader->UniformPosition(
"material.diffuse",
object->material->diffuse.x,
object->material->diffuse.y,
object->material->diffuse.z
);
shader->UniformPosition(
"material.specular",
object->material->specular.x,
object->material->specular.y,
object->material->specular.z
);
shader->UniformPosition(
"material.color",
object->material->color.x,
object->material->color.y,
object->material->color.z
);
shader->UniformVector("material.color", object->material->color);
shader->UniformVector("material.ambient", object->material->ambient);
shader->UniformVector("material.diffuse", object->material->diffuse);
shader->UniformVector("material.specular", object->material->specular);
shader->UniformFloat("material.shininess", object->material->shininess);
}

Expand Down
12 changes: 3 additions & 9 deletions src/core/render/shader/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,9 @@ void OpenglShaderProgram::UniformInt(const char *attribute, int value) {
}


void OpenglShaderProgram::UniformColor(const char *attribute, float r, float g, float b) {
GLint color = glGetUniformLocation(this->object, attribute);
glUniform3f(color, r, g, b);
}


void OpenglShaderProgram::UniformPosition(const char *attribute, float x, float y, float z) {
GLint position = glGetUniformLocation(this->object, attribute);
glUniform3f(position, x, y, z);
void OpenglShaderProgram::UniformVector(const char *attribute, glm::vec3 vector){
GLint variable = glGetUniformLocation(this->object, attribute);
glUniform3fv(variable, 1, glm::value_ptr(vector));
}


Expand Down

0 comments on commit 8a659f5

Please sign in to comment.