absence_thelab

annotate src/dungeonpart.cpp @ 0:1cffe3409164

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 01:46:07 +0300
parents
children
rev   line source
nuclear@0 1 #include "dungeonpart.h"
nuclear@0 2 #include "d3dx8.h"
nuclear@0 3
nuclear@0 4 DungeonPart::DungeonPart(GraphicsContext *gc) {
nuclear@0 5
nuclear@0 6 this->gc = gc;
nuclear@0 7
nuclear@0 8 SceneLoader::SetNormalFileSaving(true);
nuclear@0 9 SceneLoader::SetDataPath("data/textures/");
nuclear@0 10 SceneLoader::LoadScene("data/geometry/scene2.3ds", &scene);
nuclear@0 11
nuclear@0 12 // setup camera paths
nuclear@0 13 CamPath[0] = scene->GetCurve("cpath01");
nuclear@0 14 CamPath[1] = scene->GetCurve("cpath02");
nuclear@0 15 CamPath[2] = scene->GetCurve("cpath03");
nuclear@0 16 TargPath[0] = scene->GetCurve("ctarget01");
nuclear@0 17 TargPath[1] = scene->GetCurve("ctarget03");
nuclear@0 18
nuclear@0 19 CamPath[0]->SetArcParametrization(true);
nuclear@0 20 CamPath[1]->SetArcParametrization(true);
nuclear@0 21 CamPath[2]->SetArcParametrization(true);
nuclear@0 22 TargPath[0]->SetArcParametrization(true);
nuclear@0 23 TargPath[1]->SetArcParametrization(true);
nuclear@0 24
nuclear@0 25
nuclear@0 26 cam[0] = scene->GetCamera("Camera01");
nuclear@0 27 cam[1] = scene->GetCamera("Camera02");
nuclear@0 28 cam[2] = scene->GetCamera("Camera03");
nuclear@0 29 cam[3] = scene->GetCamera("FreeCam");
nuclear@0 30
nuclear@0 31 cam[0]->SetCameraPath(CamPath[0], TargPath[0], 0, 40000);
nuclear@0 32 cam[1]->SetCameraPath(CamPath[1], 0, 40000, 45000);
nuclear@0 33 cam[2]->SetCameraPath(CamPath[2], TargPath[1], 45000, 75000);
nuclear@0 34
nuclear@0 35 // get the lava crust under control
nuclear@0 36 LavaCrust = scene->GetObject("Plane02");
nuclear@0 37 scene->RemoveObject(LavaCrust);
nuclear@0 38
nuclear@0 39 // get the shadow-map walls under control
nuclear@0 40 ShadowObj[0] = scene->GetObject("TunnelLigh");
nuclear@0 41 scene->RemoveObject(ShadowObj[0]);
nuclear@0 42 ShadowObj[1] = scene->GetObject("TunnelLig0");
nuclear@0 43 scene->RemoveObject(ShadowObj[1]);
nuclear@0 44
nuclear@0 45 // load the flame textures
nuclear@0 46 for(int i=0; i<FLAME_TEXTURES; i++) {
nuclear@0 47 char num[3];
nuclear@0 48 itoa(i, num, 10);
nuclear@0 49 string fname = string("data/textures/flame/flame") + (i < 10 ? string("0") : string("")) + string(num) + string(".jpg");
nuclear@0 50 FlameTex[i] = gc->texman->AddTexture(fname.c_str());
nuclear@0 51 }
nuclear@0 52
nuclear@0 53 // get the flame objects and remove them from the scene (to take over the rendering)
nuclear@0 54 for(int i=0; i<16; i++) {
nuclear@0 55 char num[3];
nuclear@0 56 itoa(i, num, 10);
nuclear@0 57 string name = string("Fire") + (i < 10 ? string("0") : string("")) + string(num);
nuclear@0 58
nuclear@0 59 Flame[i] = scene->GetObject(name.c_str());
nuclear@0 60 scene->RemoveObject(Flame[i]);
nuclear@0 61 }
nuclear@0 62
nuclear@0 63 LightRays = scene->GetObject("LightRays");
nuclear@0 64 scene->RemoveObject(LightRays);
nuclear@0 65
nuclear@0 66 Floor[0] = scene->GetObject("floor1");
nuclear@0 67 Floor[1] = scene->GetObject("floor2");
nuclear@0 68 Floor[2] = scene->GetObject("floor3");
nuclear@0 69 for(int i=0; i<3; i++) scene->RemoveObject(Floor[i]);
nuclear@0 70
nuclear@0 71 Obj = scene->GetObject("DefSphere");
nuclear@0 72 scene->RemoveObject(Obj);
nuclear@0 73
nuclear@0 74 mobj = new Object(gc);
nuclear@0 75 mobj->GetTriMesh()->SetData(Obj->GetTriMesh()->GetVertexArray(), Obj->GetTriMesh()->GetTriangleArray(), Obj->GetTriMesh()->GetVertexCount(), Obj->GetTriMesh()->GetTriangleCount());
nuclear@0 76
nuclear@0 77 Obj->material.SetTexture(gc->texman->AddTexture("data/textures/rusty01.jpg"), TextureMap);
nuclear@0 78 Obj->material.SetTexture(gc->texman->AddTexture("data/textures/refmap1.jpg"), EnvironmentMap);
nuclear@0 79
nuclear@0 80 Obj->material.SetAmbient(Color(0.5f));
nuclear@0 81 Obj->material.SetDiffuse(Color(0.5f));
nuclear@0 82 Obj->material.SetSpecular(Color(175.0f / 256.0f, 95.0f / 256.0f, 17.0f / 256.0f));
nuclear@0 83 Obj->material.SetSpecularPower(90.0f);
nuclear@0 84 Obj->material.SpecularEnable = true;
nuclear@0 85 mobj->material = Obj->material;
nuclear@0 86
nuclear@0 87 Obj->GetTriMesh()->ChangeMode(TriMeshDynamic);
nuclear@0 88 mobj->GetTriMesh()->ChangeMode(TriMeshDynamic);
nuclear@0 89
nuclear@0 90 Crystals[0] = scene->GetObject("Box114");
nuclear@0 91 Crystals[1] = scene->GetObject("Box115");
nuclear@0 92 Crystals[2] = scene->GetObject("Box116");
nuclear@0 93 Crystals[3] = scene->GetObject("Box117");
nuclear@0 94 Crystals[4] = scene->GetObject("Box118");
nuclear@0 95
nuclear@0 96 for(int i=0; i<5; i++) {
nuclear@0 97 scene->RemoveObject(Crystals[i]);
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 Name = new Object(gc);
nuclear@0 101 Name->CreatePlane(1.7f, 0);
nuclear@0 102 Name->Scale(1.0f, 0.3f, 1.0f);
nuclear@0 103
nuclear@0 104 Fade = new Object(gc);
nuclear@0 105 Fade->CreatePlane(3.0f, 0);
nuclear@0 106 Fade->material = Material(0.0f, 0.0f, 0.0f);
nuclear@0 107
nuclear@0 108 // load name textures
nuclear@0 109 for(int i=0; i<8; i++) {
nuclear@0 110 char fname[] = "data/textures/Absence/abx.jpg";
nuclear@0 111 fname[24] = '1' + i;
nuclear@0 112 NameTex[i] = gc->texman->AddTexture(fname);
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 cam[3]->Zoom(-1.0f);
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 DungeonPart::~DungeonPart() {
nuclear@0 119 delete scene;
nuclear@0 120 }
nuclear@0 121
nuclear@0 122 #define INRANGE(a, b) (msec >= a && msec < b)
nuclear@0 123
nuclear@0 124 void DungeonPart::MainLoop() {
nuclear@0 125 dword msec = timer.GetMilliSec();
nuclear@0 126 float t = (float)msec / 1000.0f;
nuclear@0 127
nuclear@0 128 // set the active camera
nuclear@0 129 for(int i=0; i<3; i++) {
nuclear@0 130 cam[i]->FollowPath(msec);
nuclear@0 131 }
nuclear@0 132 Vector3 Cam3Pos = cam[3]->GetPosition();
nuclear@0 133
nuclear@0 134 if(msec >= 0 && msec < 40000) {
nuclear@0 135 scene->SetActiveCamera(cam[0]);
nuclear@0 136 }
nuclear@0 137 if(msec >= 39990 && msec < 45000) {
nuclear@0 138 scene->SetActiveCamera(cam[1]);
nuclear@0 139 }
nuclear@0 140 if(msec >= 45000 && msec < 75000) {
nuclear@0 141 scene->SetActiveCamera(cam[2]);
nuclear@0 142 }
nuclear@0 143
nuclear@0 144 if(INRANGE(17000, 18000) || INRANGE(22000, 23000)) {
nuclear@0 145 if(INRANGE(22000, 23000)) {
nuclear@0 146 cam[3]->Rotate(0.0f, t, 0.0f);
nuclear@0 147 } else {
nuclear@0 148 cam[3]->Rotate(0.0f, -t, 0.0f);
nuclear@0 149 }
nuclear@0 150 scene->SetActiveCamera(cam[3]);
nuclear@0 151 }
nuclear@0 152
nuclear@0 153 gc->Clear(0);
nuclear@0 154 gc->ClearZBufferStencil(1.0f, 0);
nuclear@0 155
nuclear@0 156
nuclear@0 157 scene->Render();
nuclear@0 158
nuclear@0 159 // Render flames
nuclear@0 160 gc->SetLighting(false);
nuclear@0 161 gc->SetZWrite(false);
nuclear@0 162 int ftexnum = (int)((float)msec / 33.35f) % FLAME_TEXTURES;
nuclear@0 163 gc->SetAlphaBlending(true);
nuclear@0 164 gc->SetBackfaceCulling(false);
nuclear@0 165 gc->SetBlendFunc(BLEND_ONE, BLEND_ONE);
nuclear@0 166 gc->SetTextureStageColor(0, TexBlendModulate, TexArgTexture, TexArgCurrent);
nuclear@0 167 gc->SetTextureStageAlpha(0, TexBlendSelectArg1, TexArgTexture, TexArgTexture);
nuclear@0 168 gc->SetMaterial(Flame[0]->material);
nuclear@0 169 gc->SetTexture(1, 0);
nuclear@0 170 for(int i=0; i<16; i++) {
nuclear@0 171 gc->SetTexture(0, FlameTex[ftexnum]);
nuclear@0 172 gc->SetWorldMatrix(Flame[i]->GetWorldTransform());
nuclear@0 173 Flame[i]->RenderBare();
nuclear@0 174 }
nuclear@0 175 gc->SetBackfaceCulling(true);
nuclear@0 176 gc->SetAlphaBlending(false);
nuclear@0 177 gc->SetZWrite(true);
nuclear@0 178 gc->SetLighting(true);
nuclear@0 179
nuclear@0 180 // render the shadow walls
nuclear@0 181 gc->SetAlphaBlending(true);
nuclear@0 182 gc->SetBlendFunc(BLEND_DESTCOLOR, BLEND_ZERO);
nuclear@0 183 gc->SetTexture(0, ShadowObj[0]->material.Maps[0]);
nuclear@0 184 gc->SetTextureStageColor(0, TexBlendModulate, TexArgTexture, TexArgDiffuseColor);
nuclear@0 185 gc->SetMaterial(ShadowObj[0]->material);
nuclear@0 186 gc->SetWorldMatrix(ShadowObj[0]->GetWorldTransform());
nuclear@0 187 ShadowObj[0]->RenderBare();
nuclear@0 188 gc->SetWorldMatrix(ShadowObj[1]->GetWorldTransform());
nuclear@0 189 ShadowObj[1]->RenderBare();
nuclear@0 190 gc->SetAlphaBlending(false);
nuclear@0 191
nuclear@0 192 // render volumetric light
nuclear@0 193 gc->SetAlphaBlending(true);
nuclear@0 194 gc->SetZWrite(false);
nuclear@0 195
nuclear@0 196
nuclear@0 197 LightRays->ResetScaling();
nuclear@0 198
nuclear@0 199 LightRays->material.Alpha = 0.02f;
nuclear@0 200 LightRays->material.SetEmissive(1.0f, 1.0f, 1.0f);
nuclear@0 201 for(int i=0; i<14; i++) {
nuclear@0 202 LightRays->Scale(0.975f, 1.0f, 0.975f);
nuclear@0 203 LightRays->Render();
nuclear@0 204 }
nuclear@0 205 gc->SetZWrite(true);
nuclear@0 206 gc->SetAlphaBlending(false);
nuclear@0 207
nuclear@0 208
nuclear@0 209 // The Morphing Object
nuclear@0 210
nuclear@0 211 Obj->GetTriMesh()->SetData(mobj->GetTriMesh()->GetVertexArray(), mobj->GetTriMesh()->GetTriangleArray(), mobj->GetTriMesh()->GetVertexCount(), mobj->GetTriMesh()->GetTriangleCount());
nuclear@0 212
nuclear@0 213 dword VertCount = Obj->GetTriMesh()->GetVertexCount();
nuclear@0 214 Vertex *verts = Obj->GetTriMesh()->GetModVertexArray();
nuclear@0 215 for(dword i=0; i<VertCount; i++) {
nuclear@0 216 float uangle = DotProduct(verts[i].pos.Normalized(), VECTOR3_I);
nuclear@0 217 float vangle = DotProduct(verts[i].pos.Normalized(), VECTOR3_J);
nuclear@0 218
nuclear@0 219 float sfact = 0.3f * (sinf(uangle * cosf(t) * 5.0f) + sinf(vangle * 7.0f * sinf(t)) + cosf(uangle * cosf(t*2.0f) * 3.0f) * 1.5f);
nuclear@0 220 verts[i].pos += verts[i].pos * sfact;
nuclear@0 221 }
nuclear@0 222
nuclear@0 223
nuclear@0 224 Obj->GetTriMesh()->CalculateNormals();
nuclear@0 225 Obj->Render();
nuclear@0 226
nuclear@0 227
nuclear@0 228 // make the lava move
nuclear@0 229 Matrix3x3 TexMat;
nuclear@0 230 TexMat.m[2][0] = t/50.0f;
nuclear@0 231 TexMat.m[2][1] = t/70.0f;
nuclear@0 232 gc->SetTextureMatrix(TexMat);
nuclear@0 233 LavaCrust->Render();
nuclear@0 234 gc->SetTextureMatrix(Matrix4x4());
nuclear@0 235
nuclear@0 236 /////// Render the Crystals ///////////
nuclear@0 237 /*
nuclear@0 238 float StartRise = 4;//53.0f;
nuclear@0 239 float EndRise = 8;//57.0f;
nuclear@0 240 float yoffset = -355.0f;
nuclear@0 241
nuclear@0 242 if(t >= StartRise && t < EndRise) {
nuclear@0 243 for(int i=0; i<5; i++) Crystals[i]->SetTranslation(0.0f, (t - StartRise) / (EndRise - StartRise), 0.0f);
nuclear@0 244 }
nuclear@0 245 */
nuclear@0 246 gc->SetShadingMode(FlatShading);
nuclear@0 247 for(int i=0; i<5; i++) {
nuclear@0 248 Crystals[i]->Render();
nuclear@0 249 }
nuclear@0 250 gc->SetShadingMode(GouraudShading);
nuclear@0 251
nuclear@0 252
nuclear@0 253 //////////////// render the mirroring marble floor ///////////////
nuclear@0 254
nuclear@0 255 // burn the stencil baby
nuclear@0 256 gc->SetZWrite(false);
nuclear@0 257 gc->SetColorWrite(false, false, false, false);
nuclear@0 258 gc->SetStencilBuffering(true);
nuclear@0 259 gc->SetStencilFunc(CMP_ALWAYS);
nuclear@0 260 gc->SetStencilOp(SOP_KEEP, SOP_KEEP, SOP_INC);
nuclear@0 261
nuclear@0 262 for(int i=0; i<3; i++) Floor[i]->Render();
nuclear@0 263 gc->SetZWrite(true);
nuclear@0 264 gc->SetColorWrite(true, true, true, true);
nuclear@0 265 gc->SetStencilOp(SOP_KEEP, SOP_KEEP, SOP_KEEP);
nuclear@0 266
nuclear@0 267 // render the rest of the floor normally
nuclear@0 268 Floor[0]->ResetRotation();
nuclear@0 269 Floor[0]->Rotate(0.0f, t/2.0f, 0.0f);
nuclear@0 270 Floor[1]->ResetRotation();
nuclear@0 271 Floor[1]->Rotate(0.0f, -t/4.0f, 0.0f);
nuclear@0 272
nuclear@0 273 for(int i=0; i<3; i++) Floor[i]->Render();
nuclear@0 274
nuclear@0 275 // render the reflection where stencil > 0
nuclear@0 276 gc->SetStencilFunc(CMP_EQUAL);
nuclear@0 277 gc->SetStencilReference(1);
nuclear@0 278 gc->ClearZBuffer(1.0f);
nuclear@0 279
nuclear@0 280 gc->SetFrontFace(CounterClockwise);
nuclear@0 281
nuclear@0 282 std::list<Object*> *objlist = scene->GetObjectsList();
nuclear@0 283 std::list<Object*>::iterator iter = objlist->begin();
nuclear@0 284 while(iter != objlist->end()) {
nuclear@0 285 (*iter)->Scale(1.0f, -1.0f, 1.0f);
nuclear@0 286 float alpha = (*iter)->material.Alpha;
nuclear@0 287 (*iter)->material.Alpha = 0.25f;
nuclear@0 288 (*iter)->Render();
nuclear@0 289 (*iter)->material.Alpha = alpha;
nuclear@0 290 (*iter++)->Scale(1.0f, -1.0f, 1.0f);
nuclear@0 291 }
nuclear@0 292
nuclear@0 293 gc->SetFrontFace(Clockwise);
nuclear@0 294 gc->SetStencilBuffering(false);
nuclear@0 295
nuclear@0 296 if(t < 6.0f) {
nuclear@0 297 gc->SetAlphaBlending(true);
nuclear@0 298 gc->SetLighting(false);
nuclear@0 299 gc->SetZBuffering(false);
nuclear@0 300 gc->SetBlendFunc(BLEND_ONE, BLEND_ONE);
nuclear@0 301 gc->SetMaterial(Material(1.0f, 1.0f, 1.0f));
nuclear@0 302 gc->SetTexture(0, NameTex[(msec >> 6) % 8]);
nuclear@0 303 gc->SetTextureStageColor(0, TexBlendSelectArg1, TexArgTexture, TexArgTexture);
nuclear@0 304 gc->SetTextureStageAlpha(0, TexBlendSelectArg1, TexArgTexture, TexArgTexture);
nuclear@0 305 gc->SetWorldMatrix(Name->GetWorldTransform());
nuclear@0 306 Matrix4x4 ViewMat;
nuclear@0 307 ViewMat.Translate(0.0f, 0.0f, 1.0f);
nuclear@0 308 gc->SetViewMatrix(ViewMat);
nuclear@0 309 Name->RenderBare();
nuclear@0 310 gc->SetLighting(true);
nuclear@0 311
nuclear@0 312 // fade in
nuclear@0 313 Fade->material.Alpha = min(max(0.0f, (2.0f - t)), 1.0f);
nuclear@0 314 Fade->Render();
nuclear@0 315
nuclear@0 316 gc->SetZBuffering(true);
nuclear@0 317 gc->SetAlphaBlending(false);
nuclear@0 318 }
nuclear@0 319
nuclear@0 320 cam[3]->SetPosition(Cam3Pos);
nuclear@0 321 }