Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
318 lines (255 loc) · 11.3 KB
/
Copy pathmain.cpp
File metadata and controls
318 lines (255 loc) · 11.3 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
/*******************************************
* **** DESCRIPTION ****
****************************************/
#include <iostream>
#include <Rendering/GLTools.h>
#include <Rendering/VertexArrayObjects.h>
#include <Rendering/RenderPass.h>
#include "UI/imgui/imgui.h"
#include <UI/imguiTools.h>
#include <UI/Turntable.h>
#include <Importing/TextureTools.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
////////////////////// PARAMETERS /////////////////////////////
static bool s_isRotating = false;
static glm::vec4 s_color = glm::vec4(0.75, 0.74f, 0.82f, 1.0f); // far : blueish
static glm::vec4 s_lightPos = glm::vec4(2.0,2.0,2.0,1.0);
static float s_strength = 0.05f;
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////// MAIN ///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
static GLuint s_vectorTexture = 0;
static GLuint s_texHandle = 0;
static std::vector<float> s_vectorTexData;
static const int VECTOR_TEXTURE_SIZE = 128;
void updateVectorTexture(double elapsedTime)
{
// first time, create it
if (s_vectorTexture == 0)
{
s_vectorTexData.resize(VECTOR_TEXTURE_SIZE*VECTOR_TEXTURE_SIZE*3, 0.0);
DEBUGLOG->log("VectorSize: ", s_vectorTexData.size());
glGenTextures(1, &s_vectorTexture);
glBindTexture(GL_TEXTURE_2D, s_vectorTexture);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, VECTOR_TEXTURE_SIZE, VECTOR_TEXTURE_SIZE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
}
else
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, s_vectorTexture);
auto evaluate = [&](float t, float offsetX, float offsetY){
float x = sin(t + offsetX) * 0.5f + 0.5f;
float y = cos(t + offsetY) * 0.5f + 0.5f;
float z = 0.0f;
return glm::vec3(x,y,z);
};
for (int i = 0; i < VECTOR_TEXTURE_SIZE; i++)
{
for (int j = 0; j < VECTOR_TEXTURE_SIZE; j++)
{
auto vector = evaluate( (float) elapsedTime, ((float) i / (float) VECTOR_TEXTURE_SIZE) * 5.0f , ((float) j / (float) VECTOR_TEXTURE_SIZE) * 5.0f );
s_vectorTexData[ (i * VECTOR_TEXTURE_SIZE * 3) + (j * 3 )] = vector.x;
s_vectorTexData[ (i * VECTOR_TEXTURE_SIZE * 3) + (j * 3 + 1)] = vector.y;
s_vectorTexData[ (i * VECTOR_TEXTURE_SIZE * 3) + (j * 3 + 2)] = vector.z;
}
}
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, VECTOR_TEXTURE_SIZE, VECTOR_TEXTURE_SIZE, GL_RGB, GL_FLOAT, &s_vectorTexData[0] );
glBindTexture(GL_TEXTURE_2D, 0);
}
}
int main()
{
DEBUGLOG->setAutoPrint(true);
//////////////////////////////////////////////////////////////////////////////
/////////////////////// INIT //////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// create window and opengl context
auto window = generateWindow(800,800);
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////// RENDERING ///////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
///////////////////// Scene / View Settings //////////////////////////
glm::mat4 model = glm::mat4(1.0f);
glm::vec4 eye(0.0f, 0.0f, 3.0f, 1.0f);
glm::vec4 center(0.0f,0.0f,0.0f,1.0f);
glm::mat4 view = glm::lookAt(glm::vec3(eye), glm::vec3(center), glm::vec3(0,1,0));
// glm::mat4 perspective = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -1.0f, 6.0f);
/// perspective projection is experimental; yields weird warping effects due to vertex interpolation of uv-coordinates
glm::mat4 perspective = glm::perspective(glm::radians(65.f), getRatio(window), 0.1f, 10.f);
// create object
// Sphere grid;
Grid grid(10,10,0.1f,0.1f,true);
// Volume grid;
// load grass texture
s_texHandle = TextureTools::loadTexture(RESOURCES_PATH + std::string( "/grass.png"));
glBindTexture(GL_TEXTURE_2D, s_texHandle);
/////////////////////// Renderpass ///////////////////////////
DEBUGLOG->log("Shader Compilation: volume uvw coords"); DEBUGLOG->indent();
ShaderProgram shaderProgram("/modelSpace/GBuffer.vert", "/modelSpace/GBuffer.frag"); DEBUGLOG->outdent();
shaderProgram.update("model", model);
shaderProgram.update("view", view);
shaderProgram.update("projection", perspective);
shaderProgram.update("color", glm::vec4(1.0f,0.0f,0.0f,1.0f));
DEBUGLOG->log("FrameBufferObject Creation: volume uvw coords"); DEBUGLOG->indent();
FrameBufferObject fbo(getResolution(window).x, getResolution(window).y);
FrameBufferObject::s_internalFormat = GL_RGBA32F; // to allow arbitrary values in G-Buffer
fbo.addColorAttachments(4); DEBUGLOG->outdent(); // G-Buffer
FrameBufferObject::s_internalFormat = GL_RGBA; // restore default
RenderPass renderPass(&shaderProgram, &fbo);
renderPass.addEnable(GL_DEPTH_TEST);
renderPass.addClearBit(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
renderPass.addRenderable(&grid);
ShaderProgram compShader("/screenSpace/fullscreen.vert", "/screenSpace/finalCompositing.frag");
// ShaderProgram compShader("/screenSpace/fullscreen.vert", "/screenSpace/simpleAlphaTexture.frag");
Quad quad;
RenderPass compositing(&compShader, 0);
compositing.addClearBit(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
compositing.addRenderable(&quad);
// Geometry test shader
ShaderProgram geomShader("/modelSpace/geometry.vert", "/modelSpace/simpleLighting.frag", "/geometry/simpleGeom.geom");
RenderPass geom(&geomShader, 0);
geom.addClearBit(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
geom.addRenderable(&grid);
geom.addEnable(GL_DEPTH_TEST);
geom.addEnable(GL_ALPHA_TEST);
geom.addEnable(GL_BLEND);
glAlphaFunc(GL_GREATER, 0);
geomShader.update("projection", perspective);
//////////////////////////////////////////////////////////////////////////////
/////////////////////// GUI / USER INPUT ////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Setup ImGui binding
ImGui_ImplGlfwGL3_Init(window, true);
Turntable turntable;
double old_x;
double old_y;
glfwGetCursorPos(window, &old_x, &old_y);
auto cursorPosCB = [&](double x, double y)
{
ImGuiIO& io = ImGui::GetIO();
if ( io.WantCaptureMouse )
{ return; } // ImGUI is handling this
double d_x = x - old_x;
double d_y = y - old_y;
if ( turntable.getDragActive() )
{
turntable.dragBy(d_x, d_y, view);
}
old_x = x;
old_y = y;
};
auto mouseButtonCB = [&](int b, int a, int m)
{
if (b == GLFW_MOUSE_BUTTON_LEFT && a == GLFW_PRESS)
{
turntable.setDragActive(true);
}
if (b == GLFW_MOUSE_BUTTON_LEFT && a == GLFW_RELEASE)
{
turntable.setDragActive(false);
}
ImGui_ImplGlfwGL3_MouseButtonCallback(window, b, a, m);
};
auto keyboardCB = [&](int k, int s, int a, int m)
{
if (a == GLFW_RELEASE) {return;}
switch (k)
{
case GLFW_KEY_W:
eye += glm::inverse(view) * glm::vec4(0.0f,0.0f,-0.1f,0.0f);
center += glm::inverse(view) * glm::vec4(0.0f,0.0f,-0.1f,0.0f);
break;
case GLFW_KEY_A:
eye += glm::inverse(view) * glm::vec4(-0.1f,0.0f,0.0f,0.0f);
center += glm::inverse(view) * glm::vec4(-0.1f,0.0f,0.0f,0.0f);
break;
case GLFW_KEY_S:
eye += glm::inverse(view) * glm::vec4(0.0f,0.0f,0.1f,0.0f);
center += glm::inverse(view) * glm::vec4(0.0f,0.0f,0.1f,0.0f);
break;
case GLFW_KEY_D:
eye += glm::inverse(view) * glm::vec4(0.1f,0.0f,0.0f,0.0f);
center += glm::inverse(view) * glm::vec4(0.1f,0.0f,0.0f,0.0f);
break;
default:
break;
}
ImGui_ImplGlfwGL3_KeyCallback(window,k,s,a,m);
};
setCursorPosCallback(window, cursorPosCB);
setMouseButtonCallback(window, mouseButtonCB);
setKeyCallback(window, keyboardCB);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// RENDER LOOP /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
double elapsedTime = 0.0;
render(window, [&](double dt)
{
elapsedTime += dt;
std::string window_header = "Volume Renderer - " + std::to_string( 1.0 / dt ) + " FPS";
glfwSetWindowTitle(window, window_header.c_str() );
//////////////////////////////// GUI ////////////////////////////////
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplGlfwGL3_NewFrame(); // tell ImGui a new frame is being rendered
ImGui::PushItemWidth(-100);
if (ImGui::CollapsingHeader("Geometry Shader Settings"))
{
ImGui::ColorEdit4( "color", glm::value_ptr( s_color)); // color mixed at max distance
ImGui::SliderFloat("strength", &s_strength, 0.0f, 2.0f); // influence of color shift
}
ImGui::Checkbox("auto-rotate", &s_isRotating); // enable/disable rotating volume
ImGui::PopItemWidth();
//////////////////////////////////////////////////////////////////////////////
///////////////////////////// MATRIX UPDATING ///////////////////////////////
if (s_isRotating) // update view matrix
{
model = glm::rotate(glm::mat4(1.0f), (float) dt, glm::vec3(0.0f, 1.0f, 0.0f) ) * model;
}
view = glm::lookAt(glm::vec3(eye), glm::vec3(center), glm::vec3(0.0f, 1.0f, 0.0f));
//////////////////////////////////////////////////////////////////////////////
//////////////////////// SHADER / UNIFORM UPDATING //////////////////////////
// update view related uniforms
shaderProgram.update( "view", view);
shaderProgram.update( "model", turntable.getRotationMatrix() * model);
geomShader.update( "view", view);
geomShader.update( "model", turntable.getRotationMatrix() * model);
compShader.update( "vLightPos", view * turntable.getRotationMatrix() * s_lightPos);
updateVectorTexture(elapsedTime);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, s_vectorTexture);
// glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // this is altered by ImGui::Render(), so reset it every frame
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, s_texHandle);
geomShader.update("tex", 1);
geomShader.update("blendColor", 2.0);
geomShader.update("color", s_color);
geomShader.update("strength", s_strength);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////// RENDERING //// /////////////////////////////
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, fbo.getColorAttachmentTextureHandle(GL_COLOR_ATTACHMENT0)); // color
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, fbo.getColorAttachmeHntTextureHandle(GL_COLOR_ATTACHMENT1)); // normal
// glActiveTexture(GL_TEXTURE2);
// glBindTexture(GL_TEXTURE_2D, fbo.getColorAttachmentTextureHandle(GL_COLOR_ATTACHMENT2)); // position
// glActiveTexture(GL_TEXTURE0);
// compShader.update("colorMap", 0);
// compShader.update("normalMap", 1);
// compShader.update("positionMap", 2);
// renderPass.render();
// compositing.render();
geom.render();
ImGui::Render();
glDisable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO); // this is altered by ImGui::Render(), so reset it every frame
//////////////////////////////////////////////////////////////////////////////
});
destroyWindow(window);
return 0;
}