-
Notifications
You must be signed in to change notification settings - Fork 4
/
aogl.cpp
489 lines (432 loc) · 14.1 KB
/
aogl.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <cmath>
#include "glew/glew.h"
#include "GLFW/glfw3.h"
#include "stb/stb_image.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw_gl3.h"
#include "glm/glm.hpp"
#include "glm/vec3.hpp" // glm::vec3
#include "glm/vec4.hpp" // glm::vec4, glm::ivec4
#include "glm/mat4x4.hpp" // glm::mat4
#include "glm/gtc/matrix_transform.hpp" // glm::translate, glm::rotate, glm::scale, glm::perspective
#include "glm/gtc/type_ptr.hpp" // glm::value_ptr
#ifndef DEBUG_PRINT
#define DEBUG_PRINT 1
#endif
#if DEBUG_PRINT == 0
#define debug_print(FORMAT, ...) ((void)0)
#else
#ifdef _MSC_VER
#define debug_print(FORMAT, ...) \
fprintf(stderr, "%s() in %s, line %i: " FORMAT "\n", \
__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
#else
#define debug_print(FORMAT, ...) \
fprintf(stderr, "%s() in %s, line %i: " FORMAT "\n", \
__func__, __FILE__, __LINE__, __VA_ARGS__)
#endif
#endif
// Font buffers
extern const unsigned char DroidSans_ttf[];
extern const unsigned int DroidSans_ttf_len;
// Shader utils
int check_link_error(GLuint program);
int check_compile_error(GLuint shader, const char ** sourceBuffer);
GLuint compile_shader(GLenum shaderType, const char * sourceBuffer, int bufferSize);
GLuint compile_shader_from_file(GLenum shaderType, const char * fileName);
// OpenGL utils
bool checkError(const char* title);
struct Camera
{
float radius;
float theta;
float phi;
glm::vec3 o;
glm::vec3 eye;
glm::vec3 up;
};
void camera_defaults(Camera & c);
void camera_zoom(Camera & c, float factor);
void camera_turn(Camera & c, float phi, float theta);
void camera_pan(Camera & c, float x, float y);
struct GUIStates
{
bool panLock;
bool turnLock;
bool zoomLock;
int lockPositionX;
int lockPositionY;
int camera;
double time;
bool playing;
static const float MOUSE_PAN_SPEED;
static const float MOUSE_ZOOM_SPEED;
static const float MOUSE_TURN_SPEED;
};
const float GUIStates::MOUSE_PAN_SPEED = 0.001f;
const float GUIStates::MOUSE_ZOOM_SPEED = 0.05f;
const float GUIStates::MOUSE_TURN_SPEED = 0.005f;
void init_gui_states(GUIStates & guiStates);
int main( int argc, char **argv )
{
int width = 1024, height= 768;
float widthf = (float) width, heightf = (float) height;
double t;
float fps = 0.f;
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
exit( EXIT_FAILURE );
}
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_VISIBLE, GL_TRUE);
glfwWindowHint(GLFW_DECORATED, GL_TRUE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
#if defined(__APPLE__)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
int const DPI = 2; // For retina screens only
#else
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
int const DPI = 1;
# endif
// Open a window and create its OpenGL context
GLFWwindow * window = glfwCreateWindow(width/DPI, height/DPI, "aogl", 0, 0);
if( ! window )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
exit( EXIT_FAILURE );
}
glfwMakeContextCurrent(window);
// Init glew
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
exit( EXIT_FAILURE );
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
// Enable vertical sync (on cards that support it)
glfwSwapInterval( 1 );
GLenum glerr = GL_NO_ERROR;
glerr = glGetError();
ImGui_ImplGlfwGL3_Init(window, true);
// Init viewer structures
Camera camera;
camera_defaults(camera);
GUIStates guiStates;
init_gui_states(guiStates);
float dummySlider = 0.f;
float clearColor[4] = {0.f, 0.f, 0.f, 1.f};
// Try to load and compile shaders
GLuint vertShaderId = compile_shader_from_file(GL_VERTEX_SHADER, "aogl.vert");
GLuint fragShaderId = compile_shader_from_file(GL_FRAGMENT_SHADER, "aogl.frag");
GLuint programObject = glCreateProgram();
glAttachShader(programObject, vertShaderId);
glAttachShader(programObject, fragShaderId);
glLinkProgram(programObject);
if (check_link_error(programObject) < 0)
exit(1);
// Upload uniforms
GLuint mvpLocation = glGetUniformLocation(programObject, "MVP");
if (!checkError("Uniforms"))
exit(1);
// Viewport
glViewport( 0, 0, width, height );
do
{
t = glfwGetTime();
ImGui_ImplGlfwGL3_NewFrame();
// Mouse states
int leftButton = glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_LEFT );
int rightButton = glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_RIGHT );
int middleButton = glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_MIDDLE );
if( leftButton == GLFW_PRESS )
guiStates.turnLock = true;
else
guiStates.turnLock = false;
if( rightButton == GLFW_PRESS )
guiStates.zoomLock = true;
else
guiStates.zoomLock = false;
if( middleButton == GLFW_PRESS )
guiStates.panLock = true;
else
guiStates.panLock = false;
// Camera movements
int altPressed = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT);
if (!altPressed && (leftButton == GLFW_PRESS || rightButton == GLFW_PRESS || middleButton == GLFW_PRESS))
{
double x; double y;
glfwGetCursorPos(window, &x, &y);
guiStates.lockPositionX = x;
guiStates.lockPositionY = y;
}
if (altPressed == GLFW_PRESS)
{
double mousex; double mousey;
glfwGetCursorPos(window, &mousex, &mousey);
int diffLockPositionX = mousex - guiStates.lockPositionX;
int diffLockPositionY = mousey - guiStates.lockPositionY;
if (guiStates.zoomLock)
{
float zoomDir = 0.0;
if (diffLockPositionX > 0)
zoomDir = -1.f;
else if (diffLockPositionX < 0 )
zoomDir = 1.f;
camera_zoom(camera, zoomDir * GUIStates::MOUSE_ZOOM_SPEED);
}
else if (guiStates.turnLock)
{
camera_turn(camera, diffLockPositionY * GUIStates::MOUSE_TURN_SPEED,
diffLockPositionX * GUIStates::MOUSE_TURN_SPEED);
}
else if (guiStates.panLock)
{
camera_pan(camera, diffLockPositionX * GUIStates::MOUSE_PAN_SPEED,
diffLockPositionY * GUIStates::MOUSE_PAN_SPEED);
}
guiStates.lockPositionX = mousex;
guiStates.lockPositionY = mousey;
}
// Default states
glEnable(GL_DEPTH_TEST);
// Clear the front buffer
glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Get camera matrices
glm::mat4 projection = glm::perspective(45.0f, widthf / heightf, 0.1f, 100.f);
glm::mat4 worldToView = glm::lookAt(camera.eye, camera.o, camera.up);
glm::mat4 objectToWorld;
glm::mat4 mvp = projection * worldToView * objectToWorld;
// Select shader
glUseProgram(programObject);
// Upload uniforms
glProgramUniformMatrix4fv(programObject, mvpLocation, 1, 0, glm::value_ptr(mvp));
#if 1
ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
ImGui::Begin("aogl");
ImGui::SliderFloat("dummy", &dummySlider, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", clearColor);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
#endif
ImGui::Render();
// Check for errors
checkError("End loop");
glfwSwapBuffers(window);
glfwPollEvents();
double newTime = glfwGetTime();
fps = 1.f/ (newTime - t);
} // Check if the ESC key was pressed
while( glfwGetKey( window, GLFW_KEY_ESCAPE ) != GLFW_PRESS );
// Close OpenGL window and terminate GLFW
ImGui_ImplGlfwGL3_Shutdown();
glfwTerminate();
exit( EXIT_SUCCESS );
}
// No windows implementation of strsep
char * strsep_custom(char **stringp, const char *delim)
{
register char *s;
register const char *spanp;
register int c, sc;
char *tok;
if ((s = *stringp) == NULL)
return (NULL);
for (tok = s; ; ) {
c = *s++;
spanp = delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
*stringp = s;
return (tok);
}
} while (sc != 0);
}
return 0;
}
int check_compile_error(GLuint shader, const char ** sourceBuffer)
{
// Get error log size and print it eventually
int logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 1)
{
char * log = new char[logLength];
glGetShaderInfoLog(shader, logLength, &logLength, log);
char *token, *string;
string = strdup(sourceBuffer[0]);
int lc = 0;
while ((token = strsep_custom(&string, "\n")) != NULL) {
printf("%3d : %s\n", lc, token);
++lc;
}
fprintf(stderr, "Compile : %s", log);
delete[] log;
}
// If an error happend quit
int status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
return -1;
return 0;
}
int check_link_error(GLuint program)
{
// Get link error log size and print it eventually
int logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 1)
{
char * log = new char[logLength];
glGetProgramInfoLog(program, logLength, &logLength, log);
fprintf(stderr, "Link : %s \n", log);
delete[] log;
}
int status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
return -1;
return 0;
}
GLuint compile_shader(GLenum shaderType, const char * sourceBuffer, int bufferSize)
{
GLuint shaderObject = glCreateShader(shaderType);
const char * sc[1] = { sourceBuffer };
glShaderSource(shaderObject,
1,
sc,
NULL);
glCompileShader(shaderObject);
check_compile_error(shaderObject, sc);
return shaderObject;
}
GLuint compile_shader_from_file(GLenum shaderType, const char * path)
{
FILE * shaderFileDesc = fopen( path, "rb" );
if (!shaderFileDesc)
return 0;
fseek ( shaderFileDesc , 0 , SEEK_END );
long fileSize = ftell ( shaderFileDesc );
rewind ( shaderFileDesc );
char * buffer = new char[fileSize + 1];
fread( buffer, 1, fileSize, shaderFileDesc );
buffer[fileSize] = '\0';
GLuint shaderObject = compile_shader(shaderType, buffer, fileSize );
delete[] buffer;
return shaderObject;
}
bool checkError(const char* title)
{
int error;
if((error = glGetError()) != GL_NO_ERROR)
{
std::string errorString;
switch(error)
{
case GL_INVALID_ENUM:
errorString = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
errorString = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
errorString = "GL_INVALID_OPERATION";
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
errorString = "GL_INVALID_FRAMEBUFFER_OPERATION";
break;
case GL_OUT_OF_MEMORY:
errorString = "GL_OUT_OF_MEMORY";
break;
default:
errorString = "UNKNOWN";
break;
}
fprintf(stdout, "OpenGL Error(%s): %s\n", errorString.c_str(), title);
}
return error == GL_NO_ERROR;
}
void camera_compute(Camera & c)
{
c.eye.x = cos(c.theta) * sin(c.phi) * c.radius + c.o.x;
c.eye.y = cos(c.phi) * c.radius + c.o.y ;
c.eye.z = sin(c.theta) * sin(c.phi) * c.radius + c.o.z;
c.up = glm::vec3(0.f, c.phi < M_PI ?1.f:-1.f, 0.f);
}
void camera_defaults(Camera & c)
{
c.phi = 3.14/2.f;
c.theta = 3.14/2.f;
c.radius = 10.f;
camera_compute(c);
}
void camera_zoom(Camera & c, float factor)
{
c.radius += factor * c.radius ;
if (c.radius < 0.1)
{
c.radius = 10.f;
c.o = c.eye + glm::normalize(c.o - c.eye) * c.radius;
}
camera_compute(c);
}
void camera_turn(Camera & c, float phi, float theta)
{
c.theta += 1.f * theta;
c.phi -= 1.f * phi;
if (c.phi >= (2 * M_PI) - 0.1 )
c.phi = 0.00001;
else if (c.phi <= 0 )
c.phi = 2 * M_PI - 0.1;
camera_compute(c);
}
void camera_pan(Camera & c, float x, float y)
{
glm::vec3 up(0.f, c.phi < M_PI ?1.f:-1.f, 0.f);
glm::vec3 fwd = glm::normalize(c.o - c.eye);
glm::vec3 side = glm::normalize(glm::cross(fwd, up));
c.up = glm::normalize(glm::cross(side, fwd));
c.o[0] += up[0] * y * c.radius * 2;
c.o[1] += up[1] * y * c.radius * 2;
c.o[2] += up[2] * y * c.radius * 2;
c.o[0] -= side[0] * x * c.radius * 2;
c.o[1] -= side[1] * x * c.radius * 2;
c.o[2] -= side[2] * x * c.radius * 2;
camera_compute(c);
}
void init_gui_states(GUIStates & guiStates)
{
guiStates.panLock = false;
guiStates.turnLock = false;
guiStates.zoomLock = false;
guiStates.lockPositionX = 0;
guiStates.lockPositionY = 0;
guiStates.camera = 0;
guiStates.time = 0.0;
guiStates.playing = false;
}