dungeon_crawler
view prototype/src/mesh.cc @ 61:4472ef41a209
forgot an extra mrt uniform in deferred_omni.p.glsl
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Sep 2012 06:37:56 +0300 |
parents | d0e93b4d9ec9 |
children | 6a471c87f9ca |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "mesh.h"
4 #include "opengl.h"
6 /*
7 #define DBG_NORMALS
8 #define NSZ 0.1
9 */
11 Mesh::Mesh()
12 {
13 nverts = nfaces = 0;
14 for(int i=0; i<NUM_MESH_ATTR; i++) {
15 vbo[i] = 0;
16 }
17 ibo = 0;
18 tang_loc = -1;
20 bsph_valid = false;
21 }
23 Mesh::~Mesh()
24 {
25 destroy();
26 }
28 void Mesh::set_name(const char *name)
29 {
30 this->name = name;
31 }
33 const char *Mesh::get_name() const
34 {
35 return name.c_str();
36 }
38 bool Mesh::create(const aiScene *scn, aiMesh *aim)
39 {
40 if(aim->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
41 fprintf(stderr, "%s: non-triangle mesh!? primtype mask: %x\n", __func__, aim->mPrimitiveTypes);
42 return false;
43 }
45 if(vbo[MESH_ATTR_VERTEX]) {
46 destroy();
47 }
49 name = aim->mName.data;
51 nverts = aim->mNumVertices;
52 nfaces = aim->mNumFaces;
54 glGenBuffers(1, vbo + MESH_ATTR_VERTEX);
55 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
56 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mVertices, aim->mVertices, GL_STATIC_DRAW);
58 if(aim->mNormals) {
59 glGenBuffers(1, vbo + MESH_ATTR_NORMAL);
60 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
61 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mNormals, aim->mNormals, GL_STATIC_DRAW);
62 }
63 if(aim->mTangents) {
64 glGenBuffers(1, vbo + MESH_ATTR_TANGENT);
65 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
66 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTangents, aim->mTangents, GL_STATIC_DRAW);
67 }
68 if(aim->mTextureCoords[0]) {
69 glGenBuffers(1, vbo + MESH_ATTR_TEXCOORD);
70 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
71 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof *aim->mTextureCoords[0], aim->mTextureCoords[0], GL_STATIC_DRAW);
72 }
74 glGenBuffers(1, &ibo);
75 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
76 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), 0, GL_STATIC_DRAW);
78 /* map the buffer and fill it up */
79 unsigned int *iptr = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
81 for(int i=0; i<(int)nfaces; i++) {
82 for(int j=0; j<3; j++) {
83 *iptr++ = aim->mFaces[i].mIndices[j];
84 }
85 }
86 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
87 glBindBuffer(GL_ARRAY_BUFFER, 0);
88 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
89 return true;
90 }
92 void Mesh::destroy()
93 {
94 for(int i=0; i<NUM_MESH_ATTR; i++) {
95 if(vbo[i]) {
96 glDeleteBuffers(1, vbo + i);
97 vbo[i] = 0;
98 }
99 }
100 glDeleteBuffers(1, &ibo);
101 ibo = 0;
103 nverts = nfaces = 0;
104 tang_loc = -1;
105 }
107 void Mesh::set_xform(const Matrix4x4 &mat)
108 {
109 xform = mat;
110 }
112 const Matrix4x4 &Mesh::get_xform() const
113 {
114 return xform;
115 }
117 void Mesh::set_material(const Material &mat)
118 {
119 this->mat = mat;
120 }
122 const Material &Mesh::get_material() const
123 {
124 return mat;
125 }
127 void Mesh::set_attrib_location(int attr, int loc)
128 {
129 if(attr == MESH_ATTR_TANGENT) {
130 tang_loc = loc;
131 }
132 }
134 int Mesh::get_attrib_location(int attr) const
135 {
136 return tang_loc;
137 }
139 void Mesh::draw() const
140 {
141 mat.setup();
143 glMatrixMode(GL_MODELVIEW);
144 glPushMatrix();
145 mult_matrix(xform);
147 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
148 glVertexPointer(3, GL_FLOAT, 0, 0);
149 glEnableClientState(GL_VERTEX_ARRAY);
151 if(vbo[MESH_ATTR_NORMAL]) {
152 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
153 glNormalPointer(GL_FLOAT, 0, 0);
154 glEnableClientState(GL_NORMAL_ARRAY);
155 }
156 if(vbo[MESH_ATTR_TANGENT] && tang_loc >= 0) {
157 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
158 glVertexAttribPointer(tang_loc, 3, GL_FLOAT, GL_FALSE, 0, 0);
159 glEnableVertexAttribArray(tang_loc);
160 }
161 if(vbo[MESH_ATTR_TEXCOORD]) {
162 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TEXCOORD]);
163 glTexCoordPointer(3, GL_FLOAT, 0, 0);
164 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
165 }
167 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
168 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
170 glDisableClientState(GL_VERTEX_ARRAY);
171 glDisableClientState(GL_NORMAL_ARRAY);
172 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
173 if(tang_loc >= 0) {
174 glDisableVertexAttribArray(tang_loc);
175 }
177 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
179 #ifdef DBG_NORMALS
180 glPushAttrib(GL_ENABLE_BIT);
181 glDisable(GL_LIGHTING);
183 int prog;
184 glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
185 glUseProgram(0);
187 Vector3 *varr, *narr, *tarr;
189 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
190 varr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
192 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
193 narr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
195 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
196 tarr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
198 glBegin(GL_LINES);
199 for(unsigned int i=0; i<nverts; i++) {
200 glColor3f(0, 1, 0);
201 glVertex3f(varr->x, varr->y, varr->z);
202 glVertex3f(varr->x + narr->x * NSZ, varr->y + narr->y * NSZ, varr->z + narr->z * NSZ);
204 glColor3f(1, 1, 0);
205 glVertex3f(varr->x, varr->y, varr->z);
206 glVertex3f(varr->x + tarr->x * NSZ, varr->y + tarr->y * NSZ, varr->z + tarr->z * NSZ);
207 varr++;
208 narr++;
209 tarr++;
210 }
211 glEnd();
213 glUnmapBuffer(GL_ARRAY_BUFFER);
214 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
215 glUnmapBuffer(GL_ARRAY_BUFFER);
216 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
217 glUnmapBuffer(GL_ARRAY_BUFFER);
219 glUseProgram(prog);
220 glPopAttrib();
221 #endif
223 glBindBuffer(GL_ARRAY_BUFFER, 0);
225 glPopMatrix();
226 }
229 void Mesh::calc_bsph()
230 {
231 glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
232 Vector3 *varr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
234 bsph_center = Vector3(0, 0, 0);
236 for(unsigned int i=0; i<nverts; i++) {
237 bsph_center += varr[i];
238 }
239 bsph_center /= (float)nverts;
241 // assume all vertices are equidistant from the center
242 bsph_rad = (varr[0] - bsph_center).length();
244 glUnmapBuffer(GL_ARRAY_BUFFER);
245 bsph_valid = true;
246 }
248 const Vector3 &Mesh::get_bsph_center() const
249 {
250 if(!bsph_valid) {
251 ((Mesh*)this)->calc_bsph();
252 }
253 return bsph_center;
254 }
256 float Mesh::get_bsph_radius() const
257 {
258 if(!bsph_valid) {
259 ((Mesh*)this)->calc_bsph();
260 }
261 return bsph_rad;
262 }