From 5b55adfa6e7dc45fd995c6630761ce91939942be Mon Sep 17 00:00:00 2001 From: ktyl Date: Fri, 30 Jul 2021 02:06:59 +0100 Subject: [PATCH] construct view space axes --- makefile | 2 +- res/shader/root/{compute.glsl => rt.glsl} | 21 +++++++++++-------- src/main.c | 22 +++++++++++++++----- src/main.h | 3 +++ todo.md | 25 +++++++++++++---------- 5 files changed, 47 insertions(+), 26 deletions(-) rename res/shader/root/{compute.glsl => rt.glsl} (86%) diff --git a/makefile b/makefile index aa2b4e9..133c486 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ SHADER_TARGETS = $(SHADERS:$(SHADER_ROOT_DIR)/%.glsl=$(SHADER_TARGET_DIR)/%.comp TARGET = $(BIN_DIR)/oglc CC = gcc -LIBS = `pkg-config --static --libs glew sdl2` +LIBS = `pkg-config --static --libs glew sdl2 cglm` CFLAGS = -I$(SRC_DIR) -Wall SRC = $(shell find $(SRC_DIR) -name *.c) diff --git a/res/shader/root/compute.glsl b/res/shader/root/rt.glsl similarity index 86% rename from res/shader/root/compute.glsl rename to res/shader/root/rt.glsl index 16ed4bc..4cd406d 100644 --- a/res/shader/root/compute.glsl +++ b/res/shader/root/rt.glsl @@ -2,10 +2,14 @@ layout (location = 1) uniform vec4 t; +layout (location = 2) uniform vec3 w; // view space axes +layout (location = 3) uniform vec3 u; +layout (location = 4) uniform vec3 v; + layout(local_size_x = 1, local_size_y = 1) in; // size of local work group - 1 pixel layout(rgba32f, binding = 0) uniform image2D img_output; // rgba32f defines internal format, image2d for random write to output texture -const float INF = 1000000.0f; +const float INF = 1000.0f; #include sphere.glsl @@ -54,8 +58,6 @@ Ray createCameraRay(vec2 uv) // float2 rd = _CameraLensRadius * randomInUnitDisk(); // float3 offset = _CameraU * rd.x + _CameraV * rd.y; - // ... - float max_x = 5.0; float max_y = 5.0; @@ -87,16 +89,17 @@ void main() hit.normal = vec3(0.0,0.0,0.0); Sphere sphere; - sphere.center = vec3(0.0,0.0,10.0); - sphere.radius = 3.0+t.y; + //sphere.center = vec3(0.0,0.0,10.0+0.5*t.y); + sphere.center = w*-10.0; + sphere.radius = 4.0; // ray-sphere intersection intersectSphere(ray, hit, sphere); - if (hit.distance < INF) - { - pixel = vec4(t.y,1.0-t.y,1.0,1.0); - } + float depth = (hit.distance-6.0)/4.0; + + pixel = vec4(t.z,1.0-t.z,depth,1.0); + pixel *= (1.0-depth); // output to a specific pixel in the image imageStore(img_output, pixel_coords, pixel); diff --git a/src/main.c b/src/main.c index 894738b..552e3f2 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ int main() // compile shader programs unsigned int computeProgram = compileComputeShaderProgram( - "bin/res/compute.compute"); + "bin/res/rt.compute"); unsigned int quadProgram = compileQuadShaderProgram( "bin/res/shader.vert", "bin/res/shader.frag"); @@ -32,16 +32,30 @@ int main() initBuffers(); setVertexAttributes(); + // render loop while (!checkQuit()) { glUseProgram(computeProgram); // update uniforms - float t = time(); + float t = time(); // time float sin_t = sin(t); int tLocation = glGetUniformLocation(computeProgram, "t"); - glUniform4f(tLocation, t, (1.0 + sin_t)*0.5, 0.0f, 0.0f); + glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f); + + // form view space axes + vec3 u,v; + vec3 up = {0,1.0,0}; + vec3 w = {0,sin_t*0.1,-1.0}; + glm_vec3_norm(w); + glm_vec3_cross(up,w,u); + glm_vec3_norm(u); + glm_vec3_cross(w, u, v); + int wLocation = glGetUniformLocation(computeProgram, "w"); + glUniform3f(wLocation+0, w[0], w[1], w[2]); // w + glUniform3f(wLocation+1, u[0], u[1], u[2]); // u + glUniform3f(wLocation+2, v[0], v[1], v[2]); // v // dispatch compute shader glDispatchCompute((GLuint)width, (GLuint)height, 1); @@ -51,8 +65,6 @@ int main() // normal drawing pass glUseProgram(quadProgram); - //glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - //glClear(GL_COLOR_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); // use computed texture glBindTexture(GL_TEXTURE_2D, textureOutput); diff --git a/src/main.h b/src/main.h index c8b49f2..9fd7431 100644 --- a/src/main.h +++ b/src/main.h @@ -1 +1,4 @@ #include + +#include +#include diff --git a/todo.md b/todo.md index 10d1098..dc4ab3f 100644 --- a/todo.md +++ b/todo.md @@ -1,19 +1,18 @@ * [x] basic opengl initialisation -* [-] shader pre-processor +* [x] shader pre-processor * [x] ppp.py - * [ ] read root shaders from src/shader/ - * [ ] read include shaders from src shader/ include - * [ ] write processed shaders to bin/res/shader/ - * [ ] attempt to compile processed shaders - -* [ ] output frame to a file - * [ ] detect input keydown s - * [ ] get timestamp - * [ ] create and write to file (maybe with `stb_image.h`?) + * [x] read root shaders + * [x] read include shaders + * [x] write processed shaders to bin/res/shader/ + * [x] compile processed shaders * [-] render image with compute shader * [x] render a texture to a full-screen quad * [x] pass uniforms to texture to animate it - * [ ] ray tracing time + * [-] ray tracing time + * [-] perspective + * [-] depth + * [x] acquire value + * [ ] depth texture * [ ] acquire randomness * [ ] acceleration time ! * [ ] auxiliary textures: g buffer @@ -22,6 +21,10 @@ * [ ] mandelbrot * [ ] julia * [ ] trongle +* [ ] output frame to a file + * [ ] detect input keydown s + * [ ] get timestamp + * [ ] create and write to file (maybe with `stb_image.h`?) * [ ] command line arguments * [ ] help * [ ] window dimensions