1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include "../../../include/RenderBackend/OpenGL/DeviceMemory.hpp"
#include "../../../include/RenderBackend/OpenGL/opengl.hpp"
#include <cassert>
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<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));
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<Triangle2D> &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<Triangle3D> &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);
}
}
|