Main Page | Namespace List | Class List | File List | Class Members | File Members

scene.cpp

Go to the documentation of this file.
00001 #ifndef __SCENE_CPP__
00002 #define __SCENE_CPP__
00003 
00004 #include <GL/glu.h>
00005 #include <string>
00006 #include "scene.h"
00007 #include "config.h"
00008 #include "heightfield.h"
00009 #include <stdio.h>
00010 #include <math.h>
00011 #include <iostream>
00012 #include "texture.h"
00013 
00014 #include <errno.h>
00015 //#include <string>
00016 
00017 #include "airplane.h"
00018 
00019 
00020 
00021 std::string model_name[MODEL_N] =
00022     { "gallows",
00023       "gallows_rope",
00024         "iron_maiden",
00025         "crypt",
00026         "diablo",
00027         "casket_closed",
00028         "casket_opened",
00029         "casket_opened2",
00030         "giljotina1(derevo)",
00031         "giljotina2(metal)",
00032         "giljotina3(chelovek)",
00033         "giljotina4(korzina)",
00034         "graveyard",
00035         "mancage_1(derevo)",
00036         "mancage_2(metal)",
00037         "prosto-chelovek",
00038         
00039     };
00040         
00041 bool model_in_use[MODEL_N] = 
00042     {   true,//gallows
00043         true,//rope
00044         true,//iron_maiden
00045         true,//crypt
00046         false,//diablo
00047         true,//casket_closed
00048         false,
00049         false,
00050         true,//giljotina_1
00051         true,//-2
00052         true,//-3
00053         true,//-4
00054         true, //graveyard
00055         true,//mancage-1
00056         true, //-2
00057         false, //player
00058         
00059     };
00060 
00061     
00062 unsigned int model_index[MODEL_N];
00063 bool   model_use_list[MODEL_N];
00064 int    model_handle[MODEL_N];
00065 
00066         
00067 void normalize ( double v[3])
00068 {
00069 double d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
00070 if (d != 0.0) 
00071     {
00072     v[0] /= d;
00073     v[1] /= d;
00074     v[2] /= d;
00075     }
00076 else
00077     printf(" zero normal?! hmm, strange...\n");
00078 
00079 //printf(" len of normal is %f \n", sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2] ) );
00080 }
00081 
00082                 
00083 Point3Struct operator+(Point3Struct p1, Point3Struct p2)
00084 {
00085 Point3 pp;
00086 pp.x = p1.x + p2.x;
00087 pp.y = p1.y + p2.y;
00088 pp.z = p1.z + p2.z;
00089 return(pp);
00090 }
00091 
00092 struct Model_partStruct
00093         {
00094         int nfaces;
00095         int ind_faces;
00096         int nvertices;
00097         int ind_vertices;
00098         } Model_part;
00099 
00100 struct ModelStruct
00101         {
00102         int nmodel_parts;
00103         int ind_model_parts;
00104         } Model;
00105 
00106 static void *Model_parts;
00107 static int model_parts_size = 0;
00108 static void *Models;
00109 static int models_size = 0;
00110 
00111 //int ind_gallows;              
00112 
00113 static void  *Vertices;
00114 static int vertices_size = 0;
00115 static void *Normals;
00116 static void *Faces;
00117 static int faces_size = 0;
00118 
00119 
00120 int addModel (std::string );
00121 
00122 void deleteModels();
00123 
00124 
00125 //_________________DELETE_MODELS
00126 void deleteModels(void)
00127 {
00128 free (Model_parts);
00129 free (Models);
00130 free (Vertices);
00131 free (Normals);
00132 free (Faces);
00133 }
00134 
00135 
00136 //_____________________________________ADD_MODEL
00137 int  addModel (std::string filename)
00138 {
00139 
00140 
00141 ModelStruct newModel;
00142 Model_partStruct newPart;
00143 
00144 FILE *file = fopen (filename.c_str(), "rb");
00145 //int _errno = errno;
00146 if (file == NULL) 
00147     {
00148     perror( filename.c_str() );
00149     return (-1);
00150     }
00151 
00152 newModel.ind_model_parts = model_parts_size;
00153 
00154 int n = fread( &newModel.nmodel_parts, sizeof(int), 1, file);
00155     if (n != 1) {
00156       printf( "Error reading the number of parts of the model. \n");
00157       return (-1);
00158     }
00159 
00160         model_parts_size += newModel.nmodel_parts;
00161         Model_parts = realloc ( Model_parts, model_parts_size*sizeof(Model_partStruct));
00162 
00163         
00164 for (int i=0; i < newModel.nmodel_parts; i++)
00165         {
00166         n = fread( &newPart.nvertices, sizeof(int), 1, file );
00167         if (n != 1) {   printf( "Error reading the number of vertices. \n"); return (-1); }
00168     
00169         n = fread ( &newPart.nfaces, sizeof(int), 1, file );
00170         if (n != 1) {   printf( "Error reading the number of faces. \n"); return (-1); }
00171 
00172         //now reallocating memory for vertices and appending new vertices
00173         newPart.ind_vertices = vertices_size;
00174         vertices_size = (newPart.ind_vertices + newPart.nvertices);
00175         Vertices = realloc ( Vertices, vertices_size*sizeof(Point3) );
00176 
00177                     
00178         n = fread ( ((Point3 *) Vertices + newPart.ind_vertices), sizeof(Point3), newPart.nvertices, file );
00179         if (n != newPart.nvertices) 
00180             {   printf( "Error reading vertices from the file %s. \n Only %d vertices were read. \n", filename.c_str(), n);
00181                 return (-1);
00182             }
00183        
00184         //now reallocating memory for normals and appending new normals
00185         newPart.ind_faces = faces_size;
00186         faces_size = newPart.ind_faces + newPart.nfaces;
00187 
00188         Normals = realloc ( Normals, faces_size*sizeof(Point3) );
00189 
00190         n = fread ( ((Point3 *) Normals + newPart.ind_faces), sizeof(Point3), newPart.nfaces, file );
00191         if (n != newPart.nfaces) 
00192             {   printf( "Error reading normals from the file %s. \n Only %d normals were read. \n", filename.c_str(), n);
00193                 return (-1);
00194             }
00195    
00196         //now reallocating memory for faces and appending new faces
00197         Faces = realloc (Faces, (faces_size)*sizeof(long)*3 );
00198         
00199         
00200         n = fread ( ((long *) Faces + newPart.ind_faces*3), sizeof(long), newPart.nfaces*3, file );
00201         if (n != newPart.nfaces*3) 
00202             {   printf( "Error reading faces from the file %s. \n Only %d faces were read. \n", filename.c_str(), n);
00203                 return (-1);
00204             }
00205 
00206         //adding new model part to global array
00207         memcpy ( ((Model_partStruct *)Model_parts + newModel.ind_model_parts + i  ), (void *) &newPart, sizeof(Model_partStruct) );
00208         
00209         }//end for
00210 
00211     //adding new model to global array
00212     models_size ++;
00213     Models = realloc ( Models, models_size*sizeof(ModelStruct));
00214         
00215     memcpy ( ((ModelStruct *)Models + models_size - 1 ), (void *) &newModel, sizeof(ModelStruct) );
00216 
00217 //cout << "start of faces array " << (int) ((long *)Faces) << endl;
00218 //cout << "end of faces array " << (int) ((long *)Faces + (faces_size - 1)*3) << endl;
00219 
00220 
00221 
00222 return (models_size - 1);
00223 
00224 }
00225         
00231 //___________________________DRAW_FACES         
00232 void drawFaces ( int handle, double scale)
00233 {
00234 ModelStruct *model =  ((ModelStruct *)Models + handle);  
00235 Model_partStruct *model_part;
00236 //printf(" model has %d model_parts \n", model -> nmodel_parts);
00237 
00238 Point3 vertex[3];
00239 Point3Struct * vertex_ptr;
00240 Point3 normal;
00241 Point3Struct * normal_ptr;
00242 long face;
00243 long *face_ptr;
00244 
00245 double A, B, C, D;
00246 double cosalpha, cosbeta, cosgamma, mu;
00247 Point3 tex[3];
00248 
00249 double oldbeta;
00250 double alpha, beta, gamma;
00251 
00252 //setDiffuseMaterialColor( colorWhite );
00253 
00254 
00255 
00256 //cout << "start of faces array " << (int) ((long *)Faces) << endl;
00257 
00258 //glPushMatrix();
00259 
00260 
00261 
00262 glDisable( GL_TEXTURE_GEN_S);
00263 glDisable( GL_TEXTURE_GEN_T);
00264 glDisable( GL_TEXTURE_GEN_R);
00265 /*
00266 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
00268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST );
00269 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
00270 */
00278 for (int i=0; i < model -> nmodel_parts; i++)
00279     {//drawing every part as a separate object
00280 //cout << "drawing part " << i << endl;
00281 //cout.flush();
00282 
00283     model_part = ((Model_partStruct *)Model_parts + model->ind_model_parts + i);
00284 
00285 //cout << "start of the face " << (int) ((long *)Faces + model_part->ind_faces) << endl;
00286 //cout << "end   of the face " << (int) ((long *)Faces + model_part->ind_faces + (model_part->nfaces-1)*3 + 2) << endl;
00287 
00288 //glPushMatrix();
00289 
00290     for (int j=0; j < model_part->nfaces; j++ )
00291         {//drawing every face
00292 
00293 //cout << j << ",";
00294 //cout.flush();
00295             normal_ptr = ((Point3 *)Normals + model_part->ind_faces + j);
00296             normal = *normal_ptr;
00297 
00298         for (int k=0; k < 3; k++ )
00299             {
00300             face_ptr = ((long *)Faces + model_part->ind_faces*3 + j*3 + k);
00301             face = *face_ptr;
00302             
00303             vertex_ptr = ((Point3 *)Vertices + model_part->ind_vertices + face);
00304             vertex[k] = *vertex_ptr;
00305 
00306             }//end for k
00307 
00308 /*
00309 //determining the coefficients in plane's equation
00310 A = ( (vertex[1].y - vertex[0].y)*(vertex[2].z - vertex[0].z) ) - ( (vertex[1].z - vertex[0].z)*(vertex[2].y - vertex[0].y) );
00311 B = ( (vertex[1].z - vertex[0].z)*(vertex[2].x - vertex[0].x) ) - ( (vertex[2].z - vertex[0].z)*(vertex[1].x - vertex[0].x) );
00312 C = ( (vertex[1].x - vertex[0].x)*(vertex[2].y - vertex[0].y) ) - ( (vertex[1].y - vertex[0].y)*(vertex[2].x - vertex[0].x) );
00313 D = - ( vertex[0].x*A + vertex[0].y*B + vertex[0].z*C );
00314 
00315 if (D < 0)
00316     {
00317     //normalizing
00318     D = -D;
00319     A = -A;
00320     B = -B;
00321     C = -C;
00322     }
00323 
00324 //computing normalized equation of the plane
00325     //since (D >= 0) we certainly know which root to take for mu
00326 mu = (-1) / sqrt( A*A + B*B + C*C );
00327 
00328 //printf(" mu = %f, A = %f, B = %f, C = %f, D = %f \n", mu, A, B, C, D );
00329 
00330 cosalpha = mu * A;
00331 cosbeta  = mu * B;
00332 cosgamma = mu * C;
00333 
00334 alpha = acos (cosgamma);// - M_PI/2;
00335 beta  = acos (cosbeta)  ;
00336 gamma = acos (cosgamma) ;
00337 
00338 oldbeta = beta;
00339 //alpha += M_PI/2;
00340 
00341 beta  *= cos(alpha );
00342 //beta += M_PI/2;
00343 gamma = gamma*(cos(oldbeta  ) ) ;
00344 gamma = gamma*(sin(alpha));
00345 //gamma += M_PI;
00346 //gamma *=sin(alpha );
00347 
00348 glDisable(GL_TEXTURE_2D);
00349     glMatrixMode ( GL_TEXTURE );
00350     glLoadIdentity ();
00351 
00352     glRotated ( gamma * 180.0f/M_PI , 0, 1, 0);
00353     glRotated ( beta  * 180.0f/M_PI, 0, 0, 1);
00354     glRotated ( alpha * 180.0f/M_PI, 1, 0, 0);
00355 
00356     glMatrixMode (GL_MODELVIEW);
00357 
00358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
00359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
00360 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT );
00361 
00362 glEnable(GL_TEXTURE_2D);
00363 glBindTexture(GL_TEXTURE_2D, texture(TEX_CHECK));
00364 
00365 //printf(" cos is  %f, %f, %f \n", cosalpha, cosbeta, cosgamma );
00366 //printf(" acos is %f, %f, %f \n", acos(cosalpha)*180.0f/M_PI, acos(cosbeta)*180/M_PI, acos(cosgamma)*180/M_PI ); 
00367 
00368 //glMatrixMode (GL_MODELVIEW);
00369 
00370 for (int k=0; k<3; k++) 
00371     {
00372     tex[k].x =   vertex[k].x*scale/5;
00373     tex[k].y =   vertex[k].y*scale/5;
00374     tex[k].z =   vertex[k].z*scale/5;
00375     }
00376 
00377   
00378 */
00379 //printf(" x=%f y=%f \n", texture[0].x, texture[0].y);
00380         glBegin(GL_TRIANGLES);
00382                 glNormal3d( normal.x, normal.y, normal.z);
00383                             
00384                 for (int k=0; k<3; k++)
00385                 {
00386 //              glTexCoord3d( tex[k].x, tex[k].y, tex[k].z );
00387                 glVertex3d( vertex[k].x, vertex[k].y, vertex[k].z);
00388                 }//end for k
00389         glEnd();
00390 
00391 //glPopMatrix();
00392         }//end for j
00393 
00394 //glPopMatrix();
00395 
00396     }//end for i
00397 glDisable(GL_TEXTURE_2D);
00398 //glPopMatrix();
00399 
00400 
00401 
00402 
00403 //cout << " all parts are drawn";
00404 //cout.flush();
00405     
00406 /*
00407 glBegin(GL_TRIANGLES);
00408     
00409     glNormal3d( normal.x / len, normal.y / len, normal.z / len );
00410     glVertex3d(vertexa.x, vertexa.y, vertexa.z);
00411 
00412 
00413     glNormal3d( normal.x / len, normal.y / len, normal.z / len );
00414     glVertex3d(vertexb.x, vertexb.y, vertexb.z);
00415 
00416 
00417     glNormal3d( normal.x / len, normal.y / len, normal.z / len );
00418     glVertex3d(vertexc.x, vertexc.y, vertexc.z);
00419     
00420     }
00421 glEnd();
00422 */
00423 }
00424 
00425 //__________________________DRAW_MODEL
00426 void drawModel(int model, int handle, double X, double Y, double Z, double x, double y, double z, double fi, double theta, double psi, double scale)
00427 {
00428 
00429 if (handle == -1)
00430     {
00431     printf(" Incorrect model handler. Probably, model was not initialized properly. \n ");
00432     exit(1);
00433     }
00434 
00435 float hf;
00436 
00437 hf = gethf( (int) round(x/g_cellsize), (int) round (y/g_cellsize) );
00438 
00439 //hf *= g_fHeightScale;
00440 
00441 //hf -= g_fHeight;
00442 //we start to output vertices in order, defined by face array
00443 //int n= sizeof(gallows_1_face)/ (sizeof(long)*3);
00444 
00445 
00446 glDisable(GL_AUTO_NORMAL);
00447 glEnable(GL_NORMALIZE);
00448 glDisable(GL_CULL_FACE);
00449 glCullFace(GL_NONE);
00450 glMatrixMode(GL_MODELVIEW);
00451 glPushMatrix();
00452 glTranslated(x, z + hf, y);
00453 glTranslated(-X, -Z, -Y);//, 0);/-Z);//-Y*g_cellsize, -Z);
00454 glRotated(fi,    1, 0, 0);
00455 glRotated(theta, 0, 1, 0);
00456 glRotated(psi,   0, 0, 1);
00457 glScaled(scale, scale, scale);
00458 /*
00459       if ( model_use_list[model] )
00460             {
00461             glCallList ( model_index[model] );
00462             }
00463         else
00464             {
00465 */
00466     drawFaces (handle, scale);
00467 //          }
00468 glPopMatrix();
00469 
00470 glDisable(GL_AUTO_NORMAL);
00471 glDisable(GL_NORMALIZE);
00472 
00473 //setDiffuseMaterialColor( colorWhite );
00474 //coordinate axises for object
00475 if (g_show_axis)
00476     {
00477     glPushMatrix();
00478 glMatrixMode(GL_MODELVIEW);
00479 glTranslated(x*g_cellsize, z + hf, y*g_cellsize);
00480 glTranslated(-X*g_cellsize, -Z, -Y*g_cellsize);//, 0);//-Z);//-Y*g_cellsize, -Z);
00481 
00482 glLineWidth(2);
00483     glBegin(GL_LINES);
00484         glColor3d(1, 0, 0);
00485         glVertex3d(0, 0, 0);
00486         glColor3d(1, 0, 0);
00487         glVertex3d(10, 0, 0);
00488     glEnd();
00489     
00490     glBegin(GL_LINES);
00491         glColor3d(0, 1, 0);
00492         glVertex3d(0, 0, 0);
00493         glColor3d(0, 1, 0);
00494         glVertex3d(0, 12, 0);
00495     glEnd();
00496 
00497     glBegin(GL_LINES);
00498         glColor3d(0, 0, 1);
00499         glVertex3d(0, 0, 0);
00500         glColor3d(0, 0, 1);
00501         glVertex3d(0, 0, 15);
00502     glEnd();
00503 glLineWidth(1);
00504 
00505         glColor3d(1, 1, 1);
00506 
00507     glPopMatrix();
00508     }
00509 
00510 //setDiffuseMaterialColor( colorWhite );
00511     
00512 
00513 //printf("\n X %f, Y %f , Z %f, ", X, Y, Z); 
00514 
00515 
00516 }
00517 
00518 
00519 #endif
00520 

Generated on Thu Jan 20 02:46:58 2005 for Main_Demo by doxygen 1.3.6