-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrad4.cpp
More file actions
428 lines (350 loc) · 9.11 KB
/
rad4.cpp
File metadata and controls
428 lines (350 loc) · 9.11 KB
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
//code for Windows to run well
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cassert>
#include <math.h>
#include <time.h>
#include <chrono>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
//#define __MAC__
#include <winsock2.h>
#ifdef _WIN32
#include <windows.h>
#include <glut.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#endif
#ifdef __MAC__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#endif
// ----------------------------------------------------------
struct RadLight
{
int tileX;
int tileY;
};
// ----------------------------------------------------------
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 800;
const int TILE_SIZE = 10;
const int TILES_WIDE = SCREEN_WIDTH / TILE_SIZE;
const int TILES_HIGH = SCREEN_HEIGHT / TILE_SIZE;
const int TOTAL_TILES = TILES_WIDE * TILES_HIGH;
int drawSize = 4;
GLubyte *frameBuffer = NULL;
const int RED_OFS = 0; /* offset to red byte */
const int GREEN_OFS = 1; /* offset to green byte */
const int BLUE_OFS = 2; /* offset to blue byte */
GLfloat * tiles = NULL;
// mouse
int LMBDown = 0;
int RMBDown = 0;
int mouseTileX = 0;
int mouseTileY = 0;
bool editMode = false;
// lights
const int NUM_VPLS = 64;
const int TOTAL_LIGHTS = 1 + NUM_VPLS;
RadLight lights[TOTAL_LIGHTS];
const int ONE_BILLION = 1000000000;
// ----------------------------------------------------------
void initGraphics(void)
{
// set up famebuffer for 2D rendering
frameBuffer = new GLubyte[SCREEN_WIDTH * SCREEN_HEIGHT * 3];
memset(frameBuffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * 3);
assert(frameBuffer != NULL);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, SCREEN_WIDTH, 0.0, SCREEN_HEIGHT, 0.0, 1.0);
glClearColor(1.0, 0.0, 0.0, 1.0);
}
// ----------------------------------------------------------
void cleanupGraphics(void)
{
delete(frameBuffer); frameBuffer = NULL;
}
// ----------------------------------------------------------
void setTile(int tileX, int tileY, GLfloat val)
{
assert(tileX >= 0);
assert(tileX < TILES_WIDE);
assert(tileY >= 0);
assert(tileY < TILES_HIGH);
tiles[tileY * TILES_WIDE + tileX] = val;
}
// ----------------------------------------------------------
GLfloat getTile(int tileX, int tileY)
{
// prevent ray from escaping grid.
if (tileX < 0) return -1.0;
if (tileY < 0) return -1.0;
if (tileX >= TILES_WIDE) return -1.0;
if (tileY >= TILES_HIGH) return -1.0;
return tiles[tileY * TILES_WIDE + tileX];
}
// ----------------------------------------------------------
void initScene(void)
{
tiles = new GLfloat[TILES_WIDE * TILES_HIGH];
for (int i = 0; i < TILES_WIDE; i++){
for (int j = 0; j < TILES_HIGH; j++){
setTile(i, j, 0.0);
}
}
setTile(10, 10, -1.0);
setTile(10, 9, -1.0);
setTile(15, 6, -1.0);
setTile(30, 20, -1.0);
}
// ----------------------------------------------------------
void cleanupScene(void)
{
delete(tiles);
tiles = NULL;
}
// ----------------------------------------------------------
void setPixel(int x, int y, GLfloat r, GLfloat g, GLfloat b)
{
assert(x >= 0 && x < SCREEN_WIDTH);
assert(y >= 0 && y < SCREEN_HEIGHT);
int pos = 3 * SCREEN_WIDTH*y + 3 * x;
char red = (char)(r * 255);
char green = (char)(g * 255);
char blue = (char)(b * 255);
frameBuffer[pos + RED_OFS] = red;
frameBuffer[pos + GREEN_OFS] = green;
frameBuffer[pos + BLUE_OFS] = blue;
}
// ----------------------------------------------------------
bool raycast(int x0, int y0, int x1, int y1, int &xhit, int &yhit)
{
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0<y1 ? 1 : -1;
int err = (dx>dy ? dx : -dy) / 2, e2;
int x = x0;
int y = y0;
int stepped = 0;
while(1){
if (getTile(x, y) < 0.0) {
xhit = x - stepped*sx;
yhit = y - stepped*sy;
return false;
}
if (x == x1 && y == y1) break;
e2 = err;
if (e2 > -dx) { err -= dy; x += sx; }
if (e2 < dy) { err += dx; y += sy; }
stepped = 1;
}
return true;
}
// ----------------------------------------------------------
void spawnLights(void)
{
lights[0].tileX = mouseTileX;
lights[0].tileY = mouseTileY;
// *** CAST FIRST BOUNCE ***
float deltaDegrees = 360.0f / (float)NUM_VPLS;
float deltaRadians = deltaDegrees * M_PI / 180.0f;
float twopi = 2.0f * M_PI;
int vpl = 1;
float radians = 0.0f;
while (radians < twopi && vpl<=NUM_VPLS)
{
float xdir = cos(radians);
float ydir = sin(radians);
// make it big enough to span the screen
int destX = mouseTileX + floor(xdir * 5000);
int destY = mouseTileY + floor(ydir * 5000);
int hitX;
int hitY;
bool rayfree = raycast(mouseTileX, mouseTileY, destX, destY, hitX, hitY);
assert(!rayfree); // must always hit something
lights[vpl].tileX = hitX;
lights[vpl].tileY = hitY;
radians += deltaRadians;
vpl++;
}
}
// ----------------------------------------------------------
void renderScene(void)
{
// clear screen
memset(frameBuffer, 0x22, SCREEN_WIDTH * SCREEN_HEIGHT * 3);
spawnLights();
// cast light to each pixel
for (int i = 0; i < TILES_WIDE; i++)
{
for (int j = 0; j < TILES_HIGH; j++)
{
// only cast from open tiles
if (getTile(i, j) >= 0.0)
{
if (editMode)
{
setTile(i, j, 1.0f);
}
else
{
int lightsSeen = 0;
int hitX;
int hitY;
for (int n = 0; n < TOTAL_LIGHTS; n++)
{
if (raycast(i, j, lights[n].tileX, lights[n].tileY, hitX, hitY))
{
lightsSeen++;
}
}
setTile(i, j, 1.0f * lightsSeen / (1.0f * TOTAL_LIGHTS));
}
}
}
}
// *** render world pixels based on their intensities ***
for (int i = 0; i < TILES_WIDE; i++)
{
for (int j = 0; j < TILES_HIGH; j++)
{
// convert blocker tiles to black
float intensity = getTile(i, j);
if (intensity < 0.0) intensity = 0.0;
for (int x = 0; x < TILE_SIZE; x++)
for (int y = 0; y < TILE_SIZE; y++)
setPixel(i * TILE_SIZE + x, j * TILE_SIZE + y, intensity, intensity, intensity);
}
}
}
// ----------------------------------------------------------
void displayCallback() {
glClear(GL_COLOR_BUFFER_BIT);
typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::nanoseconds ns;
typedef std::chrono::duration<float> fsec;
auto t0 = Time::now();
renderScene();
auto t1 = Time::now();
fsec duration = t1 - t0;
ns nanoseconds = std::chrono::duration_cast<ns>(duration);
std::cout << "\nRendered " << TOTAL_TILES << "in " << nanoseconds.count() << " nanoseconds";
// present frame buffer to screen
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glRasterPos3f(0.0, 0.0, 0.0);
glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, frameBuffer);
glFlush();
glutSwapBuffers();
}
// ----------------------------------------------------------
void mouseCallback(int button, int state, int x, int y)
{
mouseTileX = x / TILE_SIZE;
mouseTileY = (SCREEN_HEIGHT - y) / TILE_SIZE;
printf("mouse move %d %d\n", mouseTileX, mouseTileY);
LMBDown = (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN);
RMBDown = (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN);
if (editMode)
{
int xEnd = mouseTileX + drawSize;
int yEnd = mouseTileY + drawSize;
if (xEnd > TILES_WIDE) xEnd = TILES_WIDE;
if (yEnd > TILES_HIGH) yEnd = TILES_HIGH;
if (LMBDown)
{
for (int i = mouseTileX; i < xEnd; i++)
for (int j = mouseTileY; j < yEnd; j++)
setTile(i, j, -1.0);
}
else if (RMBDown)
{
for (int i = mouseTileX; i < xEnd; i++)
for (int j = mouseTileY; j < yEnd; j++)
setTile(i, j, 0.0);
}
}
glutPostRedisplay();
}
// ----------------------------------------------------------
void motionCallback(int x, int y)
{
mouseCallback(0, 0, x, y);
}
// ----------------------------------------------------------
// custom keyFunc with preset keys
void keyCallback(unsigned char ch, int x, int y) {
switch (ch) {
case '1':
{
printf("Draw size changed to 1.\n");
drawSize = 1;
break;
}
case '2':
{
printf("Draw size changed to 2.\n");
drawSize = 2;
break;
}
case '3':
{
printf("Draw size changed to 3.\n");
drawSize = 3;
break;
}
case '4':
{
printf("Draw size changed to 4.\n");
drawSize = 4;
break;
}
case 'd':
case 'D':
{
editMode = !editMode;
if (editMode)
printf("Edit mode ON.\n");
else
printf("Edit mode OFF.\n");
break;
}
}
glutPostRedisplay();
}
// ----------------------------------------------------------
void idleCallback(void)
{
//no need for this call since any input redraws.
//we dont care about FPS, just render time for a frame.
//glutPostRedisplay();
}
// ----------------------------------------------------------
int main(int argc, char **argv)
{
initScene();
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
int win = glutCreateWindow("2D Radiosity Demo");
glutSetWindow(win);
glutKeyboardFunc(keyCallback);
glutMouseFunc(mouseCallback);
glutMotionFunc(motionCallback);
glutDisplayFunc(displayCallback);
glutIdleFunc(idleCallback);
initGraphics();
glutMainLoop();
cleanupScene();
cleanupGraphics();
}