-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
394 lines (294 loc) · 11.1 KB
/
main.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
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glfw3.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <chrono>
#include <FastNoise.h>
#include <CubeGrid.h>
#include <CellGrid.h>
#include <Mesh.h>
#include <Camera.h>
// 3D scalar grid size
#define GRID_WIDTH 50
#define GRID_HEIGHT 50
#define GRID_DEPTH 50
// noise function parameters
#define OCTAVES 3
#define LACUNARITY 2.f
#define PERSISTANCE 0.5f
#define NOISE_SCALE 3.f
// cube grid parameters
#define SURFACE_LEVEL 0.5f
#define CUBE_SIZE 0.1f
#define MIN_REGION_SIZE 1000
// camera parameters
#define CAM_ROTATION_SPEED 0.5f
#define CAM_ZOOM_SPEED 0.3f
#define CAM_MIN_DIST 1.f
#define CAM_MAX_DIST 15.f
using namespace std;
// generator class instances
static FastNoise noise;
static CellGrid cellGrid;
static CubeGrid cubeGrid;
static Mesh mesh;
// camera
static Camera cam;
// vertex array elements params
static GLuint vbo, ibo;
static unsigned int vboSize, iboSize;
static unsigned int normalsOffset;
static unsigned int iboCount;
static float frameTime;
static bool drawAxes, drawWireBox;
// generator functions
// generate the mesh by marching cubes
static void generateMap();
static void generateBuffers();
// GLFW event callbacks
static void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods);
static void onCursorPosition(GLFWwindow *window, double x, double y);
static void onMouseButton(GLFWwindow *window, int button, int action, int mods);
static void onScrollRoll(GLFWwindow *window, double xoff, double yoff);
// mesh material and lighting
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 0.7f, 0.3f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 0.0f, 0.0f, 1.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 0.5f, 0.5f, 0.5f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
// GL window setup
if(!glfwInit()){
glfwTerminate();
return 0;
}
GLFWwindow *window;
window = glfwCreateWindow(600, 400, "Marching Cubes", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, onKeyPressed);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursorPosCallback(window, onCursorPosition);
glfwSetMouseButtonCallback(window, onMouseButton);
glfwSetScrollCallback(window, onScrollRoll);
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, 1);
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK)
{
glfwTerminate();
return 0;
}
glClearColor(0, 0, 0, 1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable( GL_LINE_SMOOTH );
glEnable( GL_POINT_SMOOTH );
glEnable( GL_BLEND );
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
// generate first map
// initialize random seed
srand(time(0));
// use simplex noise
noise.SetNoiseType(FastNoise::Simplex);
// create grid of cells (3D scalar field)
cellGrid = CellGrid(GRID_WIDTH, GRID_HEIGHT, GRID_DEPTH);
generateMap();
generateBuffers();
int winWidth, winHeight;
float sx, sy, sz;
float ax, ay, az;
// get box size for wire box, size is constant
sx = mesh.dimX / 2.f;
sy = mesh.dimY / 2.f;
sz = mesh.dimZ / 2.f;
ax = -sx * 1.1f;
ay = -sy * 1.1f;
az = -sz * 1.1f;
// camera setup : place it at some distance from the center based on mesh size
cam = Camera(vec3d(0, 0, -mesh.dimZ * 2.f), CAM_ROTATION_SPEED, CAM_ZOOM_SPEED);
drawWireBox = true;
drawAxes = false;
while(!glfwWindowShouldClose(window))
{
// rendering loop
// start recording the frame rendering time
auto frameStartTime = chrono::high_resolution_clock::now();
glfwGetWindowSize(window, &winWidth, &winHeight );
winHeight = winHeight > 0 ? winHeight : 1;
glViewport( 0, 0, winWidth, winHeight );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0f, (GLfloat)winWidth/(GLfloat)winHeight, 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// recalculate camera coordinates
cam.update();
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
0.f, 0.f, 0.f,
0.f, 1.f, 0.f);
// draw the mesh from the buffers
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glColor3f(1.f, 1.f, 1.f);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (void*)(0));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, (void*)(normalsOffset));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, iboCount, GL_UNSIGNED_INT, (void*)(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
// draw wire box
glPointSize(1.f);
if(drawWireBox){
glColor3f(1.f, 1.f, 1.f);
glBegin(GL_LINE_LOOP);
glVertex3f(-sx, -sy, -sz);
glVertex3f( sx, -sy, -sz);
glVertex3f( sx, sy, -sz);
glVertex3f(-sx, sy, -sz);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-sx, -sy, sz);
glVertex3f( sx, -sy, sz);
glVertex3f( sx, sy, sz);
glVertex3f(-sx, sy, sz);
glEnd();
glBegin(GL_LINES);
glVertex3f(-sx, -sy, -sz);
glVertex3f(-sx, -sy, sz);
glVertex3f( sx, -sy, -sz);
glVertex3f( sx, -sy, sz);
glVertex3f(-sx, sy, -sz);
glVertex3f(-sx, sy, sz);
glVertex3f( sx, sy, -sz);
glVertex3f( sx, sy, sz);
glEnd();
}
// draw axis
if(drawAxes){
glBegin(GL_LINES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(ax, ay, az);
glVertex3f(ax+1.f, ay, az);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(ax, ay, az);
glVertex3f(ax, ay+1.f, az);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(ax, ay, az );
glVertex3f(ax, ay, az+1.f);
glEnd();
}
glfwSwapBuffers(window);
glfwPollEvents();
// stop recording frame time
auto frameFinishTime = chrono::high_resolution_clock::now();
auto frameDuration = chrono::duration_cast<chrono::microseconds>(frameFinishTime - frameStartTime);
if(frameDuration.count() > 0.f)
frameTime = frameDuration.count() / 1000000.f;
}
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
glfwTerminate();
return EXIT_SUCCESS;
}
static void onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS){
if(key == GLFW_KEY_SPACE){
// clear buffers before generating a new map
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
generateMap();
generateBuffers();
} else if(key == GLFW_KEY_A){
// toggle axes drawing
drawAxes = !drawAxes;
} else if(key == GLFW_KEY_B){
// toggle wire box drawing
drawWireBox = !drawWireBox;
}
}
}
static void onCursorPosition(GLFWwindow *window, double x, double y)
{
cam.updatePos((float)x, (float)y, frameTime);
}
static void onMouseButton(GLFWwindow *window, int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
cam.enableMovement();
} else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
cam.disableMovement();
}
}
static void onScrollRoll(GLFWwindow *window, double xoff, double yoff)
{
cam.updateDistance((float)yoff, CAM_MIN_DIST, CAM_MAX_DIST);
}
static void generateMap()
{
cout << "Generating new box";
auto startTime = chrono::high_resolution_clock::now();
// delete previous mesh data
mesh.clear();
// configure noise to a random seed
noise.SetSeed(rand());
// fill the 3D scalar field
cellGrid.fillGrid(noise, OCTAVES, LACUNARITY, PERSISTANCE, NOISE_SCALE);
// generate the cube grid according to that scalar field
cubeGrid.generateGrid(cellGrid, CUBE_SIZE, SURFACE_LEVEL, MIN_REGION_SIZE);
cubeGrid.marchCubes(mesh.vertices);
mesh.generateMesh(cubeGrid, MIN_REGION_SIZE);
// free memory of useless data
cubeGrid.clear();
auto finishTime = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(finishTime - startTime);
cout << ": seed " << noise.GetSeed();
cout << " - " << mesh.vertices.size() << " vertices, ";
cout << mesh.triangles.size() << " triangles";
cout << " - " << duration.count() << "ms" << endl;
}
static void generateBuffers()
{
// generate the VBO and load the vertices and normals of the mesh inside
float *vertices = mesh.getVertexArray();
normalsOffset = mesh.vertices.size() * 3 * sizeof(float);
vboSize = normalsOffset * 2;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vboSize, &(vertices[0]), GL_STATIC_DRAW);
delete[] vertices;
// generate the IBO and load the triangle indices inside
unsigned int *triangles = mesh.getTriangleArray();
iboCount = mesh.triangles.size() * 3;
iboSize = iboCount * sizeof(unsigned int);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iboSize, &(triangles[0]), GL_STATIC_DRAW);
delete[] triangles;
}