diff options
Diffstat (limited to 'src/RenderBackend/OpenGL')
-rw-r--r-- | src/RenderBackend/OpenGL/DeviceMemory.cpp | 28 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Image.cpp | 48 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Shader.cpp | 1 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/ShaderProgram.cpp | 4 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Texture2D.cpp | 52 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Uniform.cpp | 10 |
6 files changed, 68 insertions, 75 deletions
diff --git a/src/RenderBackend/OpenGL/DeviceMemory.cpp b/src/RenderBackend/OpenGL/DeviceMemory.cpp index f071cd8..a82cdaa 100644 --- a/src/RenderBackend/OpenGL/DeviceMemory.cpp +++ b/src/RenderBackend/OpenGL/DeviceMemory.cpp @@ -15,24 +15,6 @@ namespace amalgine } } - u32 getOpenglPrimitiveType(DeviceMemory::PrimitiveType primitiveType) - { - switch(primitiveType) - { - case DeviceMemory::PrimitiveType::TRIANGLE: return GL_TRIANGLES; - default: assert(false); return -1; - } - } - - u32 getPrimitiveTypePointsPerVertices(DeviceMemory::PrimitiveType primitiveType) - { - switch(primitiveType) - { - case DeviceMemory::PrimitiveType::TRIANGLE: return 3; - default: assert(false); return -1; - } - } - DeviceMemory::DeviceMemory() : primitiveType(0), numVertices(0), type(DeviceMemoryType::NONE) { glGenBuffers(1, &vertexBufferObjectId); @@ -57,12 +39,20 @@ namespace amalgine numVertices = getPrimitiveTypePointsPerVertices(primitiveType); } */ + void DeviceMemory::copy(const DataView<vec2f> &texture_coords, StorageType storageType) { + glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + glBufferData(GL_ARRAY_BUFFER, texture_coords.getByteSize(), texture_coords.data, getOpenglStorageType(storageType)); + primitiveType = GL_TRIANGLES; + numVertices = texture_coords.size * 2; + type = DeviceMemoryType::VEC2; + } + void DeviceMemory::copy(const DataView<Triangle2D> &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; + numVertices = triangles.size * 2; type = DeviceMemoryType::VEC2; } diff --git a/src/RenderBackend/OpenGL/Image.cpp b/src/RenderBackend/OpenGL/Image.cpp deleted file mode 100644 index 128a580..0000000 --- a/src/RenderBackend/OpenGL/Image.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "../../../include/RenderBackend/OpenGL/Image.hpp" -#include <SOIL/SOIL.h> - -using namespace std; - -namespace amalgine -{ - Image::Image(unsigned char *_imageData, i32 _width, i32 _height) : imageData(_imageData), width(_width), height(_height) - { - - } - - Result<Image*> Image::loadFromFile(const char *filepath) - { - int width; - int height; - unsigned char *imageData = SOIL_load_image(filepath, &width, &height, 0, SOIL_LOAD_RGB); - if(!imageData) - { - string errMsg = "Failed to load image from file: "; - errMsg += filepath; - errMsg += "; SOIL error message: "; - errMsg += SOIL_last_result(); - return Result<Image*>::Err(errMsg); - } - return Result<Image*>::Ok(new Image(imageData, width, height)); - } - - const unsigned char* Image::getData() const - { - return imageData; - } - - i32 Image::getWidth() const - { - return width; - } - - i32 Image::getHeight() const - { - return height; - } - - Image::~Image() - { - SOIL_free_image_data(imageData); - } -} diff --git a/src/RenderBackend/OpenGL/Shader.cpp b/src/RenderBackend/OpenGL/Shader.cpp index 3b6563f..32e2fe6 100644 --- a/src/RenderBackend/OpenGL/Shader.cpp +++ b/src/RenderBackend/OpenGL/Shader.cpp @@ -10,6 +10,7 @@ namespace amalgine { return GL_FRAGMENT_SHADER; } assert(false); + return 0; } static std::string get_shader_compile_log(GLuint shaderId) diff --git a/src/RenderBackend/OpenGL/ShaderProgram.cpp b/src/RenderBackend/OpenGL/ShaderProgram.cpp index 389a929..14b15aa 100644 --- a/src/RenderBackend/OpenGL/ShaderProgram.cpp +++ b/src/RenderBackend/OpenGL/ShaderProgram.cpp @@ -73,7 +73,7 @@ namespace amalgine { return Result<Uniform>::Ok(std::move(uniform)); } - int ShaderProgram::set_vertex_input(const char *name, const DeviceMemory &data) { + int ShaderProgram::set_input_data(const char *name, const DeviceMemory &data) { GLint attrib_location = glGetAttribLocation(program_id, name); if(attrib_location == -1) { fprintf(stderr, "No such attribute in shader: %s\n", name); @@ -81,6 +81,7 @@ namespace amalgine { } data.use(); + glEnableVertexAttribArray(attrib_location); switch(data.get_type()) { case DeviceMemoryType::NONE: return -1; @@ -93,7 +94,6 @@ namespace amalgine { break; } } - glEnableVertexAttribArray(attrib_location); return 0; } diff --git a/src/RenderBackend/OpenGL/Texture2D.cpp b/src/RenderBackend/OpenGL/Texture2D.cpp index 07d807a..a561f6e 100644 --- a/src/RenderBackend/OpenGL/Texture2D.cpp +++ b/src/RenderBackend/OpenGL/Texture2D.cpp @@ -1,15 +1,54 @@ #include "../../../include/RenderBackend/OpenGL/Texture2D.hpp" -#include "../../../include/RenderBackend/OpenGL/Image.hpp" #include "../../../include/RenderBackend/OpenGL/opengl.hpp" +#include "../../../include/Image.hpp" +#include <stack> #include <cassert> -namespace amalgine -{ +namespace amalgine { + struct TextureIdAllocator { + TextureIdAllocator(){ + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units); + if(max_texture_units < 1) + max_texture_units = 1; + printf("max texture units: %d\n", max_texture_units); + } + + u32 get_free_texture_id() { + if(!free_spots.empty()) { + u32 texture_id = free_spots.top(); + free_spots.pop(); + return texture_id; + } + + if(texture_id_counter + 1 == max_texture_units) { + fprintf(stderr, "Error: No free spot left for textures, resetting counter\n"); + texture_id_counter = 0; + } + + return texture_id_counter++; + } + + void free_texture_id(i32 texture_id) { + free_spots.push(texture_id); + } + + std::stack<u32> free_spots; + i32 texture_id_counter = 0; + i32 max_texture_units = 0; + }; + + static TextureIdAllocator *texture_id_allocator = nullptr; + Texture2D::Texture2D(Image *image) { assert(image); - glGenTextures(1, &textureId); - glBindTexture(GL_TEXTURE_2D, textureId); + if(!texture_id_allocator) + texture_id_allocator = new TextureIdAllocator(); + + texture_id = texture_id_allocator->get_free_texture_id(); + glGenTextures(1, &texture_ref); + glActiveTexture(GL_TEXTURE0 + texture_id); + glBindTexture(GL_TEXTURE_2D, texture_ref); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->getData()); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -21,6 +60,7 @@ namespace amalgine Texture2D::~Texture2D() { - glDeleteTextures(1, &textureId); + texture_id_allocator->free_texture_id(texture_id); + glDeleteTextures(1, &texture_ref); } } diff --git a/src/RenderBackend/OpenGL/Uniform.cpp b/src/RenderBackend/OpenGL/Uniform.cpp index 67cf3c6..5f68bc6 100644 --- a/src/RenderBackend/OpenGL/Uniform.cpp +++ b/src/RenderBackend/OpenGL/Uniform.cpp @@ -4,6 +4,11 @@ namespace amalgine { Uniform::~Uniform() {} + void Uniform::set(float value) { + use(); + glUniform1f(uniform_id, value); + } + void Uniform::set(const vec3f &value) { use(); glUniform3f(uniform_id, value.x, value.y, value.z); @@ -14,6 +19,11 @@ namespace amalgine { glUniformMatrix4fv(uniform_id, 1, GL_FALSE, glm::value_ptr(value)); } + void Uniform::set(const Texture2D &texture) { + use(); + glUniform1i(uniform_id, texture.get_texture_id()); + } + Uniform::Uniform(i32 uniform_id, u32 shader_program_id) : uniform_id(uniform_id), program_id(shader_program_id) { } |