-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_object.cpp
326 lines (265 loc) · 6.85 KB
/
obj_object.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "obj_object.h"
// WARNING: this macro is originally defined in glm.c
// when you are using GLMtriangle by yourself,
// you have to define it by yourself. This macro
// controls the structure of the GLMtriangle...
#define MATERIAL_BY_FACE
#include "glm.h"
#include "consts.h"
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <time.h>
///
/// ObjObject implementations
///
ObjObject::ObjObject(float fReflR, float fRefrR, float fRefrK, float fEmitR)
: Object(fReflR, fRefrR, fRefrK, fEmitR)
, _bSmooth(false)
, _bHasVNorm(false)
{
///
/// Since there's no ObjObject in GPU, and I want to make primaryObj id the same as the
/// GPU primaryObj array index, I do this. This should not impact the current functional
/// code.
///
nCurrObj --;
#ifdef USE_KD_TREE_OBJ
_pKdNode = new kd_node;
#endif
}
ObjObject::~ObjObject()
{
#ifdef USE_KD_TREE_OBJ
if(_pKdNode)
{
delete _pKdNode;
_pKdNode = new kd_node;
}
#endif
if(_triangles)
{
for(int i = 0; i < _nTriNum; i ++)
{
delete _triangles[i];
}
delete [] _triangles;
}
}
void ObjObject::setSmooth(bool bSmooth)
{
_bSmooth = bSmooth;
for(int i = 0; i < _nTriNum; i ++)
{
_triangles[i]->setSmooth(bSmooth);
}
}
bool ObjObject::load(char *pObjPath)
{
assert(pObjPath);
GLMmodel *pModel = glmReadOBJ(pObjPath);
if (pModel)
{
unsigned numTri = pModel->numtriangles;
if(numTri > 0)
{
_nTriNum = numTri;
_triangles = new Triangle*[numTri];
unsigned nCurrCount = 0;
GLMgroup *group = pModel->groups;
while (group)
{
for(unsigned i = 0; i < group->numtriangles; i++)
{
GLMtriangle *triangle = & pModel->triangles[group->triangles[i]];
GLfloat points[3][3] = {0};
float vnorm[3][3] = {0};
float norm[3] = {0};
for(int j = 0; j< 3; j++)
{
assert(triangle->vindices[j]>=1 && triangle->vindices[j]<= pModel->numvertices);
// Vertex
points[j][0] = pModel->vertices[3 * triangle->vindices[j] ];
points[j][1] = pModel->vertices[3 * triangle->vindices[j] + 1];
points[j][2] = pModel->vertices[3 * triangle->vindices[j] + 2];
// Vertex Normal
if( triangle->nindices[j] != -1)
{
vnorm[j][0] = pModel->normals[3 * triangle->nindices[j]];
vnorm[j][1] = pModel->normals[3 * triangle->nindices[j] + 1];
vnorm[j][2] = pModel->normals[3 * triangle->nindices[j] + 2];
_bHasVNorm = true;
}
else
{
_bHasVNorm = false;
}
}
// Facet Normal
norm[0] = pModel->facetnorms[triangle->findex * 3];
norm[1] = pModel->facetnorms[triangle->findex * 3 + 1];
norm[2] = pModel->facetnorms[triangle->findex * 3 + 2];
// Material
_triangles[nCurrCount] = new Triangle(points, norm, _fReflectionRatio, _fRefractionRatio, _fRefractionK);
if(_bHasVNorm)
{
_triangles[nCurrCount]->setVNorm(vnorm);
}
if(triangle->material /*&& triangle->material != group->material*/)
{
GLuint material = triangle->material;
GLMmaterial *materialp = &pModel->materials[material];
vect3d spec, diff, ambi;
vecCopy(spec, materialp->specular);
vecCopy(diff, materialp->diffuse);
vecCopy(ambi, materialp->ambient);
this->setMaterial(spec, diff, ambi, materialp->shininess);
//_triangles[nCurrCount]->setMaterial(materialp->specular, materialp->diffuse, materialp->ambient, materialp->shininess);
}
else
{
//printf("Warning: This object doesn't have internal material\n");
}
nCurrCount ++;
}// group
group = group->next;
}// while
assert(nCurrCount == pModel->numtriangles);
}// if
glmDelete(pModel);
updateBBox();
return true;
}
printf("GML obj file load failure!\n");
return false;
}
void ObjObject::updateBBox()
{
float xmin = 999999.f, xmax = -999999.f;
float ymin = 999999.f, ymax = -999999.f;
float zmin = 999999.f, zmax = -999999.f;
for(int i = 0; i < _nTriNum; i ++)
{
for(int j = 0; j < 3; j ++)
{
float x = _triangles[i]->_vertices[j][0];
float y = _triangles[i]->_vertices[j][1];
float z = _triangles[i]->_vertices[j][2];
if( x < xmin) xmin = x;
if( x > xmax) xmax = x;
if( y < ymin) ymin = y;
if( y > ymax) ymax = y;
if( z < zmin) zmin = z;
if( z > zmax) zmax = z;
}
_triangles[i]->updateBBox();
}
//printf("{%.2f, %.2f}-{%.2f, %.2f}-{%.2f, %.2f}\n", xmin, xmax, ymin, ymax, zmin, zmax);
_bbox.setDim(xmin, xmax, ymin, ymax, zmin, zmax);
}
#ifdef USE_KD_TREE_OBJ
void ObjObject::buildKdTree()
{
for(int i = 0; i < _nTriNum; i ++)
{
_pKdNode->addObject(_triangles[i]);
}
_pKdNode->updateBBox();
printf(" total [%d] triangles \n", _nTriNum);
if(::nObjDepth != 0)
{
clock_t nStart = clock();
kd_node::nObjDepth = (::nObjDepth == -1)?(8 + 1.3 * log(_nTriNum * 1.f)): ::nObjDepth;
printf("[Building KD-tree for Obj [%d]...\n", ::nObjDepth);
_pKdNode->split(false);
printf(" Done %.2f \n", (clock() - nStart) / 1000.f);
}
}
#endif
void ObjObject::scale(float x, float y, float z)
{
for(int i = 0; i < _nTriNum; i ++)
{
for(int j = 0; j < 3; j ++)
{
_triangles[i]->_vertices[j][0] *= x;
_triangles[i]->_vertices[j][1] *= y;
_triangles[i]->_vertices[j][2] *= z;
}
}
updateBBox();
}
void ObjObject::translate(float x, float y, float z)
{
for(int i = 0; i < _nTriNum; i ++)
{
for(int j = 0; j < 3; j ++)
{
_triangles[i]->_vertices[j][0] += x;
_triangles[i]->_vertices[j][1] += y;
_triangles[i]->_vertices[j][2] += z;
}
}
updateBBox();// TODO: maybe don't need to iterate
}
void ObjObject::rotate(float angle, vect3d& axis)
{
angle *= - PIon180;
set_matrix(sinf(angle), cosf(angle), axis);
for(int i = 0; i < _nTriNum; i ++)
{
vect3d tmp;
for(int j = 0; j < 3; j ++)
{
mat_rot(_triangles[i]->_vertices[j], tmp); vecCopy(_triangles[i]->_vertices[j], tmp);
// set a bool for existance of vnormal
mat_rot(_triangles[i]->_vnormal[j], tmp); vecCopy(_triangles[i]->_vnormal[j], tmp);
}
mat_rot(_triangles[i]->_normal, tmp); vecCopy(_triangles[i]->_normal, tmp);
}
updateBBox();
}
bool ObjObject::isHit(Ray &ray, vect3d &pNormal, float *pt, vect3d *pTexColor)
{
#ifndef USE_KD_TREE_OBJ
if(!_bbox.isHit(ray))
{
return false;
}
vect3d norm;
float t = 999999.f;
float u = 0, v = 0;
unsigned nHitTriInx = 0;
for(int i = 0; i < _nTriNum; i ++)
{
float currT = 999999.f;
vect3d currNorm;
vect3d currTexColor;
bool bHit = _triangles[i]->isHit(ray, currNorm, &currT);
if(bHit && (currT < t))
{
t = currT;
vecCopy(norm, currNorm);
}
}
if(t < 999999.f && t > epsi)
{
*pt = t;
vecCopy(pNormal, norm);
return true;
}
return false;
#else
assert(_pKdNode);
vect3d norm;
float tmpT = 0.f;
Object *pObj = _pKdNode->isHit(ray, norm, &tmpT, pTexColor);
if(pObj)
{
*pt = tmpT;
vecCopy(pNormal, norm);
return true;
}
return false;
#endif
}