#include "../../../include/RenderBackend/OpenGL/DeviceMemory.hpp" #include "../../../include/RenderBackend/OpenGL/opengl.hpp" #include 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; default: assert(false); return -1; } } 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, &vertex_buffer_object_id); } void DeviceMemory::use() { glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); } 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)); 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::set(const DataView &triangles, StorageType storageType) { glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); 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::set(const DataView &triangles, StorageType storageType) { glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object_id); glBufferData(GL_ARRAY_BUFFER, triangles.getByteSize(), triangles.data, getOpenglStorageType(storageType)); 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); } }