absence_thelab

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