-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.cpp
418 lines (345 loc) · 12.4 KB
/
demo.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
#include "csg.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include "flythrough_camera.h"
#include "shader_program.hpp"
#include "defer.hpp"
#include <randomcolor.h>
using namespace csg;
using namespace glm;
using namespace std;
static RandomColor random_color;
static float signed_distance(const vec3& point, const plane_t& plane) {
return dot(plane.normal, point) + plane.offset;
}
static vec3 project_point(const vec3& point,
const plane_t& plane)
{
return point - signed_distance(point, plane) * plane.normal;
}
static plane_t make_plane(
const vec3& point,
const vec3& normal)
{
return plane_t{ normal, -dot(point, normal) };
}
static plane_t transformed(
const plane_t& plane,
const mat4& transform)
{
vec3 origin_transformed = vec3(
transform * vec4(project_point(vec3(0,0,0), plane), 1.0f)
);
vec3 normal_transformed = glm::normalize(vec3(
transpose(inverse(transform)) * vec4(plane.normal, 0.0f)
));
plane_t transformed_plane = make_plane(origin_transformed, normal_transformed);
return transformed_plane;
}
static constexpr volume_t AIR = 0;
static constexpr volume_t SOLID = 1;
enum brush_type_t {
AIR_BRUSH,
SOLID_BRUSH
};
struct cube_brush_userdata_t {
void set_brush_type(brush_type_t type){
this->type = type;
switch (type) {
case AIR_BRUSH: brush->set_volume_operation(make_fill_operation(AIR)); break;
case SOLID_BRUSH: brush->set_volume_operation(make_fill_operation(SOLID)); break;
default: ;
}
}
brush_type_t get_brush_type() const {
return type;
}
void set_transform(const mat4& transform) {
static vector<plane_t> cube_planes = {
make_plane(vec3(+1,0,0), vec3(+1,0,0)),
make_plane(vec3(-1,0,0), vec3(-1,0,0)),
make_plane(vec3(0,+1,0), vec3(0,+1,0)),
make_plane(vec3(0,-1,0), vec3(0,-1,0)),
make_plane(vec3(0,0,+1), vec3(0,0,+1)),
make_plane(vec3(0,0,-1), vec3(0,0,-1))
};
this->transform = transform;
vector<plane_t> transformed_planes ;
for (const plane_t& plane: cube_planes)
transformed_planes.push_back( transformed(plane, transform) );
brush->set_planes(transformed_planes);
}
const mat4& get_transform() const {
return transform;
}
void update_display_list() {
glNewList(display_list, GL_COMPILE);
auto faces = brush->get_faces();
for (face_t& face: faces) {
glBegin(GL_TRIANGLES);
for (fragment_t& fragment: face.fragments) {
if (fragment.back_volume == fragment.front_volume) {
continue;
}
const vector<vertex_t>& vertices = fragment.vertices;
vec3 normal = face.plane->normal;
bool flip_face = fragment.back_volume == AIR;
if (flip_face) {
normal = -normal;
}
glNormal3fv(value_ptr(normal));
vector<triangle_t> tris = triangulate(fragment);
for (triangle_t& tri: tris) {
if (flip_face) {
glVertex3fv(value_ptr(vertices[tri.i].position));
glVertex3fv(value_ptr(vertices[tri.k].position));
glVertex3fv(value_ptr(vertices[tri.j].position));
} else {
glVertex3fv(value_ptr(vertices[tri.i].position));
glVertex3fv(value_ptr(vertices[tri.j].position));
glVertex3fv(value_ptr(vertices[tri.k].position));
}
}
}
glEnd();
}
glEndList();
}
void render_fill() {
glColor3ub(red, green, blue);
glCallList(display_list);
}
void render_wire() {
glCallList(display_list);
}
cube_brush_userdata_t(brush_t* brush): brush(brush) {
display_list = glGenLists(1);
int argb = random_color.generate();
blue = (argb >> 0) & 0xff;
green = (argb >> 8) & 0xff;
red = (argb >> 16) & 0xff;
}
private:
mat4 transform;
char red,green,blue;
brush_t *brush;
brush_type_t type;
GLuint display_list;
};
void render_world(world_t& world, const mat4& viewproj, ShaderProgram* shader) {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glColor3f(1,1,1);
// frustum culling
vector<brush_t*> visible = world.query_frustum(viewproj);
// draw the fragments
// ----------------------------
shader->use();
for (brush_t* brush: visible) {
any_cast<cube_brush_userdata_t>(&brush->userdata)->render_fill();
}
glUseProgram(0);
// draw the wireframe
// ---------------------------
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPolygonOffset(-1.0f, -1.0f);
glColor3f(1,1,1);
for (brush_t* brush: visible) {
any_cast<cube_brush_userdata_t>(&brush->userdata)->render_wire();
}
glPolygonOffset(0.0f, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDisable(GL_POLYGON_OFFSET_LINE);
glDisable(GL_CULL_FACE);
}
// simple shader with headlight
// ---------------------------------------
const char *solid_vert = R"(
#version 120
varying vec3 v_vertex_viewspace;
varying vec3 v_color;
varying vec3 v_normal_viewspace;
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
v_vertex_viewspace = (gl_ModelViewMatrix * gl_Vertex).xyz;
v_color = gl_Color.rgb;
v_normal_viewspace = (gl_ModelViewMatrix * vec4(gl_Normal,0)).xyz;
}
)";
const char *solid_frag = R"(
#version 120
varying vec3 v_vertex_viewspace;
varying vec3 v_color;
varying vec3 v_normal_viewspace;
void main() {
vec3 to_camera = -normalize(v_vertex_viewspace);
float distance = length(v_vertex_viewspace);
float density = 0.1;
float lambert = dot(normalize(v_normal_viewspace), to_camera);
gl_FragColor.rgb = v_color * lambert * (1/exp(distance*density));
gl_FragColor.a = 1;
}
)";
// ---------------------------------------
int main(int, char**) {
SDL_Init(SDL_INIT_EVERYTHING);
DEFER( SDL_Quit() );
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
SDL_Window *window = SDL_CreateWindow(
"tiny_csg demo",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800, 600,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED
);
DEFER( SDL_DestroyWindow(window) );
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
DEFER( SDL_GL_DeleteContext(context) );
// vsync
if (SDL_GL_SetSwapInterval(-1) != 0 )
SDL_GL_SetSwapInterval(1);
glewInit();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
// init the shader
ShaderProgram *solid_shader = 0;
{
Shader *vert = new Shader(solid_vert, strlen(solid_vert), GL_VERTEX_SHADER);
DEFER( delete vert );
Shader *frag = new Shader(solid_frag, strlen(solid_frag), GL_FRAGMENT_SHADER);
DEFER( delete frag );
vert->compile();
frag->compile();
Shader *shaders[] = {vert, frag};
solid_shader = new ShaderProgram(shaders, 2);
}
DEFER( delete solid_shader );
/*
init the camera
*/
vec3 eye(0,0,0);
vec3 look(0,0,-1);
vec3 up(0,1,0);
mat4 view(1);
/*
build the world
*/
world_t world;
world.set_void_volume(SOLID);
brush_t *room = world.add();
{
room->userdata = cube_brush_userdata_t(room);
cube_brush_userdata_t *userdata = any_cast<cube_brush_userdata_t>(&room->userdata);
userdata->set_brush_type(AIR_BRUSH);
userdata->set_transform(mat4(1));
}
brush_t *pillar = world.add();
{
pillar->userdata = cube_brush_userdata_t(pillar);
cube_brush_userdata_t *userdata = any_cast<cube_brush_userdata_t>(&pillar->userdata);
userdata->set_brush_type(SOLID_BRUSH);
userdata->set_transform(
scale(mat4(1), vec3(0.25f, 2.0f, 0.25f))
);
}
brush_t *tunnel = world.add();
{
tunnel->userdata = cube_brush_userdata_t(tunnel);
cube_brush_userdata_t *userdata = any_cast<cube_brush_userdata_t>(&tunnel->userdata);
userdata->set_brush_type(AIR_BRUSH);
userdata->set_transform(
scale(mat4(1), vec3(2.0f, 0.25f, 0.25f))
);
}
/*
main loop
*/
bool done = false;
int ticks_now = SDL_GetTicks();
int ticks_last = 0;
float delta_time = 0;
while (!done) {
ticks_last = ticks_now;
ticks_now = SDL_GetTicks();
delta_time = (ticks_now - ticks_last) / 1000.0f;
ivec2 mouse_delta(0,0);
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
done = true;
break;
case SDL_MOUSEMOTION:
mouse_delta = ivec2(ev.motion.xrel, ev.motion.yrel);
break;
}
}
// rotate the tunnel brush
// ---------------------------------------
static cube_brush_userdata_t*
tunnel_userdata = any_cast<cube_brush_userdata_t>(&tunnel->userdata);
static const mat4
tunnel_original_transform = tunnel_userdata->get_transform();
float seconds_now = ticks_now / 1000.0f;
tunnel_userdata->set_transform(
glm::rotate(mat4(1), radians(seconds_now*45.0f), vec3(0,1,0)) *
tunnel_original_transform
);
// ---------------------------------------
// rebuild the world and update display list for any rebuilt brush
// ---------------------------------------
auto rebuilt = world.rebuild();
for (brush_t* b: rebuilt)
any_cast<cube_brush_userdata_t>(&b->userdata)->update_display_list();
// ---------------------------------------
// update camera
// ---------------------------------------
auto keys_down = SDL_GetKeyboardState(NULL);
flythrough_camera_update(
value_ptr(eye), value_ptr(look), value_ptr(up), value_ptr(view),
delta_time, 4.0f, 0.4f, 80.0f,
mouse_delta.x, mouse_delta.y,
keys_down[SDL_SCANCODE_W],
keys_down[SDL_SCANCODE_A],
keys_down[SDL_SCANCODE_S],
keys_down[SDL_SCANCODE_D],
keys_down[SDL_SCANCODE_SPACE],
keys_down[SDL_SCANCODE_C],
0
) ;
int w, h;
SDL_GetWindowSize(window, &w, &h);
mat4 proj = perspective(
radians(60.0f),
float(w)/h,
0.01f,
20.0f
);
// ---------------------------------------
// drawing
// ---------------------------------------
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(value_ptr(view));
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(value_ptr(proj));
mat4 viewproj = proj * view;
glViewport(0,0,w,h);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render_world(world, viewproj, solid_shader);
// ---------------------------------------
// swap buffers
SDL_GL_SwapWindow(window);
}
return 0;
}