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

scene.cpp File Reference

#include <GL/glu.h>
#include <string>
#include "scene.h"
#include "config.h"
#include "heightfield.h"
#include <stdio.h>
#include <math.h>
#include <iostream>
#include "texture.h"
#include <errno.h>
#include "airplane.h"

Go to the source code of this file.

Classes

struct  Model_partStruct
struct  ModelStruct

Functions

void normalize (double v[3])
Point3Struct operator+ (Point3Struct p1, Point3Struct p2)
int addModel (std::string)
void deleteModels ()
void drawFaces (int handle, double scale)
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)

Variables

std::string model_name [MODEL_N]
bool model_in_use [MODEL_N]
unsigned int model_index [MODEL_N]
bool model_use_list [MODEL_N]
int model_handle [MODEL_N]
Model_partStruct Model_part
ModelStruct Model
void * Model_parts
int model_parts_size = 0
void * Models
int models_size = 0
void * Vertices
int vertices_size = 0
void * Normals
void * Faces
int faces_size = 0


Function Documentation

int addModel std::string   ) 
 

Definition at line 137 of file scene.cpp.

References Faces, faces_size, Model_partStruct::ind_faces, ModelStruct::ind_model_parts, Model_partStruct::ind_vertices, Model_parts, model_parts_size, Models, models_size, Model_partStruct::nfaces, ModelStruct::nmodel_parts, Normals, Model_partStruct::nvertices, Point3, Vertices, and vertices_size.

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 }

void deleteModels  ) 
 

Definition at line 126 of file scene.cpp.

References Faces, Model_parts, Models, Normals, and Vertices.

Referenced by main().

00127 {
00128 free (Model_parts);
00129 free (Models);
00130 free (Vertices);
00131 free (Normals);
00132 free (Faces);
00133 }

void drawFaces int  handle,
double  scale
 

Draw Faces Given the array of vertices and array of faces this function ouputs the vertices of triangle strips or triangles defining these faces Input: handle, scaling factor

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT ); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture(TEX_CHECK));

Definition at line 232 of file scene.cpp.

References Faces, Model_partStruct::ind_faces, ModelStruct::ind_model_parts, Model_partStruct::ind_vertices, Model_parts, Models, Model_partStruct::nfaces, normal, Normals, Point3, and Vertices.

Referenced by drawModel().

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 }

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
 

Definition at line 426 of file scene.cpp.

References drawFaces(), g_cellsize, g_show_axis, and gethf().

Referenced by __drawModel().

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 }

void normalize double  v[3]  ) 
 

Definition at line 67 of file scene.cpp.

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 }

Point3Struct operator+ Point3Struct  p1,
Point3Struct  p2
 

Definition at line 83 of file scene.cpp.

References Point3, Point3Struct::x, Point3Struct::y, and Point3Struct::z.

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 }


Variable Documentation

void* Faces [static]
 

Definition at line 116 of file scene.cpp.

Referenced by addModel(), deleteModels(), and drawFaces().

int faces_size = 0 [static]
 

Definition at line 117 of file scene.cpp.

Referenced by addModel().

struct ModelStruct Model
 

int model_handle[MODEL_N]
 

Definition at line 64 of file scene.cpp.

Referenced by __drawModel(), and initGL().

bool model_in_use[MODEL_N]
 

Initial value:

 
    {   true,
        true,
        true,
        true,
        false,
        true,
        false,
        false,
        true,
        true,
        true,
        true,
        true, 
        true,
        true, 
        false, 
        
    }

Definition at line 41 of file scene.cpp.

Referenced by initGL(), and renderScene().

unsigned int model_index[MODEL_N]
 

Definition at line 62 of file scene.cpp.

Referenced by initGL(), and renderScene().

std::string model_name[MODEL_N]
 

Initial value:

    { "gallows",
      "gallows_rope",
        "iron_maiden",
        "crypt",
        "diablo",
        "casket_closed",
        "casket_opened",
        "casket_opened2",
        "giljotina1(derevo)",
        "giljotina2(metal)",
        "giljotina3(chelovek)",
        "giljotina4(korzina)",
        "graveyard",
        "mancage_1(derevo)",
        "mancage_2(metal)",
        "prosto-chelovek",
        
    }

Definition at line 21 of file scene.cpp.

Referenced by initGL().

struct Model_partStruct Model_part
 

void* Model_parts [static]
 

Definition at line 106 of file scene.cpp.

Referenced by addModel(), deleteModels(), and drawFaces().

int model_parts_size = 0 [static]
 

Definition at line 107 of file scene.cpp.

Referenced by addModel().

bool model_use_list[MODEL_N]
 

Definition at line 63 of file scene.cpp.

Referenced by initGL(), and renderScene().

void* Models [static]
 

Definition at line 108 of file scene.cpp.

Referenced by addModel(), deleteModels(), and drawFaces().

int models_size = 0 [static]
 

Definition at line 109 of file scene.cpp.

Referenced by addModel().

void* Normals [static]
 

Definition at line 115 of file scene.cpp.

Referenced by addModel(), deleteModels(), and drawFaces().

void* Vertices [static]
 

Definition at line 113 of file scene.cpp.

Referenced by addModel(), deleteModels(), and drawFaces().

int vertices_size = 0 [static]
 

Definition at line 114 of file scene.cpp.

Referenced by addModel().


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