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
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,
00043 true,
00044 true,
00045 true,
00046 false,
00047 true,
00048 false,
00049 false,
00050 true,
00051 true,
00052 true,
00053 true,
00054 true,
00055 true,
00056 true,
00057 false,
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
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
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
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
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
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
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
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
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
00207 memcpy ( ((Model_partStruct *)Model_parts + newModel.ind_model_parts + i ), (void *) &newPart, sizeof(Model_partStruct) );
00208
00209 }
00210
00211
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
00218
00219
00220
00221
00222 return (models_size - 1);
00223
00224 }
00225
00231
00232 void drawFaces ( int handle, double scale)
00233 {
00234 ModelStruct *model = ((ModelStruct *)Models + handle);
00235 Model_partStruct *model_part;
00236
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
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 glDisable( GL_TEXTURE_GEN_S);
00263 glDisable( GL_TEXTURE_GEN_T);
00264 glDisable( GL_TEXTURE_GEN_R);
00265
00266
00267
00268
00269
00270
00278 for (int i=0; i < model -> nmodel_parts; i++)
00279 {
00280
00281
00282
00283 model_part = ((Model_partStruct *)Model_parts + model->ind_model_parts + i);
00284
00285
00286
00287
00288
00289
00290 for (int j=0; j < model_part->nfaces; j++ )
00291 {
00292
00293
00294
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 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 glBegin(GL_TRIANGLES);
00382 glNormal3d( normal.x, normal.y, normal.z);
00383
00384 for (int k=0; k<3; k++)
00385 {
00386
00387 glVertex3d( vertex[k].x, vertex[k].y, vertex[k].z);
00388 }
00389 glEnd();
00390
00391
00392 }
00393
00394
00395
00396 }
00397 glDisable(GL_TEXTURE_2D);
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 }
00424
00425
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
00440
00441
00442
00443
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);
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
00460
00461
00462
00463
00464
00465
00466 drawFaces (handle, scale);
00467
00468 glPopMatrix();
00469
00470 glDisable(GL_AUTO_NORMAL);
00471 glDisable(GL_NORMALIZE);
00472
00473
00474
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);
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
00511
00512
00513
00514
00515
00516 }
00517
00518
00519 #endif
00520