|
| 1 | +package org.lwjgl.demo.opengl.sampling; |
| 2 | + |
| 3 | +import org.lwjgl.BufferUtils; |
| 4 | +import org.lwjgl.demo.opengl.util.DemoUtils; |
| 5 | +import org.lwjgl.glfw.GLFWKeyCallback; |
| 6 | +import org.lwjgl.opengl.GL; |
| 7 | +import org.lwjgl.system.MemoryStack; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.nio.FloatBuffer; |
| 12 | +import java.nio.IntBuffer; |
| 13 | + |
| 14 | +import static org.lwjgl.demo.util.IOUtils.ioResourceToByteBuffer; |
| 15 | +import static org.lwjgl.glfw.GLFW.*; |
| 16 | +import static org.lwjgl.opengl.GL33C.*; |
| 17 | +import static org.lwjgl.stb.STBImage.*; |
| 18 | +import static org.lwjgl.system.MemoryStack.stackPush; |
| 19 | +import static org.lwjgl.system.MemoryUtil.NULL; |
| 20 | +import static org.lwjgl.system.MemoryUtil.memAddress; |
| 21 | + |
| 22 | +/** |
| 23 | + * Demo showcasing Hierarchical Sample Warping (HSW) as first proposed in the paper |
| 24 | + * "Wavelet Importance: Efficiently Evaluating Products of Complex Functions" by Clarberg et al. |
| 25 | + * <p> |
| 26 | + * This demo implements importance sampling via hierarchical sample warping in its associated fragment shader |
| 27 | + * by using a 2D lightmap texture and generating samples with probabilities proportional to a texel's luminance. |
| 28 | + * <p> |
| 29 | + * See: |
| 30 | + * <ul> |
| 31 | + * <li><a href="https://link.springer.com/content/pdf/10.1007/978-1-4842-4427-2_16.pdf">section 16.4.2.3 HIERARCHICAL TRANSFORMATION in chapter "Transformations Zoo" of "Ray Tracing Gems".</a></li> |
| 32 | + * <li><a href="http://graphics.ucsd.edu/~henrik/papers/wavelet_importance_sampling.pdf">"Wavelet Importance: Efficiently Evaluating Products of Complex Functions" by Clarberg et al.</a></li> |
| 33 | + * <li><a href="https://www.ea.com/seed/news/siggraph21-global-illumination-surfels">SIGGRAPH 21: Global Illumination Based on Surfels</a></li> |
| 34 | + * </ul> |
| 35 | + * |
| 36 | + * @author Kai Burjack |
| 37 | + */ |
| 38 | +public class HierarchicalSampleWarping { |
| 39 | + private static int width = 800, height = 800; |
| 40 | + public static void main(String[] args) throws IOException { |
| 41 | + if (!glfwInit()) |
| 42 | + throw new IllegalStateException("Unable to initialize GLFW"); |
| 43 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); |
| 44 | + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); |
| 45 | + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
| 46 | + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); |
| 47 | + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); |
| 48 | + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); |
| 49 | + long window = glfwCreateWindow(width, height, "Hello HSW!", NULL, NULL); |
| 50 | + if (window == NULL) |
| 51 | + throw new RuntimeException("Failed to create the GLFW window"); |
| 52 | + glfwSetKeyCallback(window, new GLFWKeyCallback() { |
| 53 | + public void invoke(long window, int key, int scancode, int action, int mods) { |
| 54 | + if (action != GLFW_RELEASE) |
| 55 | + return; |
| 56 | + if (key == GLFW_KEY_ESCAPE) |
| 57 | + glfwSetWindowShouldClose(window, true); |
| 58 | + } |
| 59 | + }); |
| 60 | + glfwMakeContextCurrent(window); |
| 61 | + glfwSwapInterval(1); |
| 62 | + GL.createCapabilities(); |
| 63 | + |
| 64 | + // Read actualy framebuffer size for HiDPI displays |
| 65 | + try (MemoryStack stack = stackPush()) { |
| 66 | + IntBuffer framebufferSize = stack.mallocInt(2); |
| 67 | + nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4); |
| 68 | + width = framebufferSize.get(0); |
| 69 | + height = framebufferSize.get(1); |
| 70 | + } |
| 71 | + |
| 72 | + // Create shader program which implements visualization of the hierarchical sample warping |
| 73 | + int program = glCreateProgram(); |
| 74 | + { |
| 75 | + int vshader = DemoUtils.createShader("org/lwjgl/demo/opengl/sampling/hsw.vs.glsl", GL_VERTEX_SHADER, null); |
| 76 | + int fshader = DemoUtils.createShader("org/lwjgl/demo/opengl/sampling/hsw.fs.glsl", GL_FRAGMENT_SHADER, null); |
| 77 | + glAttachShader(program, vshader); |
| 78 | + glAttachShader(program, fshader); |
| 79 | + glBindFragDataLocation(program, 0, "color"); |
| 80 | + glLinkProgram(program); |
| 81 | + int linked = glGetProgrami(program, GL_LINK_STATUS); |
| 82 | + String programLog = glGetProgramInfoLog(program); |
| 83 | + if (programLog.trim().length() > 0) |
| 84 | + System.err.println(programLog); |
| 85 | + if (linked == 0) |
| 86 | + throw new AssertionError("Could not link program"); |
| 87 | + } |
| 88 | + glUseProgram(program); |
| 89 | + glUniform1i(glGetUniformLocation(program, "tex"), 0); |
| 90 | + int timeLocation = glGetUniformLocation(program, "time"); |
| 91 | + int blendFactorLocation = glGetUniformLocation(program, "blendFactor"); |
| 92 | + |
| 93 | + // Create empty VAO |
| 94 | + int vao = glGenVertexArrays(); |
| 95 | + glBindVertexArray(vao); |
| 96 | + |
| 97 | + // Create FBO with 32-bit floating point color buffer to do the sample accumulation |
| 98 | + int fbo = glGenFramebuffers(); |
| 99 | + glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 100 | + int framebufferTex = glGenTextures(); |
| 101 | + { |
| 102 | + glBindTexture(GL_TEXTURE_2D, framebufferTex); |
| 103 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, (ByteBuffer) null); |
| 104 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 105 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 106 | + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, framebufferTex, 0); |
| 107 | + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 108 | + throw new AssertionError("Incomplete framebuffer"); |
| 109 | + } |
| 110 | + |
| 111 | + // Load an image, that we want to sample via hierarchical sample warping, into a texture |
| 112 | + // and generate mipmaps for it |
| 113 | + int tex = glGenTextures(); |
| 114 | + { |
| 115 | + glBindTexture(GL_TEXTURE_2D, tex); |
| 116 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 117 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 118 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 119 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 120 | + IntBuffer w = BufferUtils.createIntBuffer(1); |
| 121 | + IntBuffer h = BufferUtils.createIntBuffer(1); |
| 122 | + IntBuffer comp = BufferUtils.createIntBuffer(1); |
| 123 | + ByteBuffer imageBuffer = ioResourceToByteBuffer("org/lwjgl/demo/opengl/sampling/env-square2.hdr", 64 * 1024); |
| 124 | + stbi_set_flip_vertically_on_load(true); |
| 125 | + if (!stbi_info_from_memory(imageBuffer, w, h, comp)) |
| 126 | + throw new IOException("Failed to read image information: " + stbi_failure_reason()); |
| 127 | + FloatBuffer image = stbi_loadf_from_memory(imageBuffer, w, h, comp, 3); |
| 128 | + if (image == null) |
| 129 | + throw new IOException("Failed to load image: " + stbi_failure_reason()); |
| 130 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, w.get(0), h.get(0), 0, GL_RGB, GL_FLOAT, (ByteBuffer) null); |
| 131 | + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w.get(0), h.get(0), GL_RGB, GL_FLOAT, image); |
| 132 | + stbi_image_free(image); |
| 133 | + glGenerateMipmap(GL_TEXTURE_2D); |
| 134 | + } |
| 135 | + |
| 136 | + glfwShowWindow(window); |
| 137 | + |
| 138 | + long lastTime = System.nanoTime(); |
| 139 | + float time = 0.0f; |
| 140 | + int frameNumber = 0; |
| 141 | + |
| 142 | + // Enable blending for sample accumulation in the FBO |
| 143 | + glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA); |
| 144 | + glEnable(GL_BLEND); |
| 145 | + |
| 146 | + while (!glfwWindowShouldClose(window)) { |
| 147 | + glfwPollEvents(); |
| 148 | + |
| 149 | + long thisTime = System.nanoTime(); |
| 150 | + float elapsed = (thisTime - lastTime) / 1E9f; |
| 151 | + time += elapsed; |
| 152 | + lastTime = thisTime; |
| 153 | + |
| 154 | + glViewport(0, 0, width, height); |
| 155 | + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); |
| 156 | + glUseProgram(program); |
| 157 | + glBindTexture(GL_TEXTURE_2D, tex); |
| 158 | + glUniform1f(blendFactorLocation, frameNumber / (frameNumber + 1.0f)); |
| 159 | + glUniform1f(timeLocation, time); |
| 160 | + glDrawArrays(GL_TRIANGLES, 0, 3); |
| 161 | + glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo); |
| 162 | + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 163 | + glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 164 | + |
| 165 | + glfwSwapBuffers(window); |
| 166 | + frameNumber++; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments