00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "renderglobj.h"
00022 #include "defs.h"
00023
00024
00025
00026 void rotate_x_matrix(double sita, MSLMatrix& m)
00027 {
00028 m(0, 0)=1.0; m(0,1)=0.0; m(0,2)=0.0; m(0,3)=0.0;
00029 m(1,0)=0.0; m(1,1)=cos(sita); m(1,2)=-sin(sita); m(1,3)=0.0;
00030 m(2,0)=0.0; m(2,1)=sin(sita); m(2,2)=cos(sita); m(2,3)=0.0;
00031 m(3,0)=0.0; m(3,1)=0.0; m(3,2)=0.0; m(3,3)=1.0;
00032 }
00033
00034 void rotate_y_matrix(double sita, MSLMatrix& m)
00035 {
00036 m(0,0)=cos(sita); m(0,1)=0.0; m(0,2)=sin(sita); m(0,3)=0.0;
00037 m(1,0)=0.0; m(1,1)=1.0; m(1,2)=0.0; m(1,3)=0.0;
00038 m(2,0)=-sin(sita); m(2,1)=0.0; m(2,2)=cos(sita); m(2,3)=0.0;
00039 m(3,0)=0.0; m(3,1)=0.0; m(3,2)=0.0; m(3,3)=1.0;
00040 }
00041
00042 void rotate_z_matrix(double sita, MSLMatrix& m)
00043 {
00044 m(0,0)=cos(sita); m(0,1)=-sin(sita); m(0,2)=0.0; m(0,3)=0.0;
00045 m(1,0)=sin(sita); m(1,1)=cos(sita); m(1,2)=0.0; m(1,3)=0.0;
00046 m(2,0)=0.0; m(2,1)=0.0; m(2,2)=1.0; m(2,3)=0.0;
00047 m(3,0)=0.0; m(3,1)=0.0; m(3,2)=0.0; m(3,3)=1.0;
00048 }
00049
00050 MSLVector point_x_rotation(const double& sita, const MSLVector& p1)
00051 {
00052 MSLVector p2(3), p21(4), p11(4);
00053 MSLMatrix m(4,4);
00054
00055 rotate_x_matrix(sita, m);
00056 p11[0] = p1[0];
00057 p11[1] = p1[1];
00058 p11[2] = p1[2];
00059 p11[3] = 1.0;
00060
00061 p21 = m * p11;
00062
00063 p2[0] = p21[0];
00064 p2[1] = p21[1];
00065 p2[2] = p21[2];
00066
00067 return p2;
00068 }
00069
00070 MSLVector point_y_rotation(const double& sita, const MSLVector& p1)
00071 {
00072 MSLVector p2(3), p21(4), p11(4);
00073 MSLMatrix m(4,4);
00074
00075 rotate_y_matrix(sita, m);
00076 p11[0] = p1[0];
00077 p11[1] = p1[1];
00078 p11[2] = p1[2];
00079 p11[3] = 1.0;
00080
00081 p21 = m * p11;
00082
00083 p2[0] = p21[0];
00084 p2[1] = p21[1];
00085 p2[2] = p21[2];
00086
00087 return p2;
00088 }
00089
00090 MSLVector point_z_rotation(const double& sita, const MSLVector& p1)
00091 {
00092 MSLVector p2(3), p21(4), p11(4);
00093 MSLMatrix m(4,4);
00094
00095 rotate_z_matrix(sita, m);
00096 p11[0] = p1[0];
00097 p11[1] = p1[1];
00098 p11[2] = p1[2];
00099 p11[3] = 1.0;
00100
00101 p21 = m * p11;
00102
00103 p2[0] = p21[0];
00104 p2[1] = p21[1];
00105 p2[2] = p21[2];
00106
00107 return p2;
00108 }
00109
00110
00111
00112 void crossproduct(const MSLVector& v1, const MSLVector& v2, MSLVector& v)
00113 {
00114 v[0] = v1[1]*v2[2] - v1[2]*v2[1];
00115 v[1] = v1[2]*v2[0] - v1[0]*v2[2];
00116 v[2] = v1[0]*v2[1] - v1[1]*v2[0];
00117 }
00118
00119
00120 void normalMSLVector(const MSLVector& v1, const MSLVector& v2, MSLVector& v)
00121 {
00122 crossproduct(v1, v2, v);
00123 v = v.norm();
00124 }
00125
00126 MSLVector irpy(const MSLMatrix& R)
00127 {
00128 MSLVector k(3);
00129
00130 if (R(2,0) == 1) {
00131 k[0] = atan2(-R(0, 1), -R(0, 2));
00132 k[1] = -PI/2.0;
00133 k[2] = 0.0;
00134 } else if (R(2,0) == -1) {
00135 k[0] = atan2(R(0,1), R(0,2));
00136 k[1] = PI/2.0;
00137 k[2] = 0.0;
00138 } else {
00139 k[0] = atan2(R(2,1), R(2,2));
00140 k[1] = atan2(-R(2,0), sqrt(R(0,0)*R(0,0) + R(1,0)*R(1,0)));
00141 k[2] = atan2(R(1,0), R(0,0));
00142 }
00143
00144 return k;
00145 }
00146
00147 MSLMatrix rpy(const MSLVector& a)
00148 {
00149
00150 MSLMatrix rot(4,4);
00151 double ca, sa, cb, sb, cc, sc;
00152
00153 ca = cos(a[0]);
00154 sa = sin(a[0]);
00155 cb = cos(a[1]);
00156 sb = sin(a[1]);
00157 cc = cos(a[2]);
00158 sc = sin(a[2]);
00159
00160 rot(0,0) = cb*cc;
00161 rot(0,1) = sa*sb*cc-ca*sc;
00162 rot(0,2) = ca*sb*cc+sa*sc;
00163 rot(1,0) = cb*sc;
00164 rot(1,1) = sa*sb*sc+ca*cc;
00165 rot(1,2) = ca*sb*sc-sa*cc;
00166 rot(2,0) = -sb;
00167 rot(2,1) = sa*cb;
00168 rot(2,2) = ca*cb;
00169
00170 rot(0, 3) = rot(1, 3) = rot(2, 3) = rot(3, 0) = rot(3, 1) = rot(3, 2) = 0.0;
00171 rot(3, 3) = 1.0;
00172
00173 return rot;
00174
00175 }
00176
00177 MSLVector free_rotate(const MSLVector& axis, const MSLVector& v, double sita)
00178 {
00179 double rx, ry;
00180 double diagnal_xy;
00181 MSLVector vt(3);
00182
00183
00184 rx = atan2(axis[1], axis[2]);
00185
00187
00188 diagnal_xy = sqrt(axis[1]*axis[1]+axis[2]*axis[2]);
00189
00190
00191 ry = atan2(axis[0], diagnal_xy);
00192
00193 vt = v;
00194
00195 vt = point_x_rotation(rx, vt);
00196 vt = point_y_rotation(-ry, vt);
00197
00198 vt = point_z_rotation(sita, vt);
00199
00200 vt = point_y_rotation(ry, vt);
00201 vt = point_x_rotation(-rx, vt);
00202
00203 return vt;
00204 }
00205
00206
00207 MSLMatrix free_rotate(const MSLVector& axis, const MSLMatrix& m, double sita)
00208 {
00209 double rx, ry;
00210 double diagnal_xy;
00211 MSLMatrix mt(4, 4), mr(4, 4);
00212
00213
00214 rx = atan2(axis[1], axis[2]);
00215
00217
00218 diagnal_xy = sqrt(axis[1]*axis[1]+axis[2]*axis[2]);
00219
00220
00221 ry = atan2(axis[0], diagnal_xy);
00222
00223 mt = m;
00224
00225 rotate_x_matrix(rx, mr);
00226 mt = mr * mt;
00227
00228 rotate_y_matrix(-ry, mr);
00229 mt = mr * mt;
00230
00231 rotate_z_matrix(sita, mr);
00232 mt = mr * mt;
00233
00234 rotate_y_matrix(ry, mr);
00235 mt = mr * mt;
00236
00237 rotate_x_matrix(-rx, mr);
00238 mt = mr * mt;
00239
00240 return mt;
00241 }
00242
00243
00244
00245
00246
00247 static list<string> MaterialFileList;
00248 static int numSkip = 0;
00249 static int numOther = 0;
00250
00251 #define GROW(_v, _t) \
00252 if (_v == NULL) \
00253 { \
00254 _v ## Available = CHUNK; \
00255 _v = (_t *) malloc(sizeof(_t)*_v ## Available); \
00256 } \
00257 else \
00258 if (_v ## Count >= _v ## Available) \
00259 { \
00260 _v ## Available *= 2; \
00261 _v = (_t *) realloc(_v, sizeof(_t)*_v ## Available); \
00262 }
00263
00264
00265 static unsigned int getint(FILE *fp)
00266 {
00267 int c, c1, c2, c3;
00268
00269
00270 c = getc(fp);
00271 c1 = getc(fp);
00272 c2 = getc(fp);
00273 c3 = getc(fp);
00274
00275 return ((unsigned int) c) +
00276 (((unsigned int) c1) << 8) +
00277 (((unsigned int) c2) << 16) +
00278 (((unsigned int) c3) << 24);
00279 }
00280
00281 static unsigned int getshort(FILE *fp)
00282 {
00283 int c, c1;
00284
00285
00286 c = getc(fp);
00287 c1 = getc(fp);
00288
00289 return ((unsigned int) c) + (((unsigned int) c1) << 8);
00290 }
00291
00292
00293 Image::Image()
00294 {
00295 sizeX = (unsigned long) 256;
00296 sizeY = (unsigned long) 256;
00297
00298 data = new char[10];
00299 }
00300
00301 Image::~Image()
00302 {
00303
00304
00305 }
00306
00307
00308 mslGLMaterial::mslGLMaterial()
00309 {
00310 TextureImage = new Image;
00311 TextureHandle = 0;
00312
00313 AmbientOn = 0;
00314 SpecularOn = 0;
00315 DiffuseOn = 0;
00316 ShininessOn = 0;
00317 AlphaOn = 0;
00318 ReflectOn = 0;
00319 TwosideOn = 0;
00320 TextureOn = 0;
00321
00322 ID = 0;
00323 }
00324
00325
00326 mslGLMaterial::~mslGLMaterial()
00327 {
00328
00329
00330 }
00331
00332 void mslGLMaterial::SetMaterial()
00333 {
00334 GLenum drawmode;
00335
00336 if(TwosideOn)
00337 drawmode = GL_FRONT_AND_BACK;
00338 else
00339 drawmode = GL_FRONT;
00340
00341 if(TextureOn)
00342 {
00343 glEnable(GL_TEXTURE_2D);
00344 glBindTexture(GL_TEXTURE_2D, TextureHandle);
00345 }
00346 else
00347 glDisable(GL_TEXTURE_2D);
00348
00349 if(DiffuseOn)
00350 {
00351 glMaterialfv(drawmode, GL_DIFFUSE, Diffuse);
00352 if(AlphaOn)
00353 {
00354 glEnable(GL_BLEND);
00355 glColor4f(Color[0], Color[1], Color[2], Alpha);
00356 }
00357 else
00358 {
00359 glDisable(GL_BLEND);
00360 glColor4f(Color[0], Color[1], Color[2], 1.0);
00361 }
00362 }
00363
00364 if(AmbientOn) glMaterialfv(drawmode, GL_AMBIENT, Ambient);
00365 if(SpecularOn) glMaterialfv(drawmode, GL_SPECULAR, Specular);
00366 if(ShininessOn) glMaterialf(drawmode, GL_SHININESS, Shininess);
00367 }
00368
00369
00370 int mslGLMaterial::ImageLoad(int id, string path, string filename)
00371 {
00372 FILE *file;
00373 unsigned long size;
00374 unsigned long i;
00375 unsigned short int planes;
00376 unsigned short int bpp;
00377 GLuint gutmp;
00378 char temp;
00379 void * mem;
00380
00381 if ((file = fopen((path + filename).c_str(), "rb"))==NULL) {
00382 cout << "File " << filename << " Not Found!\n";
00383 TextureOn = 0;
00384 return 0;
00385 }
00386 else
00387 TextureOn = 1;
00388
00389
00390 fseek(file, 18, SEEK_CUR);
00391
00392
00393
00394
00395 TextureImage->sizeX = getint (file);
00396 cout << "Width of" << filename <<" : " << TextureImage->sizeX << "\n";
00397 TextureImage->sizeY = getint (file);
00398 cout << "Height of" << filename <<" : " << TextureImage->sizeY << "\n";
00399 size = TextureImage->sizeX * TextureImage->sizeY * 3;
00400 planes = getshort(file);
00401 if (planes != 1) {
00402 cout << "Planes from " << filename << " is not 1: " <<planes << "\n";
00403 return 0;
00404 }
00405 bpp = getshort(file);
00406 if (bpp != 24) {
00407 cout << "Bpp from " << filename << " is not 24: " << bpp <<" \n";
00408 return 0;
00409 }
00410
00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435 00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447
00448
00449 fseek(file, 24, SEEK_CUR);
00450
00451 if((mem = realloc(TextureImage->data,
00452 sizeof(char)*(size))) == 0) {
00453 printf("Error reallocating mem\n");
00454 exit(-1);
00455 }
00456
00457 TextureImage->data = (char *) mem;
00458
00459 if ((i = fread(TextureImage->data, size, 1, file)) != 1) {
00460 cout << "Error reading image data from " << filename <<" \n";
00461 return 0;
00462 }
00463
00464 for (i=0;i<size;i+=3) { /* reverse all of the colors. (bgr -> rgb)*/
00465 temp = TextureImage->data[i];
00466 TextureImage->data[i] = TextureImage->data[i+2];
00467 TextureImage->data[i+2] = temp;
00468 }
00469
00470 glGenTextures((GLsizei)1, &gutmp);
00471 TextureHandle = (GLuint) gutmp;
00472
00473 glBindTexture(GL_TEXTURE_2D, TextureHandle);
00474 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00475 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00476 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00477 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00478 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureImage->sizeX,
00479 TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE,
00480 TextureImage->data);
00481
00482 return 1;
00483 }
00484
00485
00486 void mslGLMaterial::Clear()
00487 {
00488 TextureHandle = 0;
00489
00490 TextureOn = 0;
00491 AmbientOn = 0;
00492 SpecularOn = 0;
00493 DiffuseOn = 0;
00494 ShininessOn = 0;
00495 AlphaOn = 0;
00496 ReflectOn = 0;
00497 TwosideOn = 0;
00498 }
00499
00500 void mslGLMaterial::Print()
00501 {
00502 cout << "Texture Name: " << Name << endl;
00503 cout << "Texture Handel: " << TextureHandle << endl;
00504 cout << "Material ID: " << ID << endl;
00505
00506 if(DiffuseOn)
00507 cout << "Diffuse: " << Diffuse[0] << " " << Diffuse[1] << " " << Diffuse[2] << endl;
00508 if(AmbientOn)
00509 cout << "Ambient: " << Ambient[0] << " " << Ambient[1] << " " << Ambient[2] << endl;
00510 if(SpecularOn)
00511 cout << "Specular: " << Specular[0] << " " << Specular[1] << " " << Specular[2] << endl;
00512 if(ShininessOn)
00513 cout << "Shininess: " << Shininess << endl;
00514 }
00515
00516
00517 mslGLFace::mslGLFace()
00518 {
00519 VerticeCoord = NULL;
00520 NormalCoord = NULL ;
00521 TextureCoord = NULL;
00522
00523 NumberOfPoint = 0;
00524 NumberOfNormal = 0;
00525 NumberOfTexCoord = 0;
00526
00527 NormalOn = 1;
00528 TextureOn = 1;
00529 ColorOn = 1;
00530
00531 MaterialID = -1;
00532 }
00533
00534
00535 mslGLFace::~mslGLFace()
00536 {
00537 00538 00539 00540 00541 00542 00543 00544 00545 00546
00547 }
00548
00549 void mslGLFace::AddVertex(const mslGLVertex& ver)
00550 {
00551 if(VerticeCoord == NULL)
00552 {
00553 if((VerticeCoord = (mslGLVertex *) malloc(sizeof(mslGLVertex))) == 0)
00554 {
00555 printf("Error reallocating mem\n");
00556 exit(-1);
00557 }
00558 }
00559 else
00560 if((VerticeCoord = (mslGLVertex *) realloc(VerticeCoord,
00561 sizeof(mslGLVertex)*(NumberOfPoint+1))) == 0)
00562 {
00563 printf("Error reallocating mem\n");
00564 exit(-1);
00565 }
00566
00567 VerticeCoord[NumberOfPoint].x = ver.x;
00568 VerticeCoord[NumberOfPoint].y = ver.y;
00569 VerticeCoord[NumberOfPoint].z = ver.z;
00570
00571 NumberOfPoint ++;
00572 }
00573
00574 void mslGLFace::AddNormal(const mslGLNormal& nor)
00575 {
00576 if(NormalCoord == NULL)
00577 {
00578 if((NormalCoord = (mslGLNormal *) malloc(sizeof(mslGLNormal))) == 0)
00579 {
00580 printf("Error reallocating mem\n");
00581 exit(-1);
00582 }
00583 }
00584 else
00585 if((NormalCoord = (mslGLNormal *) realloc(NormalCoord,
00586 sizeof(mslGLNormal)*(NumberOfNormal+1))) == 0)
00587 {
00588 printf("Error reallocating mem\n");
00589 exit(-1);
00590 }
00591
00592 NormalCoord[NumberOfNormal].x = nor.x;
00593 NormalCoord[NumberOfNormal].y = nor.y;
00594 NormalCoord[NumberOfNormal].z = nor.z;
00595
00596 NumberOfNormal ++;
00597 }
00598
00599 void mslGLFace::AddTexCoord(const mslGLTexCoord& tex)
00600 {
00601 if(TextureCoord == NULL)
00602 {
00603 if((TextureCoord = (mslGLTexCoord *) malloc(sizeof(mslGLTexCoord))) == 0)
00604 {
00605 printf("Error reallocating mem\n");
00606 exit(-1);
00607 }
00608 }
00609 else
00610 if((TextureCoord = (mslGLTexCoord *) realloc(TextureCoord,
00611 sizeof(mslGLTexCoord)*(NumberOfTexCoord+1))) == 0)
00612 {
00613 printf("Error reallocating mem\n");
00614 exit(-1);
00615 }
00616
00617 TextureCoord[NumberOfTexCoord].x = tex.x;
00618 TextureCoord[NumberOfTexCoord++].y = tex.y;
00619 }
00620
00621 void mslGLFace::AddVertex(const MSLVector& ver)
00622 {
00623 void *mem;
00624
00625 if((mem = realloc(VerticeCoord,
00626 sizeof(mslGLVertex)*(NumberOfPoint+1))) == 0) {
00627 printf("Error reallocating mem\n");
00628 exit(-1);
00629 }
00630
00631 VerticeCoord=(mslGLVertex *) mem;
00632
00633 VerticeCoord[NumberOfPoint].x = ver[0];
00634 VerticeCoord[NumberOfPoint].y = ver[1];
00635 VerticeCoord[NumberOfPoint].z = ver[2];
00636
00637 NumberOfPoint++;
00638 }
00639
00640 void mslGLFace::AddNormal(const MSLVector& nor)
00641 {
00642 void *mem;
00643
00644 if((mem = realloc(NormalCoord,
00645 sizeof(mslGLNormal)*(NumberOfNormal+1))) == 0) {
00646 printf("Error reallocating mem\n");
00647 exit(-1);
00648 }
00649
00650 NormalCoord=(mslGLNormal *) mem;
00651
00652 NormalCoord[NumberOfNormal].x = nor[0];
00653 NormalCoord[NumberOfNormal].y = nor[1];
00654 NormalCoord[NumberOfNormal].z = nor[2];
00655
00656 NumberOfNormal ++;
00657 }
00658
00659 void mslGLFace::AddTexCoord(const MSLVector& tex)
00660 {
00661 void *mem;
00662
00663 if((mem = realloc(TextureCoord,
00664 sizeof(mslGLTexCoord)*(NumberOfTexCoord+1))) == 0) {
00665 printf("Error reallocating mem\n");
00666 exit(-1);
00667 }
00668
00669 TextureCoord=(mslGLTexCoord *) mem;
00670
00671 TextureCoord[NumberOfTexCoord].x = tex[0];
00672 TextureCoord[NumberOfTexCoord].y = tex[1];
00673
00674 NumberOfTexCoord++;
00675 }
00676
00677
00678 void mslGLFace::Clear()
00679 {
00680 NumberOfPoint = 0;
00681 NumberOfNormal = 0;
00682 NumberOfTexCoord = 0;
00683
00684 00685 00686 00687 00688 00689 00690 00691
00692
00693 VerticeCoord = NULL;
00694 NormalCoord = NULL;
00695 TextureCoord = NULL;
00696
00697 NormalOn = 1;
00698 TextureOn = 1;
00699 ColorOn = 1;
00700
00701 MaterialID = -1;
00702 }
00703
00704 void mslGLFace::PrintVertex()
00705 {
00706 int i;
00707 mslGLVertex v;
00708
00709 for(i=0; i<NumberOfPoint; i++)
00710 {
00711 v = VerticeCoord[i];
00712 cout << "(" << v.x << " " << v.y << " "<< v.z << ") ";
00713 }
00714
00715 cout << endl;
00716 }
00717
00718
00719 void mslGLFace::DrawFace()
00720 {
00721 int i;
00722
00723 mslGLVertex v1, v2, v3;
00724 mslGLNormal n1, n2, n3, n;
00725 mslGLTexCoord t1, t2, t3;
00726
00727 v1 = VerticeCoord[0];
00728 v2 = VerticeCoord[1];
00729
00730 if(NormalOn)
00731 {
00732 n1 = NormalCoord[0];
00733 n2 = NormalCoord[1];
00734 }
00735
00736 if(TextureOn)
00737 {
00738 t1 = TextureCoord[0];
00739 t2 = TextureCoord[1];
00740 }
00741
00742 glBegin(GL_TRIANGLES);
00743
00744 for(i=0; i<NumberOfPoint-2; i++)
00745 {
00746 v3 = VerticeCoord[i+2];
00747 if(NormalOn) n3 = NormalCoord[i+2];
00748 if(TextureOn) t3 = TextureCoord[i+2];
00749
00750 if((float)i/2.0==0)
00751 {
00752 if(NormalOn)
00753 {
00754 glNormal3f(n1.x, n1.y, n1.z);
00755 if(TextureOn) glTexCoord2f(t1.x, t1.y);
00756 glVertex3f(v1.x, v1.y, v1.z);
00757
00758 glNormal3f(n2.x, n2.y, n2.z);
00759 if(TextureOn) glTexCoord2f(t2.x, t2.y);
00760 glVertex3f(v2.x, v2.y, v2.z);
00761
00762 glNormal3f(n3.x, n3.y, n3.z);
00763 if(TextureOn) glTexCoord2f(t3.x, t3.y);
00764 glVertex3f(v3.x, v3.y, v3.z);
00765 }
00766 else
00767 {
00768 n = NormalCompute(v1, v2, v3);
00769 glNormal3f(n.x, n.y, n.z);
00770
00771 if(TextureOn) glTexCoord2f(t1.x, t1.y);
00772 glVertex3f(v1.x, v1.y, v1.z);
00773 if(TextureOn) glTexCoord2f(t2.x, t2.y);
00774 glVertex3f(v2.x, v2.y, v2.z);
00775 if(TextureOn) glTexCoord2f(t3.x, t3.y);
00776 glVertex3f(v3.x, v3.y, v3.z);
00777 }
00778 }
00779 else
00780 {
00781 if(NormalOn)
00782 {
00783 glNormal3f(n2.x, n2.y, n2.z);
00784 if(TextureOn) glTexCoord2f(t2.x, t2.y);
00785 glVertex3f(v2.x, v2.y, v2.z);
00786
00787 glNormal3f(n1.x, n1.y, n1.z);
00788 if(TextureOn) glTexCoord2f(t1.x, t1.y);
00789 glVertex3f(v1.x, v1.y, v1.z);
00790
00791 glNormal3f(n3.x, n3.y, n3.z);
00792 if(TextureOn) glTexCoord2f(t3.x, t3.y);
00793 glVertex3f(v3.x, v3.y, v3.z);
00794 }
00795 else
00796 {
00797 n = NormalCompute(v2, v1, v3);
00798 glNormal3f(n.x, n.y, n.z);
00799
00800 if(TextureOn) glTexCoord2f(t2.x, t2.y);
00801 glVertex3f(v2.x, v2.y, v2.z);
00802 if(TextureOn) glTexCoord2f(t1.x, t1.y);
00803 glVertex3f(v1.x, v1.y, v1.z);
00804 if(TextureOn) glTexCoord2f(t3.x, t3.y);
00805 glVertex3f(v3.x, v3.y, v3.z);
00806 }
00807 }
00808
00809 v1 = v2; v2 = v3;
00810 n1 = n2; n2 = n3;
00811 t1 = t2; t2 = t3;
00812 }
00813
00814 glEnd();
00815 }
00816
00817
00818 mslGLObject::mslGLObject()
00819 {
00820
00821
00822 ObjectMaterialLib = NULL;
00823 ObjectFaceLib = NULL;
00824
00825 NumberOfMaterial = 0;
00826 NumberOfFace = 0;
00827
00828 BoundingBoxMin[0] = 0.0;
00829 BoundingBoxMin[1] = 0.0;
00830 BoundingBoxMin[2] = 0.0;
00831
00832 BoundingBoxMax[0] = 0.0;
00833 BoundingBoxMax[1] = 0.0;
00834 BoundingBoxMax[2] = 0.0;
00835 }
00836
00837
00838 mslGLObject::~mslGLObject()
00839 {
00840 }
00841
00842
00843 void mslGLObject::Clear()
00844 {
00845
00846 ObjectMaterialLib = NULL;
00847 ObjectFaceLib = NULL;
00848
00849 NumberOfMaterial = 0;
00850 NumberOfFace = 0;
00851
00852 BoundingBoxMin[0] = 0.0;
00853 BoundingBoxMin[1] = 0.0;
00854 BoundingBoxMin[2] = 0.0;
00855
00856 BoundingBoxMax[0] = 0.0;
00857 BoundingBoxMax[1] = 0.0;
00858 BoundingBoxMax[2] = 0.0;
00859 }
00860
00861 int mslGLObject::ReadModelFile(const string& path, const string& fileName)
00862 {
00863 FILE *objFile;
00864
00865 int currentMaterialID = -1;
00866
00867 char buffer[BUFFER_SIZE];
00868 char token[BUFFER_SIZE];
00869 char *next = NULL;
00870 char *backslash = NULL;
00871
00872 int width = 0;
00873
00874 int numTris = 0;
00875 int numPolys = 0;
00876 int numGroups = 0;
00877
00878 mslGLVertex* v = NULL;
00879 unsigned int vCount = 0;
00880 unsigned int vAvailable = 0;
00881
00882 mslGLNormal* n = NULL;
00883 unsigned int nCount = 0;
00884 unsigned int nAvailable = 0;
00885
00886 mslGLTexCoord* t = NULL;
00887 unsigned int tCount = 0;
00888 unsigned int tAvailable = 0;
00889
00890
00891 int i, j;
00892 float f1, f2, f3;
00893 MSLVector tv1(3), tv2(2), tv3(9);
00894
00895 Position[0] = 0.0;
00896 Position[1] = 0.0;
00897 Position[2] = 0.0;
00898
00899 Orientation[0] = 0.0;
00900 Orientation[1] = 0.0;
00901 Orientation[2] = 0.0;
00902
00903 Scale[0] = 1.0;
00904 Scale[1] = 1.0;
00905 Scale[2] = 1.0;
00906
00907 if ((objFile = fopen((path + fileName).c_str(), "r")) == NULL)
00908 {
00909 cout << "can not open model file!" << endl;
00910 return 1;
00911 }
00912
00913 Name = fileName;
00914
00915 while (fgets(buffer, BUFFER_SIZE, objFile) != NULL)
00916 {
00917
00918 while ((backslash = strchr(buffer, '\\')) != NULL)
00919 {
00920
00921 *backslash++ = ' ';
00922 *backslash = '\0';
00923
00924
00925 if (fgets(backslash, (int)(BUFFER_SIZE - strlen(buffer)), objFile)
00926 == NULL)
00927 break;
00928 }
00929
00930
00931 for (next = buffer; *next != '\0' && isspace(*next); next++)
00932 {};
00933
00934
00935 if (*next == '\0' || *next == '#' || *next == '!' || *next == '$')
00936 continue;
00937
00938
00939 sscanf(next, "%s%n", token, &width);
00940 next += width;
00941
00942 if (SAME(token, "v"))
00943 {
00944 sscanf(next, "%f %f %f", &f1, &f2, &f3);
00945
00946 GROW(v, mslGLVertex);
00947 if(v == 0)
00948 {
00949 cout << "memeory allocation error!" << endl;
00950 exit(-1);
00951 }
00952
00953 v[vCount].x = f1;
00954 v[vCount].y = f2;
00955 v[vCount].z = f3;
00956
00957 ++vCount;
00958 }
00959 else
00960 if (SAME(token, "vn"))
00961 {
00962 sscanf(next, "%f %f %f", &f1, &f2, &f3);
00963
00964 GROW(n, mslGLNormal);
00965 if(n == 0)
00966 {
00967 cout << "memeory allocation error!" << endl;
00968 exit(-1);
00969 }
00970
00971 n[nCount].x = f1;
00972 n[nCount].y = f2;
00973 n[nCount].z = f3;
00974
00975 ++nCount;
00976 }
00977 else
00978 if (SAME(token, "vt"))
00979 {
00980 sscanf(next, "%f %f", &f1, &f2);
00981
00982 GROW(t, mslGLTexCoord);
00983 if(t == 0)
00984 {
00985 cout << "memeory allocation error!" << endl;
00986 exit(-1);
00987 }
00988
00989 t[tCount].x = f1;
00990 t[tCount].y = f2;
00991
00992 ++tCount;
00993 }
00994 else
00995 if (SAME(token, "g"))
00996 {
00997 ++numGroups;
00998 }
00999 else
01000 if (SAME(token, "f") ||
01001 SAME(token, "fo"))
01002 {
01003 int count;
01004 int textureValid = 1;
01005 int normalsValid = 1;
01006 int vi[FACE_SIZE];
01007 int ti[FACE_SIZE];
01008 int ni[FACE_SIZE];
01009
01010 char *slash;
01011 char vertexData[256];
01012
01013 mslGLFace tFace;
01014
01015 for (count = 0; count < FACE_SIZE; count++)
01016 {
01017 if (sscanf(next, "%s%n", vertexData, &width) != 1)
01018 break;
01019 next += width;
01020 vi[count] = (int)strtol(vertexData, NULL, 10);
01021 ti[count] = 0;
01022 if ((slash = strchr(vertexData, '/')) == NULL ||
01023 (ti[count] = (int)strtol(slash+1, NULL, 10)) == 0)
01024 textureValid = 0;
01025 ni[count] = 0;
01026 if (slash == NULL || (slash = strchr(slash+1, '/')) == NULL ||
01027 (ni[count] = (int)strtol(slash+1, NULL, 10)) == 0)
01028 normalsValid = 0;
01029
01030 01031 01032 01033 01034
01035 if (vi[count] >= 0)
01036 vi[count] -= 1;
01037 else
01038 vi[count] = vCount - vi[count];
01039
01040 if (ti[count] >= 0)
01041 ti[count] -= 1;
01042 else
01043 ti[count] = tCount - ti[count];
01044
01045 if (ni[count] >= 0)
01046 ni[count] -= 1;
01047 else
01048 ni[count] = nCount - ni[count];
01049 }
01050
01051 for (i = 0; i < count; i++)
01052 {
01053 tFace.AddVertex(v[vi[i]]);
01054 }
01055
01056 tFace.ColorOn = 0;
01057 tFace.MaterialID = currentMaterialID;
01058
01059
01060 if (normalsValid)
01061 {
01062 tFace.NormalOn = 1;
01063 for (i = 0; i < count; i++)
01064 tFace.AddNormal(n[ni[i]]);
01065 }
01066 else
01067 tFace.NormalOn = 0;
01068
01069 if (textureValid)
01070 {
01071 tFace.TextureOn = 1;
01072 for (i = 0; i < count; i++)
01073 tFace.AddTexCoord(t[ti[i]]);
01074 }
01075 else
01076 tFace.TextureOn = 0;
01077
01078 if (count > 2)
01079 {
01080 tFace.NumberOfPoint = count;
01081 AddFace(tFace);
01082 numTris += count - 2;
01083 numPolys++;
01084
01085 }
01086 }
01087 else
01088 if (SAME(token, "usemtl"))
01089 {
01090 char mtlName[MAXNAME_LENGTH];
01091 sscanf(next, "%s", mtlName);
01092 currentMaterialID = SetCurrentMaterialID(mtlName);
01093 }
01094 else
01095 if (SAME(token, "mtllib"))
01096 {
01097 char libName[MAXNAME_LENGTH];
01098 sscanf(next, "%s", libName);
01099 LoadMaterialFile(path, libName);
01100 }
01101 else
01102 if (
01103 SAME(token, "bevel") ||
01104 SAME(token, "bmat") ||
01105 SAME(token, "bsp") ||
01106 SAME(token, "bzp") ||
01107 SAME(token, "c_interp") ||
01108 SAME(token, "cdc") ||
01109 SAME(token, "con") ||
01110 SAME(token, "cstype") ||
01111 SAME(token, "ctech") ||
01112 SAME(token, "curv") ||
01113 SAME(token, "curv2") ||
01114 SAME(token, "d_interp") ||
01115 SAME(token, "deg") ||
01116 SAME(token, "end") ||
01117 SAME(token, "hole") ||
01118 SAME(token, "l") ||
01119 SAME(token, "lod") ||
01120 SAME(token, "maplib") ||
01121 SAME(token, "mg") ||
01122 SAME(token, "o") ||
01123 SAME(token, "p") ||
01124 SAME(token, "param") ||
01125 SAME(token, "parm") ||
01126 SAME(token, "res") ||
01127 SAME(token, "s") ||
01128 SAME(token, "scrv") ||
01129 SAME(token, "shadow_obj") ||
01130 SAME(token, "sp") ||
01131 SAME(token, "stech") ||
01132 SAME(token, "step") ||
01133 SAME(token, "surf") ||
01134 SAME(token, "trace_obj") ||
01135 SAME(token, "trim") ||
01136 SAME(token, "usemap") ||
01137 SAME(token, "vp"))
01138 {
01139 ++numSkip;
01140 }
01141 #ifndef STRICT_OBJ_FORMAT
01142 01143 01144 01145
01146 else
01147 if (SAME(token, "RESET"))
01148 {
01149 vCount = 0;
01150 nCount = 0;
01151 tCount = 0;
01152 }
01153 #endif
01154 else
01155 {
01156 cout << "unrecognize format" << endl;
01157 ++numOther;
01158 }
01159 }
01160
01161
01162 fclose(objFile);
01163
01164 if (v != NULL) free(v);
01165 if (n != NULL) free(n);
01166 if (t != NULL) free(t);
01167
01168 for(i=0; i<NumberOfFace; i++)
01169 {
01170 for(j=0; j<ObjectFaceLib[i].NumberOfPoint; j++)
01171 {
01172 if(ObjectFaceLib[i].VerticeCoord[j].x>BoundingBoxMax[0])
01173 BoundingBoxMax[0] = ObjectFaceLib[i].VerticeCoord[j].x;
01174 if(ObjectFaceLib[i].VerticeCoord[j].y>BoundingBoxMax[1])
01175 BoundingBoxMax[1] = ObjectFaceLib[i].VerticeCoord[j].y;
01176 if(ObjectFaceLib[i].VerticeCoord[j].z>BoundingBoxMax[2])
01177 BoundingBoxMax[2] = ObjectFaceLib[i].VerticeCoord[j].z;
01178
01179 if(ObjectFaceLib[i].VerticeCoord[j].x<BoundingBoxMin[0])
01180 BoundingBoxMin[0] = ObjectFaceLib[i].VerticeCoord[j].x;
01181 if(ObjectFaceLib[i].VerticeCoord[j].y<BoundingBoxMin[1])
01182 BoundingBoxMin[1] = ObjectFaceLib[i].VerticeCoord[j].y;
01183 if(ObjectFaceLib[i].VerticeCoord[j].z<BoundingBoxMin[2])
01184 BoundingBoxMin[2] = ObjectFaceLib[i].VerticeCoord[j].z;
01185 }
01186 }
01187
01188 return 0;
01189 }
01190
01191
01192 MSLPoint3d mslGLObject::PointCurrentState(const MSLPoint3d& po, int mode)
01193 {
01194 MSLVector vp1(3);
01195
01196 vp1[0] = po.xcoord() * Scale[0];
01197 vp1[1] = po.ycoord() * Scale[1];
01198 vp1[2] = po.zcoord() * Scale[2];
01199
01200 vp1 = point_x_rotation(Orientation[0]*PI/180.0, vp1);
01201 vp1 = point_y_rotation(Orientation[1]*PI/180.0, vp1);
01202 vp1 = point_z_rotation(Orientation[2]*PI/180.0, vp1);
01203
01204 if(mode == OBSTACLE_MODE)
01205 {
01206 vp1[0] = vp1[0] + Position[0];
01207 vp1[1] = vp1[1] + Position[1];
01208 vp1[2] = vp1[2] + Position[2];
01209 }
01210
01211 return MSLPoint3d(vp1[0],vp1[1],vp1[2]);
01212 }
01213
01214
01215
01216 list<MSLTriangle> mslGLObject::SetBoundingBoxTriangle(int mode)
01217 {
01218 list<MSLTriangle> BoundingBoxTriangle;
01219
01220 BoundingBoxTriangle.clear();
01221 MSLPoint3d p1, p2, p3;
01222
01223 p1 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01224 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01225 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01226 p1 = PointCurrentState(p1, mode);
01227 p2 = PointCurrentState(p2, mode);
01228 p3 = PointCurrentState(p3, mode);
01229 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01230
01231 p1 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01232 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01233 p3 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01234 p1 = PointCurrentState(p1, mode);
01235 p2 = PointCurrentState(p2, mode);
01236 p3 = PointCurrentState(p3, mode);
01237 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01238
01239 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01240 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01241 p3 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01242 p1 = PointCurrentState(p1, mode);
01243 p2 = PointCurrentState(p2, mode);
01244 p3 = PointCurrentState(p3, mode);
01245 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01246
01247 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01248 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01249 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01250 p1 = PointCurrentState(p1, mode);
01251 p2 = PointCurrentState(p2, mode);
01252 p3 = PointCurrentState(p3, mode);
01253 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01254
01255 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01256 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01257 p3 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01258 p1 = PointCurrentState(p1, mode);
01259 p2 = PointCurrentState(p2, mode);
01260 p3 = PointCurrentState(p3, mode);
01261 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01262
01263 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01264 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01265 p3 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01266 p1 = PointCurrentState(p1, mode);
01267 p2 = PointCurrentState(p2, mode);
01268 p3 = PointCurrentState(p3, mode);
01269 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01270
01271 p1 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01272 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01273 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01274 p1 = PointCurrentState(p1, mode);
01275 p2 = PointCurrentState(p2, mode);
01276 p3 = PointCurrentState(p3, mode);
01277 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01278
01279 p1 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01280 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01281 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01282 p1 = PointCurrentState(p1, mode);
01283 p2 = PointCurrentState(p2, mode);
01284 p3 = PointCurrentState(p3, mode);
01285 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01286
01287 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01288 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01289 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01290 p1 = PointCurrentState(p1, mode);
01291 p2 = PointCurrentState(p2, mode);
01292 p3 = PointCurrentState(p3, mode);
01293 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01294
01295 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01296 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01297 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01298 p1 = PointCurrentState(p1, mode);
01299 p2 = PointCurrentState(p2, mode);
01300 p3 = PointCurrentState(p3, mode);
01301 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01302
01303 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01304 p2 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01305 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01306 p1 = PointCurrentState(p1, mode);
01307 p2 = PointCurrentState(p2, mode);
01308 p3 = PointCurrentState(p3, mode);
01309 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01310
01311 p1 = MSLPoint3d(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01312 p2 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01313 p3 = MSLPoint3d(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01314 p1 = PointCurrentState(p1, mode);
01315 p2 = PointCurrentState(p2, mode);
01316 p3 = PointCurrentState(p3, mode);
01317 BoundingBoxTriangle.push_front(MSLTriangle(p1, p2, p3));
01318
01319 return BoundingBoxTriangle;
01320 }
01321
01322
01323
01324 void mslGLObject::LoadMaterialFile(const string& path, const string& filename)
01325 {
01326 FILE *mtlFile;
01327 char buffer[BUFFER_SIZE];
01328 char token[BUFFER_SIZE];
01329 char *next;
01330 char *backslash;
01331 int width = 0;
01332
01333 int inProgress = 0;
01334
01335 mslGLMaterial tmat;
01336 list<string>::iterator tstr;
01337
01338
01339 forall(tstr, MaterialFileList)
01340 if (strcmp(filename.c_str(), (*tstr).c_str()) == 0)
01341 {
01342 return;
01343 }
01344
01345
01346 if (MaterialFileList.size() < MAX_MTL_FILES)
01347 {
01348 MaterialFileList.push_back(filename);
01349 }
01350 else
01351 {
01352 cout << "You have exceed the limit of the number of the material file" << endl;
01353 }
01354
01355
01356 if ((mtlFile = fopen((path + filename).c_str(), "r")) == NULL)
01357 {
01358 cout << "Can not open the material file" << endl;
01359 return;
01360 }
01361
01362
01363 while (fgets(buffer, BUFFER_SIZE, mtlFile) != NULL)
01364 {
01365 while ((backslash = strchr(buffer, '\\')) != NULL)
01366 if (fgets(backslash, BUFFER_SIZE - strlen(buffer), mtlFile) == NULL)
01367 break;
01368
01369 for (next = buffer; *next != '\0' && isspace(*next); next++)
01370 {};
01371 if (*next == '\0' || *next == '#' || *next == '!' || *next == '$')
01372 continue;
01373 sscanf(next, "%s%n", token, &width);
01374 next += width;
01375
01376
01377 if (SAME(token, "newmtl"))
01378 {
01379 if(inProgress)
01380 {
01381 AddMaterial(path, tmat);
01382
01383 }
01384
01385 tmat.Clear();
01386 inProgress = 1;
01387 sscanf(next, "%s", tmat.Name);
01388 }
01389 else
01390 if (SAME(token, "Ka"))
01391 {
01392 sscanf(next, "%f %f %f",
01393 &tmat.Ambient[0],
01394 &tmat.Ambient[1],
01395 &tmat.Ambient[2]);
01396 tmat.AmbientOn= 1;
01397 }
01398 else
01399 if (SAME(token, "Kd"))
01400 {
01401 sscanf(next, "%f %f %f",
01402 &tmat.Diffuse[0],
01403 &tmat.Diffuse[1],
01404 &tmat.Diffuse[2]);
01405 tmat.Color[0] = tmat.Diffuse[0];
01406 tmat.Color[1] = tmat.Diffuse[1];
01407 tmat.Color[2] = tmat.Diffuse[2];
01408 tmat.DiffuseOn = 1;
01409 }
01410 else
01411 if (SAME(token, "Ks"))
01412 {
01413 sscanf(next, "%f %f %f",
01414 &tmat.Specular[0],
01415 &tmat.Specular[1],
01416 &tmat.Specular[2]);
01417 tmat.SpecularOn = 1;
01418 }
01419 else
01420 if (SAME(token, "Tr"))
01421 {
01422 float alpha = 1.0f;
01423 sscanf(next, "%f", &alpha);
01424 tmat.Alpha = 1.0f - alpha;
01425 tmat.AlphaOn = 1;
01426 }
01427 else
01428 if (SAME(token, "Ns"))
01429 {
01430 sscanf(next, "%f", &tmat.Shininess);
01431
01432
01433 if (tmat.Shininess < 0.0f)
01434 tmat.Shininess = 0.0f;
01435 else
01436 if (tmat.Shininess > 128.0f)
01437 tmat.Shininess = 128.0f;
01438
01439 tmat.ShininessOn = 1;
01440 }
01441 else
01442 if (SAME(token, "map_Kd"))
01443 {
01444 ParseTexture(next, &tmat);
01445 tmat.TextureOn = 1;
01446 }
01447 else
01448 if (SAME(token, "refl"))
01449 {
01450 strcpy(tmat.Reflect, "sphere");
01451 ParseTexture(next, &tmat);
01452 tmat.ReflectOn = 1;
01453 }
01454 else
01455 if (
01456 SAME(token, "Ni") ||
01457 SAME(token, "Tf") ||
01458 SAME(token, "bump") ||
01459 SAME(token, "d") ||
01460 SAME(token, "decal") ||
01461 SAME(token, "illum") ||
01462 SAME(token, "map_Ka") ||
01463 SAME(token, "map_Ks") ||
01464 SAME(token, "map_Ns") ||
01465 SAME(token, "map_d") ||
01466 SAME(token, "sharpness") ||
01467 SAME(token, "vp"))
01468 {
01469 numSkip++;
01470 }
01471 #ifndef STRICT_OBJ_FORMAT
01472 01473 01474
01475 else
01476 if (SAME(token, "TWOSIDE"))
01477 {
01478 tmat.TwosideOn = 1;
01479 }
01480 #endif
01481 else
01482 {
01483 cout << "unrecognized format" << endl;
01484 numOther++;
01485 }
01486 }
01487
01488 if (inProgress)
01489 AddMaterial(path, tmat);
01490
01491
01492 fclose(mtlFile);
01493 }
01494
01495
01496
01497 int mslGLObject::SetCurrentMaterialID(char * name)
01498 {
01499 int i, id;
01500
01501 id = -1;
01502 for(i=0; i<NumberOfMaterial; i++)
01503 {
01504 if(strcmp(ObjectMaterialLib[i].Name, name) == 0)
01505 {
01506 id = ObjectMaterialLib[i].ID;
01507 }
01508 }
01509
01510 return id;
01511
01512 }
01513
01514
01515 void mslGLObject::AddMaterial(const string& path, const mslGLMaterial& mat)
01516 {
01517 if(ObjectMaterialLib == NULL)
01518 {
01519 if((ObjectMaterialLib = (mslGLMaterial *) malloc(sizeof(mslGLMaterial))) == 0)
01520 {
01521 printf("Error reallocating mem\n");
01522 exit(-1);
01523 }
01524 }
01525 else
01526 if((ObjectMaterialLib =
01527 (mslGLMaterial *) realloc(ObjectMaterialLib,
01528 sizeof(mslGLMaterial)*(NumberOfMaterial+1))) == 0)
01529 {
01530 printf("Error reallocating mem\n");
01531 exit(-1);
01532 }
01533
01534 ObjectMaterialLib[NumberOfMaterial] = mat;
01535
01536 ObjectMaterialLib[NumberOfMaterial].ID = NumberOfMaterial;
01537 ObjectMaterialLib[NumberOfMaterial].Shininess = mat.Shininess;
01538 if(mat.TextureOn)
01539 ObjectMaterialLib[NumberOfMaterial].ImageLoad(NumberOfMaterial, path, mat.TextureName);
01540
01541
01542
01543 NumberOfMaterial++;
01544 }
01545
01546
01547 void mslGLObject::AddFace(const mslGLFace& face)
01548 {
01549 if(ObjectFaceLib == NULL)
01550 {
01551 if((ObjectFaceLib = (mslGLFace *) malloc(sizeof(mslGLFace))) == 0)
01552 {
01553 printf("Error reallocating mem\n");
01554 exit(-1);
01555 }
01556 }
01557 else
01558 if((ObjectFaceLib = (mslGLFace *) realloc(ObjectFaceLib,
01559 sizeof(mslGLFace)*(NumberOfFace+1))) == 0) {
01560 printf("Error reallocating mem\n");
01561 exit(-1);
01562 }
01563
01564 ObjectFaceLib[NumberOfFace] = face;
01565
01566 NumberOfFace++;
01567 }
01568
01569
01570 01571 01572
01573 void mslGLObject::ParseTexture (char *next, mslGLMaterial *m)
01574 {
01575 int width = 0;
01576
01577
01578 m->Su = 1.0f;
01579 m->Sv = 1.0f;
01580
01581
01582 sscanf(next, "%s%n", m->TextureName, &width);
01583
01584
01585 do
01586 {
01587 next += width;
01588
01589 if (strcmp(m->TextureName, "-mm") == 0)
01590 sscanf(next, "%f %f %s%n", &m->Su, &m->Sv, m->TextureName, &width);
01591 else
01592 if (strcmp(m->TextureName, "-s") == 0)
01593 sscanf(next, "%f %f %*f %s%n", &m->Su, &m->Sv, m->TextureName, &width);
01594 else
01595 if (strcmp(m->TextureName, "-t") == 0)
01596 sscanf(next, "%f %f %*f %s%n", &m->Su, &m->Sv, m->TextureName, &width);
01597 else
01598 if (strcmp(m->TextureName, "-type") == 0)
01599 sscanf(next, "%s %s%n", m->Reflect, m->TextureName, &width);
01600 else
01601 break;
01602 } while (1);
01603 }
01604
01605
01606 void mslGLObject::ObjectDraw()
01607 {
01608 int i;
01609 int currentmat;
01610
01611 glPushMatrix();
01612
01613 glTranslatef(Position[0], Position[1], Position[2]);
01614 glRotatef(Orientation[2], 1.0, 0.0, 0.0);
01615 glRotatef(Orientation[1], 0.0, 1.0, 0.0);
01616 glRotatef(Orientation[0], 0.0, 0.0, 1.0);
01617 glScalef(Scale[0], Scale[1], Scale[2]);
01618
01619 currentmat = ObjectFaceLib[0].MaterialID;
01620 if(currentmat != -1)
01621 SetMaterial(currentmat);
01622
01623 for(i=0; i<NumberOfFace; i++)
01624 {
01625 if(currentmat != ObjectFaceLib[i].MaterialID)
01626 {
01627 currentmat = ObjectFaceLib[i].MaterialID;
01628 if(currentmat != -1)
01629 SetMaterial(currentmat);
01630 }
01631
01632 ObjectFaceLib[i].DrawFace();
01633 }
01634
01635 glPopMatrix();
01636 }
01637
01638
01639
01640
01641 void mslGLObject::ObjectBoundingBoxDraw()
01642 {
01643
01644 glDisable(GL_TEXTURE_2D);
01645 glDisable(GL_LIGHTING);
01646
01647 GLfloat Diffuse[] = {1.0, 0.0, 0.0};
01648 GLfloat Ambient[] = {1.0, 0.0, 0.0};
01649 GLfloat Specular[] = {1.0, 0.0, 0.0};
01650 GLfloat Shininess[] = {1.0, 0.0, 0.0};
01651
01652 glPushMatrix();
01653
01654 glTranslatef(Position[0], Position[1], Position[2]);
01655 glRotatef(Orientation[2], 1.0, 0.0, 0.0);
01656 glRotatef(Orientation[1], 0.0, 1.0, 0.0);
01657 glRotatef(Orientation[0], 0.0, 0.0, 1.0);
01658 glScalef(Scale[0], Scale[1], Scale[2]);
01659
01660 glLineWidth(0.2);
01661
01662 glColor4f(1.0, 0.0, 1.0, 1.0);
01663 glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
01664 glMaterialfv(GL_FRONT, GL_AMBIENT, Ambient);
01665 glMaterialfv(GL_FRONT, GL_SPECULAR, Specular);
01666 glMaterialfv(GL_FRONT, GL_SHININESS, Shininess);
01667
01668 glBegin(GL_LINES);
01669
01670 glNormal3f(-1.0, -1.0, 1.0);
01671 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01672 glNormal3f(1.0, -1.0, 1.0);
01673 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01674
01675 glNormal3f(-1.0, -1.0, 1.0);
01676 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01677 glNormal3f(-1.0, 1.0, 1.0);
01678 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01679
01680 glNormal3f(1.0, 1.0, 1.0);
01681 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01682 glNormal3f(-1.0, 1.0, 1.0);
01683 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01684
01685 glNormal3f(1.0, 1.0, 1.0);
01686 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01687 glNormal3f(1.0, -1.0, 1.0);
01688 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01689
01690
01691
01692 glNormal3f(1.0, 1.0, -1.0);
01693 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01694 glNormal3f(-1.0, 1.0, -1.0);
01695 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01696
01697 glNormal3f(1.0, 1.0, -1.0);
01698 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01699 glNormal3f(1.0, -1.0, -1.0);
01700 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01701
01702 glNormal3f(-1.0, -1.0, -1.0);
01703 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01704 glNormal3f(1.0, -1.0, -1.0);
01705 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01706
01707 glNormal3f(-1.0, -1.0, -1.0);
01708 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01709 glNormal3f(-1.0, 1.0, -1.0);
01710 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01711
01712
01713
01714 glNormal3f(1.0, -1.0, 1.0);
01715 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01716 glNormal3f(1.0, -1.0, -1.0);
01717 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01718
01719 glNormal3f(1.0, 1.0, -1.0);
01720 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01721 glNormal3f(1.0, 1.0, 1.0);
01722 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01723
01724 glNormal3f(-1.0, 1.0, 1.0);
01725 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01726 glNormal3f(-1.0, 1.0, -1.0);
01727 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01728
01729 glNormal3f(-1.0, -1.0, 1.0);
01730 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01731 glNormal3f(-1.0, -1.0, 1.0);
01732 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01733
01734 glEnd();
01735
01736 glPopMatrix();
01737
01738 glLineWidth(1.0);
01739
01740 glEnable(GL_TEXTURE_2D);
01741 glEnable(GL_LIGHTING);
01742 }
01743
01744
01745 void mslGLObject::SetMaterial(int matindex)
01746 {
01747 ObjectMaterialLib[matindex].SetMaterial();
01748
01749
01750 }
01751
01752
01753
01754 void mslGLObject::ObjectHighlight()
01755 {
01756
01757 glDisable(GL_TEXTURE_2D);
01758 glDisable(GL_LIGHTING);
01759
01760 GLfloat Diffuse[] = {1.0, 0.0, 0.0};
01761 GLfloat Ambient[] = {1.0, 0.0, 0.0};
01762 GLfloat Specular[] = {1.0, 0.0, 0.0};
01763 GLfloat Shininess[] = {1.0, 0.0, 0.0};
01764
01765 glPushMatrix();
01766
01767 glTranslatef(Position[0], Position[1], Position[2]);
01768 glRotatef(Orientation[2], 1.0, 0.0, 0.0);
01769 glRotatef(Orientation[1], 0.0, 1.0, 0.0);
01770 glRotatef(Orientation[0], 0.0, 0.0, 1.0);
01771 glScalef(Scale[0], Scale[1], Scale[2]);
01772
01773 glLineWidth(0.5);
01774
01775 glColor4f(1.0, 1.0, 0.0, 1.0);
01776 glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
01777 glMaterialfv(GL_FRONT, GL_AMBIENT, Ambient);
01778 glMaterialfv(GL_FRONT, GL_SPECULAR, Specular);
01779 glMaterialfv(GL_FRONT, GL_SHININESS, Shininess);
01780
01781 glBegin(GL_LINES);
01782
01783 glNormal3f(-1.0, -1.0, 1.0);
01784 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01785 glNormal3f(1.0, -1.0, 1.0);
01786 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01787
01788 glNormal3f(-1.0, -1.0, 1.0);
01789 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01790 glNormal3f(-1.0, 1.0, 1.0);
01791 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01792
01793 glNormal3f(1.0, 1.0, 1.0);
01794 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01795 glNormal3f(-1.0, 1.0, 1.0);
01796 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01797
01798 glNormal3f(1.0, 1.0, 1.0);
01799 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01800 glNormal3f(1.0, -1.0, 1.0);
01801 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01802
01803
01804
01805 glNormal3f(1.0, 1.0, -1.0);
01806 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01807 glNormal3f(-1.0, 1.0, -1.0);
01808 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01809
01810 glNormal3f(1.0, 1.0, -1.0);
01811 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01812 glNormal3f(1.0, -1.0, -1.0);
01813 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01814
01815 glNormal3f(-1.0, -1.0, -1.0);
01816 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01817 glNormal3f(1.0, -1.0, -1.0);
01818 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01819
01820 glNormal3f(-1.0, -1.0, -1.0);
01821 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01822 glNormal3f(-1.0, 1.0, -1.0);
01823 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01824
01825
01826
01827 glNormal3f(1.0, -1.0, 1.0);
01828 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01829 glNormal3f(1.0, -1.0, -1.0);
01830 glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01831
01832 glNormal3f(1.0, 1.0, -1.0);
01833 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01834 glNormal3f(1.0, 1.0, 1.0);
01835 glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01836
01837 glNormal3f(-1.0, 1.0, 1.0);
01838 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01839 glNormal3f(-1.0, 1.0, -1.0);
01840 glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01841
01842 glNormal3f(-1.0, -1.0, 1.0);
01843 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01844 glNormal3f(-1.0, -1.0, 1.0);
01845 glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01846
01847 glEnd();
01848
01849 glPopMatrix();
01850
01851 glLineWidth(1.0);
01852
01853 glEnable(GL_TEXTURE_2D);
01854 glEnable(GL_LIGHTING);
01855 }
01856
01857 void mslGLObject::SetObjectPosition(const MSLVector& pos)
01858 {
01859 Position[0] = pos[0];
01860 Position[1] = pos[1];
01861 Position[2] = pos[2];
01862 }
01863
01864 void mslGLObject::SetObjectOrientation(const MSLVector& ori)
01865 {
01866 Orientation[0] = ori[0];
01867 Orientation[1] = ori[1];
01868 Orientation[2] = ori[2];
01869 }
01870
01871 void mslGLObject::SetObjectScale(const MSLVector& sca)
01872 {
01873 Scale[0] = sca[0];
01874 Scale[1] = sca[1];
01875 Scale[2] = sca[2];
01876 }
01877
01878
01879 void mslGLObject::SetBodyPositionChange(const MSLVector& posc)
01880 {
01881 Position[0] = Position[0] + posc[0];
01882 Position[1] = Position[1] + posc[1];
01883 Position[2] = Position[2] + posc[2];
01884 }
01885
01886
01887 void mslGLObject::SetBodyOrientationChange(const MSLVector& oric)
01888 {
01889 Orientation[0] = Orientation[0] + oric[0];
01890 Orientation[1] = Orientation[1] + oric[1];
01891 Orientation[2] = Orientation[2] + oric[2];
01892
01893 for(int i=0; i<3; i++)
01894 {
01895 while(Orientation[i]>=2*PI) Orientation[i] = Orientation[i] - 360.0;
01896 while(Orientation[i]<0) Orientation[i] = Orientation[i] + 360.0;
01897 }
01898 }
01899
01900 void mslGLObject::SetBodyScaleChange(const MSLVector& scac)
01901 {
01902 Scale[0] = Scale[0] * scac[0];
01903 Scale[1] = Scale[1] * scac[1];
01904 Scale[2] = Scale[2] * scac[2];
01905 }
01906
01907
01908 void mslGLObject::PrintFace()
01909 {
01910 int i;
01911
01912 cout << "Name of Object: " << Name << endl;
01913
01914 cout << "NumberOfFace: " << NumberOfFace << endl;
01915
01916 for(i=0; i<NumberOfFace; i++)
01917 {
01918 cout << "face " << i << ": ";
01919 ObjectFaceLib[i].PrintVertex();
01920 }
01921 }
01922
01923 void mslGLObject::PrintMaterial()
01924 {
01925 int i;
01926
01927 for(i=0; i<NumberOfMaterial; i++)
01928 ObjectMaterialLib[i].Print();
01929 }
01930
01931
01932 void mslGLObject::PrintState()
01933 {
01934 cout << "Position: " << Position[0]
01935 << " " << Position[1]
01936 << " " << Position[2]
01937 << endl;
01938
01939 cout << "Orientation: " << Orientation[0]
01940 << " " << Orientation[1]
01941 << " " << Orientation[2]
01942 << endl;
01943
01944 cout << "Scale: " << Scale[0]
01945 << " " << Scale[1]
01946 << " " << Scale[2]
01947 << endl;
01948
01949 }
01950
01951
01952
01953 mslGLNormal NormalCompute(const mslGLVertex& v1, const mslGLVertex& v2,
01954 const mslGLVertex& v3)
01955 {
01956 mslGLNormal n;
01957 MSLVector vv1(3), vv2(3), vv(3);
01958
01959 vv1[0] = v2.x - v1.x;
01960 vv1[1] = v2.y - v1.y;
01961 vv1[2] = v2.z - v1.z;
01962
01963 vv2[0] = v3.x - v1.x;
01964 vv2[1] = v3.y - v1.y;
01965 vv2[2] = v3.z - v1.z;
01966
01967 crossproduct(vv1, vv2, vv);
01968
01969 n.x = vv[0];
01970 n.y = vv[1];
01971 n.z = vv[2];
01972
01973 return n;
01974 }
01975
01976
01977
01978