-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Object.cpp
More file actions
173 lines (128 loc) · 4.11 KB
/
D_Object.cpp
File metadata and controls
173 lines (128 loc) · 4.11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifdef DOBJ
#define DOBJ
#include "D_Obj.hpp"
D_Object::D_Object( Mesh meh, GLuint tex){
mesh = (Mesh*)malloc(sizeof(Mesh) +
meh.vertices.size() * sizeof(vec3) +
meh.uvs.size() * sizeof(vec2) +
meh.normals.size() * sizeof(vec3) +
meh.indices.size() * sizeof(unsigned short));
mesh->vertices = meh.vertices;
mesh->uvs = meh.uvs;
mesh->normals = meh.normals;
mesh->indices = meh.indices;
texture = tex;
buffers = (GLuint**)malloc(sizeof(GLuint*)*4);
buffers[0] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).vertices.size());
buffers[1] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).uvs.size());
buffers[2] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).normals.size());
buffers[3] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).indices.size());
GLuint *vb = buffers[0],
*uv = buffers[1],
*norm = buffers[2],
*idx = buffers[3];
glGenBuffers(1, vb);
glBindBuffer(GL_ARRAY_BUFFER, *vb);
glBufferData(GL_ARRAY_BUFFER, (*mesh).vertices.size() * sizeof(vec3),
&(*mesh).vertices, GL_STATIC_DRAW);
glGenBuffers(1, uv);
glBindBuffer(GL_ARRAY_BUFFER, *uv);
glBufferData(GL_ARRAY_BUFFER, (*mesh).uvs.size() * sizeof(vec2),
&(*mesh).uvs, GL_STATIC_DRAW);
glGenBuffers(1, norm);
glBindBuffer(GL_ARRAY_BUFFER, *norm);
glBufferData(GL_ARRAY_BUFFER, (*mesh).normals.size() * sizeof(vec3),
&(*mesh).normals, GL_STATIC_DRAW);
glGenBuffers(1, idx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *idx);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(*mesh).indices.size() * sizeof(unsigned short),
&(*mesh).indices, GL_STATIC_DRAW);
}
D_Object::D_Object(const char* object_path, const char* tex_path){
vector<vec3> verts, normals;
vector<vec2> uvs;
if(!loadOBJ(object_path, verts, uvs, normals)){
fprintf(stderr, "Failed to load %s\n", object_path);
return;
}
Mesh m;
indexVBO_slow(verts, uvs, normals,
m.indices,
m.vertices, m.uvs, m.normals);
mesh = (Mesh*)malloc(sizeof(Mesh) +
m.vertices.size() * sizeof(vec3) +
m.uvs.size() * sizeof(vec2) +
m.normals.size() * sizeof(vec3) +
m.indices.size() * sizeof(unsigned short));
mesh->vertices = m.vertices;
mesh->uvs = m.uvs;
mesh->normals = m.normals;
mesh->indices = m.indices;
texture = SOIL_load_OGL_texture(
tex_path,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS |
SOIL_FLAG_INVERT_Y |
SOIL_FLAG_NTSC_SAFE_RGB
);
buffers = (GLuint**)malloc(sizeof(GLuint*)*4);
buffers[0] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).vertices.size());
buffers[1] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).uvs.size());
buffers[2] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).normals.size());
buffers[3] = (GLuint*)malloc(sizeof(GLuint) * (*mesh).indices.size());
GLuint *vb = buffers[0],
*uv = buffers[1],
*norm = buffers[2],
*idx = buffers[3];
glGenBuffers(1, vb);
glBindBuffer(GL_ARRAY_BUFFER, *vb);
glBufferData(GL_ARRAY_BUFFER, (*mesh).vertices.size() * sizeof(vec3),
&(*mesh).vertices, GL_STATIC_DRAW);
glGenBuffers(1, uv);
glBindBuffer(GL_ARRAY_BUFFER, *uv);
glBufferData(GL_ARRAY_BUFFER, (*mesh).uvs.size() * sizeof(vec2),
&(*mesh).uvs, GL_STATIC_DRAW);
glGenBuffers(1, norm);
glBindBuffer(GL_ARRAY_BUFFER, *norm);
glBufferData(GL_ARRAY_BUFFER, (*mesh).normals.size() * sizeof(vec3),
&(*mesh).normals, GL_STATIC_DRAW);
glGenBuffers(1, idx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *idx);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(*mesh).indices.size() * sizeof(unsigned short),
&(*mesh).indices, GL_STATIC_DRAW);
}
Mesh D_Object:: GetMesh(){
return *mesh;
}
GLuint **D_Object:: GetBuffers(){
return buffers;
}
GLuint *D_Object:: getTexture(){
return &texture;
}
void D_Object:: setShaderIdx(int idx){
shader_idx = idx;
}
int D_Object:: getShaderIdx(){
return shader_idx;
}
mat4 D_Object:: getModelMatrix(){
mat4 id = mat4(1);
return translate(id, translation) *
scale(id, scale_factor) *
rotate(id, rotation, rotation_axis);
}
void D_Object:: set_translation(vec3 trans){
translation = trans;
}
void D_Object:: set_rotation(float rot, vec3 rot_axis){
rotation = rot;
rotation_axis = rot_axis;
}
void D_Object:: set_scale(vec3 factor){
scale_factor = factor;
}
#endif