forked from LWJGL/lwjgl3-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo33Ubo.java
502 lines (439 loc) · 18.2 KB
/
Demo33Ubo.java
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
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package org.lwjgl.demo.opengl.raytracing;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLUtil;
import org.lwjgl.system.*;
import org.lwjgl.demo.opengl.util.DemoUtils;
import org.joml.Matrix4f;
import org.joml.Matrix4x3f;
import org.joml.Vector3f;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static java.lang.Math.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL33C.*;
import static org.lwjgl.system.MemoryUtil.*;
/**
* Raytracing demo.
* <p/>
* Same as {@link Demo33} but with using a Uniform Buffer Object to transfer the
* camera parameters to the shader.
*
* @author Kai Burjack
*/
public class Demo33Ubo {
private long window;
private int width = 1024;
private int height = 768;
private boolean resetFramebuffer = true;
private int tex;
private int vao;
private int fbo;
private int rayTracingProgram;
private int quadProgram;
private int sampler;
private int cameraSettingsUbo;
private ByteBuffer cameraSettingsUboData = BufferUtils.createByteBuffer(4 * 4 * 5);
private FloatBuffer cameraSettingsUboDataFb = cameraSettingsUboData.asFloatBuffer();
/**
* The UBO binding point that we assign manually. In OpenGL 4.3 this can be
* queried from the linked program via glGetProgramResource.
*/
private int cameraSettingsUboBinding = 1;
private int timeUniform;
private int blendFactorUniform;
private int framebufferUniform;
private int widthUniform;
private int heightUniform;
private int bounceCountUniform;
private float mouseDownX;
private float mouseX;
private boolean mouseDown;
private float currRotationAboutY = 0.0f;
private float rotationAboutY = 0.8f;
private long firstTime;
private int frameNumber;
private int bounceCount = 1;
private Matrix4f projMatrix = new Matrix4f();
private Matrix4x3f viewMatrix = new Matrix4x3f();
private Matrix4f invViewProjMatrix = new Matrix4f();
private Vector3f tmpVector = new Vector3f();
private Vector3f cameraPosition = new Vector3f();
private Vector3f cameraLookAt = new Vector3f(0.0f, 0.5f, 0.0f);
private Vector3f cameraUp = new Vector3f(0.0f, 1.0f, 0.0f);
GLFWErrorCallback errCallback;
GLFWKeyCallback keyCallback;
GLFWFramebufferSizeCallback fbCallback;
GLFWCursorPosCallback cpCallback;
GLFWMouseButtonCallback mbCallback;
Callback debugProc;
static {
/*
* Tell LWJGL that we only want 3.3 functionality.
*/
System.setProperty("org.lwjgl.opengl.maxVersion", "3.3");
}
private void init() throws IOException {
glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
private GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);
@Override
public void invoke(int error, long description) {
if (error == GLFW_VERSION_UNAVAILABLE)
System.err.println("This demo requires OpenGL 3.3 or higher.");
delegate.invoke(error, description);
}
@Override
public void free() {
delegate.free();
}
});
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "Raytracing Demo (fragment shader)", NULL, NULL);
if (window == NULL) {
throw new AssertionError("Failed to create the GLFW window");
}
System.out.println("Press keypad '+' or 'page up' to increase the number of bounces.");
System.out.println("Press keypad '-' or 'page down' to decrease the number of bounces.");
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (action != GLFW_RELEASE) {
return;
}
if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, true);
} else if (key == GLFW_KEY_KP_ADD || key == GLFW_KEY_PAGE_UP) {
int newBounceCount = Math.min(4, Demo33Ubo.this.bounceCount + 1);
if (newBounceCount != Demo33Ubo.this.bounceCount) {
Demo33Ubo.this.bounceCount = newBounceCount;
System.out.println("Ray bounce count is now: " + Demo33Ubo.this.bounceCount);
Demo33Ubo.this.frameNumber = 0;
}
} else if (key == GLFW_KEY_KP_SUBTRACT || key == GLFW_KEY_PAGE_DOWN) {
int newBounceCount = Math.max(1, Demo33Ubo.this.bounceCount - 1);
if (newBounceCount != Demo33Ubo.this.bounceCount) {
Demo33Ubo.this.bounceCount = newBounceCount;
System.out.println("Ray bounce count is now: " + Demo33Ubo.this.bounceCount);
Demo33Ubo.this.frameNumber = 0;
}
}
}
});
glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
if ( width > 0 && height > 0 && (Demo33Ubo.this.width != width || Demo33Ubo.this.height != height) ) {
Demo33Ubo.this.width = width;
Demo33Ubo.this.height = height;
Demo33Ubo.this.resetFramebuffer = true;
Demo33Ubo.this.frameNumber = 0;
}
}
});
glfwSetCursorPosCallback(window, cpCallback = new GLFWCursorPosCallback() {
@Override
public void invoke(long window, double x, double y) {
Demo33Ubo.this.mouseX = (float)x;
if ( mouseDown ) {
Demo33Ubo.this.frameNumber = 0;
}
}
});
glfwSetMouseButtonCallback(window, mbCallback = new GLFWMouseButtonCallback() {
@Override
public void invoke(long window, int button, int action, int mods) {
if ( action == GLFW_PRESS ) {
Demo33Ubo.this.mouseDownX = Demo33Ubo.this.mouseX;
Demo33Ubo.this.mouseDown = true;
} else if ( action == GLFW_RELEASE ) {
Demo33Ubo.this.mouseDown = false;
Demo33Ubo.this.rotationAboutY = Demo33Ubo.this.currRotationAboutY;
}
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
try (MemoryStack frame = MemoryStack.stackPush()) {
IntBuffer framebufferSize = frame.mallocInt(2);
nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
width = framebufferSize.get(0);
height = framebufferSize.get(1);
}
GL.createCapabilities();
debugProc = GLUtil.setupDebugMessageCallback();
/* Create all needed GL resources */
createFramebufferTexture();
createSampler();
createFrameBufferObject();
createCameraSettingsUbo();
vao = glGenVertexArrays();
createRayTracingProgram();
initRayTracingProgram();
createQuadProgram();
initQuadProgram();
glfwShowWindow(window);
firstTime = System.nanoTime();
}
/**
* Create the full-scren quad shader.
*
* @throws IOException
*/
private void createQuadProgram() throws IOException {
int program = glCreateProgram();
int vshader = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/quad.vs", GL_VERTEX_SHADER, "330");
int fshader = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/quad.fs", GL_FRAGMENT_SHADER, "330");
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glBindFragDataLocation(program, 0, "color");
glLinkProgram(program);
int linked = glGetProgrami(program, GL_LINK_STATUS);
String programLog = glGetProgramInfoLog(program);
if (programLog.trim().length() > 0) {
System.err.println(programLog);
}
if (linked == 0) {
throw new AssertionError("Could not link program");
}
this.quadProgram = program;
}
/**
* Create the ray tracing shader program.
*
* @return that program id
*
* @throws IOException
*/
private void createRayTracingProgram() throws IOException {
int program = glCreateProgram();
int vshader = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/quad.vs", GL_VERTEX_SHADER, "330");
int fshader = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/raytracingUbo.fs", GL_FRAGMENT_SHADER);
int random = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/random.glsl", GL_FRAGMENT_SHADER);
int randomCommon = DemoUtils.createShader("org/lwjgl/demo/opengl/raytracing/randomCommon.glsl", GL_FRAGMENT_SHADER, "330");
glAttachShader(program, vshader);
glAttachShader(program, fshader);
glAttachShader(program, random);
glAttachShader(program, randomCommon);
glBindFragDataLocation(program, 0, "color");
glLinkProgram(program);
int linked = glGetProgrami(program, GL_LINK_STATUS);
String programLog = glGetProgramInfoLog(program);
if (programLog.trim().length() > 0) {
System.err.println(programLog);
}
if (linked == 0) {
throw new AssertionError("Could not link program");
}
this.rayTracingProgram = program;
}
/**
* Initialize the full-screen-quad program.
*/
private void initQuadProgram() {
glUseProgram(quadProgram);
int texUniform = glGetUniformLocation(quadProgram, "tex");
glUniform1i(texUniform, 0);
glUseProgram(0);
}
/**
* Initialize the ray tracing shader.
*/
private void initRayTracingProgram() {
glUseProgram(rayTracingProgram);
timeUniform = glGetUniformLocation(rayTracingProgram, "time");
blendFactorUniform = glGetUniformLocation(rayTracingProgram, "blendFactor");
framebufferUniform = glGetUniformLocation(rayTracingProgram, "framebuffer");
widthUniform = glGetUniformLocation(rayTracingProgram, "width");
heightUniform = glGetUniformLocation(rayTracingProgram, "height");
bounceCountUniform = glGetUniformLocation(rayTracingProgram, "bounceCount");
glUniform1i(framebufferUniform, 0);
int cameraSettingsIndex = glGetUniformBlockIndex(rayTracingProgram, "CameraSettings");
glUniformBlockBinding(rayTracingProgram, cameraSettingsIndex, this.cameraSettingsUboBinding);
glUseProgram(0);
}
/**
* Create the texture that will serve as our framebuffer.
*/
private void createFramebufferTexture() {
this.tex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, (ByteBuffer) null);
glBindTexture(GL_TEXTURE_2D, 0);
}
/**
* Create the sampler to sample the framebuffer texture within the shader.
*/
private void createSampler() {
this.sampler = glGenSamplers();
glSamplerParameteri(this.sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameteri(this.sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
private void resizeFramebufferTexture() {
glDeleteTextures(tex);
glDeleteFramebuffers(fbo);
createFramebufferTexture();
createFrameBufferObject();
}
/**
* Create the frame buffer object that our ray tracing shader uses to render
* into the framebuffer texture.
*/
private int createFrameBufferObject() {
this.fbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this.tex, 0);
int fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE) {
throw new AssertionError("Could not create FBO: " + fboStatus);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return fbo;
}
private void createCameraSettingsUbo() {
this.cameraSettingsUbo = glGenBuffers();
glBindBuffer(GL_UNIFORM_BUFFER, cameraSettingsUbo);
glBufferData(GL_UNIFORM_BUFFER, 4 * 4 * 5, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
private void updateCameraSettingsUbo() {
FloatBuffer fv = cameraSettingsUboDataFb;
/* Set viewing frustum corner rays in shader */
fv.put(cameraPosition.x).put(cameraPosition.y).put(cameraPosition.z).put(0.0f);
invViewProjMatrix.transformProject(tmpVector.set(-1, -1, 0)).sub(cameraPosition);
fv.put(tmpVector.x).put(tmpVector.y).put(tmpVector.z).put(0.0f);
invViewProjMatrix.transformProject(tmpVector.set(-1, 1, 0)).sub(cameraPosition);
fv.put(tmpVector.x).put(tmpVector.y).put(tmpVector.z).put(0.0f);
invViewProjMatrix.transformProject(tmpVector.set( 1, -1, 0)).sub(cameraPosition);
fv.put(tmpVector.x).put(tmpVector.y).put(tmpVector.z).put(0.0f);
invViewProjMatrix.transformProject(tmpVector.set( 1, 1, 0)).sub(cameraPosition);
fv.put(tmpVector.x).put(tmpVector.y).put(tmpVector.z).put(0.0f);
fv.rewind();
glBindBuffer(GL_UNIFORM_BUFFER, this.cameraSettingsUbo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, this.cameraSettingsUboData);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
/**
* Compute one frame by tracing the scene using our ray tracing shader and
* presenting that image on the screen.
*/
private void trace() {
glUseProgram(rayTracingProgram);
if (mouseDown) {
/*
* If mouse is down, compute the camera rotation based on mouse
* cursor location.
*/
currRotationAboutY = rotationAboutY + (mouseX - mouseDownX) * 0.01f;
} else {
currRotationAboutY = rotationAboutY;
}
/* Rotate camera about Y axis. */
cameraPosition.set((float) sin(-currRotationAboutY) * 3.0f, 2.0f, (float) cos(-currRotationAboutY) * 3.0f);
viewMatrix.setLookAt(cameraPosition, cameraLookAt, cameraUp);
if (resetFramebuffer) {
projMatrix.setPerspective((float) Math.toRadians(60.0f), (float) width / height, 1f, 2f);
resizeFramebufferTexture();
resetFramebuffer = false;
}
projMatrix.invertPerspectiveView(viewMatrix, invViewProjMatrix);
/* Update cameraSettings UBO */
updateCameraSettingsUbo();
long thisTime = System.nanoTime();
float elapsedSeconds = (thisTime - firstTime) / 1E9f;
glUniform1f(timeUniform, elapsedSeconds);
/*
* We are going to average multiple successive frames, so here we
* compute the blend factor between old frame and new frame. 0.0 - use
* only the new frame > 0.0 - blend between old frame and new frame
*/
float blendFactor = frameNumber / (frameNumber + 1.0f);
glUniform1f(blendFactorUniform, blendFactor);
glUniform1i(bounceCountUniform, bounceCount);
glUniform1f(widthUniform, width);
glUniform1f(heightUniform, height);
/* Bind UBO for rendering */
glBindBufferBase(GL_UNIFORM_BUFFER, this.cameraSettingsUboBinding, this.cameraSettingsUbo);
/*
* Draw full-screen quad to generate frame with our tracing shader
* program.
*/
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, tex);
glBindSampler(0, this.sampler);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindSampler(0, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(0);
/* Unbind UBO */
glBindBufferBase(GL_UNIFORM_BUFFER, this.cameraSettingsUboBinding, 0);
frameNumber++;
}
/**
* Present the final image on the screen/viewport.
*/
private void present() {
/*
* Draw the rendered image on the screen using textured full-screen
* quad.
*/
glUseProgram(quadProgram);
glBindVertexArray(vao);
glBindTexture(GL_TEXTURE_2D, tex);
glBindSampler(0, this.sampler);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindSampler(0, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
}
private void loop() {
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glViewport(0, 0, width, height);
trace();
present();
glfwSwapBuffers(window);
}
}
private void run() {
try {
init();
loop();
if (debugProc != null)
debugProc.free();
errCallback.free();
keyCallback.free();
fbCallback.free();
cpCallback.free();
mbCallback.free();
glfwDestroyWindow(window);
} catch (Throwable t) {
t.printStackTrace();
} finally {
glfwTerminate();
}
}
public static void main(String[] args) {
new Demo33Ubo().run();
}
}