00001
00002 #include <blort/TomGine/tgModel.h>
00003 #include <blort/TomGine/headers.h>
00004 #include <blort/TomGine/tgShapeCreator.h>
00005
00006 using namespace TomGine;
00007
00008 void tgModel::DrawFaces() const{
00009 int i,j;
00010 int v;
00011
00012
00013 for(i=0; i<(int)m_faces.size(); i++){
00014
00015 if(m_faces[i].v.size() == 3)
00016 glBegin(GL_TRIANGLES);
00017 else if(m_faces[i].v.size() == 4)
00018 glBegin(GL_QUADS);
00019 else{
00020 printf("[tgModel::DrawFaces()] Warning, no suitable face format\n");
00021 printf("[tgModel::DrawFaces()] Face has %d vertices (supported: 3 or 4)\n", (int)m_faces[i].v.size());
00022 return;
00023 }
00024
00025 for(j=0; j<(int)m_faces[i].v.size(); j++){
00026 v = m_faces[i].v[j];
00027 glTexCoord2f(m_vertices[v].texCoord.x, m_vertices[v].texCoord.y);
00028 glNormal3f(m_vertices[v].normal.x, m_vertices[v].normal.y, m_vertices[v].normal.z);
00029 glVertex3f(m_vertices[v].pos.x, m_vertices[v].pos.y, m_vertices[v].pos.z);
00030 }
00031 glEnd();
00032 }
00033 }
00034
00035 void tgModel::DrawLines(const vec3 &color) const{
00036 glDisable(GL_TEXTURE_2D);
00037 glDisable(GL_LIGHTING);
00038 glColor3f(color.x, color.y, color.z);
00039
00040 glBegin(GL_LINES);
00041 for(unsigned i=0; i<m_points.size(); i++){
00042 glVertex3f(m_lines[i].start.x, m_lines[i].start.y, m_lines[i].start.z);
00043 glVertex3f(m_lines[i].end.x, m_lines[i].end.y, m_lines[i].end.z);
00044 }
00045 glEnd();
00046 glColor3f(1.0f, 1.0f, 1.0f);
00047 }
00048
00049
00050 void tgModel::DrawPoints(const vec3 &color) const{
00051 glDisable(GL_TEXTURE_2D);
00052 glDisable(GL_LIGHTING);
00053 glColor3f(color.x, color.y, color.z);
00054
00055 glBegin(GL_POINTS);
00056 for(unsigned i=0; i<m_points.size(); i++){
00057 glVertex3f(m_points[i].x, m_points[i].y, m_points[i].z);
00058 }
00059 glEnd();
00060 glColor3f(1.0f, 1.0f, 1.0f);
00061 }
00062
00063 void tgModel::DrawNormals(float normal_length) const{
00064 glDisable(GL_TEXTURE_2D);
00065 glDisable(GL_LIGHTING);
00066 glColor3f(0.0f, 0.0f, 1.0f);
00067
00068 glBegin(GL_LINES);
00069
00070 for(unsigned j=0; j<m_vertices.size(); j++){
00071 glVertex3f( m_vertices[j].pos.x,
00072 m_vertices[j].pos.y,
00073 m_vertices[j].pos.z );
00074 glVertex3f( m_vertices[j].pos.x + m_vertices[j].normal.x * normal_length,
00075 m_vertices[j].pos.y + m_vertices[j].normal.y * normal_length,
00076 m_vertices[j].pos.z + m_vertices[j].normal.z * normal_length );
00077 }
00078 glEnd();
00079
00080 glColor3f(1.0f, 1.0f, 1.0f);
00081 }
00082
00083
00084 void tgModel::ComputeNormals(){
00085 unsigned i,j;
00086 tgFace* f;
00087 vec3 v0, v1, v2, e1, e2, n;
00088
00089
00090 for(i=0; i<m_faces.size(); i++){
00091 f = &m_faces[i];
00092
00093 v0 = vec3(m_vertices[f->v[0]].pos.x, m_vertices[f->v[0]].pos.y, m_vertices[f->v[0]].pos.z);
00094 v1 = vec3(m_vertices[f->v[1]].pos.x, m_vertices[f->v[1]].pos.y, m_vertices[f->v[1]].pos.z);
00095 v2 = vec3(m_vertices[f->v[2]].pos.x, m_vertices[f->v[2]].pos.y, m_vertices[f->v[2]].pos.z);
00096 e1 = v1 - v0;
00097 e2 = v2 - v0;
00098
00099 n.cross(e1,e2);
00100 n.normalize();
00101 f->normal = vec3(n);
00102 for(j=0; j<m_faces[i].v.size(); j++){
00103 m_vertices[f->v[j]].normal.x = n.x;
00104 m_vertices[f->v[j]].normal.y = n.y;
00105 m_vertices[f->v[j]].normal.z = n.z;
00106 }
00107 }
00108 }
00109
00110 void tgModel::ComputeFaceNormals()
00111 {
00112 unsigned i;
00113 tgFace* f;
00114 vec3 v0, v1, v2, e1, e2, n;
00115
00116
00117 for(i=0; i<m_faces.size(); i++){
00118 f = &m_faces[i];
00119
00120 v0 = vec3(m_vertices[f->v[0]].pos.x, m_vertices[f->v[0]].pos.y, m_vertices[f->v[0]].pos.z);
00121 v1 = vec3(m_vertices[f->v[1]].pos.x, m_vertices[f->v[1]].pos.y, m_vertices[f->v[1]].pos.z);
00122 v2 = vec3(m_vertices[f->v[2]].pos.x, m_vertices[f->v[2]].pos.y, m_vertices[f->v[2]].pos.z);
00123 e1 = v1 - v0;
00124 e2 = v2 - v0;
00125
00126 n.cross(e1,e2);
00127 n.normalize();
00128 f->normal = vec3(n);
00129 }
00130 }
00131
00132 void tgModel::ComputeBoundingSphere()
00133 {
00134 vec3 min;
00135 vec3 max;
00136 vec3 v;
00137
00138
00139
00140 if(m_vertices.empty())
00141 return;
00142
00143 min = m_vertices[0].pos;
00144 max = min;
00145
00146 for(unsigned i=1; i<m_vertices.size(); i++)
00147 {
00148 v = m_vertices[i].pos;
00149 if(v.x < min.x)
00150 min.x = v.x;
00151 if(v.y < min.y)
00152 min.y = v.y;
00153 if(v.z < min.z)
00154 min.z = v.z;
00155
00156 if(v.x > max.x)
00157 max.x = v.x;
00158 if(v.y > max.y)
00159 max.y = v.y;
00160 if(v.z > max.z)
00161 max.z = v.z;
00162 }
00163 m_bs.center.x = (max.x + min.x) * 0.5f;
00164 m_bs.center.y = (max.y + min.y) * 0.5f;
00165 m_bs.center.z = (max.z + min.z) * 0.5f;
00166
00167 float rad = 0.0f;
00168 for(unsigned i=0; i<m_vertices.size(); i++)
00169 {
00170 v = m_vertices[i].pos - m_bs.center;
00171 float rv = v.x*v.x + v.y*v.y + v.z*v.z;
00172 if( rad < rv )
00173 rad = rv;
00174 }
00175
00176 m_bs.radius = sqrt(rad);
00177
00178 }
00179
00180 void tgModel::Clear(){
00181 m_vertices.clear();
00182 m_faces.clear();
00183
00184
00185
00186
00187
00188 }
00189
00190 void tgModel::Print() const{
00191
00192 for(unsigned i=0; i<m_vertices.size(); i++){
00193 printf("Vertex %u: %f %f %f, %f %f %f\n", i, m_vertices[i].pos.x, m_vertices[i].pos.y, m_vertices[i].pos.z, m_vertices[i].normal.x, m_vertices[i].normal.y, m_vertices[i].normal.z);
00194 }
00195
00196 for(unsigned i=0; i<m_faces.size(); i++){
00197
00198 printf("Face %u:",i);
00199 for(unsigned j=0; j<m_faces[i].v.size(); j++){
00200 printf(" %d", m_faces[i].v[j]);
00201 }
00202 printf("\n");
00203 }
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305