rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <float.h>
|
nuclear@0
|
4 #include <assert.h>
|
nuclear@0
|
5 #include "opengl.h"
|
nuclear@0
|
6 #include "mesh.h"
|
nuclear@0
|
7 //#include "xform_node.h"
|
nuclear@0
|
8
|
nuclear@11
|
9 #define USE_OLDGL
|
nuclear@11
|
10
|
nuclear@15
|
11 bool Mesh::use_custom_sdr_attr = true;
|
nuclear@1
|
12 int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 };
|
nuclear@1
|
13 /*
|
nuclear@0
|
14 (int)SDR_ATTR_VERTEX,
|
nuclear@0
|
15 (int)SDR_ATTR_NORMAL,
|
nuclear@0
|
16 (int)SDR_ATTR_TANGENT,
|
nuclear@0
|
17 (int)SDR_ATTR_TEXCOORD,
|
nuclear@0
|
18 (int)SDR_ATTR_COLOR,
|
nuclear@0
|
19 -1, -1};
|
nuclear@1
|
20 */
|
nuclear@0
|
21 unsigned int Mesh::intersect_mode = ISECT_DEFAULT;
|
nuclear@0
|
22 float Mesh::vertex_sel_dist = 0.01;
|
nuclear@0
|
23 float Mesh::vis_vecsize = 1.0;
|
nuclear@0
|
24
|
nuclear@0
|
25 Mesh::Mesh()
|
nuclear@0
|
26 {
|
nuclear@0
|
27 clear();
|
nuclear@0
|
28
|
nuclear@0
|
29 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
|
nuclear@0
|
30
|
nuclear@0
|
31 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
32 vattr[i].vbo = buffer_objects[i];
|
nuclear@0
|
33 }
|
nuclear@0
|
34 ibo = buffer_objects[NUM_MESH_ATTR];
|
nuclear@0
|
35 wire_ibo = 0;
|
nuclear@0
|
36 }
|
nuclear@0
|
37
|
nuclear@0
|
38 Mesh::~Mesh()
|
nuclear@0
|
39 {
|
nuclear@0
|
40 glDeleteBuffers(NUM_MESH_ATTR + 1, buffer_objects);
|
nuclear@0
|
41
|
nuclear@0
|
42 if(wire_ibo) {
|
nuclear@0
|
43 glDeleteBuffers(1, &wire_ibo);
|
nuclear@0
|
44 }
|
nuclear@0
|
45 }
|
nuclear@0
|
46
|
nuclear@0
|
47 Mesh::Mesh(const Mesh &rhs)
|
nuclear@0
|
48 {
|
nuclear@0
|
49 clear();
|
nuclear@0
|
50
|
nuclear@0
|
51 glGenBuffers(NUM_MESH_ATTR + 1, buffer_objects);
|
nuclear@0
|
52
|
nuclear@0
|
53 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
54 vattr[i].vbo = buffer_objects[i];
|
nuclear@0
|
55 }
|
nuclear@0
|
56 ibo = buffer_objects[NUM_MESH_ATTR];
|
nuclear@0
|
57 wire_ibo = 0;
|
nuclear@0
|
58
|
nuclear@0
|
59 clone(rhs);
|
nuclear@0
|
60 }
|
nuclear@0
|
61
|
nuclear@0
|
62 Mesh &Mesh::operator =(const Mesh &rhs)
|
nuclear@0
|
63 {
|
nuclear@0
|
64 if(&rhs != this) {
|
nuclear@0
|
65 clone(rhs);
|
nuclear@0
|
66 }
|
nuclear@0
|
67 return *this;
|
nuclear@0
|
68 }
|
nuclear@0
|
69
|
nuclear@0
|
70 bool Mesh::clone(const Mesh &m)
|
nuclear@0
|
71 {
|
nuclear@0
|
72 clear();
|
nuclear@0
|
73
|
nuclear@0
|
74 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
75 if(m.has_attrib(i)) {
|
nuclear@0
|
76 m.get_attrib_data(i); // force validation of the actual data on the source mesh
|
nuclear@0
|
77
|
nuclear@0
|
78 vattr[i].nelem = m.vattr[i].nelem;
|
nuclear@0
|
79 vattr[i].data = m.vattr[i].data; // copy the actual data
|
nuclear@0
|
80 vattr[i].data_valid = true;
|
nuclear@0
|
81 }
|
nuclear@0
|
82 }
|
nuclear@0
|
83
|
nuclear@0
|
84 if(m.is_indexed()) {
|
nuclear@0
|
85 m.get_index_data(); // again, force validation
|
nuclear@0
|
86
|
nuclear@0
|
87 // copy the index data
|
nuclear@0
|
88 idata = m.idata;
|
nuclear@0
|
89 idata_valid = true;
|
nuclear@0
|
90 }
|
nuclear@0
|
91
|
nuclear@0
|
92 name = m.name;
|
nuclear@0
|
93 nverts = m.nverts;
|
nuclear@0
|
94 nfaces = m.nfaces;
|
nuclear@0
|
95
|
nuclear@0
|
96 //bones = m.bones;
|
nuclear@0
|
97
|
nuclear@0
|
98 memcpy(cur_val, m.cur_val, sizeof cur_val);
|
nuclear@0
|
99
|
nuclear@0
|
100 aabb = m.aabb;
|
nuclear@0
|
101 aabb_valid = m.aabb_valid;
|
nuclear@0
|
102 bsph = m.bsph;
|
nuclear@0
|
103 bsph_valid = m.bsph_valid;
|
nuclear@0
|
104
|
nuclear@0
|
105 hitface = m.hitface;
|
nuclear@0
|
106 hitvert = m.hitvert;
|
nuclear@0
|
107
|
nuclear@0
|
108 intersect_mode = m.intersect_mode;
|
nuclear@0
|
109 vertex_sel_dist = m.vertex_sel_dist;
|
nuclear@0
|
110 vis_vecsize = m.vis_vecsize;
|
nuclear@0
|
111
|
nuclear@0
|
112 return true;
|
nuclear@0
|
113 }
|
nuclear@0
|
114
|
nuclear@0
|
115 void Mesh::set_name(const char *name)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 this->name = name;
|
nuclear@0
|
118 }
|
nuclear@0
|
119
|
nuclear@0
|
120 const char *Mesh::get_name() const
|
nuclear@0
|
121 {
|
nuclear@0
|
122 return name.c_str();
|
nuclear@0
|
123 }
|
nuclear@0
|
124
|
nuclear@0
|
125 bool Mesh::has_attrib(int attr) const
|
nuclear@0
|
126 {
|
nuclear@0
|
127 if(attr < 0 || attr >= NUM_MESH_ATTR) {
|
nuclear@0
|
128 return false;
|
nuclear@0
|
129 }
|
nuclear@0
|
130
|
nuclear@0
|
131 // if neither of these is valid, then nobody has set this attribute
|
nuclear@0
|
132 return vattr[attr].vbo_valid || vattr[attr].data_valid;
|
nuclear@0
|
133 }
|
nuclear@0
|
134
|
nuclear@0
|
135 bool Mesh::is_indexed() const
|
nuclear@0
|
136 {
|
nuclear@0
|
137 return ibo_valid || idata_valid;
|
nuclear@0
|
138 }
|
nuclear@0
|
139
|
nuclear@0
|
140 void Mesh::clear()
|
nuclear@0
|
141 {
|
nuclear@0
|
142 //bones.clear();
|
nuclear@0
|
143
|
nuclear@0
|
144 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
145 vattr[i].nelem = 0;
|
nuclear@0
|
146 vattr[i].vbo_valid = false;
|
nuclear@0
|
147 vattr[i].data_valid = false;
|
nuclear@0
|
148 //vattr[i].sdr_loc = -1;
|
nuclear@0
|
149 vattr[i].data.clear();
|
nuclear@0
|
150 }
|
nuclear@0
|
151 ibo_valid = idata_valid = false;
|
nuclear@0
|
152 idata.clear();
|
nuclear@0
|
153
|
nuclear@0
|
154 wire_ibo_valid = false;
|
nuclear@0
|
155
|
nuclear@0
|
156 nverts = nfaces = 0;
|
nuclear@0
|
157
|
nuclear@0
|
158 bsph_valid = false;
|
nuclear@0
|
159 aabb_valid = false;
|
nuclear@0
|
160 }
|
nuclear@0
|
161
|
nuclear@0
|
162 float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data)
|
nuclear@0
|
163 {
|
nuclear@0
|
164 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
|
nuclear@0
|
165 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
|
nuclear@0
|
166 return 0;
|
nuclear@0
|
167 }
|
nuclear@0
|
168
|
nuclear@0
|
169 if(nverts && num != nverts) {
|
nuclear@0
|
170 fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
|
nuclear@0
|
171 return 0;
|
nuclear@0
|
172 }
|
nuclear@0
|
173 nverts = num;
|
nuclear@0
|
174
|
nuclear@0
|
175 vattr[attrib].data.clear();
|
nuclear@0
|
176 vattr[attrib].nelem = nelem;
|
nuclear@0
|
177 vattr[attrib].data.resize(num * nelem);
|
nuclear@0
|
178
|
nuclear@0
|
179 if(data) {
|
nuclear@0
|
180 memcpy(&vattr[attrib].data[0], data, num * nelem * sizeof *data);
|
nuclear@0
|
181 }
|
nuclear@0
|
182
|
nuclear@0
|
183 vattr[attrib].data_valid = true;
|
nuclear@0
|
184 vattr[attrib].vbo_valid = false;
|
nuclear@0
|
185 return &vattr[attrib].data[0];
|
nuclear@0
|
186 }
|
nuclear@0
|
187
|
nuclear@0
|
188 float *Mesh::get_attrib_data(int attrib)
|
nuclear@0
|
189 {
|
nuclear@0
|
190 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
|
nuclear@0
|
191 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
|
nuclear@0
|
192 return 0;
|
nuclear@0
|
193 }
|
nuclear@0
|
194
|
nuclear@0
|
195 vattr[attrib].vbo_valid = false;
|
nuclear@0
|
196 return (float*)((const Mesh*)this)->get_attrib_data(attrib);
|
nuclear@0
|
197 }
|
nuclear@0
|
198
|
nuclear@0
|
199 const float *Mesh::get_attrib_data(int attrib) const
|
nuclear@0
|
200 {
|
nuclear@0
|
201 if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
|
nuclear@0
|
202 fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
|
nuclear@0
|
203 return 0;
|
nuclear@0
|
204 }
|
nuclear@0
|
205
|
nuclear@0
|
206 if(!vattr[attrib].data_valid) {
|
nuclear@0
|
207 #if GL_ES_VERSION_2_0
|
nuclear@0
|
208 fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
|
nuclear@0
|
209 return 0;
|
nuclear@0
|
210 #else
|
nuclear@0
|
211 if(!vattr[attrib].vbo_valid) {
|
nuclear@0
|
212 fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
|
nuclear@0
|
213 return 0;
|
nuclear@0
|
214 }
|
nuclear@0
|
215
|
nuclear@0
|
216 // local data copy is unavailable, grab the data from the vbo
|
nuclear@0
|
217 Mesh *m = (Mesh*)this;
|
nuclear@0
|
218 m->vattr[attrib].data.resize(nverts * vattr[attrib].nelem);
|
nuclear@0
|
219
|
nuclear@0
|
220 glBindBuffer(GL_ARRAY_BUFFER, vattr[attrib].vbo);
|
nuclear@0
|
221 void *data = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
|
nuclear@0
|
222 memcpy(&m->vattr[attrib].data[0], data, nverts * vattr[attrib].nelem * sizeof(float));
|
nuclear@0
|
223 glUnmapBuffer(GL_ARRAY_BUFFER);
|
nuclear@0
|
224
|
nuclear@0
|
225 vattr[attrib].data_valid = true;
|
nuclear@0
|
226 #endif
|
nuclear@0
|
227 }
|
nuclear@0
|
228
|
nuclear@0
|
229 return &vattr[attrib].data[0];
|
nuclear@0
|
230 }
|
nuclear@0
|
231
|
nuclear@0
|
232 void Mesh::set_attrib(int attrib, int idx, const Vector4 &v)
|
nuclear@0
|
233 {
|
nuclear@0
|
234 float *data = get_attrib_data(attrib);
|
nuclear@0
|
235 if(data) {
|
nuclear@0
|
236 data += idx * vattr[attrib].nelem;
|
nuclear@0
|
237 for(int i=0; i<vattr[attrib].nelem; i++) {
|
nuclear@0
|
238 data[i] = v[i];
|
nuclear@0
|
239 }
|
nuclear@0
|
240 }
|
nuclear@0
|
241 }
|
nuclear@0
|
242
|
nuclear@0
|
243 Vector4 Mesh::get_attrib(int attrib, int idx) const
|
nuclear@0
|
244 {
|
nuclear@0
|
245 Vector4 v(0.0, 0.0, 0.0, 1.0);
|
nuclear@0
|
246 const float *data = get_attrib_data(attrib);
|
nuclear@0
|
247 if(data) {
|
nuclear@0
|
248 data += idx * vattr[attrib].nelem;
|
nuclear@0
|
249 for(int i=0; i<vattr[attrib].nelem; i++) {
|
nuclear@0
|
250 v[i] = data[i];
|
nuclear@0
|
251 }
|
nuclear@0
|
252 }
|
nuclear@0
|
253 return v;
|
nuclear@0
|
254 }
|
nuclear@0
|
255
|
nuclear@0
|
256 int Mesh::get_attrib_count(int attrib) const
|
nuclear@0
|
257 {
|
nuclear@0
|
258 return has_attrib(attrib) ? nverts : 0;
|
nuclear@0
|
259 }
|
nuclear@0
|
260
|
nuclear@0
|
261
|
nuclear@0
|
262 unsigned int *Mesh::set_index_data(int num, const unsigned int *indices)
|
nuclear@0
|
263 {
|
nuclear@0
|
264 int nidx = nfaces * 3;
|
nuclear@0
|
265 if(nidx && num != nidx) {
|
nuclear@0
|
266 fprintf(stderr, "%s: index count missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
|
nuclear@0
|
267 return 0;
|
nuclear@0
|
268 }
|
nuclear@0
|
269 nfaces = num / 3;
|
nuclear@0
|
270
|
nuclear@0
|
271 idata.clear();
|
nuclear@0
|
272 idata.resize(num);
|
nuclear@0
|
273
|
nuclear@0
|
274 if(indices) {
|
nuclear@0
|
275 memcpy(&idata[0], indices, num * sizeof *indices);
|
nuclear@0
|
276 }
|
nuclear@0
|
277
|
nuclear@0
|
278 idata_valid = true;
|
nuclear@0
|
279 ibo_valid = false;
|
nuclear@0
|
280
|
nuclear@0
|
281 return &idata[0];
|
nuclear@0
|
282 }
|
nuclear@0
|
283
|
nuclear@0
|
284 unsigned int *Mesh::get_index_data()
|
nuclear@0
|
285 {
|
nuclear@0
|
286 ibo_valid = false;
|
nuclear@0
|
287 return (unsigned int*)((const Mesh*)this)->get_index_data();
|
nuclear@0
|
288 }
|
nuclear@0
|
289
|
nuclear@0
|
290 const unsigned int *Mesh::get_index_data() const
|
nuclear@0
|
291 {
|
nuclear@0
|
292 if(!idata_valid) {
|
nuclear@0
|
293 #if GL_ES_VERSION_2_0
|
nuclear@0
|
294 fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
|
nuclear@0
|
295 return 0;
|
nuclear@0
|
296 #else
|
nuclear@0
|
297 if(!ibo_valid) {
|
nuclear@0
|
298 fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__);
|
nuclear@0
|
299 return 0;
|
nuclear@0
|
300 }
|
nuclear@0
|
301
|
nuclear@0
|
302 // local data copy is unavailable, gram the data from the ibo
|
nuclear@0
|
303 Mesh *m = (Mesh*)this;
|
nuclear@0
|
304 int nidx = nfaces * 3;
|
nuclear@0
|
305 m->idata.resize(nidx);
|
nuclear@0
|
306
|
nuclear@0
|
307 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
nuclear@0
|
308 void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
|
nuclear@0
|
309 memcpy(&m->idata[0], data, nidx * sizeof(unsigned int));
|
nuclear@0
|
310 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
|
nuclear@0
|
311
|
nuclear@0
|
312 idata_valid = true;
|
nuclear@0
|
313 #endif
|
nuclear@0
|
314 }
|
nuclear@0
|
315
|
nuclear@0
|
316 return &idata[0];
|
nuclear@0
|
317 }
|
nuclear@0
|
318
|
nuclear@0
|
319 int Mesh::get_index_count() const
|
nuclear@0
|
320 {
|
nuclear@0
|
321 return nfaces * 3;
|
nuclear@0
|
322 }
|
nuclear@0
|
323
|
nuclear@0
|
324 void Mesh::append(const Mesh &mesh)
|
nuclear@0
|
325 {
|
nuclear@0
|
326 unsigned int idxoffs = nverts;
|
nuclear@0
|
327
|
nuclear@7
|
328 if(!nverts) {
|
nuclear@7
|
329 clone(mesh);
|
nuclear@7
|
330 return;
|
nuclear@7
|
331 }
|
nuclear@7
|
332
|
nuclear@0
|
333 nverts += mesh.nverts;
|
nuclear@0
|
334 nfaces += mesh.nfaces;
|
nuclear@0
|
335
|
nuclear@0
|
336 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
337 if(has_attrib(i) && mesh.has_attrib(i)) {
|
nuclear@0
|
338 // force validating the data arrays
|
nuclear@0
|
339 get_attrib_data(i);
|
nuclear@0
|
340 mesh.get_attrib_data(i);
|
nuclear@0
|
341
|
nuclear@0
|
342 // append the mesh data
|
nuclear@0
|
343 vattr[i].data.insert(vattr[i].data.end(), mesh.vattr[i].data.begin(), mesh.vattr[i].data.end());
|
nuclear@0
|
344 }
|
nuclear@0
|
345 }
|
nuclear@0
|
346
|
nuclear@0
|
347 if(ibo_valid || idata_valid) {
|
nuclear@0
|
348 // make index arrays valid
|
nuclear@0
|
349 get_index_data();
|
nuclear@0
|
350 mesh.get_index_data();
|
nuclear@0
|
351
|
nuclear@0
|
352 size_t orig_sz = idata.size();
|
nuclear@0
|
353
|
nuclear@0
|
354 idata.insert(idata.end(), mesh.idata.begin(), mesh.idata.end());
|
nuclear@0
|
355
|
nuclear@0
|
356 // fixup all the new indices
|
nuclear@0
|
357 for(size_t i=orig_sz; i<idata.size(); i++) {
|
nuclear@0
|
358 idata[i] += idxoffs;
|
nuclear@0
|
359 }
|
nuclear@0
|
360 }
|
nuclear@0
|
361
|
nuclear@0
|
362 // fuck everything
|
nuclear@0
|
363 wire_ibo_valid = false;
|
nuclear@0
|
364 aabb_valid = false;
|
nuclear@0
|
365 bsph_valid = false;
|
nuclear@0
|
366 }
|
nuclear@0
|
367
|
nuclear@0
|
368 // assemble a complete vertex by adding all the useful attributes
|
nuclear@0
|
369 void Mesh::vertex(float x, float y, float z)
|
nuclear@0
|
370 {
|
nuclear@0
|
371 cur_val[MESH_ATTR_VERTEX] = Vector4(x, y, z, 1.0f);
|
nuclear@0
|
372 vattr[MESH_ATTR_VERTEX].data_valid = true;
|
nuclear@0
|
373 vattr[MESH_ATTR_VERTEX].nelem = 3;
|
nuclear@0
|
374
|
nuclear@0
|
375 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
376 if(vattr[i].data_valid) {
|
nuclear@0
|
377 for(int j=0; j<vattr[MESH_ATTR_VERTEX].nelem; j++) {
|
nuclear@0
|
378 vattr[i].data.push_back(cur_val[i][j]);
|
nuclear@0
|
379 }
|
nuclear@0
|
380 }
|
nuclear@0
|
381 vattr[i].vbo_valid = false;
|
nuclear@0
|
382 }
|
nuclear@0
|
383
|
nuclear@0
|
384 if(idata_valid) {
|
nuclear@0
|
385 idata.clear();
|
nuclear@0
|
386 }
|
nuclear@0
|
387 ibo_valid = idata_valid = false;
|
nuclear@0
|
388 }
|
nuclear@0
|
389
|
nuclear@0
|
390 void Mesh::normal(float nx, float ny, float nz)
|
nuclear@0
|
391 {
|
nuclear@0
|
392 cur_val[MESH_ATTR_NORMAL] = Vector4(nx, ny, nz, 1.0f);
|
nuclear@0
|
393 vattr[MESH_ATTR_NORMAL].data_valid = true;
|
nuclear@0
|
394 vattr[MESH_ATTR_NORMAL].nelem = 3;
|
nuclear@0
|
395 }
|
nuclear@0
|
396
|
nuclear@0
|
397 void Mesh::tangent(float tx, float ty, float tz)
|
nuclear@0
|
398 {
|
nuclear@0
|
399 cur_val[MESH_ATTR_TANGENT] = Vector4(tx, ty, tz, 1.0f);
|
nuclear@0
|
400 vattr[MESH_ATTR_TANGENT].data_valid = true;
|
nuclear@0
|
401 vattr[MESH_ATTR_TANGENT].nelem = 3;
|
nuclear@0
|
402 }
|
nuclear@0
|
403
|
nuclear@0
|
404 void Mesh::texcoord(float u, float v, float w)
|
nuclear@0
|
405 {
|
nuclear@0
|
406 cur_val[MESH_ATTR_TEXCOORD] = Vector4(u, v, w, 1.0f);
|
nuclear@0
|
407 vattr[MESH_ATTR_TEXCOORD].data_valid = true;
|
nuclear@0
|
408 vattr[MESH_ATTR_TEXCOORD].nelem = 3;
|
nuclear@0
|
409 }
|
nuclear@0
|
410
|
nuclear@0
|
411 void Mesh::boneweights(float w1, float w2, float w3, float w4)
|
nuclear@0
|
412 {
|
nuclear@0
|
413 cur_val[MESH_ATTR_BONEWEIGHTS] = Vector4(w1, w2, w3, w4);
|
nuclear@0
|
414 vattr[MESH_ATTR_BONEWEIGHTS].data_valid = true;
|
nuclear@0
|
415 vattr[MESH_ATTR_BONEWEIGHTS].nelem = 4;
|
nuclear@0
|
416 }
|
nuclear@0
|
417
|
nuclear@0
|
418 void Mesh::boneidx(int idx1, int idx2, int idx3, int idx4)
|
nuclear@0
|
419 {
|
nuclear@0
|
420 cur_val[MESH_ATTR_BONEIDX] = Vector4(idx1, idx2, idx3, idx4);
|
nuclear@0
|
421 vattr[MESH_ATTR_BONEIDX].data_valid = true;
|
nuclear@0
|
422 vattr[MESH_ATTR_BONEIDX].nelem = 4;
|
nuclear@0
|
423 }
|
nuclear@0
|
424
|
nuclear@0
|
425 int Mesh::get_poly_count() const
|
nuclear@0
|
426 {
|
nuclear@0
|
427 if(nfaces) {
|
nuclear@0
|
428 return nfaces;
|
nuclear@0
|
429 }
|
nuclear@0
|
430 if(nverts) {
|
nuclear@0
|
431 return nverts / 3;
|
nuclear@0
|
432 }
|
nuclear@0
|
433 return 0;
|
nuclear@0
|
434 }
|
nuclear@0
|
435
|
nuclear@0
|
436 /// static function
|
nuclear@0
|
437 void Mesh::set_attrib_location(int attr, int loc)
|
nuclear@0
|
438 {
|
nuclear@0
|
439 if(attr < 0 || attr >= NUM_MESH_ATTR) {
|
nuclear@0
|
440 return;
|
nuclear@0
|
441 }
|
nuclear@0
|
442 Mesh::global_sdr_loc[attr] = loc;
|
nuclear@0
|
443 }
|
nuclear@0
|
444
|
nuclear@0
|
445 /// static function
|
nuclear@0
|
446 int Mesh::get_attrib_location(int attr)
|
nuclear@0
|
447 {
|
nuclear@0
|
448 if(attr < 0 || attr >= NUM_MESH_ATTR) {
|
nuclear@0
|
449 return -1;
|
nuclear@0
|
450 }
|
nuclear@0
|
451 return Mesh::global_sdr_loc[attr];
|
nuclear@0
|
452 }
|
nuclear@0
|
453
|
nuclear@0
|
454 /// static function
|
nuclear@0
|
455 void Mesh::clear_attrib_locations()
|
nuclear@0
|
456 {
|
nuclear@0
|
457 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
458 Mesh::global_sdr_loc[i] = -1;
|
nuclear@0
|
459 }
|
nuclear@0
|
460 }
|
nuclear@0
|
461
|
nuclear@0
|
462 /// static function
|
nuclear@0
|
463 void Mesh::set_vis_vecsize(float sz)
|
nuclear@0
|
464 {
|
nuclear@0
|
465 Mesh::vis_vecsize = sz;
|
nuclear@0
|
466 }
|
nuclear@0
|
467
|
nuclear@0
|
468 float Mesh::get_vis_vecsize()
|
nuclear@0
|
469 {
|
nuclear@0
|
470 return Mesh::vis_vecsize;
|
nuclear@0
|
471 }
|
nuclear@0
|
472
|
nuclear@0
|
473 void Mesh::apply_xform(const Matrix4x4 &xform)
|
nuclear@0
|
474 {
|
nuclear@0
|
475 Matrix4x4 dir_xform = xform;
|
nuclear@0
|
476 dir_xform[0][3] = dir_xform[1][3] = dir_xform[2][3] = 0.0f;
|
nuclear@0
|
477 dir_xform[3][0] = dir_xform[3][1] = dir_xform[3][2] = 0.0f;
|
nuclear@0
|
478 dir_xform[3][3] = 1.0f;
|
nuclear@0
|
479
|
nuclear@0
|
480 apply_xform(xform, dir_xform);
|
nuclear@0
|
481 }
|
nuclear@0
|
482
|
nuclear@0
|
483 void Mesh::apply_xform(const Matrix4x4 &xform, const Matrix4x4 &dir_xform)
|
nuclear@0
|
484 {
|
nuclear@0
|
485 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@0
|
486 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
|
nuclear@0
|
487 set_attrib(MESH_ATTR_VERTEX, i, v.transformed(xform));
|
nuclear@0
|
488
|
nuclear@0
|
489 if(has_attrib(MESH_ATTR_NORMAL)) {
|
nuclear@0
|
490 Vector3 n = get_attrib(MESH_ATTR_NORMAL, i);
|
nuclear@0
|
491 set_attrib(MESH_ATTR_NORMAL, i, n.transformed(dir_xform));
|
nuclear@0
|
492 }
|
nuclear@0
|
493 if(has_attrib(MESH_ATTR_TANGENT)) {
|
nuclear@0
|
494 Vector3 t = get_attrib(MESH_ATTR_TANGENT, i);
|
nuclear@0
|
495 set_attrib(MESH_ATTR_TANGENT, i, t.transformed(dir_xform));
|
nuclear@0
|
496 }
|
nuclear@0
|
497 }
|
nuclear@0
|
498 }
|
nuclear@0
|
499
|
nuclear@0
|
500 void Mesh::flip()
|
nuclear@0
|
501 {
|
nuclear@0
|
502 flip_faces();
|
nuclear@0
|
503 flip_normals();
|
nuclear@0
|
504 }
|
nuclear@0
|
505
|
nuclear@0
|
506 void Mesh::flip_faces()
|
nuclear@0
|
507 {
|
nuclear@0
|
508 if(is_indexed()) {
|
nuclear@0
|
509 unsigned int *indices = get_index_data();
|
nuclear@0
|
510 if(!indices) return;
|
nuclear@0
|
511
|
nuclear@0
|
512 int idxnum = get_index_count();
|
nuclear@0
|
513 for(int i=0; i<idxnum; i+=3) {
|
nuclear@0
|
514 unsigned int tmp = indices[i + 2];
|
nuclear@0
|
515 indices[i + 2] = indices[i + 1];
|
nuclear@0
|
516 indices[i + 1] = tmp;
|
nuclear@0
|
517 }
|
nuclear@0
|
518
|
nuclear@0
|
519 } else {
|
nuclear@0
|
520 Vector3 *verts = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
|
nuclear@0
|
521 if(!verts) return;
|
nuclear@0
|
522
|
nuclear@0
|
523 int vnum = get_attrib_count(MESH_ATTR_VERTEX);
|
nuclear@0
|
524 for(int i=0; i<vnum; i+=3) {
|
nuclear@0
|
525 Vector3 tmp = verts[i + 2];
|
nuclear@0
|
526 verts[i + 2] = verts[i + 1];
|
nuclear@0
|
527 verts[i + 1] = tmp;
|
nuclear@0
|
528 }
|
nuclear@0
|
529 }
|
nuclear@0
|
530 }
|
nuclear@0
|
531
|
nuclear@0
|
532 void Mesh::flip_normals()
|
nuclear@0
|
533 {
|
nuclear@0
|
534 Vector3 *normals = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
|
nuclear@0
|
535 if(!normals) return;
|
nuclear@0
|
536
|
nuclear@0
|
537 int num = get_attrib_count(MESH_ATTR_NORMAL);
|
nuclear@0
|
538 for(int i=0; i<num; i++) {
|
nuclear@0
|
539 normals[i] = -normals[i];
|
nuclear@0
|
540 }
|
nuclear@0
|
541 }
|
nuclear@0
|
542
|
nuclear@0
|
543 /*
|
nuclear@0
|
544 int Mesh::add_bone(XFormNode *bone)
|
nuclear@0
|
545 {
|
nuclear@0
|
546 int idx = bones.size();
|
nuclear@0
|
547 bones.push_back(bone);
|
nuclear@0
|
548 return idx;
|
nuclear@0
|
549 }
|
nuclear@0
|
550
|
nuclear@0
|
551 const XFormNode *Mesh::get_bone(int idx) const
|
nuclear@0
|
552 {
|
nuclear@0
|
553 if(idx < 0 || idx >= (int)bones.size()) {
|
nuclear@0
|
554 return 0;
|
nuclear@0
|
555 }
|
nuclear@0
|
556 return bones[idx];
|
nuclear@0
|
557 }
|
nuclear@0
|
558
|
nuclear@0
|
559 int Mesh::get_bones_count() const
|
nuclear@0
|
560 {
|
nuclear@0
|
561 return (int)bones.size();
|
nuclear@0
|
562 }
|
nuclear@0
|
563 */
|
nuclear@0
|
564
|
nuclear@0
|
565 void Mesh::draw() const
|
nuclear@0
|
566 {
|
nuclear@16
|
567 int cur_sdr = 0;
|
nuclear@16
|
568 if(glcaps.shaders) {
|
nuclear@16
|
569 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
|
nuclear@16
|
570 }
|
nuclear@0
|
571
|
nuclear@0
|
572 ((Mesh*)this)->update_buffers();
|
nuclear@0
|
573
|
nuclear@0
|
574 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
|
nuclear@0
|
575 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
|
nuclear@0
|
576 return;
|
nuclear@0
|
577 }
|
nuclear@0
|
578
|
nuclear@15
|
579 if(cur_sdr && use_custom_sdr_attr) {
|
nuclear@0
|
580 // rendering with shaders
|
nuclear@0
|
581 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
|
nuclear@0
|
582 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
|
nuclear@0
|
583 return;
|
nuclear@0
|
584 }
|
nuclear@0
|
585
|
nuclear@0
|
586 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
587 int loc = global_sdr_loc[i];
|
nuclear@0
|
588 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
589 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
|
nuclear@0
|
590 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
|
nuclear@0
|
591 glEnableVertexAttribArray(loc);
|
nuclear@0
|
592 }
|
nuclear@0
|
593 }
|
nuclear@0
|
594 } else {
|
nuclear@0
|
595 #ifndef GL_ES_VERSION_2_0
|
nuclear@0
|
596 // rendering with fixed-function (not available in GLES2)
|
nuclear@0
|
597 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
|
nuclear@0
|
598 glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
|
nuclear@0
|
599 glEnableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
600
|
nuclear@0
|
601 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
|
nuclear@0
|
602 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
|
nuclear@0
|
603 glNormalPointer(GL_FLOAT, 0, 0);
|
nuclear@0
|
604 glEnableClientState(GL_NORMAL_ARRAY);
|
nuclear@0
|
605 }
|
nuclear@0
|
606 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
|
nuclear@0
|
607 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
|
nuclear@0
|
608 glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
|
nuclear@0
|
609 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
610 }
|
nuclear@0
|
611 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
|
nuclear@0
|
612 glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
|
nuclear@0
|
613 glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
|
nuclear@0
|
614 glEnableClientState(GL_COLOR_ARRAY);
|
nuclear@0
|
615 }
|
nuclear@0
|
616 #endif
|
nuclear@0
|
617 }
|
nuclear@0
|
618 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@0
|
619
|
nuclear@0
|
620 if(ibo_valid) {
|
nuclear@0
|
621 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
nuclear@0
|
622 glDrawElements(GL_TRIANGLES, nfaces * 3, GL_UNSIGNED_INT, 0);
|
nuclear@0
|
623 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
nuclear@0
|
624 } else {
|
nuclear@0
|
625 glDrawArrays(GL_TRIANGLES, 0, nverts);
|
nuclear@0
|
626 }
|
nuclear@0
|
627
|
nuclear@15
|
628 if(cur_sdr && use_custom_sdr_attr) {
|
nuclear@0
|
629 // rendered with shaders
|
nuclear@0
|
630 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
631 int loc = global_sdr_loc[i];
|
nuclear@0
|
632 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
633 glDisableVertexAttribArray(loc);
|
nuclear@0
|
634 }
|
nuclear@0
|
635 }
|
nuclear@0
|
636 } else {
|
nuclear@0
|
637 #ifndef GL_ES_VERSION_2_0
|
nuclear@0
|
638 // rendered with fixed-function
|
nuclear@0
|
639 glDisableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
640 if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
|
nuclear@0
|
641 glDisableClientState(GL_NORMAL_ARRAY);
|
nuclear@0
|
642 }
|
nuclear@0
|
643 if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
|
nuclear@0
|
644 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
645 }
|
nuclear@0
|
646 if(vattr[MESH_ATTR_COLOR].vbo_valid) {
|
nuclear@0
|
647 glDisableClientState(GL_COLOR_ARRAY);
|
nuclear@0
|
648 }
|
nuclear@0
|
649 #endif
|
nuclear@0
|
650 }
|
nuclear@0
|
651 }
|
nuclear@0
|
652
|
nuclear@0
|
653 void Mesh::draw_wire() const
|
nuclear@0
|
654 {
|
nuclear@0
|
655 ((Mesh*)this)->update_wire_ibo();
|
nuclear@0
|
656
|
nuclear@0
|
657 if(!vattr[MESH_ATTR_VERTEX].vbo_valid || !wire_ibo_valid) {
|
nuclear@0
|
658 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
|
nuclear@0
|
659 return;
|
nuclear@0
|
660 }
|
nuclear@0
|
661 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
|
nuclear@0
|
662 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
|
nuclear@0
|
663 return;
|
nuclear@0
|
664 }
|
nuclear@0
|
665
|
nuclear@0
|
666 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
667 int loc = global_sdr_loc[i];
|
nuclear@0
|
668 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
669 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
|
nuclear@0
|
670 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
|
nuclear@0
|
671 glEnableVertexAttribArray(loc);
|
nuclear@0
|
672 }
|
nuclear@0
|
673 }
|
nuclear@0
|
674 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@0
|
675
|
nuclear@0
|
676 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
|
nuclear@0
|
677 glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
|
nuclear@0
|
678 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
nuclear@0
|
679
|
nuclear@0
|
680 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
681 int loc = global_sdr_loc[i];
|
nuclear@0
|
682 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
683 glDisableVertexAttribArray(loc);
|
nuclear@0
|
684 }
|
nuclear@0
|
685 }
|
nuclear@0
|
686 }
|
nuclear@0
|
687
|
nuclear@0
|
688 void Mesh::draw_vertices() const
|
nuclear@0
|
689 {
|
nuclear@0
|
690 ((Mesh*)this)->update_buffers();
|
nuclear@0
|
691
|
nuclear@0
|
692 if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
|
nuclear@0
|
693 fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
|
nuclear@0
|
694 return;
|
nuclear@0
|
695 }
|
nuclear@0
|
696 if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
|
nuclear@0
|
697 fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
|
nuclear@0
|
698 return;
|
nuclear@0
|
699 }
|
nuclear@0
|
700
|
nuclear@0
|
701 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
702 int loc = global_sdr_loc[i];
|
nuclear@0
|
703 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
704 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
|
nuclear@0
|
705 glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
|
nuclear@0
|
706 glEnableVertexAttribArray(loc);
|
nuclear@0
|
707 }
|
nuclear@0
|
708 }
|
nuclear@0
|
709 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@0
|
710
|
nuclear@0
|
711 glDrawArrays(GL_POINTS, 0, nverts);
|
nuclear@0
|
712
|
nuclear@0
|
713 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
714 int loc = global_sdr_loc[i];
|
nuclear@0
|
715 if(loc >= 0 && vattr[i].vbo_valid) {
|
nuclear@0
|
716 glDisableVertexAttribArray(loc);
|
nuclear@0
|
717 }
|
nuclear@0
|
718 }
|
nuclear@0
|
719 }
|
nuclear@0
|
720
|
nuclear@0
|
721 void Mesh::draw_normals() const
|
nuclear@0
|
722 {
|
nuclear@0
|
723 #ifdef USE_OLDGL
|
nuclear@16
|
724 int cur_sdr = 0;
|
nuclear@16
|
725 if(glcaps.shaders) {
|
nuclear@16
|
726 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
|
nuclear@16
|
727 }
|
nuclear@11
|
728
|
nuclear@0
|
729 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
|
nuclear@0
|
730 Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
|
nuclear@0
|
731 if(!varr || !norm) {
|
nuclear@0
|
732 return;
|
nuclear@0
|
733 }
|
nuclear@0
|
734
|
nuclear@0
|
735 glBegin(GL_LINES);
|
nuclear@15
|
736 if(cur_sdr && use_custom_sdr_attr) {
|
nuclear@0
|
737 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
|
nuclear@0
|
738 if(vert_loc < 0) {
|
nuclear@0
|
739 glEnd();
|
nuclear@0
|
740 return;
|
nuclear@0
|
741 }
|
nuclear@0
|
742
|
nuclear@0
|
743 for(size_t i=0; i<nverts; i++) {
|
nuclear@0
|
744 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
|
nuclear@0
|
745 Vector3 end = varr[i] + norm[i] * vis_vecsize;
|
nuclear@0
|
746 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
|
nuclear@0
|
747 }
|
nuclear@0
|
748 } else {
|
nuclear@0
|
749 for(size_t i=0; i<nverts; i++) {
|
nuclear@0
|
750 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
|
nuclear@0
|
751 Vector3 end = varr[i] + norm[i] * vis_vecsize;
|
nuclear@0
|
752 glVertex3f(end.x, end.y, end.z);
|
nuclear@0
|
753 }
|
nuclear@0
|
754 }
|
nuclear@0
|
755 glEnd();
|
nuclear@0
|
756 #endif // USE_OLDGL
|
nuclear@0
|
757 }
|
nuclear@0
|
758
|
nuclear@0
|
759 void Mesh::draw_tangents() const
|
nuclear@0
|
760 {
|
nuclear@0
|
761 #ifdef USE_OLDGL
|
nuclear@16
|
762 int cur_sdr = 0;
|
nuclear@16
|
763 if(glcaps.shaders) {
|
nuclear@16
|
764 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
|
nuclear@16
|
765 }
|
nuclear@11
|
766
|
nuclear@0
|
767 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
|
nuclear@0
|
768 Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT);
|
nuclear@0
|
769 if(!varr || !tang) {
|
nuclear@0
|
770 return;
|
nuclear@0
|
771 }
|
nuclear@0
|
772
|
nuclear@0
|
773 glBegin(GL_LINES);
|
nuclear@15
|
774 if(cur_sdr && use_custom_sdr_attr) {
|
nuclear@0
|
775 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX];
|
nuclear@0
|
776 if(vert_loc < 0) {
|
nuclear@0
|
777 glEnd();
|
nuclear@0
|
778 return;
|
nuclear@0
|
779 }
|
nuclear@0
|
780
|
nuclear@0
|
781 for(size_t i=0; i<nverts; i++) {
|
nuclear@0
|
782 glVertexAttrib3f(vert_loc, varr[i].x, varr[i].y, varr[i].z);
|
nuclear@0
|
783 Vector3 end = varr[i] + tang[i] * vis_vecsize;
|
nuclear@0
|
784 glVertexAttrib3f(vert_loc, end.x, end.y, end.z);
|
nuclear@0
|
785 }
|
nuclear@0
|
786 } else {
|
nuclear@0
|
787 for(size_t i=0; i<nverts; i++) {
|
nuclear@0
|
788 glVertex3f(varr[i].x, varr[i].y, varr[i].z);
|
nuclear@0
|
789 Vector3 end = varr[i] + tang[i] * vis_vecsize;
|
nuclear@0
|
790 glVertex3f(end.x, end.y, end.z);
|
nuclear@0
|
791 }
|
nuclear@0
|
792 }
|
nuclear@0
|
793 glEnd();
|
nuclear@0
|
794 #endif // USE_OLDGL
|
nuclear@0
|
795 }
|
nuclear@0
|
796
|
nuclear@0
|
797 void Mesh::get_aabbox(Vector3 *vmin, Vector3 *vmax) const
|
nuclear@0
|
798 {
|
nuclear@0
|
799 if(!aabb_valid) {
|
nuclear@0
|
800 ((Mesh*)this)->calc_aabb();
|
nuclear@0
|
801 }
|
nuclear@0
|
802 *vmin = aabb.min;
|
nuclear@0
|
803 *vmax = aabb.max;
|
nuclear@0
|
804 }
|
nuclear@0
|
805
|
nuclear@0
|
806 const AABox &Mesh::get_aabbox() const
|
nuclear@0
|
807 {
|
nuclear@0
|
808 if(!aabb_valid) {
|
nuclear@0
|
809 ((Mesh*)this)->calc_aabb();
|
nuclear@0
|
810 }
|
nuclear@0
|
811 return aabb;
|
nuclear@0
|
812 }
|
nuclear@0
|
813
|
nuclear@0
|
814 float Mesh::get_bsphere(Vector3 *center, float *rad) const
|
nuclear@0
|
815 {
|
nuclear@0
|
816 if(!bsph_valid) {
|
nuclear@0
|
817 ((Mesh*)this)->calc_bsph();
|
nuclear@0
|
818 }
|
nuclear@0
|
819 *center = bsph.center;
|
nuclear@0
|
820 *rad = bsph.radius;
|
nuclear@0
|
821 return bsph.radius;
|
nuclear@0
|
822 }
|
nuclear@0
|
823
|
nuclear@0
|
824 const Sphere &Mesh::get_bsphere() const
|
nuclear@0
|
825 {
|
nuclear@0
|
826 if(!bsph_valid) {
|
nuclear@0
|
827 ((Mesh*)this)->calc_bsph();
|
nuclear@0
|
828 }
|
nuclear@0
|
829 return bsph;
|
nuclear@0
|
830 }
|
nuclear@0
|
831
|
nuclear@0
|
832 /// static function
|
nuclear@0
|
833 void Mesh::set_intersect_mode(unsigned int mode)
|
nuclear@0
|
834 {
|
nuclear@0
|
835 Mesh::intersect_mode = mode;
|
nuclear@0
|
836 }
|
nuclear@0
|
837
|
nuclear@0
|
838 /// static function
|
nuclear@0
|
839 unsigned int Mesh::get_intersect_mode()
|
nuclear@0
|
840 {
|
nuclear@0
|
841 return Mesh::intersect_mode;
|
nuclear@0
|
842 }
|
nuclear@0
|
843
|
nuclear@0
|
844 /// static function
|
nuclear@0
|
845 void Mesh::set_vertex_select_distance(float dist)
|
nuclear@0
|
846 {
|
nuclear@0
|
847 Mesh::vertex_sel_dist = dist;
|
nuclear@0
|
848 }
|
nuclear@0
|
849
|
nuclear@0
|
850 /// static function
|
nuclear@0
|
851 float Mesh::get_vertex_select_distance()
|
nuclear@0
|
852 {
|
nuclear@0
|
853 return Mesh::vertex_sel_dist;
|
nuclear@0
|
854 }
|
nuclear@0
|
855
|
nuclear@0
|
856 bool Mesh::intersect(const Ray &ray, HitPoint *hit) const
|
nuclear@0
|
857 {
|
nuclear@0
|
858 assert((Mesh::intersect_mode & (ISECT_VERTICES | ISECT_FACE)) != (ISECT_VERTICES | ISECT_FACE));
|
nuclear@0
|
859
|
nuclear@0
|
860 const Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
|
nuclear@0
|
861 const Vector3 *narr = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
|
nuclear@0
|
862 if(!varr) {
|
nuclear@0
|
863 return false;
|
nuclear@0
|
864 }
|
nuclear@0
|
865 const unsigned int *idxarr = get_index_data();
|
nuclear@0
|
866
|
nuclear@0
|
867 // first test with the bounding box
|
nuclear@0
|
868 AABox box;
|
nuclear@0
|
869 get_aabbox(&box.min, &box.max);
|
nuclear@0
|
870 if(!box.intersect(ray)) {
|
nuclear@0
|
871 return false;
|
nuclear@0
|
872 }
|
nuclear@0
|
873
|
nuclear@0
|
874 HitPoint nearest_hit;
|
nuclear@0
|
875 nearest_hit.dist = FLT_MAX;
|
nuclear@0
|
876 nearest_hit.obj = 0;
|
nuclear@0
|
877
|
nuclear@0
|
878 if(Mesh::intersect_mode & ISECT_VERTICES) {
|
nuclear@0
|
879 // we asked for "intersections" with the vertices of the mesh
|
nuclear@0
|
880 long nearest_vidx = -1;
|
nuclear@0
|
881 float thres_sq = Mesh::vertex_sel_dist * Mesh::vertex_sel_dist;
|
nuclear@0
|
882
|
nuclear@0
|
883 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@0
|
884
|
nuclear@0
|
885 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(narr[i], ray.dir) > 0) {
|
nuclear@0
|
886 continue;
|
nuclear@0
|
887 }
|
nuclear@0
|
888
|
nuclear@0
|
889 // project the vertex onto the ray line
|
nuclear@0
|
890 float t = dot_product(varr[i] - ray.origin, ray.dir);
|
nuclear@0
|
891 Vector3 vproj = ray.origin + ray.dir * t;
|
nuclear@0
|
892
|
nuclear@0
|
893 float dist_sq = (vproj - varr[i]).length_sq();
|
nuclear@0
|
894 if(dist_sq < thres_sq) {
|
nuclear@0
|
895 if(!hit) {
|
nuclear@0
|
896 return true;
|
nuclear@0
|
897 }
|
nuclear@0
|
898 if(t < nearest_hit.dist) {
|
nuclear@0
|
899 nearest_hit.dist = t;
|
nuclear@0
|
900 nearest_vidx = i;
|
nuclear@0
|
901 }
|
nuclear@0
|
902 }
|
nuclear@0
|
903 }
|
nuclear@0
|
904
|
nuclear@0
|
905 if(nearest_vidx != -1) {
|
nuclear@0
|
906 hitvert = varr[nearest_vidx];
|
nuclear@0
|
907 nearest_hit.obj = &hitvert;
|
nuclear@0
|
908 }
|
nuclear@0
|
909
|
nuclear@0
|
910 } else {
|
nuclear@0
|
911 // regular intersection test with polygons
|
nuclear@0
|
912
|
nuclear@0
|
913 for(unsigned int i=0; i<nfaces; i++) {
|
nuclear@0
|
914 Triangle face(i, varr, idxarr);
|
nuclear@0
|
915
|
nuclear@0
|
916 // ignore back-facing polygons if the mode flags include ISECT_FRONT
|
nuclear@0
|
917 if((Mesh::intersect_mode & ISECT_FRONT) && dot_product(face.get_normal(), ray.dir) > 0) {
|
nuclear@0
|
918 continue;
|
nuclear@0
|
919 }
|
nuclear@0
|
920
|
nuclear@0
|
921 HitPoint fhit;
|
nuclear@0
|
922 if(face.intersect(ray, hit ? &fhit : 0)) {
|
nuclear@0
|
923 if(!hit) {
|
nuclear@0
|
924 return true;
|
nuclear@0
|
925 }
|
nuclear@0
|
926 if(fhit.dist < nearest_hit.dist) {
|
nuclear@0
|
927 nearest_hit = fhit;
|
nuclear@0
|
928 hitface = face;
|
nuclear@0
|
929 }
|
nuclear@0
|
930 }
|
nuclear@0
|
931 }
|
nuclear@0
|
932 }
|
nuclear@0
|
933
|
nuclear@0
|
934 if(nearest_hit.obj) {
|
nuclear@0
|
935 if(hit) {
|
nuclear@0
|
936 *hit = nearest_hit;
|
nuclear@0
|
937
|
nuclear@0
|
938 // if we are interested in the mesh and not the faces set obj to this
|
nuclear@0
|
939 if(Mesh::intersect_mode & ISECT_FACE) {
|
nuclear@0
|
940 hit->obj = &hitface;
|
nuclear@0
|
941 } else if(Mesh::intersect_mode & ISECT_VERTICES) {
|
nuclear@0
|
942 hit->obj = &hitvert;
|
nuclear@0
|
943 } else {
|
nuclear@0
|
944 hit->obj = this;
|
nuclear@0
|
945 }
|
nuclear@0
|
946 }
|
nuclear@0
|
947 return true;
|
nuclear@0
|
948 }
|
nuclear@0
|
949 return false;
|
nuclear@0
|
950 }
|
nuclear@0
|
951
|
nuclear@0
|
952
|
nuclear@6
|
953 // texture coordinate manipulation
|
nuclear@6
|
954 void Mesh::texcoord_apply_xform(const Matrix4x4 &xform)
|
nuclear@6
|
955 {
|
nuclear@6
|
956 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
|
nuclear@6
|
957 return;
|
nuclear@6
|
958 }
|
nuclear@6
|
959
|
nuclear@6
|
960 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@6
|
961 Vector4 tc = get_attrib(MESH_ATTR_TEXCOORD, i);
|
nuclear@6
|
962 set_attrib(MESH_ATTR_TEXCOORD, i, tc.transformed(xform));
|
nuclear@6
|
963 }
|
nuclear@6
|
964 }
|
nuclear@6
|
965
|
nuclear@6
|
966 void Mesh::texcoord_gen_plane(const Vector3 &norm, const Vector3 &tang)
|
nuclear@6
|
967 {
|
nuclear@6
|
968 if(!nverts) return;
|
nuclear@6
|
969
|
nuclear@6
|
970 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
|
nuclear@6
|
971 // allocate texture coordinate attribute array
|
nuclear@6
|
972 set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
|
nuclear@6
|
973 }
|
nuclear@6
|
974
|
nuclear@6
|
975 Vector3 n = norm.normalized();
|
nuclear@6
|
976 Vector3 b = cross_product(n, tang).normalized();
|
nuclear@6
|
977 Vector3 t = cross_product(b, n);
|
nuclear@6
|
978
|
nuclear@6
|
979 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@6
|
980 Vector3 pos = get_attrib(MESH_ATTR_VERTEX, i);
|
nuclear@6
|
981
|
nuclear@6
|
982 // distance along the tangent direction
|
nuclear@6
|
983 float u = dot_product(pos, t);
|
nuclear@6
|
984 // distance along the bitangent direction
|
nuclear@6
|
985 float v = dot_product(pos, b);
|
nuclear@6
|
986
|
nuclear@6
|
987 set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(u, v, 0, 1));
|
nuclear@6
|
988 }
|
nuclear@6
|
989 }
|
nuclear@6
|
990
|
nuclear@6
|
991 void Mesh::texcoord_gen_box()
|
nuclear@6
|
992 {
|
nuclear@6
|
993 if(!nverts || !has_attrib(MESH_ATTR_NORMAL)) return;
|
nuclear@6
|
994
|
nuclear@6
|
995 if(!has_attrib(MESH_ATTR_TEXCOORD)) {
|
nuclear@6
|
996 // allocate texture coordinate attribute array
|
nuclear@6
|
997 set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts);
|
nuclear@6
|
998 }
|
nuclear@6
|
999
|
nuclear@6
|
1000 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@6
|
1001 Vector3 pos = Vector3(get_attrib(MESH_ATTR_VERTEX, i)) * 0.5 + Vector3(0.5, 0.5, 0.5);
|
nuclear@6
|
1002 Vector3 norm = get_attrib(MESH_ATTR_NORMAL, i);
|
nuclear@6
|
1003
|
nuclear@6
|
1004 float abs_nx = fabs(norm.x);
|
nuclear@6
|
1005 float abs_ny = fabs(norm.y);
|
nuclear@6
|
1006 float abs_nz = fabs(norm.z);
|
nuclear@6
|
1007 int dom = abs_nx > abs_ny && abs_nx > abs_nz ? 0 : (abs_ny > abs_nz ? 1 : 2);
|
nuclear@6
|
1008
|
nuclear@6
|
1009 float uv[2], *uvptr = uv;
|
nuclear@6
|
1010 for(int j=0; j<3; j++) {
|
nuclear@6
|
1011 if(j == dom) continue; // skip dominant axis
|
nuclear@6
|
1012
|
nuclear@6
|
1013 *uvptr++ = pos[j];
|
nuclear@6
|
1014 }
|
nuclear@6
|
1015 set_attrib(MESH_ATTR_TEXCOORD, i, Vector4(uv[0], uv[1], 0, 1));
|
nuclear@6
|
1016 }
|
nuclear@6
|
1017 }
|
nuclear@6
|
1018
|
nuclear@0
|
1019 // ------ private member functions ------
|
nuclear@0
|
1020
|
nuclear@0
|
1021 void Mesh::calc_aabb()
|
nuclear@0
|
1022 {
|
nuclear@0
|
1023 // the cast is to force calling the const version which doesn't invalidate
|
nuclear@0
|
1024 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
|
nuclear@0
|
1025 return;
|
nuclear@0
|
1026 }
|
nuclear@0
|
1027
|
nuclear@0
|
1028 aabb.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
|
nuclear@0
|
1029 aabb.max = -aabb.min;
|
nuclear@0
|
1030
|
nuclear@0
|
1031 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@0
|
1032 Vector4 v = get_attrib(MESH_ATTR_VERTEX, i);
|
nuclear@0
|
1033 for(int j=0; j<3; j++) {
|
nuclear@0
|
1034 if(v[j] < aabb.min[j]) {
|
nuclear@0
|
1035 aabb.min[j] = v[j];
|
nuclear@0
|
1036 }
|
nuclear@0
|
1037 if(v[j] > aabb.max[j]) {
|
nuclear@0
|
1038 aabb.max[j] = v[j];
|
nuclear@0
|
1039 }
|
nuclear@0
|
1040 }
|
nuclear@0
|
1041 }
|
nuclear@0
|
1042 aabb_valid = true;
|
nuclear@0
|
1043 }
|
nuclear@0
|
1044
|
nuclear@0
|
1045 void Mesh::calc_bsph()
|
nuclear@0
|
1046 {
|
nuclear@0
|
1047 // the cast is to force calling the const version which doesn't invalidate
|
nuclear@0
|
1048 if(!((const Mesh*)this)->get_attrib_data(MESH_ATTR_VERTEX)) {
|
nuclear@0
|
1049 return;
|
nuclear@0
|
1050 }
|
nuclear@0
|
1051
|
nuclear@0
|
1052 Vector3 v;
|
nuclear@0
|
1053 bsph.center = Vector3(0, 0, 0);
|
nuclear@0
|
1054
|
nuclear@0
|
1055 // first find the center
|
nuclear@0
|
1056 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@0
|
1057 v = get_attrib(MESH_ATTR_VERTEX, i);
|
nuclear@0
|
1058 bsph.center += v;
|
nuclear@0
|
1059 }
|
nuclear@0
|
1060 bsph.center /= (float)nverts;
|
nuclear@0
|
1061
|
nuclear@0
|
1062 bsph.radius = 0.0f;
|
nuclear@0
|
1063 for(unsigned int i=0; i<nverts; i++) {
|
nuclear@0
|
1064 v = get_attrib(MESH_ATTR_VERTEX, i);
|
nuclear@0
|
1065 float dist_sq = (v - bsph.center).length_sq();
|
nuclear@0
|
1066 if(dist_sq > bsph.radius) {
|
nuclear@0
|
1067 bsph.radius = dist_sq;
|
nuclear@0
|
1068 }
|
nuclear@0
|
1069 }
|
nuclear@0
|
1070 bsph.radius = sqrt(bsph.radius);
|
nuclear@0
|
1071
|
nuclear@0
|
1072 bsph_valid = true;
|
nuclear@0
|
1073 }
|
nuclear@0
|
1074
|
nuclear@0
|
1075 void Mesh::update_buffers()
|
nuclear@0
|
1076 {
|
nuclear@0
|
1077 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
1078 if(has_attrib(i) && !vattr[i].vbo_valid) {
|
nuclear@0
|
1079 glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
|
nuclear@0
|
1080 glBufferData(GL_ARRAY_BUFFER, nverts * vattr[i].nelem * sizeof(float), &vattr[i].data[0], GL_STATIC_DRAW);
|
nuclear@0
|
1081 vattr[i].vbo_valid = true;
|
nuclear@0
|
1082 }
|
nuclear@0
|
1083 }
|
nuclear@0
|
1084 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@0
|
1085
|
nuclear@0
|
1086 if(idata_valid && !ibo_valid) {
|
nuclear@0
|
1087 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
|
nuclear@0
|
1088 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 3 * sizeof(unsigned int), &idata[0], GL_STATIC_DRAW);
|
nuclear@0
|
1089 ibo_valid = true;
|
nuclear@0
|
1090 }
|
nuclear@0
|
1091 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
nuclear@0
|
1092 }
|
nuclear@0
|
1093
|
nuclear@0
|
1094 void Mesh::update_wire_ibo()
|
nuclear@0
|
1095 {
|
nuclear@0
|
1096 update_buffers();
|
nuclear@0
|
1097
|
nuclear@0
|
1098 if(wire_ibo_valid) {
|
nuclear@0
|
1099 return;
|
nuclear@0
|
1100 }
|
nuclear@0
|
1101
|
nuclear@0
|
1102 if(!wire_ibo) {
|
nuclear@0
|
1103 glGenBuffers(1, &wire_ibo);
|
nuclear@0
|
1104 }
|
nuclear@0
|
1105
|
nuclear@0
|
1106 unsigned int *wire_idxarr = new unsigned int[nfaces * 6];
|
nuclear@0
|
1107 unsigned int *dest = wire_idxarr;
|
nuclear@0
|
1108
|
nuclear@0
|
1109 if(ibo_valid) {
|
nuclear@0
|
1110 // we're dealing with an indexed mesh
|
nuclear@0
|
1111 const unsigned int *idxarr = ((const Mesh*)this)->get_index_data();
|
nuclear@0
|
1112
|
nuclear@0
|
1113 for(unsigned int i=0; i<nfaces; i++) {
|
nuclear@0
|
1114 *dest++ = idxarr[0];
|
nuclear@0
|
1115 *dest++ = idxarr[1];
|
nuclear@0
|
1116 *dest++ = idxarr[1];
|
nuclear@0
|
1117 *dest++ = idxarr[2];
|
nuclear@0
|
1118 *dest++ = idxarr[2];
|
nuclear@0
|
1119 *dest++ = idxarr[0];
|
nuclear@0
|
1120 idxarr += 3;
|
nuclear@0
|
1121 }
|
nuclear@0
|
1122 } else {
|
nuclear@0
|
1123 // not an indexed mesh ...
|
nuclear@0
|
1124 for(unsigned int i=0; i<nfaces; i++) {
|
nuclear@0
|
1125 int vidx = i * 3;
|
nuclear@0
|
1126 *dest++ = vidx;
|
nuclear@0
|
1127 *dest++ = vidx + 1;
|
nuclear@0
|
1128 *dest++ = vidx + 1;
|
nuclear@0
|
1129 *dest++ = vidx + 2;
|
nuclear@0
|
1130 *dest++ = vidx + 2;
|
nuclear@0
|
1131 *dest++ = vidx;
|
nuclear@0
|
1132 }
|
nuclear@0
|
1133 }
|
nuclear@0
|
1134
|
nuclear@0
|
1135 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
|
nuclear@0
|
1136 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
|
nuclear@0
|
1137 delete [] wire_idxarr;
|
nuclear@0
|
1138 wire_ibo_valid = true;
|
nuclear@0
|
1139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
nuclear@0
|
1140 }
|
nuclear@0
|
1141
|
nuclear@0
|
1142
|
nuclear@0
|
1143 // ------ class Triangle ------
|
nuclear@0
|
1144 Triangle::Triangle()
|
nuclear@0
|
1145 {
|
nuclear@0
|
1146 normal_valid = false;
|
nuclear@0
|
1147 id = -1;
|
nuclear@0
|
1148 }
|
nuclear@0
|
1149
|
nuclear@0
|
1150 Triangle::Triangle(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2)
|
nuclear@0
|
1151 {
|
nuclear@0
|
1152 v[0] = v0;
|
nuclear@0
|
1153 v[1] = v1;
|
nuclear@0
|
1154 v[2] = v2;
|
nuclear@0
|
1155 normal_valid = false;
|
nuclear@0
|
1156 id = -1;
|
nuclear@0
|
1157 }
|
nuclear@0
|
1158
|
nuclear@0
|
1159 Triangle::Triangle(int n, const Vector3 *varr, const unsigned int *idxarr)
|
nuclear@0
|
1160 {
|
nuclear@0
|
1161 if(idxarr) {
|
nuclear@0
|
1162 v[0] = varr[idxarr[n * 3]];
|
nuclear@0
|
1163 v[1] = varr[idxarr[n * 3 + 1]];
|
nuclear@0
|
1164 v[2] = varr[idxarr[n * 3 + 2]];
|
nuclear@0
|
1165 } else {
|
nuclear@0
|
1166 v[0] = varr[n * 3];
|
nuclear@0
|
1167 v[1] = varr[n * 3 + 1];
|
nuclear@0
|
1168 v[2] = varr[n * 3 + 2];
|
nuclear@0
|
1169 }
|
nuclear@0
|
1170 normal_valid = false;
|
nuclear@0
|
1171 id = n;
|
nuclear@0
|
1172 }
|
nuclear@0
|
1173
|
nuclear@0
|
1174 void Triangle::calc_normal()
|
nuclear@0
|
1175 {
|
nuclear@0
|
1176 normal = cross_product(v[1] - v[0], v[2] - v[0]).normalized();
|
nuclear@0
|
1177 normal_valid = true;
|
nuclear@0
|
1178 }
|
nuclear@0
|
1179
|
nuclear@0
|
1180 const Vector3 &Triangle::get_normal() const
|
nuclear@0
|
1181 {
|
nuclear@0
|
1182 if(!normal_valid) {
|
nuclear@0
|
1183 ((Triangle*)this)->calc_normal();
|
nuclear@0
|
1184 }
|
nuclear@0
|
1185 return normal;
|
nuclear@0
|
1186 }
|
nuclear@0
|
1187
|
nuclear@0
|
1188 void Triangle::transform(const Matrix4x4 &xform)
|
nuclear@0
|
1189 {
|
nuclear@0
|
1190 v[0].transform(xform);
|
nuclear@0
|
1191 v[1].transform(xform);
|
nuclear@0
|
1192 v[2].transform(xform);
|
nuclear@0
|
1193 normal_valid = false;
|
nuclear@0
|
1194 }
|
nuclear@0
|
1195
|
nuclear@0
|
1196 void Triangle::draw() const
|
nuclear@0
|
1197 {
|
nuclear@0
|
1198 Vector3 n[3];
|
nuclear@0
|
1199 n[0] = get_normal();
|
nuclear@0
|
1200 n[1] = get_normal();
|
nuclear@0
|
1201 n[2] = get_normal();
|
nuclear@0
|
1202
|
nuclear@0
|
1203 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
|
nuclear@0
|
1204 int nloc = Mesh::get_attrib_location(MESH_ATTR_NORMAL);
|
nuclear@0
|
1205
|
nuclear@0
|
1206 glEnableVertexAttribArray(vloc);
|
nuclear@0
|
1207 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
|
nuclear@0
|
1208 glVertexAttribPointer(nloc, 3, GL_FLOAT, GL_FALSE, 0, &n[0].x);
|
nuclear@0
|
1209
|
nuclear@0
|
1210 glDrawArrays(GL_TRIANGLES, 0, 3);
|
nuclear@0
|
1211
|
nuclear@0
|
1212 glDisableVertexAttribArray(vloc);
|
nuclear@0
|
1213 glDisableVertexAttribArray(nloc);
|
nuclear@0
|
1214 }
|
nuclear@0
|
1215
|
nuclear@0
|
1216 void Triangle::draw_wire() const
|
nuclear@0
|
1217 {
|
nuclear@0
|
1218 static const int idxarr[] = {0, 1, 1, 2, 2, 0};
|
nuclear@0
|
1219 int vloc = Mesh::get_attrib_location(MESH_ATTR_VERTEX);
|
nuclear@0
|
1220
|
nuclear@0
|
1221 glEnableVertexAttribArray(vloc);
|
nuclear@0
|
1222 glVertexAttribPointer(vloc, 3, GL_FLOAT, GL_FALSE, 0, &v[0].x);
|
nuclear@0
|
1223
|
nuclear@0
|
1224 glDrawElements(GL_LINES, 6, GL_UNSIGNED_INT, idxarr);
|
nuclear@0
|
1225
|
nuclear@0
|
1226 glDisableVertexAttribArray(vloc);
|
nuclear@0
|
1227 }
|
nuclear@0
|
1228
|
nuclear@0
|
1229 Vector3 Triangle::calc_barycentric(const Vector3 &pos) const
|
nuclear@0
|
1230 {
|
nuclear@0
|
1231 Vector3 norm = get_normal();
|
nuclear@0
|
1232
|
nuclear@0
|
1233 float area_sq = fabs(dot_product(cross_product(v[1] - v[0], v[2] - v[0]), norm));
|
nuclear@0
|
1234 if(area_sq < 1e-5) {
|
nuclear@0
|
1235 return Vector3(0, 0, 0);
|
nuclear@0
|
1236 }
|
nuclear@0
|
1237
|
nuclear@0
|
1238 float asq0 = fabs(dot_product(cross_product(v[1] - pos, v[2] - pos), norm));
|
nuclear@0
|
1239 float asq1 = fabs(dot_product(cross_product(v[2] - pos, v[0] - pos), norm));
|
nuclear@0
|
1240 float asq2 = fabs(dot_product(cross_product(v[0] - pos, v[1] - pos), norm));
|
nuclear@0
|
1241
|
nuclear@0
|
1242 return Vector3(asq0 / area_sq, asq1 / area_sq, asq2 / area_sq);
|
nuclear@0
|
1243 }
|
nuclear@0
|
1244
|
nuclear@0
|
1245 bool Triangle::intersect(const Ray &ray, HitPoint *hit) const
|
nuclear@0
|
1246 {
|
nuclear@0
|
1247 Vector3 normal = get_normal();
|
nuclear@0
|
1248
|
nuclear@0
|
1249 float ndotdir = dot_product(ray.dir, normal);
|
nuclear@0
|
1250 if(fabs(ndotdir) < 1e-4) {
|
nuclear@0
|
1251 return false;
|
nuclear@0
|
1252 }
|
nuclear@0
|
1253
|
nuclear@0
|
1254 Vector3 vertdir = v[0] - ray.origin;
|
nuclear@0
|
1255 float t = dot_product(normal, vertdir) / ndotdir;
|
nuclear@0
|
1256
|
nuclear@0
|
1257 Vector3 pos = ray.origin + ray.dir * t;
|
nuclear@0
|
1258 Vector3 bary = calc_barycentric(pos);
|
nuclear@0
|
1259
|
nuclear@0
|
1260 if(bary.x + bary.y + bary.z > 1.00001) {
|
nuclear@0
|
1261 return false;
|
nuclear@0
|
1262 }
|
nuclear@0
|
1263
|
nuclear@0
|
1264 if(hit) {
|
nuclear@0
|
1265 hit->dist = t;
|
nuclear@0
|
1266 hit->pos = ray.origin + ray.dir * t;
|
nuclear@0
|
1267 hit->normal = normal;
|
nuclear@0
|
1268 hit->obj = this;
|
nuclear@0
|
1269 }
|
nuclear@0
|
1270 return true;
|
nuclear@0
|
1271 }
|