Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

ObjLevel.cpp

Go to the documentation of this file.
00001 
00007 #include "ObjLevel.h"
00008 
00009 ObjLevel::ObjLevel(LevelInfo &lvl, Renderer *renderer) {
00010         m_wallVertices = NULL;
00011         m_floorVertices = NULL;
00012         m_shadowVertices = NULL;
00013         m_waterVertices = NULL;
00014         m_waterSurface = NULL;
00015         mp_lvl = &lvl;
00016         mp_renderer = renderer;
00017         m_nSpawnpools = 0;
00018 }
00019 
00020 bool ObjLevel::load() {
00021         Set <Vertex> wallVertices;
00022         Set <Vertex> floorVertices;
00023         Set <Vert> shadowVertices;
00024         Set <Vert> shadowVerticesFinal;
00025         Set <Vertex> waterVertices;
00026         Set <Vertex> waterSurfaceVertices;
00027         int wallIndex = 0;
00028         int floorIndex = 0;
00029         int shadowIndex = 0;
00030         int shadowIndexFinal = 0;
00031         int waterIndex = 0;
00032         waterSurfaceIndex = 0;
00033         m_numObjects = 0;
00034         m_nExtras = 0;
00035         m_nSpawnpools = 0;
00036 
00037         int size = mp_lvl->width*mp_lvl->height*3;
00038         int x = 0, y = 0;
00039         unsigned char *d = mp_lvl->data;
00040         unsigned int i;
00041 
00042         for (i=0; i<mp_lvl->numModels; i++) {
00043                 m_models[i].loadFromAsciiFile(mp_lvl->modelsPath, mp_lvl->models[i]);
00044         }
00045 
00046 
00047         for (i=0; i<size; i+=3) {
00048 
00049                 // ========== WALL ==========
00050                 if (isWall(&d[i])) {
00051 
00052                         // add ALL walls and remove, or check first time?
00053 
00054                         if (x > 0) {
00055                                 if (!isWall(&d[i-3])) {
00056                                         wallVertices.add(newVertex(0.0f, 0.0f,  -1.0f, 0.0f, 0.0f,      -0.5f+x, 0.0f,-0.5f+y));
00057                                         wallVertices.add(newVertex(0.0f, 1.0f,  -1.0f, 0.0f, 0.0f,      -0.5f+x, 1.0f,-0.5f+y));
00058                                         wallVertices.add(newVertex(1.0f, 1.0f,  -1.0f, 0.0f, 0.0f,      -0.5f+x, 1.0f, 0.5f+y));
00059                                         wallVertices.add(newVertex(1.0f, 0.0f,  -1.0f, 0.0f, 0.0f,      -0.5f+x, 0.0f, 0.5f+y));
00060                                         wallIndex += 4;
00061 
00062                                         // add wall to LEFT side
00063                                         //LOG_MISC(("Do wall LEFT, %d %d",x,y));
00064                                 }
00065                         }
00066 
00067                         if (x < mp_lvl->width-1) {
00068                                 if (!isWall(&d[i+3])) {
00069                                         wallVertices.add(newVertex(0.0f, 0.0f,   1.0f, 0.0f, 0.0f,       0.5f+x, 0.0f, 0.5f+y));
00070                                         wallVertices.add(newVertex(0.0f, 1.0f,   1.0f, 0.0f, 0.0f,       0.5f+x, 1.0f, 0.5f+y));
00071                                         wallVertices.add(newVertex(1.0f, 1.0f,   1.0f, 0.0f, 0.0f,       0.5f+x, 1.0f,-0.5f+y));
00072                                         wallVertices.add(newVertex(1.0f, 0.0f,   1.0f, 0.0f, 0.0f,       0.5f+x, 0.0f,-0.5f+y));
00073                                         wallIndex += 4;
00074 
00075                                         // add wall to RIGHT side
00076                                         //LOG_MISC(("Do wall RIGHT, %d %d",x,y));
00077                                 }
00078                         }
00079 
00080                         if (y > 0) {
00081                                 if (!isWall(&d[i-mp_lvl->width*3])) {
00082                                         wallVertices.add(newVertex(0.0f, 0.0f,   0.0f, 0.0f, 1.0f,       0.5f+x, 0.0f,-0.5f+y));
00083                                         wallVertices.add(newVertex(0.0f, 1.0f,   0.0f, 0.0f, 1.0f,       0.5f+x, 1.0f,-0.5f+y));
00084                                         wallVertices.add(newVertex(1.0f, 1.0f,   0.0f, 0.0f, 1.0f,      -0.5f+x, 1.0f,-0.5f+y));
00085                                         wallVertices.add(newVertex(1.0f, 0.0f,   0.0f, 0.0f, 1.0f,      -0.5f+x, 0.0f,-0.5f+y));
00086                                         wallIndex += 4;
00087 
00088                                         // add wall to TOP side
00089                                         //LOG_MISC(("Do wall TOP, %d %d",x,y));
00090                                 }
00091                         }
00092 
00093                         if (y < mp_lvl->height-1) {
00094                                 if (!isWall(&d[i+mp_lvl->width*3])) {
00095                                         wallVertices.add(newVertex(0.0f, 0.0f,   0.0f, 0.0f, -1.0f,     -0.5f+x, 0.0f, 0.5f+y));
00096                                         wallVertices.add(newVertex(0.0f, 1.0f,   0.0f, 0.0f, -1.0f,     -0.5f+x, 1.0f, 0.5f+y));
00097                                         wallVertices.add(newVertex(1.0f, 1.0f,   0.0f, 0.0f, -1.0f,      0.5f+x, 1.0f, 0.5f+y));
00098                                         wallVertices.add(newVertex(1.0f, 0.0f,   0.0f, 0.0f, -1.0f,      0.5f+x, 0.0f, 0.5f+y));
00099                                         wallIndex += 4;
00100 
00101                                         // add wall to BOTTOM side
00102                                         //LOG_MISC(("Do wall BOTTOM, %d %d",x,y));
00103                                 }
00104                         }
00105 
00106                 }
00107 
00108                 // ========== FLOOR ==========
00109                 else if (isColor(&d[i], 15) || isColor(&d[i], 14)) {
00110                         floorVertices.add(newVertex(0.0f, 0.0f, 0.0f, 1.0f, 0.0f,       -0.5f+x, 0.0f, 0.5f+y));
00111                         floorVertices.add(newVertex(0.0f, 1.0f, 0.0f, 1.0f, 0.0f,       -0.5f+x, 0.0f,-0.5f+y));
00112                         floorVertices.add(newVertex(1.0f, 1.0f, 0.0f, 1.0f, 0.0f,        0.5f+x, 0.0f,-0.5f+y));
00113                         floorVertices.add(newVertex(1.0f, 0.0f, 0.0f, 1.0f, 0.0f,        0.5f+x, 0.0f, 0.5f+y));
00114                         floorIndex += 4;
00115 
00116                         // ========== SPAWN POOL ==========
00117                         if (isSpawnPool(&d[i])) {
00118                                 if (m_nSpawnpools < MAX_SPAWNPOOLS) {
00119                                         m_spawnpools[m_nSpawnpools].x = (float)x;
00120                                         m_spawnpools[m_nSpawnpools].y = 0.0f;
00121                                         m_spawnpools[m_nSpawnpools].z = (float)y;
00122                                         m_nSpawnpools++;
00123                                 }
00124                         }
00125                         // ========== EXTRAS ==========
00126                         else if (d[i+2] == 140) {
00127                                 if (m_nExtras < MAX_EXTRAS) {
00128                                         m_extras[m_nExtras].x = (float)x;
00129                                         m_extras[m_nExtras].y = 0.0f;
00130                                         m_extras[m_nExtras].z = (float)y;
00131                                         m_nExtras++;
00132                                 }
00133                         }
00134 
00135                         // ========== OBJECT 0 ==========
00136                         else if (d[i+1] == 240) {
00137                                 if (m_numObjects < MAX_LEVEL_OBJECTS) {
00138                                         //LOG_MISC(("LEVEL OBJ #%d type 0 added", m_numObjects));
00139                                         m_objects[m_numObjects].alive = true;
00140                                         m_objects[m_numObjects].x = (float)x;
00141                                         m_objects[m_numObjects].y = 0.0f;
00142                                         m_objects[m_numObjects].z = (float)y;
00143                                         m_objects[m_numObjects].model = 0;
00144                                         m_numObjects++;
00145                                 }
00146                         }
00147                         // ========== OBJECT 1 ==========
00148                         else if (d[i+1] == 140) {
00149                                 if (m_numObjects < MAX_LEVEL_OBJECTS) {
00150                                         //LOG_MISC(("LEVEL OBJ #%d type 1 added", m_numObjects));
00151                                         m_objects[m_numObjects].alive = true;
00152                                         m_objects[m_numObjects].x = x;
00153                                         m_objects[m_numObjects].y = 0.0f;
00154                                         m_objects[m_numObjects].z = y;
00155                                         m_objects[m_numObjects].model = 1;
00156                                         m_numObjects++;
00157                                 }
00158                         }
00159 
00160                 }
00161 
00162                 // ========== WATER ==========
00163                 else if (isColor(&d[i], 0, 0,  240)) {
00164                         bool left = false, right = false, top = false, bottom = false;
00165                         Vec3 tuu, tuv, tvv, tvu;
00166                         tuu = Vec3(0.0f, 1.0f, 0.0f);
00167                         tuv = Vec3(0.0f, 1.0f, 0.0f);
00168                         tvv = Vec3(0.0f, 1.0f, 0.0f);
00169                         tvu = Vec3(0.0f, 1.0f, 0.0f);
00170 
00171                         // left
00172                         if (x > 0) {
00173                                 if (!isColor(&d[i-3], 0, 0, 240)) {
00174                                         left = true;
00175                                         if (x < mp_lvl->width-1) {
00176                                                 // top
00177                                                 if (y > 0) {
00178                                                         if (!isColor(&d[i-mp_lvl->width*3], 0, 0,  240)) {
00179                                                                 tuv = Vec3(0.333333f, 0.333333f, 0.333333f);
00180                                                         }
00181                                                 }
00182 
00183                                                 // bottom
00184                                                 if (y < mp_lvl->height-1) {
00185                                                         if (!isColor(&d[i+mp_lvl->width*3], 0, 0,  240))
00186                                                                 tuu = Vec3(0.333333f, 0.333333f,-0.333333f);
00187                                                 }
00188                                         }
00189                                 }
00190                         }
00191 
00192                         // right
00193                         if (x < mp_lvl->width-1) {
00194                                 if (!isColor(&d[i+3], 0, 0,  240)) {
00195                                         right = true;
00196 
00197                                         if (x > 0) {
00198                                                 // top
00199                                                 if (y > 0) {
00200                                                         if (!isColor(&d[i-mp_lvl->width*3], 0, 0,  240))
00201                                                                 tvv = Vec3(-0.333333f, 0.333333f, 0.333333f);
00202                                                 }
00203 
00204                                                 // bottom
00205                                                 if (y < mp_lvl->height-1) {
00206                                                         if (!isColor(&d[i+mp_lvl->width*3], 0, 0,  240))
00207                                                                 tvu = Vec3(-0.333333f, 0.333333f,-0.333333f);
00208                                                 }
00209                                         }
00210                                 }
00211                         }
00212 
00213                         // top
00214                         if (y > 0) {
00215                                 if (!isColor(&d[i-mp_lvl->width*3], 0, 0,  240)) {
00216                                         top = true;
00217 
00218                                         if (y < mp_lvl->width) {
00219                                                 // left
00220                                                 if (x > 0) {
00221                                                         if (!isColor(&d[i-3], 0, 0,  240))
00222                                                                 tuv = Vec3(0.333333f, 0.333333f, 0.333333f);
00223                                                 }
00224 
00225                                                 // right
00226                                                 if (x < mp_lvl->width-1) {
00227                                                         if (!isColor(&d[i+3], 0, 0,  240))
00228                                                                 tvv = Vec3(-0.333333f, 0.333333f,0.333333f);
00229                                                 }
00230                                         }
00231                                 }
00232                         }
00233 
00234                         // bottom
00235                         if (y < mp_lvl->height-1) {
00236                                 if (!isColor(&d[i+mp_lvl->width*3], 0, 0,  240)) {
00237                                         bottom = true;
00238 
00239                                         if (y < mp_lvl->width) {
00240                                                 // left
00241                                                 if (x > 0) {
00242                                                         if (!isColor(&d[i-3], 0, 0,  240))
00243                                                                 tuu = Vec3(0.333333f, 0.333333f,-0.333333f);
00244                                                 }
00245 
00246                                                 // right
00247                                                 if (x < mp_lvl->width-1) {
00248                                                         if (!isColor(&d[i+3], 0, 0,  240))
00249                                                                 tvu = Vec3(-0.333333f, 0.333333f,-0.333333f);
00250                                                 }
00251                                         }
00252                                 }
00253                         }
00254 
00255 
00256                         if (left) {
00257                                 waterVertices.add(newVertex(0.25f, 0.25f,       tuu.x, tuu.y, tuu.z,    -0.5f+x,-0.5f, 0.5f+y));
00258                                 waterVertices.add(newVertex(0.0f, 0.25f,        0.5f, 0.5f, 0.0f,               -0.5f+x, 0.0f, 0.5f+y));
00259                                 waterVertices.add(newVertex(0.0f, 0.75f,        0.5f, 0.5f, 0.0f,               -0.5f+x, 0.0f,-0.5f+y));
00260                                 waterVertices.add(newVertex(0.25f, 0.75f,       tuv.x, tuv.y, tuv.z,    -0.5f+x,-0.5f,-0.5f+y));
00261                                 waterIndex += 4;
00262                         }
00263 
00264                         if (right) {
00265                                 waterVertices.add(newVertex(0.75f, 0.75f,       tvv.x, tvv.y, tvv.z,     0.5f+x,-0.5f,-0.5f+y));
00266                                 waterVertices.add(newVertex(1.0f, 0.75f,        -0.5f, 0.5f, 0.0f,       0.5f+x, 0.0f,-0.5f+y));
00267                                 waterVertices.add(newVertex(1.0f, 0.25f,        -0.5f, 0.5f, 0.0f,       0.5f+x, 0.0f, 0.5f+y));
00268                                 waterVertices.add(newVertex(0.75f, 0.25f,       tvu.x, tvu.y, tvu.z,     0.5f+x,-0.5f, 0.5f+y));
00269                                 waterIndex += 4;
00270                         }
00271 
00272                         if (top) {
00273                                 waterVertices.add(newVertex(0.25f, 0.75f,       tuv.x, tuv.y, tuv.z,    -0.5f+x,-0.5f,-0.5f+y));
00274                                 waterVertices.add(newVertex(0.25f, 1.0f,        0.0f, 0.5f, 0.5f,               -0.5f+x, 0.0f,-0.5f+y));
00275                                 waterVertices.add(newVertex(0.75f, 1.0f,        0.0f, 0.5f, 0.5f,                0.5f+x, 0.0f,-0.5f+y));
00276                                 waterVertices.add(newVertex(0.75f, 0.75f,       tvv.x, tvv.y, tvv.z,     0.5f+x,-0.5f,-0.5f+y));
00277                                 waterIndex += 4;
00278                         }
00279 
00280                         if (bottom) {
00281                                 waterVertices.add(newVertex(0.75f, 0.25f,       tvu.x, tvu.y, tvu.z,     0.5f+x,-0.5f, 0.5f+y));
00282                                 waterVertices.add(newVertex(0.75f, 0.0f,        0.0f, 0.5f, -0.5f,       0.5f+x, 0.0f, 0.5f+y));
00283                                 waterVertices.add(newVertex(0.25f, 0.0f,        0.0f, 0.5f, -0.5f,      -0.5f+x, 0.0f, 0.5f+y));
00284                                 waterVertices.add(newVertex(0.25f, 0.25f,       tuu.x, tuu.y, tuu.z,    -0.5f+x,-0.5f, 0.5f+y));
00285                                 waterIndex += 4;
00286                         }
00287 
00288                         waterVertices.add(newVertex(0.25f, 0.25f,       tuu.x, tuu.y, tuu.z,            -0.5f+x,-0.5f, 0.5f+y));
00289                         waterVertices.add(newVertex(0.25f, 0.75f,       tuv.x, tuv.y, tuv.z,            -0.5f+x,-0.5f,-0.5f+y));
00290                         waterVertices.add(newVertex(0.75f, 0.75f,       tvv.x, tvv.y, tvv.z,             0.5f+x,-0.5f,-0.5f+y));
00291                         waterVertices.add(newVertex(0.75f, 0.25f,       tvu.x, tvu.y, tvu.z,             0.5f+x,-0.5f, 0.5f+y));
00292 
00293                         waterIndex += 4;
00294 
00295                         waterSurfaceVertices.add(newVertex(0.0f, 0.0f,  tuu.x, tuu.y, tuu.z,    -0.5f+x,-0.3f, 0.5f+y));
00296                         waterSurfaceVertices.add(newVertex(0.0f, 1.0f,  tuv.x, tuv.y, tuv.z,    -0.5f+x,-0.3f,-0.5f+y));
00297                         waterSurfaceVertices.add(newVertex(1.0f, 1.0f,  tvv.x, tvv.y, tvv.z,     0.5f+x,-0.3f,-0.5f+y));
00298                         waterSurfaceVertices.add(newVertex(1.0f, 0.0f,  tvu.x, tvu.y, tvu.z,     0.5f+x,-0.3f, 0.5f+y));
00299                         waterSurfaceIndex += 4;
00300                 }
00301 
00302 
00303 
00304                 x++;
00305                 if (x == mp_lvl->width) {
00306                         x = 0;
00307                         y++;
00308                 }
00309 
00310         }
00311 
00312 
00313         //LOG_MISC(("Spawnpools: %d", m_nSpawnpools));
00314 
00315         // ========== WALL ==========
00316         if (wallIndex > 0) {
00317                 //LOG_MISC(("wallIndex: %d (%d polygons)", wallIndex, wallIndex/4));
00318                 m_wallVertices = new Vertex[wallIndex];
00319                 memcpy(m_wallVertices, wallVertices.getArray(), sizeof(Vertex)*wallIndex);
00320 
00321                 m_walls = new OpenGLObject();
00322                 m_walls->addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00323                 m_walls->addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00324                 m_walls->addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00325                 m_walls->setPrimitive(PRIM_QUADS);
00326                 m_walls->setVertices(m_wallVertices, wallIndex, sizeof(Vertex));
00327 
00328                 //LOG_SUCCESS(("Object level walls loaded"));
00329 
00330 
00331                 // ===== calculate SHADOW VOLUMES =====
00332                 //lvl.lights[0].pos = Vec3(10.0f, 10.0f, 60.0f);        // remove this later
00333                 Vec3 lPos = mp_lvl->lights[0].pos;
00334 
00335                 for (int i=0; i<wallIndex; i+=4) {
00336                         float dist = dot(Vec3(m_wallVertices[i].nx, m_wallVertices[i].ny, m_wallVertices[i].nz), lPos);
00337                         // do not face light
00338                         if (dist >= 0.0f) {
00339                                 Vec3 v0 = (Vec3(m_wallVertices[i+0].vx, m_wallVertices[i+0].vy, m_wallVertices[i+0].vz)-lPos)*100.0f;
00340                                 Vec3 v1 = (Vec3(m_wallVertices[i+1].vx, m_wallVertices[i+1].vy, m_wallVertices[i+1].vz)-lPos)*100.0f;
00341                                 Vec3 v2 = (Vec3(m_wallVertices[i+2].vx, m_wallVertices[i+2].vy, m_wallVertices[i+2].vz)-lPos)*100.0f;
00342                                 Vec3 v3 = (Vec3(m_wallVertices[i+3].vx, m_wallVertices[i+3].vy, m_wallVertices[i+3].vz)-lPos)*100.0f;
00343 
00344                                 Vert v[4];
00345 
00346                                 // top
00347                                 v[0].x = m_wallVertices[i+1].vx; v[0].y = m_wallVertices[i+1].vy; v[0].z = m_wallVertices[i+1].vz;
00348                                 v[1].x = v1.x; v[1].y = v1.y; v[1].z = v1.z;
00349                                 v[2].x = v2.x; v[2].y = v2.y; v[2].z = v2.z;
00350                                 v[3].x = m_wallVertices[i+2].vx; v[3].y = m_wallVertices[i+2].vy; v[3].z = m_wallVertices[i+2].vz;
00351 
00352                                 shadowVertices.add(v[0]);
00353                                 shadowVertices.add(v[1]);
00354                                 shadowVertices.add(v[2]);
00355                                 shadowVertices.add(v[3]);
00356 
00357                                 shadowIndex += 4;
00358 
00359                                 // right
00360                                 v[0].x = m_wallVertices[i+2].vx; v[0].y = m_wallVertices[i+2].vy; v[0].z = m_wallVertices[i+2].vz;
00361                                 v[1].x = v2.x; v[1].y = v2.y; v[1].z = v2.z;
00362                                 v[2].x = v3.x; v[2].y = v3.y; v[2].z = v3.z;
00363                                 v[3].x = m_wallVertices[i+3].vx; v[3].y = m_wallVertices[i+3].vy; v[3].z = m_wallVertices[i+3].vz;
00364 
00365                                 shadowVertices.add(v[0]);
00366                                 shadowVertices.add(v[1]);
00367                                 shadowVertices.add(v[2]);
00368                                 shadowVertices.add(v[3]);
00369 
00370                                 shadowIndex += 4;
00371 
00372                                 // bottom (skip)
00383                                 // left
00384                                 v[0].x = m_wallVertices[i+0].vx; v[0].y = m_wallVertices[i+0].vy; v[0].z = m_wallVertices[i+0].vz;
00385                                 v[1].x = v0.x; v[1].y = v0.y; v[1].z = v0.z;
00386                                 v[2].x = v1.x; v[2].y = v1.y; v[2].z = v1.z;
00387                                 v[3].x = m_wallVertices[i+1].vx; v[3].y = m_wallVertices[i+1].vy; v[3].z = m_wallVertices[i+1].vz;
00388 
00389                                 shadowVertices.add(v[0]);
00390                                 shadowVertices.add(v[1]);
00391                                 shadowVertices.add(v[2]);
00392                                 shadowVertices.add(v[3]);
00393 
00394                                 shadowIndex += 4;
00395                         }
00396                 }
00397 
00398                 // ========== OPTIMIZE SHADOW VOLUMES ==========
00399                 for (int p=0; p<shadowVertices.getCount(); p+=4) {
00400                         bool found = false;
00401                         int equal = 0;
00402                         for (int n=0; n<shadowVertices.getCount() && !found; n+=4) {
00403                                 if (p==n)
00404                                         continue;
00405 
00406                                 equal = 0;
00407                                 for (int a=0; a<4 && !found; a++) {
00408                                         for (int b=0; b<4 && !found; b++) {
00409                                                 if (vertEqual(shadowVertices[p+a], shadowVertices[n+b])) {
00410                                                         equal++;
00411                                                 }
00412                                         }
00413                                 }
00414 
00415                                 if (equal == 4)
00416                                         found = true;
00417                         }
00418 
00419                         //found = false;
00420                         if (!found) {
00421                                 shadowVerticesFinal.add(shadowVertices[p]);
00422                                 shadowVerticesFinal.add(shadowVertices[p+1]);
00423                                 shadowVerticesFinal.add(shadowVertices[p+2]);
00424                                 shadowVerticesFinal.add(shadowVertices[p+3]);
00425                                 shadowIndexFinal +=4;
00426                         }
00427                 }
00428 
00429 
00430                 //if (shadowIndexFinal > 0) {
00431                         //LOG_MISC(("shadowIndexFinal: %d (%d polygons)", shadowIndexFinal, shadowIndexFinal/4));
00432                         m_shadowVertices = new Vert[shadowIndexFinal];
00433                         memcpy(m_shadowVertices, shadowVerticesFinal.getArray(), sizeof(Vert)*shadowIndexFinal);
00434 
00435                         m_shadow = new OpenGLObject();
00436                         m_shadow->addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*0);
00437                         m_shadow->setPrimitive(PRIM_QUADS);
00438                         m_shadow->setVertices(m_shadowVertices, shadowIndexFinal, sizeof(Vert));
00439 
00440                         //LOG_SUCCESS(("Object level shadows loaded"));
00441                 //}
00442         }
00443 
00444         // ========== FLOOR ==========
00445         if (floorIndex > 0) {
00446                 //LOG_MISC(("floorIndex: %d (%d polygons)", wallIndex, wallIndex/4));
00447                 m_floorVertices = new Vertex[floorIndex];
00448                 memcpy(m_floorVertices, floorVertices.getArray(), sizeof(Vertex)*floorIndex);
00449 
00450                 m_floor = new OpenGLObject();
00451                 m_floor->addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00452                 m_floor->addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00453                 m_floor->addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00454                 m_floor->setPrimitive(PRIM_QUADS);
00455                 m_floor->setVertices(m_floorVertices, floorIndex, sizeof(Vertex));
00456 
00457                 //LOG_SUCCESS(("Object level floor loaded"));
00458         }
00459         
00460 
00461         // ========== WATER ==========
00462         //LOG_MISC(("waterIndex: %d (%d polygons)", waterIndex, waterIndex/4));
00463         m_waterVertices = new Vertex[waterIndex];
00464         memcpy(m_waterVertices, waterVertices.getArray(), sizeof(Vertex)*waterIndex);
00465 
00466         m_water = new OpenGLObject();
00467         m_water->addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00468         m_water->addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00469         m_water->addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00470         m_water->setPrimitive(PRIM_QUADS);
00471         m_water->setVertices(m_waterVertices, waterIndex, sizeof(Vertex));
00472 
00473         //LOG_SUCCESS(("Object level water loaded"));
00474 
00475 
00476         // ========== WATER SURFACE ==========
00477         //LOG_MISC(("waterSurfaceIndex: %d (%d polygons)", waterSurfaceIndex, waterSurfaceIndex/4));
00478         m_waterSurfaceVertices = new Vertex[waterSurfaceIndex];
00479         memcpy(m_waterSurfaceVertices, waterSurfaceVertices.getArray(), sizeof(Vertex)*waterSurfaceIndex);
00480 
00481         m_waterSurface = new OpenGLObject();
00482         m_waterSurface->addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00483         m_waterSurface->addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00484         m_waterSurface->addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00485         m_waterSurface->setPrimitive(PRIM_QUADS);
00486         m_waterSurface->setVertices(m_waterSurfaceVertices, waterSurfaceIndex, sizeof(Vertex));
00487 
00488         //LOG_SUCCESS(("Object level water surface loaded"));
00489 
00490         String str;
00491 
00492         str = "data\\textures\\walls\\";        str += mp_lvl->texWall;
00493         if ((m_texWall = mp_renderer->addTexture(str)) == TEXTURE_NONE) return false;
00494 
00495         str = "data\\textures\\floor\\";        str += mp_lvl->texFloor;
00496         if ((m_texFloor = mp_renderer->addTexture(str)) == TEXTURE_NONE) return false;
00497 
00498         str = "data\\textures\\water\\";        str += mp_lvl->texWater;
00499         if ((m_texWater = mp_renderer->addTexture(str)) == TEXTURE_NONE) return false;
00500 
00501         str = "data\\textures\\water\\";        str += mp_lvl->texWaterSurface;
00502         if ((m_texWaterSurface = mp_renderer->addTexture(str)) == TEXTURE_NONE) return false;
00503 
00504         LOG_SUCCESS(("Level object loaded"));
00505 
00506         return true;
00507 }
00508 
00509 bool ObjLevel::isColor(unsigned char *data, int r, int g, int b) {
00510         return (data[0] == r && data[1] == g && data[2] == b);
00511 }
00512 
00513 bool ObjLevel::isColor(unsigned char *data, int r, int g) {
00514         return (data[0] == r && data[1] == g);
00515 }
00516 
00517 bool ObjLevel::isColor(unsigned char *data, int r) {
00518         return (data[0] == r);
00519 }
00520 
00521 bool ObjLevel::isWall(unsigned char *data) {
00522         return isColor(data, 255, 255, 255);
00523 }
00524 
00525 bool ObjLevel::isSpawnPool(unsigned char *data) {
00526         return (data[1] == 240 && data[2] == 240);
00527 }
00528 
00529 void ObjLevel::setVertex(Vertex &v, float tu, float tv, float nx, float ny, float nz, float vx, float vy, float vz) {
00530         v.tu = tu;
00531         v.tv = tv;
00532         v.nx = nx;
00533         v.ny = ny;
00534         v.nz = nz;
00535         v.vx = vx;
00536         v.vy = vy;
00537         v.vz = vz;
00538 }
00539 
00540 Vertex ObjLevel::newVertex(float tu, float tv, float nx, float ny, float nz, float vx, float vy, float vz) {
00541         Vertex v;
00542         v.tu = tu;
00543         v.tv = tv;
00544         v.nx = nx;
00545         v.ny = ny;
00546         v.nz = nz;
00547         v.vx = vx;
00548         v.vy = vy;
00549         v.vz = vz;
00550         return v;
00551 }
00552 
00553 ObjLevel::~ObjLevel() {
00554         if (m_walls) delete m_walls;
00555         m_walls = NULL;
00556         m_wallVertices = NULL;          // deletion handled by Object.cpp
00557 
00558         if (m_walls) delete m_floor;
00559         m_floor = NULL;
00560         m_floorVertices = NULL;         // deletion handled by Object.cpp
00561 
00562         if (m_shadow) delete m_shadow;
00563         m_shadow = NULL;
00564         m_shadowVertices = NULL;                // deletion handled by Object.cpp
00565 
00566         if (m_water) delete m_water;
00567         m_water = NULL;
00568         m_waterVertices = NULL;         // deletion handled by Object.cpp
00569 
00570         mp_renderer->deleteTexture(m_texWall);
00571         mp_renderer->deleteTexture(m_texFloor);
00572         mp_renderer->deleteTexture(m_texWater);
00573         mp_renderer->deleteTexture(m_texWaterSurface);
00574 
00575         for (unsigned int i=0; i<mp_lvl->numModels; i++) {
00576                 m_models[i].clear();
00577         }
00578 
00579         LOG_SUCCESS(("Level object unloaded"));
00580 }
00581 
00582 void ObjLevel::draw() {
00583         m_floor->draw();
00584         m_walls->draw();
00585 }
00586 
00587 void ObjLevel::drawFloor() {
00588         //mp_renderer->setDepthFunc(EQUAL);
00589         mp_renderer->setTextures(m_texFloor);
00590         mp_renderer->apply();
00591         m_floor->draw();
00592 }
00593 
00594 void ObjLevel::drawWalls() {
00595         //mp_renderer->setDepthFunc(EQUAL);
00596         mp_renderer->setTextures(m_texWall);
00597         mp_renderer->apply();
00598         m_walls->draw();
00599 }
00600 
00601 void ObjLevel::drawShadow() {
00602 
00603         m_shadow->draw();
00604         return;
00644 }
00645 
00646 void ObjLevel::drawWater() {
00647         //mp_renderer->setDepthFunc(EQUAL);
00648         mp_renderer->setTextures(m_texWater);
00649         mp_renderer->apply();
00650         m_water->draw();
00651 }
00652 
00653 void ObjLevel::drawWaterSurface() {
00654         Vertex *vertices = (Vertex *)m_waterSurface->getVertices();
00655 
00656         static float angle = 0.0f;
00657 
00658         mp_renderer->setBlending(ONE, ONE);
00659         mp_renderer->setTextures(m_texWaterSurface);
00660         mp_renderer->apply();
00661 
00662         //m_waterSurface->draw();
00663         m_waterSurface->draw();
00664 
00665         int n = 0;
00666         float sinfa = sinf(angle)*0.035f*mp_lvl->waterSurfaceSpeed;
00667         float dtv = g_frameTime*0.0003f*mp_lvl->waterSurfaceSpeed;
00668         for (int i=0; i<waterSurfaceIndex; i+=1) {
00669                 vertices[i].tv += dtv;
00670                 
00671                 if (n < 2)
00672                         vertices[i].tu = 0.0f + sinfa;
00673                 else
00674                         vertices[i].tu = 1.0f + sinfa;
00675 
00676                 if (++n == 4)
00677                         n = 0;
00678         }
00679 
00680         angle += g_frameTime*0.001f;
00681 
00682         m_waterSurface->draw();
00683 }
00684 
00685 void ObjLevel::createVertexBuffer() {
00686         m_walls->createVertexBuffer();
00687         m_floor->createVertexBuffer();
00688         m_shadow->createVertexBuffer();
00689         m_water->createVertexBuffer();
00690         //m_waterSurface->createVertexBuffer();
00691 }
00692 
00693 void ObjLevel::clearVertexBuffer() {
00694         m_walls->clearVertexBuffer();
00695         m_floor->clearVertexBuffer();
00696         m_shadow->clearVertexBuffer();
00697         m_water->clearVertexBuffer();
00698         //m_waterSurface->clearVertexBuffer();
00699 }
00700 
00701 bool ObjLevel::vertEqual(Vert &a, Vert &b) {
00702         return (a.x == b.x && a.y == b.y && a.z == b.z);
00703 }
00704 
00705 bool ObjLevel::isWater(Vec3 pos) {
00706         int x = (int)round(pos.x);
00707         int z = (int)round(pos.z);
00708         int offset = z*mp_lvl->width*3 + x*3;
00709         return isColor(&mp_lvl->data[offset], 0, 0, 240);
00710 }
00711 
00712 bool ObjLevel::isWall(Vec3 pos) {
00713         int x = (int)round(pos.x);
00714         int z = (int)round(pos.z);
00715         int offset = z*mp_lvl->width*3 + x*3;
00716         return isWall(&mp_lvl->data[offset]);
00717 }
00718 
00719 bool ObjLevel::collission(Vec3 &pos, float radius) {
00720         int x;
00721         int z;
00722         int offset;
00723         //int len = mp_lvl->width*mp_lvl->height*3;
00724 
00725         for (int i=-1; i<=1; i++) {
00726                 for (int n=-1; n<=1; n++) {
00727                         if (i == 0 && n == 0)
00728                                 continue;
00729 
00730                         x = (int)round(pos.x + i*radius);
00731                         z = (int)round(pos.z + n*radius);
00732                         offset = z*mp_lvl->width*3 + x*3;
00733                         if (mp_lvl->data[offset] != 15 && !isColor(&mp_lvl->data[offset], 0, 0,  240))
00734                                 return true;
00735                 }
00736         }
00737 
00738         return false;
00739 }
00740 
00741 Vec3 ObjLevel::getSpawnpool() {
00742         static unsigned int last = 0;
00743 
00744         if (m_nSpawnpools == 0)
00745                 return Vec3(5.0f, 0.0f, 5.0f);
00746         else if (m_nSpawnpools == 1)
00747                 return m_spawnpools[0];
00748 
00749 
00750         unsigned int i = 0;
00751         unsigned int n = 0;
00752 
00753         srand(GetTickCount()+last);
00754 
00755         do {
00756                 i = round(((float)rand() / (float)RAND_MAX)*(m_nSpawnpools-1));
00757         } while (i == last && n++ < 10);
00758 
00759         last = i;
00760 
00761         return m_spawnpools[last];
00762 }
00763 
00764 Vec3 ObjLevel::getExtras() {
00765         static unsigned int last = 0;
00766 
00767         if (m_nExtras == 0)
00768                 return Vec3(5.0f, 0.0f, 5.0f);
00769         else if (m_nExtras == 1)
00770                 return m_extras[0];
00771 
00772         unsigned int i = 0;
00773         unsigned int n = 0;
00774 
00775         srand(GetTickCount()+last);
00776 
00777         do {
00778                 i = round(((float)rand() / (float)RAND_MAX)*(m_nExtras-1));
00779         } while (i == last && n++ < 10);
00780 
00781         last = i;
00782 
00783         return m_extras[last];
00784 }
00785 
00786 void ObjLevel::drawObjects() {
00787         for (unsigned int i=0; i<m_numObjects; i++) {
00788                 if (m_objects[i].alive) {
00789                         glPushMatrix();
00790                                 glTranslatef(m_objects[i].x, m_objects[i].y, m_objects[i].z);
00791                                 glScalef(0.05f, 0.05f, 0.05f);
00792                                 m_models[m_objects[i].model].render();
00793                         glPopMatrix();
00794                 }
00795         }
00796 }

Generated on Sun Jun 5 15:47:04 2005 for Defacto by  doxygen 1.4.3