-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceMesh.cpp
More file actions
104 lines (86 loc) · 2.61 KB
/
ResourceMesh.cpp
File metadata and controls
104 lines (86 loc) · 2.61 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "ResourceMesh.h"
#include "Color.h"
ResourceMesh::ResourceMesh(std::string uid) : Resource(uid, Res_Type::mesh)
{}
ResourceMesh::~ResourceMesh()
{
UnLoadToMemory();
delete[] buffer;
}
void ResourceMesh::LoadToMemory()
{
char* cursor = buffer;
uint ranges[5]; // amount of vertices / tris / normals / colors / texture_coords
uint bytes = sizeof(ranges);
memcpy(ranges, cursor, bytes);
num_vertices = ranges[0];
num_tris = ranges[1];
num_normals = ranges[2];
num_colors = ranges[3];
num_texcoords = ranges[4];
// Load vertices
cursor += bytes;
bytes = sizeof(float3) * num_vertices;
vertices = new float3[num_vertices];
memcpy(vertices, cursor, bytes);
// Load indices
cursor += bytes;
bytes = sizeof(Tri) * num_tris;
tris = new Tri[num_tris];
memcpy(tris, cursor, bytes);
if (num_tris > 0)
{
glGenBuffers(1, (GLuint*) &(id_tris));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id_tris);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Tri) * num_tris, tris, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
// Load normals
cursor += bytes;
bytes = sizeof(float3) * num_normals;
normals = new float3[num_normals];
memcpy(normals, cursor, bytes);
if (num_normals > 0)
{
glGenBuffers(1, (GLuint*) &(id_normals));
glBindBuffer(GL_ARRAY_BUFFER, id_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(float3) * num_normals, normals, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Load colors
cursor += bytes;
bytes = sizeof(Color) * num_colors;
colors = new Color[num_colors];
memcpy(colors, cursor, bytes);
if (num_colors > 0)
{
glGenBuffers(1, (GLuint*) &(id_colors));
glBindBuffer(GL_ARRAY_BUFFER, id_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(Color) * num_colors, colors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// Load texcoords
cursor += bytes;
bytes = sizeof(float2) * num_texcoords;
texcoords = new float2[num_texcoords];
memcpy(texcoords, cursor, bytes);
if (num_texcoords > 0)
{
glGenBuffers(1, (GLuint*) &(id_texcoords));
glBindBuffer(GL_ARRAY_BUFFER, id_texcoords);
glBufferData(GL_ARRAY_BUFFER, sizeof(float2) * num_texcoords, texcoords, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void ResourceMesh::UnLoadToMemory()
{
if (tris) delete[] tris;
if (vertices) delete[] vertices;
if (normals) delete[] normals;
if (colors) delete[] colors;
if (texcoords) delete[] texcoords;
if (num_tris) glDeleteBuffers(1, (GLuint*) &(id_tris));
if (num_normals) glDeleteBuffers(1, (GLuint*) &(id_normals));
if (id_colors) glDeleteBuffers(1, &id_colors);
if (num_texcoords) glDeleteBuffers(1, (GLuint*) &(id_texcoords));
}