diff options
| author | Mel <einebeere@gmail.com> | 2022-10-04 01:18:19 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-04 01:18:19 +0200 |
| commit | 0631fb666d2a28a6eb9b8d1578675699b41a5de6 (patch) | |
| tree | 1db9b88d9c11074864f6959d32960eb6c54d3c4b /src/Shader | |
| parent | 75f3941579c756655fc7d4d29e7b92b6eae436b7 (diff) | |
| download | meowcraft-0631fb666d2a28a6eb9b8d1578675699b41a5de6.tar.zst meowcraft-0631fb666d2a28a6eb9b8d1578675699b41a5de6.zip | |
Cube Rendering
Diffstat (limited to 'src/Shader')
| -rw-r--r-- | src/Shader/ShaderProgram.cpp | 10 | ||||
| -rw-r--r-- | src/Shader/ShaderProgram.hpp | 10 | ||||
| -rw-r--r-- | src/Shader/Uniform.cpp | 14 | ||||
| -rw-r--r-- | src/Shader/Uniform.hpp | 23 | ||||
| -rw-r--r-- | src/Shader/fragment.glsl | 5 | ||||
| -rw-r--r-- | src/Shader/vertex.glsl | 6 |
6 files changed, 62 insertions, 6 deletions
diff --git a/src/Shader/ShaderProgram.cpp b/src/Shader/ShaderProgram.cpp index 854b1bf..4d0b684 100644 --- a/src/Shader/ShaderProgram.cpp +++ b/src/Shader/ShaderProgram.cpp @@ -29,4 +29,14 @@ void ShaderProgram::bind() const { glUseProgram(m_program); } +Uniform ShaderProgram::uniform(const std::string& name) const { + auto index = glGetUniformLocation(m_program, name.c_str()); + + return {name, static_cast<uint32_t>(index)}; +} + +uint32_t ShaderProgram::get() const { + return m_program; +} + } diff --git a/src/Shader/ShaderProgram.hpp b/src/Shader/ShaderProgram.hpp index 800e3ff..857dc2f 100644 --- a/src/Shader/ShaderProgram.hpp +++ b/src/Shader/ShaderProgram.hpp @@ -1,6 +1,10 @@ #pragma once +#include <string> +#include <vector> #include "Shader.hpp" +#include "../Math/Math.hpp" +#include "Uniform.hpp" namespace MC { @@ -8,9 +12,11 @@ class ShaderProgram { public: ShaderProgram(Shader fragment, Shader vertex); - void bind() const; + uint32_t get() const; + + Uniform uniform(const std::string& name) const; - uint32_t get(); + void bind() const; private: uint32_t m_program; diff --git a/src/Shader/Uniform.cpp b/src/Shader/Uniform.cpp new file mode 100644 index 0000000..71b7633 --- /dev/null +++ b/src/Shader/Uniform.cpp @@ -0,0 +1,14 @@ +#include <GL/glew.h> +#include "Uniform.hpp" + +namespace MC { + +void Uniform::set(Matrix<4, 4> value) const { + glUniformMatrix4fv(m_index, 1, GL_TRUE, value.elements); +} + +void Uniform::set(Vector<3> value) const { + glUniform3f(m_index, value.x(), value.y(), value.z()); +} + +} diff --git a/src/Shader/Uniform.hpp b/src/Shader/Uniform.hpp new file mode 100644 index 0000000..58cdc28 --- /dev/null +++ b/src/Shader/Uniform.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include <cstdint> +#include <string> +#include <utility> +#include "../Math/Math.hpp" + +namespace MC { + +class Uniform { +public: + Uniform(std::string name, uint32_t index) + : m_name(std::move(name)), m_index(index) {}; + + void set(Matrix<4, 4> value) const; + void set(Vector<3> value) const; + +private: + std::string m_name; + uint32_t m_index; +}; + +} diff --git a/src/Shader/fragment.glsl b/src/Shader/fragment.glsl index 017a2c8..ba5ed93 100644 --- a/src/Shader/fragment.glsl +++ b/src/Shader/fragment.glsl @@ -2,7 +2,6 @@ out vec4 color; -void main() -{ - color = vec4(1.0f, 0.5f, 0.2f, 1.0f); +void main() { + color = vec4(0.2f, 0.6f, 0.6f, 1.0f); // #339999 } \ No newline at end of file diff --git a/src/Shader/vertex.glsl b/src/Shader/vertex.glsl index 8b1bbbd..e7b9e3d 100644 --- a/src/Shader/vertex.glsl +++ b/src/Shader/vertex.glsl @@ -1,7 +1,11 @@ #version 330 core +uniform mat4 model_matrix; +uniform mat4 view_matrix; +uniform mat4 projection_matrix; + in vec3 position; void main() { - gl_Position = vec4(position.x, position.y, position.z, 1.0); + gl_Position = projection_matrix * view_matrix * model_matrix * vec4(position, 1.0); } \ No newline at end of file |
