From 8d9b24b1b84107c90a77d94c86a810cc068fe073 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 19 Feb 2020 22:14:12 +0100 Subject: Fix rendering of texture with multiple frames --- src/RenderBackend/OpenGL/DeviceMemory.cpp | 80 +++++++++++++------------------ 1 file changed, 33 insertions(+), 47 deletions(-) (limited to 'src/RenderBackend/OpenGL/DeviceMemory.cpp') diff --git a/src/RenderBackend/OpenGL/DeviceMemory.cpp b/src/RenderBackend/OpenGL/DeviceMemory.cpp index a82cdaa..7447657 100644 --- a/src/RenderBackend/OpenGL/DeviceMemory.cpp +++ b/src/RenderBackend/OpenGL/DeviceMemory.cpp @@ -2,12 +2,9 @@ #include "../../../include/RenderBackend/OpenGL/opengl.hpp" #include -namespace amalgine -{ - u32 getOpenglStorageType(DeviceMemory::StorageType storageType) - { - switch(storageType) - { +namespace amalgine { + u32 getOpenglStorageType(DeviceMemory::StorageType storageType) { + switch(storageType) { case DeviceMemory::StorageType::STATIC: return GL_STATIC_DRAW; case DeviceMemory::StorageType::DYNAMIC: return GL_DYNAMIC_DRAW; case DeviceMemory::StorageType::STREAM: return GL_STREAM_DRAW; @@ -15,58 +12,47 @@ namespace amalgine } } - DeviceMemory::DeviceMemory() : primitiveType(0), numVertices(0), type(DeviceMemoryType::NONE) - { - glGenBuffers(1, &vertexBufferObjectId); + DeviceMemory::DeviceMemory(u32 vertex_array_object_id, i32 attrib_location) : + vertex_array_object_id(vertex_array_object_id), vertex_buffer_object_id(-1), attrib_location(attrib_location), + num_vertices(0), memory_type(DeviceMemoryType::NONE) { + glGenBuffers(1, &vertex_buffer_object_id); } - DeviceMemory::~DeviceMemory() - { - glDeleteBuffers(1, &vertexBufferObjectId); + DeviceMemory::~DeviceMemory() { + glDeleteBuffers(1, &vertex_buffer_object_id); } - void DeviceMemory::use() const - { - // TODO: Bind vao here? - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + void DeviceMemory::use() { + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); } - /* - void DeviceMemory::copy(const DataView &data, StorageType storageType, PrimitiveType primitiveType) - { - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); - glBufferData(GL_ARRAY_BUFFER, data.getByteSize(), data.data, getOpenglStorageType(storageType)); - this->primitiveType = getOpenglPrimitiveType(primitiveType); - numVertices = getPrimitiveTypePointsPerVertices(primitiveType); - } - */ - void DeviceMemory::copy(const DataView &texture_coords, StorageType storageType) { - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + + void DeviceMemory::set(const DataView &texture_coords, StorageType storageType) { + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); glBufferData(GL_ARRAY_BUFFER, texture_coords.getByteSize(), texture_coords.data, getOpenglStorageType(storageType)); - primitiveType = GL_TRIANGLES; - numVertices = texture_coords.size * 2; - type = DeviceMemoryType::VEC2; + num_vertices = texture_coords.size * 2; + memory_type = DeviceMemoryType::VEC2; + + glBindVertexArray(vertex_array_object_id); // Encapsulate DeviceMemory (vertex buffer object) inside the active vertex array object + glVertexAttribPointer(attrib_location, 2, GL_FLOAT, GL_FALSE, 0, 0); } - void DeviceMemory::copy(const DataView &triangles, StorageType storageType) - { - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + void DeviceMemory::set(const DataView &triangles, StorageType storageType) { + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); - primitiveType = GL_TRIANGLES; - numVertices = triangles.size * 2; - type = DeviceMemoryType::VEC2; + num_vertices = triangles.size * 2; + memory_type = DeviceMemoryType::VEC2; + + glBindVertexArray(vertex_array_object_id); // Encapsulate DeviceMemory (vertex buffer object) inside the active vertex array object + glVertexAttribPointer(attrib_location, 2, GL_FLOAT, GL_FALSE, 0, 0); } - void DeviceMemory::copy(const DataView &triangles, StorageType storageType) - { - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId); + void DeviceMemory::set(const DataView &triangles, StorageType storageType) { + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); - primitiveType = GL_TRIANGLES; - numVertices = triangles.size * 3; - type = DeviceMemoryType::VEC3; - } - - void DeviceMemory::draw() - { - glDrawArrays(primitiveType, 0, numVertices); + num_vertices = triangles.size * 3; + memory_type = DeviceMemoryType::VEC3; + + glBindVertexArray(vertex_array_object_id); // Encapsulate DeviceMemory (vertex buffer object) inside the active vertex array object + glVertexAttribPointer(attrib_location, 3, GL_FLOAT, GL_FALSE, 0, 0); } } -- cgit v1.2.3