00001
00002
00003 #include "CoreEngine.h"
00004 #include "DynamicCity.h"
00005 #include "EffectTest.h"
00006 #include "GLApplication.h"
00007 #include "GPUFire.h"
00008 #include "GPUWater.h"
00009 #include "Light.h"
00010 #include "LightShader.h"
00011 #include "MathUtils.h"
00012 #include "Mesh.h"
00013 #include "MeshModel.h"
00014 #include "MeshFactory.h"
00015 #include "Octree.h"
00016 #include "ParticleSystem.h"
00017 #include "Quadtree.h"
00018 #include "SingleTexturedMesh.h"
00019 #include "SGFFile.h"
00020 #include "SGFMeshModel.h"
00021 #include "SGFMeshModelFactory.h"
00022 #include "SkyBox.h"
00023 #include "Terrain.h"
00024 #include "TextureDatabase.h"
00025 #include "Vector3f.h"
00026 #include "World.h"
00027 #include "XMLDocument.h"
00028
00029 #include <stdlib.h>
00030
00031
00032 using namespace pge;
00033
00034
00035
00036
00037
00038 void createGrass(Vector3f bottomCenter, float width, float height, Triangle *tr0, Triangle *tr1,
00039 Triangle *tr2, Triangle *tr3) {
00040
00041 float halfWidth = width / 2.0f;
00042
00043 Vector3f topLeft = Vector3f(bottomCenter.m_v[0] - halfWidth, bottomCenter.m_v[1] + height,
00044 bottomCenter.m_v[2]);
00045 Vector3f bottomLeft = Vector3f(bottomCenter.m_v[0] - halfWidth, bottomCenter.m_v[1],
00046 bottomCenter.m_v[2]);
00047 Vector3f bottomRight = Vector3f(bottomCenter.m_v[0] + halfWidth, bottomCenter.m_v[1],
00048 bottomCenter.m_v[2]);
00049 Vector3f topRight = Vector3f(bottomCenter.m_v[0] + halfWidth, bottomCenter.m_v[1] + height,
00050 bottomCenter.m_v[2]);
00051
00052
00053 tr0->m_vertices[0] = topLeft;
00054 tr0->m_vertices[1] = bottomLeft;
00055 tr0->m_vertices[2] = bottomRight;
00056 tr0->m_texCoords[0] = Vector2f(0.0f, 0.0f);
00057 tr0->m_texCoords[1] = Vector2f(0.0f, 1.0f);
00058 tr0->m_texCoords[2] = Vector2f(1.0f, 1.0f);
00059
00060 tr1->m_vertices[0] = topLeft;
00061 tr1->m_vertices[1] = bottomRight;
00062 tr1->m_vertices[2] = topRight;
00063 tr1->m_texCoords[0] = Vector2f(0.0f, 0.0f);
00064 tr1->m_texCoords[1] = Vector2f(1.0f, 1.0f);
00065 tr1->m_texCoords[2] = Vector2f(1.0f, 0.0f);
00066
00067 topLeft = Vector3f(bottomCenter.m_v[0], bottomCenter.m_v[1] + height, bottomCenter.m_v[2] - halfWidth);
00068 bottomLeft = Vector3f(bottomCenter.m_v[0], bottomCenter.m_v[1], bottomCenter.m_v[2] - halfWidth);
00069 bottomRight = Vector3f(bottomCenter.m_v[0], bottomCenter.m_v[1], bottomCenter.m_v[2] + halfWidth);
00070 topRight = Vector3f(bottomCenter.m_v[0], bottomCenter.m_v[1] + height, bottomCenter.m_v[2] + halfWidth);
00071
00072 tr2->m_vertices[0] = topLeft;
00073 tr2->m_vertices[1] = bottomLeft;
00074 tr2->m_vertices[2] = bottomRight;
00075 tr2->m_texCoords[0] = Vector2f(0.0f, 0.0f);
00076 tr2->m_texCoords[1] = Vector2f(0.0f, 1.0f);
00077 tr2->m_texCoords[2] = Vector2f(1.0f, 1.0f);
00078
00079 tr3->m_vertices[0] = topLeft;
00080 tr3->m_vertices[1] = bottomRight;
00081 tr3->m_vertices[2] = topRight;
00082 tr3->m_texCoords[0] = Vector2f(0.0f, 0.0f);
00083 tr3->m_texCoords[1] = Vector2f(1.0f, 1.0f);
00084 tr3->m_texCoords[2] = Vector2f(1.0f, 0.0f);
00085 }
00086
00087
00088
00089
00090
00091 MeshModel* createGrass(MeshModel *terrain, float startX, float startZ, float width, float depth) {
00092 float endX = startX + width;
00093 float endZ = startZ + depth;
00094 float diffX = mathutils::absf(endX - startX);
00095 float diffZ = mathutils::absf(endZ - startZ);
00096 float sx;
00097 float sz;
00098 int grassNum;
00099 int tileX;
00100 int tileZ;
00101 MeshModel *model;
00102
00103
00104 tileX = 10;
00105 tileZ = 10;
00106 grassNum = 30;
00107
00108 model = new MeshModel();
00109
00110 Texture *tex = TextureDatabase::getInstance()->addTexture("grass_0.bmp", "grass", true, true);
00111
00112 for(int z = 0; z < tileZ; z++) {
00113 for(int x = 0; x < tileX; x++) {
00114 sx = startX + (float)x * (diffX / tileX);
00115 sz = startZ + (float)z * (diffZ / tileZ);
00116
00117
00118 SingleTexturedMesh *mesh = new SingleTexturedMesh(true);
00119
00120 for(int i = 0; i < grassNum; i++) {
00121 float gx = sx + (mathutils::random() * (diffX / tileX));
00122 float gz = sz + (mathutils::random() * (diffZ / tileZ));
00123 float height = 0.9f + mathutils::random() * 0.2f;
00124
00125 Triangle tr0;
00126 Triangle tr1;
00127 Triangle tr2;
00128 Triangle tr3;
00129
00130 createGrass(Vector3f(gx, 0.0f, gz), height, height, &tr0, &tr1, &tr2, &tr3);
00131 mesh->addTriangle(tr0);
00132 mesh->addTriangle(tr1);
00133 mesh->addTriangle(tr2);
00134 mesh->addTriangle(tr3);
00135 }
00136
00137 mesh->setTexture(tex);
00138 model->addMesh(mesh);
00139 }
00140 }
00141 return model;
00142 }
00143
00144
00145 void addModels(World *world, MeshModel *terrain, float startX, float startZ, float width, float depth, int num) {
00146 int i;
00147 float endX = startX + width;
00148 float endZ = startZ + depth;
00149 float x;
00150 float z;
00151 float y;
00152
00153 SGFFile file("../data/models/test.sgf");
00154
00155
00156 for(i = 0; i < num; i++) {
00157 x = 0.0f;
00158 z = 0.0f;
00159 y = terrain->getHeightAt(x, z);
00160
00161 SGFMeshModel *model = new SGFMeshModel(&file, Vector3f(x, y, z), 1.0f);
00162 world->addRenderableObject(model);
00163 }
00164 }
00165
00166
00167
00168
00169
00170 int main(int argc, char *argv[]) {
00171
00172
00173
00174 GLApplication *glApp;
00175 World *world;
00176
00177
00178 xmlInitParser();
00179 LIBXML_TEST_VERSION
00180
00181
00182 glApp = new GLApplication(800, 600, 24, true, "World Of Phobos");
00183 if(!glApp->isWindowOpened()) {
00184 exit(0);
00185 }
00186
00187 world = new World();
00188 CoreEngine::getInstance()->setCurrentWorld(world);
00189
00190
00192
00194
00195
00196 EffectTest *lightEffects = new EffectTest();
00197
00198
00199 SkyBox *sky = new SkyBox("sky", 10.0f);
00200 world->setSky(sky);
00201
00202
00203 Terrain *terrain = new Terrain("../data/textures/heightmap.tga", "gras_4_dl.tga", Vector3f(-400.0f, -20.0f, -400.0f), 0.1f, 100, 100, 800.0f, 800.0f);
00204 Quadtree *terrainTree = new Quadtree(terrain, 10, false);
00205 world->addRenderableObject(terrainTree, true);
00206
00207
00208 GPUWater *water = new GPUWater(Vector3f(-1500.0f, -10.0f, -1500.0f), 3000.0f, 3000.0f);
00209 world->addRenderableObject(water, true);
00210
00211
00212 MeshModel *worldModel = new MeshModel();
00213
00214 SGFMeshModel *model0 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/wall.sgf", Vector3f(0.0f, terrain->getHeightAt(0.0f, 0.0f), 0.0f), 1.0f);
00215 SGFMeshModel *model1 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(0.0f, terrain->getHeightAt(0.0f, 0.0f), 0.0f), 1.0f);
00216 SGFMeshModel *model2 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/well.sgf", Vector3f(10.0f, terrain->getHeightAt(0.0f, 0.0f), 10.0f), 0.5f);
00217 SGFMeshModel *model3 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/fountain.sgf", Vector3f(-10.0f, terrain->getHeightAt(0.0f, 0.0f), 20.0f), 0.6f);
00218
00219 SGFMeshModel *model4 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(10.0f, terrain->getHeightAt(0.0f, 0.0f), -10.0f), 1.0f);
00220 SGFMeshModel *model5 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(20.0f, terrain->getHeightAt(0.0f, 0.0f), 20.0f), 1.0f);
00221 SGFMeshModel *model6 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(10.0f, terrain->getHeightAt(0.0f, 0.0f), 20.0f), 1.0f);
00222 SGFMeshModel *model7 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(-20.0f, terrain->getHeightAt(0.0f, 0.0f), 20.0f), 1.0f);
00223 SGFMeshModel *model8 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(-20.0f, terrain->getHeightAt(0.0f, 0.0f), -15.0f), 1.0f);
00224 SGFMeshModel *model9 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/house.sgf", Vector3f(-20.0f, terrain->getHeightAt(0.0f, 0.0f), -25.0f), 1.0f);
00225
00226 SGFMeshModel *model10 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/tower.sgf", Vector3f(30.0f, terrain->getHeightAt(0.0f, 0.0f), -30.0f), 1.0f);
00227
00228 SGFMeshModel *model11 = sgfmeshmodelfactory::createSGFMeshModel("../data/models/fireplace.sgf", Vector3f(20.0f, terrain->getHeightAt(0.0f, 0.0f) + 0.01f, 5.0f), 0.4f);
00229
00230 worldModel->addMeshModel(model0);
00231 worldModel->addMeshModel(model1);
00232 worldModel->addMeshModel(model2);
00233 worldModel->addMeshModel(model3);
00234 worldModel->addMeshModel(model4);
00235 worldModel->addMeshModel(model5);
00236 worldModel->addMeshModel(model6);
00237 worldModel->addMeshModel(model7);
00238 worldModel->addMeshModel(model8);
00239 worldModel->addMeshModel(model9);
00240 worldModel->addMeshModel(model10);
00241 worldModel->addMeshModel(model11);
00242
00243 Octree *worldTree = new Octree(worldModel, 10, false);
00244 world->addRenderableObject(worldTree);
00245
00246
00247 GPUFire *fire = new GPUFire(Vector3f(20.5f, -0.5f, 5.7f), 1.0f);
00248 world->addRenderableObject(fire);
00249
00250
00251 ParticleSystem *p = new ParticleSystem("waterparticle_0.tga", Vector3f(-10.0f, 2.5f, 20.0f), Vector3f(0.1f, 1.0f, 0.1f), 1000);
00252 p->setLifeDegeneration(0.04f);
00253 p->setVelocity(0.005f);
00254 p->setDistribution(100);
00255
00256 p->setFadeOutEnabled(true);
00257 p->setFadeOutStart(Vector3f(-10.0f, 2.0f, 20.0f));
00258 p->setFadeOutEnd(Vector3f(-10.0f, 1.0f, 20.0f));
00259 p->activate();
00260 world->addRenderableObject(p);
00261
00262
00263 lightEffects->setBoundingBox(worldModel->getBoundingBox());
00264 world->addRenderableObject(lightEffects);
00265
00266 glApp->execute();
00267
00268 if(glApp != NULL) {
00269 delete glApp;
00270 }
00271
00272 xmlCleanupParser();
00273
00274 return 0;
00275 }