-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolyMeshNode.cpp
230 lines (180 loc) · 6.76 KB
/
PolyMeshNode.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
/*
* PolyMesh.cpp
* OBJViewer
*
* Created by Leonard Teo on 10-10-05.
*
*/
#include "PolyMeshNode.h"
//Constructor
PolyMeshNode::PolyMeshNode() : Node()
{
this->mesh = NULL;
this->texture = NULL;
this->material = NULL;
}
//Destructor
PolyMeshNode::~PolyMeshNode()
{
delete this->mesh;
delete this->texture;
delete this;
}
//attachModel
void PolyMeshNode::attachModel(OBJModel* model)
{
this->mesh = model;
}
//attachTexture
void PolyMeshNode::attachTexture(Texture* texture)
{
this->texture = texture;
}
//Attach material
void PolyMeshNode::attachMaterial(Material* material)
{
this->material = material;
}
void PolyMeshNode::draw()
{
//Load texture
if (this->texture != NULL)
{
glBindTexture(GL_TEXTURE_2D, this->texture->textureID);
}
//Load material
if (this->material != NULL)
{
glMaterialfv(GL_FRONT, GL_AMBIENT, this->material->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, this->material->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, this->material->specular);
glMaterialfv(GL_FRONT, GL_SHININESS, this->material->shininess);
}
//Check vertex buffer object extension
if (glewGetExtension("GL_ARB_vertex_buffer_object"))
{
//Load vertices
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_vertices);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
//Load normals
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_normals);
glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_NORMAL_ARRAY);
//Load UVs
glBindBuffer(GL_ARRAY_BUFFER, this->mesh->vbo_uvs);
glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->mesh->vbo_index);
glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
} else {
//Drawing with vertex arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, this->mesh->vertexArray);
glNormalPointer(GL_FLOAT, 0, this->mesh->normalsArray);
glTexCoordPointer(2, GL_FLOAT, 0, this->mesh->uvArray);
glDrawElements(GL_TRIANGLES, 3*this->mesh->numFaces, GL_UNSIGNED_INT, this->mesh->indexArray);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
/*
//Old immediate mode drawing code
glBegin(GL_TRIANGLES);
for (int face = 0; face < this->numFaces; face++)
{
//Draw the vertex at each of the vertex numbders
glTexCoord2f((GLfloat)this->uvs[this->uvIndex[face][0]]->u, (GLfloat)this->uvs[this->uvIndex[face][0]]->v);
glNormal3f((GLfloat)this->normals[this->normalsIndex[face][0]]->x, (GLfloat)this->normals[this->normalsIndex[face][0]]->y, (GLfloat)this->normals[this->normalsIndex[face][0]]->z);
glVertex3f((GLfloat)this->vertices[this->index[face][0]]->x, (GLfloat)this->vertices[this->index[face][0]]->y, (GLfloat)this->vertices[this->index[face][0]]->z);
glTexCoord2f((GLfloat)this->uvs[this->uvIndex[face][1]]->u, (GLfloat)this->uvs[this->uvIndex[face][1]]->v);
glNormal3f((GLfloat)this->normals[this->normalsIndex[face][1]]->x, (GLfloat)this->normals[this->normalsIndex[face][1]]->y, (GLfloat)this->normals[this->normalsIndex[face][1]]->z);
glVertex3f((GLfloat)this->vertices[this->index[face][1]]->x, (GLfloat)this->vertices[this->index[face][1]]->y, (GLfloat)this->vertices[this->index[face][1]]->z);
glTexCoord2f((GLfloat)this->uvs[this->uvIndex[face][2]]->u, (GLfloat)this->uvs[this->uvIndex[face][2]]->v);
glNormal3f((GLfloat)this->normals[this->normalsIndex[face][2]]->x, (GLfloat)this->normals[this->normalsIndex[face][2]]->y, (GLfloat)this->normals[this->normalsIndex[face][2]]->z);
glVertex3f((GLfloat)this->vertices[this->index[face][2]]->x, (GLfloat)this->vertices[this->index[face][2]]->y, (GLfloat)this->vertices[this->index[face][2]]->z);
/* Non-Optimized code for debugging
//Get vertex numbers
int v1, v2, v3;
v1 = this->index[face][0];
v2 = this->index[face][1];
v3 = this->index[face][2];
//Get Normal numbers
int n1, n2, n3;
n1 = this->normalsIndex[face][0];
n2 = this->normalsIndex[face][1];
n3 = this->normalsIndex[face][2];
//Get UVs
int uv1, uv2, uv3;
uv1 = this->uvIndex[face][0];
uv2 = this->uvIndex[face][1];
uv3 = this->uvIndex[face][2];
//Draw the vertex at each of the vertex numbders
glTexCoord2f((GLfloat)this->uvs[uv1]->u, (GLfloat)this->uvs[uv1]->v);
glNormal3f((GLfloat)this->normals[n1]->x, (GLfloat)this->normals[n1]->y, (GLfloat)this->normals[n1]->z);
glVertex3f((GLfloat)this->vertices[v1]->x, (GLfloat)this->vertices[v1]->y, (GLfloat)this->vertices[v1]->z);
glTexCoord2f((GLfloat)this->uvs[uv2]->u, (GLfloat)this->uvs[uv2]->v);
glNormal3f((GLfloat)this->normals[n2]->x, (GLfloat)this->normals[n2]->y, (GLfloat)this->normals[n2]->z);
glVertex3f((GLfloat)this->vertices[v2]->x, (GLfloat)this->vertices[v2]->y, (GLfloat)this->vertices[v2]->z);
glTexCoord2f((GLfloat)this->uvs[uv3]->u, (GLfloat)this->uvs[uv3]->v);
glNormal3f((GLfloat)this->normals[n3]->x, (GLfloat)this->normals[n3]->y, (GLfloat)this->normals[n3]->z);
glVertex3f((GLfloat)this->vertices[v3]->x, (GLfloat)this->vertices[v3]->y, (GLfloat)this->vertices[v3]->z);
*/
/*
}
glEnd();
*/
}
void PolyMeshNode::render(enum RenderType renderType)
{
//Draw this
glPushMatrix();
glTranslatef(this->translate->x, this->translate->y, this->translate->z);
//This piece of code is not good because the order of rotation is actually important.
//We need to figure out a way to rotate an entire local coordinate system.
//This should be relatively simple but we'll need to figure it out...
glRotatef(this->rotate->z, 0.0f, 0.0f, 1.0f);
glRotatef(this->rotate->y, 0.0f, 1.0f, 0.0f);
glRotatef(this->rotate->x, 1.0f, 0.0f, 0.0f);
glScalef(this->scale->x, this->scale->y, this->scale->z);
//Drawing
//Figure out if this is an opaque or transparent object, and draw appropriately
if (renderType == ALL_OBJECTS)
{
this->draw();
} else {
if (this->material != NULL)
{
//Check material
if (this->material->diffuse[3] < 1.0f || this->material->ambient[3] < 1.0f)
{
//If there is transparency
//Check the render type. If transparent, draw
if (renderType == TRANSPARENT_OBJECTS)
{
this->draw();
}
} else {
//If there is no transparency
if (renderType == OPAQUE_OBJECTS)
{
this->draw();
}
}
} else {
//No material -- assume that it is opaque, so draw
this->draw();
}
}
//Draw children
for (int i=0; i < this->children->size(); i++)
{
this->children->at(i)->render(renderType);
}
glPopMatrix();
}