aboutsummaryrefslogtreecommitdiff
path: root/src/RenderBackend/OpenGL/DeviceMemory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/RenderBackend/OpenGL/DeviceMemory.cpp')
-rw-r--r--src/RenderBackend/OpenGL/DeviceMemory.cpp80
1 files changed, 33 insertions, 47 deletions
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 <cassert>
-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<f32> &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<vec2f> &texture_coords, StorageType storageType) {
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId);
+
+ void DeviceMemory::set(const DataView<vec2f> &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<Triangle2D> &triangles, StorageType storageType)
- {
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId);
+ void DeviceMemory::set(const DataView<Triangle2D> &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<Triangle3D> &triangles, StorageType storageType)
- {
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjectId);
+ void DeviceMemory::set(const DataView<Triangle3D> &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);
}
}