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