From 83c78e2b5cc9b0cb737ec3785722ae280bd29b65 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 14 Feb 2020 10:43:26 +0100 Subject: Use shader from file.. --- src/RenderBackend/OpenGL/DeviceMemory.cpp | 9 +++++++++ src/RenderBackend/OpenGL/ShaderProgram.cpp | 19 +++++++++++-------- src/RenderBackend/OpenGL/Uniform.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 src/RenderBackend/OpenGL/Uniform.cpp (limited to 'src/RenderBackend') diff --git a/src/RenderBackend/OpenGL/DeviceMemory.cpp b/src/RenderBackend/OpenGL/DeviceMemory.cpp index 2d40569..f071cd8 100644 --- a/src/RenderBackend/OpenGL/DeviceMemory.cpp +++ b/src/RenderBackend/OpenGL/DeviceMemory.cpp @@ -65,6 +65,15 @@ namespace amalgine numVertices = triangles.size * 3; type = DeviceMemoryType::VEC2; } + + void DeviceMemory::copy(const DataView &triangles, StorageType storageType) + { + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); + primitiveType = GL_TRIANGLES; + numVertices = triangles.size * 3; + type = DeviceMemoryType::VEC3; + } void DeviceMemory::draw() { diff --git a/src/RenderBackend/OpenGL/ShaderProgram.cpp b/src/RenderBackend/OpenGL/ShaderProgram.cpp index e613668..389a929 100644 --- a/src/RenderBackend/OpenGL/ShaderProgram.cpp +++ b/src/RenderBackend/OpenGL/ShaderProgram.cpp @@ -64,13 +64,13 @@ namespace amalgine { return Result>::Ok(std::move(shader_program)); } - int ShaderProgram::set_uniform(const char *name, const vec3f &value) { - GLint uniformId = glGetUniformLocation(program_id, name); - if(uniformId == -1) - return -1; - use(); - glUniform3f(uniformId, value.x, value.y, value.z); - return 0; + Result ShaderProgram::get_uniform_by_name(const char *name) { + GLint uniform_id = glGetUniformLocation(program_id, name); + if(uniform_id == -1) + return Result::Err(std::string("Uniform with name ") + name + " was not found"); + + Uniform uniform(uniform_id, program_id); + return Result::Ok(std::move(uniform)); } int ShaderProgram::set_vertex_input(const char *name, const DeviceMemory &data) { @@ -88,7 +88,10 @@ namespace amalgine { glVertexAttribPointer(attrib_location, 2, GL_FLOAT, GL_FALSE, 0, 0); break; } - case DeviceMemoryType::VEC3: assert(false); return -1; + case DeviceMemoryType::VEC3: { + glVertexAttribPointer(attrib_location, 3, GL_FLOAT, GL_FALSE, 0, 0); + break; + } } glEnableVertexAttribArray(attrib_location); return 0; diff --git a/src/RenderBackend/OpenGL/Uniform.cpp b/src/RenderBackend/OpenGL/Uniform.cpp new file mode 100644 index 0000000..67cf3c6 --- /dev/null +++ b/src/RenderBackend/OpenGL/Uniform.cpp @@ -0,0 +1,24 @@ +#include "../../../include/RenderBackend/OpenGL/Uniform.hpp" +#include "../../../include/RenderBackend/OpenGL/opengl.hpp" + +namespace amalgine { + Uniform::~Uniform() {} + + void Uniform::set(const vec3f &value) { + use(); + glUniform3f(uniform_id, value.x, value.y, value.z); + } + + void Uniform::set(const glm::mat4 &value) { + use(); + glUniformMatrix4fv(uniform_id, 1, GL_FALSE, glm::value_ptr(value)); + } + + Uniform::Uniform(i32 uniform_id, u32 shader_program_id) : uniform_id(uniform_id), program_id(shader_program_id) { + + } + + void Uniform::use() { + glUseProgram(program_id); + } +} \ No newline at end of file -- cgit v1.2.3