-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscene_lighttest.cpp
436 lines (383 loc) · 16.3 KB
/
scene_lighttest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "scene.hpp"
#include "textrender.hpp"
#include "game.hpp"
#include "util.hpp"
#define _USE_MATH_DEFINES // For MSVC
#include <math.h>
#include <assert.h>
#include <wchar.h>
extern int WIN_W, WIN_H;
std::vector<ChunkGrid*> LightTestScene::model_backgrounds;
std::map<char, ChunkGrid*> LightTestScene::model_digits;
ChunkGrid* LightTestScene::model_clock;
extern bool g_main_menu_visible;
extern DirectionalLight* g_dir_light1;
extern ID3D11Buffer* g_lightscatter_cb;
float LightTestScene::ACTOR_INIT_Y = -38;
LightTestScene::LightTestScene() {
for (int i=0; i<6; i++)
bg_sprites[i] = new ChunkSprite(model_backgrounds[i]);
clock_sprite = new ChunkSprite(model_clock);
clock_sprite->pos.z = -35;
clock_sprite->pos.y = 50;
clock_sprite->scale = glm::vec3(2, 2, 2);
for (int i = 0; i < 20; i++) {
digit_sprites[i] = new ChunkSprite(model_digits[' ']);
ChunkSprite* s = digit_sprites[i];
s->scale = glm::vec3(2, 2, 2);
int y = i / 10, x = i % 10;
int xx = -90 + 20 * x, yy = clock_sprite->pos.y - 12 + 32 * (1 - y);
s->pos.z = clock_sprite->pos.z + 1;
s->pos.x = xx;
s->pos.y = yy;
}
bg_sprites[1]->pos.x = -100; // Left
bg_sprites[2]->pos.x = 100; // Right
bg_sprites[3]->pos.y = 100; // Top
bg_sprites[4]->pos = glm::vec3(-100, 100, 0); // Top-Left
bg_sprites[5]->pos = glm::vec3(100, 100, 0);
camera = new Camera();
camera->pos = glm::vec3(0, 23, 180);
camera->lookdir = glm::vec3(0, 0, -1);
camera->up = glm::vec3(0, 1, 0);
lights[0] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(-100, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
lights[1] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(-60, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
lights[2] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(-20, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
lights[3] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(20, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
lights[4] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(60, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
lights[5] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(100, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
for (int i=6; i<16; i++)
lights[i] = new DirectionalLight(glm::vec3(0, -1, 0), glm::vec3(100, 200, 0), glm::vec3(1, 0, 0), 13 * 3.14159f / 180.0f);
for (int i = 0; i < 16; i++) {
char_sprites[i] = new ChunkSprite(ClimbScene::model_char);
int x = i / 4, z = i % 4;
ChunkSprite* s = char_sprites[i];
s->scale = glm::vec3(1.5, 1.5, 1.5);
const float DX = 66, DZ = 33;
s->pos.x = -DX + 2 * DX / 3.0 * x;
s->pos.z = -DZ + 2 * DZ / 3.0 * z;
s->pos.y = ACTOR_INIT_Y;
if (z % 2) s->pos.x -= DZ / 3; else s->pos.x += DZ / 3;
deltays[i] = 0;
}
curr_light_angle = 0;
last_sec = 0;
total_secs = 0;
}
extern int g_scene_idx;
void LightTestScene::InitStatic() {
if (g_scene_idx != 2) return;
// Middle
model_backgrounds.push_back(new ChunkGrid("stage\\stage1.vox"));
// Left
model_backgrounds.push_back(new ChunkGrid("stage\\stage1_L.vox"));
// Right
model_backgrounds.push_back(new ChunkGrid("stage\\stage1_R.vox"));
// Top
model_backgrounds.push_back(new ChunkGrid("stage\\stage1_T.vox"));
// Top-Left
model_backgrounds.push_back(new ChunkGrid("stage\\stage1_TL.vox"));
// Top-Right
model_backgrounds.push_back(new ChunkGrid("stage\\stage1_TR.vox"));
std::string base = "C:\\Users\\nitroglycerine\\Downloads\\MagicaVoxel-0.99.1-alpha-win64\\vox\\climb\\";
model_clock = new ChunkGrid(std::string(base + "digitboard.vox").c_str());
for (int i = 0; i <= 9; i++) {
char buf[233];
sprintf(buf, "digit%d.vox", i);
model_digits['0' + i] = new ChunkGrid(std::string(base + std::string(buf)).c_str());
}
model_digits['-'] = new ChunkGrid(std::string(base + "digit_hyphen.vox").c_str());
model_digits[':'] = new ChunkGrid(std::string(base + "digit_colon.vox").c_str());
model_digits[' '] = new ChunkGrid(std::string(base + "digit_null.vox").c_str());
}
void LightTestScene::PrepareSpriteListForRender() {
g_main_menu_visible = false; // HACK
sprite_render_list.clear();
for (int i=0; i<sizeof(bg_sprites)/sizeof(bg_sprites[0]); i++)
sprite_render_list.push_back(bg_sprites[i]);
for (int i = 0; i < 16; i++)
sprite_render_list.push_back(char_sprites[i]);
sprite_render_list.push_back(clock_sprite);
for (int i = 0; i < 20; i++) {
char ch = ' ';
DigitState& st = digit_states[i];
// tween is started @ update
const float TWEEN_LEN = 0.2f;
if (st.tween_end_sec < total_secs) {
st.old_char = st.new_char;
digit_sprites[i]->scale.y = 2;
ch = st.new_char;
}
else if (total_secs > st.tween_end_sec - TWEEN_LEN) {
float completed = total_secs - (st.tween_end_sec - TWEEN_LEN);
float yscale = 2.0f;
if (completed < TWEEN_LEN / 2) {
ch = st.old_char;
yscale = 2 * (1.0f - completed / (TWEEN_LEN / 2));
}
else {
ch = st.new_char;
yscale = 2 * ((completed - TWEEN_LEN / 2) / (TWEEN_LEN / 2));
}
digit_sprites[i]->scale.y = yscale;
}
if (ch != ' ' && model_digits.find(ch) != model_digits.end()) {
digit_sprites[i]->chunk = model_digits[ch];
sprite_render_list.push_back(digit_sprites[i]);
}
}
}
std::vector<Sprite*>* LightTestScene::GetSpriteListForRender() {
return &sprite_render_list;
}
extern struct VolumetricLightCB g_vol_light_cb;
extern ID3D11DeviceContext *g_context11;
extern GraphicsAPI g_api;
extern ID3D11Buffer* g_perscene_cb_light11;
extern Camera* GetCurrentSceneCamera();
void LightTestScene::PrepareLights() {
if (g_api == ClimbOpenGL) return;
//UpdatePerSceneCB(&g_dir_light->GetDir_D3D11(), &(g_dir_light->GetPV_D3D11()), &(GetCurrentSceneCamera()->GetPos_D3D11()), nullptr);
D3D11_MAPPED_SUBRESOURCE mapped;
assert(SUCCEEDED(g_context11->Map(g_perscene_cb_light11, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)));
const int NC = 6;
float colors[][3] = {
{1,0,0}, {0,1,0}, {0,0,1}, {1,1,0}, {1,0,1}, {0,1,1}
};
float dz = abs(sin(curr_light_angle)), dy = -abs(cos(curr_light_angle));
if (GetMacroState(nullptr) == OLD_YEAR) {
for (int i = 0; i < 6; i++) {
int x = -100 + i * 40;
int cidx = 0;
g_vol_light_cb.spotlightColors[i].m128_f32[0] = colors[cidx][0];
g_vol_light_cb.spotlightColors[i].m128_f32[1] = colors[cidx][1];
g_vol_light_cb.spotlightColors[i].m128_f32[2] = colors[cidx][2];
lights[i]->pos.x = x;
lights[i]->pos.y = 200;
lights[i]->pos.z = 0;
lights[i]->dir = glm::normalize(glm::vec3(0, dy, dz));
g_vol_light_cb.spotlightPV[i] = lights[i]->GetPV_D3D11();
}
g_vol_light_cb.spotlightCount = 6;
g_vol_light_cb.aspect = WIN_W * 1.0f / WIN_H;
g_vol_light_cb.fovy = 60 * 3.14159f / 180.0f;
g_vol_light_cb.forceAlwaysOn = false;
}
else {
if (total_secs - last_update_total_secs > 0.1) {
last_update_total_secs = total_secs;
const float HX = 100.0f, HY = 80.0f, HZ = 50.0f, LIGHT_MULT = 3;
for (int i = 0; i < 16; i++) {
float x = rand() * 1.0f / RAND_MAX;
if (x < 0.3 || x > 0.7) { // Left wall
glm::vec3 p(-HX, -HY + 2 * HY*1.0f*rand() / RAND_MAX, -HZ + 2 * HZ*1.0f*rand() / RAND_MAX);
glm::vec3 q( HX, -HY + 2 * HY*1.0f*rand() / RAND_MAX, -HZ + 2 * HZ*1.0f*rand() / RAND_MAX);
if (x < 0.3) {
lights[i]->dir = glm::normalize(q - p);
lights[i]->pos = p;
}
else {
lights[i]->dir = glm::normalize(p - q);
lights[i]->pos = q;
}
int cidx = rand() % NC;
g_vol_light_cb.spotlightColors[i].m128_f32[0] = colors[cidx][0] * LIGHT_MULT;
g_vol_light_cb.spotlightColors[i].m128_f32[1] = colors[cidx][1] * LIGHT_MULT;
g_vol_light_cb.spotlightColors[i].m128_f32[2] = colors[cidx][2] * LIGHT_MULT;
g_vol_light_cb.spotlightPV[i] = lights[i]->GetPV_D3D11();
}
else {
glm::vec3 p(-HX + 2 * HX*rand()*1.0f / RAND_MAX, HY, -HZ + 2 * HZ*1.0f*rand() / RAND_MAX);
glm::vec3 q(-HX + 2 * HX*rand()*1.0f / RAND_MAX, -HY, -HZ + 2 * HZ*1.0f*rand() / RAND_MAX);
lights[i]->dir = glm::normalize(q - p);
lights[i]->pos = p;
int cidx = rand() % NC;
g_vol_light_cb.spotlightColors[i].m128_f32[0] = colors[cidx][0] * LIGHT_MULT;
g_vol_light_cb.spotlightColors[i].m128_f32[1] = colors[cidx][1] * LIGHT_MULT;
g_vol_light_cb.spotlightColors[i].m128_f32[2] = colors[cidx][2] * LIGHT_MULT;
g_vol_light_cb.spotlightPV[i] = lights[i]->GetPV_D3D11();
}
}
g_vol_light_cb.spotlightCount = 16;
g_vol_light_cb.forceAlwaysOn = false;
}
}
g_vol_light_cb.cam_pos = GetCurrentSceneCamera()->GetPos_D3D11();
memcpy(mapped.pData, &g_vol_light_cb, sizeof(g_vol_light_cb));
g_context11->Unmap(g_perscene_cb_light11, 0);
}
void ClimbScene::PrepareLights() {
if (g_api == ClimbOpenGL) return;
{
if (is_all_rockets_collected) {
for (Platform* p : platforms) {
ExitPlatform* ep = dynamic_cast<ExitPlatform*>(p);
if (ep) { // There should be only 1 exit platform
glm::vec3 world_pos = ep->sprite->pos;
float a = light_phase;
world_pos.x += cos(3.0f * a) * 10.0f;
world_pos.y += sin(3.0f * a) * 10.0f;
world_pos.z *= -1;
glm::mat4 V = camera->GetViewMatrix();
glm::mat4 P = glm::perspective(60.0f * 3.14159f / 180.0f, WIN_W * 1.0f / WIN_H, 0.1f, 499.0f);
glm::vec4 clip_pos = P * V * glm::vec4(world_pos, 1.0f);
float x = (clip_pos.x / clip_pos.w + 1.0f) * 0.5f * WIN_W;
float y = WIN_H - (clip_pos.y / clip_pos.w + 1.0f) * 0.5f * WIN_H;
LightScatterDrawLightCB h_lightscatter_cb{};
h_lightscatter_cb.light_x = x;
h_lightscatter_cb.light_y = y;
h_lightscatter_cb.light_z = -world_pos.z;
h_lightscatter_cb.light_r = 50 + sin(a) * 20.0f;
h_lightscatter_cb.WIN_W = WIN_W;
h_lightscatter_cb.WIN_H = WIN_H;
h_lightscatter_cb.light_color.m128_f32[0] = 1.0f;
h_lightscatter_cb.light_color.m128_f32[1] = 1.0f;
h_lightscatter_cb.light_color.m128_f32[2] = 0.1f;
h_lightscatter_cb.light_color.m128_f32[3] = 1.0f;
h_lightscatter_cb.global_alpha = 1.0f;
D3D11_MAPPED_SUBRESOURCE mapped;
CE(g_context11->Map(g_lightscatter_cb, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped));
memcpy(mapped.pData, &h_lightscatter_cb, sizeof(LightScatterDrawLightCB));
g_context11->Unmap(g_lightscatter_cb, 0);
}
}
}
return;
}
{
float tmp = 3.1415926 * 2 / 3;
float a = light_phase;
const float dx = 4, dy = 10;
glm::vec3 x(cos(a) * dx, -dy, sin(a) * dx);
x = glm::normalize(x);
lights[0]->dir = x;
x = glm::normalize(glm::vec3(cos(a + tmp) * dx, -dy, sin(a + tmp) * dx));
lights[1]->dir = x;
x = glm::normalize(glm::vec3(cos(a - tmp) * dx, -dy, sin(a - tmp) * dx));
lights[2]->dir = x;
}
//UpdatePerSceneCB(&g_dir_light->GetDir_D3D11(), &(g_dir_light->GetPV_D3D11()), &(GetCurrentSceneCamera()->GetPos_D3D11()), nullptr);
D3D11_MAPPED_SUBRESOURCE mapped;
assert(SUCCEEDED(g_context11->Map(g_perscene_cb_light11, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)));
if (is_all_rockets_collected) {
g_vol_light_cb.spotlightCount = 0;
for (Platform* p : platforms) {
ExitPlatform* ep = dynamic_cast<ExitPlatform*>(p);
if (ep) { // There should be only 1 exit platform
glm::vec3 pos = ep->sprite->pos;
g_vol_light_cb.spotlightColors[0].m128_f32[0] = 1;
g_vol_light_cb.spotlightColors[0].m128_f32[1] = 1;
g_vol_light_cb.spotlightColors[0].m128_f32[2] = 0;
lights[0]->pos = pos;
g_vol_light_cb.spotlightPV[0] = lights[0]->GetPV_D3D11();
g_vol_light_cb.spotlightColors[1].m128_f32[0] = 1;
g_vol_light_cb.spotlightColors[1].m128_f32[1] = 0;
g_vol_light_cb.spotlightColors[1].m128_f32[2] = 1;
lights[1]->pos = pos;
g_vol_light_cb.spotlightPV[1] = lights[1]->GetPV_D3D11();
g_vol_light_cb.spotlightColors[2].m128_f32[0] = 0;
g_vol_light_cb.spotlightColors[2].m128_f32[1] = 1;
g_vol_light_cb.spotlightColors[2].m128_f32[2] = 1;
g_vol_light_cb.spotlightPV[2] = lights[2]->GetPV_D3D11();
lights[2]->pos = pos;
g_vol_light_cb.spotlightCount = 3;
g_vol_light_cb.aspect = WIN_W * 1.0f / WIN_H;
g_vol_light_cb.fovy = 60 * 3.14159f / 180.0f;
g_vol_light_cb.forceAlwaysOn = 0;
break;
}
}
}
else {
g_vol_light_cb.spotlightCount = 0;
}
g_vol_light_cb.forceAlwaysOn = false;
g_vol_light_cb.cam_pos = GetCurrentSceneCamera()->GetPos_D3D11();
memcpy(mapped.pData, &g_vol_light_cb, sizeof(g_vol_light_cb));
g_context11->Unmap(g_perscene_cb_light11, 0);
}
extern ID3D11VertexShader* g_vs_lightscatter_drawlight;
extern ID3D11VertexShader* g_vs_lightscatter_combine;
extern ID3D11PixelShader* g_ps_lightscatter_drawlight;
extern ID3D11PixelShader* g_ps_lightscatter_combine;
extern ID3D11ShaderResourceView* g_lightmask_srv11;
extern ID3D11RenderTargetView* g_lightmask_rtv11;
extern ID3D11InputLayout* g_inputlayout_lightscatter;
extern ID3D11Buffer* g_lightscatter_cb;
extern ID3D11Buffer* g_fsquad_for_lightscatter11;
extern ID3D11Texture2D* g_maincanvas;
extern ID3D11Texture2D* g_backbuffer;
extern ID3D11ShaderResourceView* g_lightmask_srv11;
extern ID3D11ShaderResourceView* g_maincanvas_srv11;
extern ID3D11ShaderResourceView* g_gbuffer_srv11;
extern ID3D11SamplerState* g_sampler11;
extern ID3D11RenderTargetView* g_backbuffer_rtv11;
void ClimbScene::RenderLights() {
if (!is_all_rockets_collected) return;
ID3D11RenderTargetView* null_rtvs[] = { nullptr, nullptr, nullptr };
float zero4[] = { 0, 0, 0, 0 };
g_context11->ClearRenderTargetView(g_lightmask_rtv11, zero4);
g_context11->IASetInputLayout(g_inputlayout_lightscatter);
g_context11->OMSetRenderTargets(1, &g_lightmask_rtv11, nullptr);
g_context11->PSSetConstantBuffers(0, 1, &g_lightscatter_cb);
g_context11->PSSetShader(g_ps_lightscatter_drawlight, nullptr, 0);
g_context11->VSSetShader(g_vs_lightscatter_drawlight, nullptr, 0);
UINT zero = 0, stride = sizeof(float) * 5;
g_context11->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
g_context11->IASetVertexBuffers(0, 1, &g_fsquad_for_lightscatter11, &stride, &zero);
g_context11->Draw(6, 0);
g_context11->OMSetRenderTargets(2, null_rtvs, nullptr);
g_context11->CopyResource(g_maincanvas, g_backbuffer);
g_context11->VSSetShader(g_vs_lightscatter_combine, nullptr, 0);
g_context11->PSSetShader(g_ps_lightscatter_combine, nullptr, 0);
ID3D11ShaderResourceView* srvs[] = {
g_maincanvas_srv11,
g_lightmask_srv11,
g_gbuffer_srv11
};
g_context11->PSSetConstantBuffers(0, 1, &g_lightscatter_cb);
g_context11->IASetVertexBuffers(0, 1, &g_fsquad_for_lightscatter11, &stride, &zero);
g_context11->PSSetSamplers(0, 1, &g_sampler11);
g_context11->PSSetShaderResources(0, 3, srvs);
g_context11->OMSetRenderTargets(1, &g_backbuffer_rtv11, nullptr);
g_context11->Draw(6, 0);
g_context11->OMSetRenderTargets(3, null_rtvs, nullptr);
}
void LightTestScene::Update(float secs) {
total_secs += secs;
for (int i = 0; i < NUM_ACTORS; i++) {
float mult = (GetMacroState(nullptr) == OLD_YEAR) ? 3 : 12;
deltays[i] = mult * abs(cos(total_secs * 2 * 3.14159));
char_sprites[i]->pos.y = deltays[i] + ACTOR_INIT_Y;
}
int curr_sec = 0;
if (GetMacroState(&curr_sec) == OLD_YEAR) {
status_string = "2019-12-31 23:59:" + std::to_string(50 + curr_sec);
if (curr_sec != last_sec) {
curr_light_angle = 0;
}
else {
curr_light_angle += secs * 0.2 * 3.14159;
curr_light_angle = std::min(curr_light_angle, 3.14159f / 2);
}
}
else {
status_string = "2020-01-01 00:00:0" + std::to_string(curr_sec - 10);
curr_light_angle = 0;
}
// Set tween
for (int i = 0; i < 20; i++) {
DigitState& st = digit_states[i];
if (st.new_char != status_string[i]) {
st.tween_end_sec = total_secs + 0.2f;
st.new_char = status_string[i];
}
}
last_sec = curr_sec;
}
LightTestScene::MacroState LightTestScene::GetMacroState(int* curr_sec) {
int s = int(total_secs) % 20;
if (curr_sec != nullptr)
*curr_sec = s;
if (s < 10) return OLD_YEAR; else return NEW_YEAR;
}