-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
194 lines (152 loc) · 5.73 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
// glew must be before glfw
#include <GL/glew.h>
#include <GLFW/glfw3.h>
// contains helper functions such as shader compiler
#include "icg_helper.h"
#include "glm/gtc/matrix_transform.hpp"
#include "framebuffer.h"
#include "cube/cube.h"
#include "quad/quad.h"
#include "screenquad/screenquad.h"
Cube cube;
Quad quad;
int window_width = 800;
int window_height = 600;
FrameBuffer framebuffer;
ScreenQuad screenquad;
using namespace glm;
mat4 projection_matrix;
mat4 view_matrix;
mat4 cube_model_matrix;
void Init(GLFWwindow* window) {
glClearColor(1.0, 1.0, 1.0 /*white*/, 1.0 /*solid*/);
glEnable(GL_DEPTH_TEST);
cube.Init();
quad.Init();
// setup view and projection matrices
vec3 cam_pos(2.0f, 2.0f, 2.0f);
vec3 cam_look(0.0f, 0.0f, 0.0f);
vec3 cam_up(0.0f, 0.0f, 1.0f);
view_matrix = lookAt(cam_pos, cam_look, cam_up);
float ratio = window_width / (float) window_height;
projection_matrix = perspective(45.0f, ratio, 0.1f, 10.0f);
// create the model matrix (remember OpenGL is right handed)
// accumulated transformation
cube_model_matrix = scale(IDENTITY_MATRIX, vec3(0.5));
cube_model_matrix = translate(cube_model_matrix, vec3(0.0, 0.0, 0.6));
// on retina/hidpi displays, pixels != screen coordinates
// this unsures that the framebuffer has the same size as the window
// (see http://www.glfw.org/docs/latest/window.html#window_fbsize)
glfwGetFramebufferSize(window, &window_width, &window_height);
GLuint framebuffer_texture_id, framebuffer_tmp_texture_id;
std::tie(framebuffer_texture_id, framebuffer_tmp_texture_id) = framebuffer.Init(window_width, window_height, true);
cout << framebuffer_texture_id << endl;
cout << framebuffer_tmp_texture_id << endl;
screenquad.Init(window_width, window_height, framebuffer_texture_id, framebuffer_tmp_texture_id);
}
void Display() {
// render to framebuffer
framebuffer.Clear();
framebuffer.Bind();
{
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
cube.Draw(cube_model_matrix, view_matrix, projection_matrix);
quad.Draw(IDENTITY_MATRIX, view_matrix, projection_matrix);
screenquad.Draw(0);
}
framebuffer.Unbind();
// render to Window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, window_width, window_height);
screenquad.Draw(1);
}
// gets called when the windows/framebuffer is resized.
void ResizeCallback(GLFWwindow* window, int width, int height) {
window_width = width;
window_height = height;
float ratio = window_width / (float) window_height;
projection_matrix = perspective(45.0f, ratio, 0.1f, 10.0f);
glViewport(0, 0, window_width, window_height);
// when the window is resized, the framebuffer and the screenquad
// should also be resized
framebuffer.Cleanup();
GLuint framebuffer_texture_id, framebuffer_tmp_texture_id;
std::tie(framebuffer_texture_id, framebuffer_tmp_texture_id) = framebuffer.Init(window_width, window_height, true);
screenquad.Init(window_width, window_height, framebuffer_texture_id, framebuffer_tmp_texture_id);
screenquad.UpdateSize(window_width, window_height);
}
void ErrorCallback(int error, const char* description) {
fputs(description, stderr);
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if(action != GLFW_RELEASE) {
return;
}
switch(key) {
case 'Q':
screenquad.UpdateVariance(0.25);
break;
case 'W':
screenquad.UpdateVariance(-0.25);
break;
default:
break;
}
}
int main(int argc, char *argv[]) {
// GLFW Initialization
if(!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return EXIT_FAILURE;
}
glfwSetErrorCallback(ErrorCallback);
// hint GLFW that we would like an OpenGL 3 context (at least)
// http://www.glfw.org/faq.html#how-do-i-create-an-opengl-30-context
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// attempt to open the window: fails if required version unavailable
// note some Intel GPUs do not support OpenGL 3.2
// note update the driver of your graphic card
GLFWwindow* window = glfwCreateWindow(window_width, window_height,
"framebuffer", NULL, NULL);
if(!window) {
glfwTerminate();
return EXIT_FAILURE;
}
// makes the OpenGL context of window current on the calling thread
glfwMakeContextCurrent(window);
// set the callback for escape key
glfwSetKeyCallback(window, KeyCallback);
// set the framebuffer resize callback
glfwSetFramebufferSizeCallback(window, ResizeCallback);
// GLEW Initialization (must have a context)
// https://www.opengl.org/wiki/OpenGL_Loading_Library
glewExperimental = GL_TRUE; // fixes glew error (see above link)
if(glewInit() != GLEW_NO_ERROR) {
fprintf( stderr, "Failed to initialize GLEW\n");
return EXIT_FAILURE;
}
cout << "OpenGL" << glGetString(GL_VERSION) << endl;
// initialize our OpenGL program
Init(window);
// render loop
while(!glfwWindowShouldClose(window)){
Display();
glfwSwapBuffers(window);
glfwPollEvents();
}
// cleanup
quad.Cleanup();
cube.Cleanup();
framebuffer.Cleanup();
screenquad.Cleanup();
// close OpenGL window and terminate GLFW
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}