From 47005798779027615c5d70a2e24349c83bcc0a4f Mon Sep 17 00:00:00 2001 From: ktyl Date: Sat, 7 Aug 2021 03:44:08 +0100 Subject: [PATCH] extract time to file --- shader/root/rt.glsl | 5 +--- src/main.c | 60 +++------------------------------------------ src/sphere.c | 42 +++++++++++++++++++++++++++++++ src/sphere.h | 3 +++ src/time.c | 6 +++++ src/time.h | 6 +++++ todo.md | 6 ++--- 7 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 src/time.c create mode 100644 src/time.h diff --git a/shader/root/rt.glsl b/shader/root/rt.glsl index 10a3ff5..66171d9 100644 --- a/shader/root/rt.glsl +++ b/shader/root/rt.glsl @@ -23,7 +23,7 @@ layout (location = 13) uniform Sphere _spheres[SPHERES]; 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 = 20.0; +const float INF = 30.0; const float PI = 3.14159; struct Ray @@ -75,9 +75,6 @@ Ray createCameraRay(vec2 uv) dir = _camll + uv.x*_camh + uv.y*_camv; dir = normalize(dir); - float max_x = 5.0; - float max_y = 5.0; - Ray ray; ray.origin = _cpos; ray.direction = dir; diff --git a/src/main.c b/src/main.c index 70622fb..056aad0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "main.h" #include "gfx.h" +#include "time.h" #include "sphere.h" @@ -11,14 +12,9 @@ const int HEIGHT = 420; // input int checkQuit(); -// time -float time(); - void updateUniforms(GLuint shaderProgram); void updateCameraUniforms(GLuint shaderProgram); -void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); - int main() { // create a window and opengl context @@ -66,51 +62,10 @@ int main() return 0; } -void makeSpheres(struct Sphere *spheres, int count) -{ - float t = time(); - - vec3 albedos[] = - { - {0.0,0.0,1.0}, - {0.0,1.0,0.0}, - {0.0,1.0,1.0}, - {1.0,0.0,0.0}, - {1.0,0.0,1.0}, - {1.0,1.0,0.0}, - {1.0,1.0,1.0} - }; - - // distance from center - float d = 6.0; - float radius = 0.5; - float x; - vec3 sc = {0.0,0.0,1.0}; - - for (int i = 0; i < count; i++) - { - x = t*0.1 + 2.0*CGLM_PI * i/(float)count; - sc[0] = sin(x)*d; - sc[2] = cos(x)*d; - - float ic = i/(float)count; - float r = sin(ic); - float g = sin(ic+1.0); - float b = sin(ic+2.0); - g = 1.0; - b = 1.0; - - vec3 col = {r,g,b}; - glm_vec3_scale(col, 0.5, col); - glm_vec3_adds(col, 0.5, col); - - spheres[i] = makeSphere(sc,radius,col); - } -} void updateUniforms(GLuint shaderProgram) { - float t = time(); // time + float t = now(); float sin_t = sin(t); int tLocation = glGetUniformLocation(shaderProgram, "_t"); glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f); @@ -137,7 +92,7 @@ void updateCameraUniforms(GLuint shaderProgram) int inverseProjLocation = glGetUniformLocation(shaderProgram, "_cameraInverseProjection"); glUniformMatrix4fv(inverseProjLocation, 1, GL_FALSE, proji[0]); - float t = time(); + float t = now(); vec3 cdir, cright, cup; vec3 up = {0.1*sin(t),1.0,0.2*cos(t)}; // world up @@ -145,7 +100,7 @@ void updateCameraUniforms(GLuint shaderProgram) // lookat vector and view matrix float d = 10.0 + sin(t); - float pt = -t*0.1; + float pt = -t; vec3 cpos = {sin(pt)*d,cos(0.5*t)*5.0,cos(pt)*d}; // camera pos vec3 tpos = {0.0,0.0,0.0}; // target pos glm_vec3_sub(cpos,tpos,cdir); // look dir (inverted cause opengl noises) @@ -212,13 +167,6 @@ void updateCameraUniforms(GLuint shaderProgram) glUniform3f(camllLocation, camll[0], camll[1], camll[2]); } - -float time() -{ - // ms / 1000.0 = seconds since start - return (float)SDL_GetTicks() / 1000.0f; -} - int checkQuit() { SDL_Event event; diff --git a/src/sphere.c b/src/sphere.c index 35517d0..df97fb1 100644 --- a/src/sphere.c +++ b/src/sphere.c @@ -1,5 +1,47 @@ #include "sphere.h" +void makeSpheres(struct Sphere *spheres, int count) +{ + //float t = time(); + + vec3 albedos[] = + { + {0.0,0.0,1.0}, + {0.0,1.0,0.0}, + {0.0,1.0,1.0}, + {1.0,0.0,0.0}, + {1.0,0.0,1.0}, + {1.0,1.0,0.0}, + {1.0,1.0,1.0} + }; + + // distance from center + float d = 6.0; + float radius = 0.5; + float x; + vec3 sc = {0.0,0.0,1.0}; + + float t = now(); + for (int i = 0; i < count; i++) + { + x = 2.0*CGLM_PI * (float)i/(float)count; + sc[0] = sin(x)*d; + sc[1] = sin(x*3.0-2.5*sin(t)); + sc[2] = cos(x)*d; + + float ic = i/(float)count*CGLM_PI*2.0; + float r = sin(ic); + float g = sin(ic+CGLM_PI/1.5); + float b = sin(ic+2.0*CGLM_PI/1.5); + + vec3 col = {r,g,b}; + glm_vec3_scale(col, 0.5, col); + glm_vec3_adds(col, 0.5, col); + + spheres[i] = makeSphere(sc,radius,col); + } +} + struct Sphere makeSphere(vec3 center, float radius, vec3 albedo) { struct Sphere s; diff --git a/src/sphere.h b/src/sphere.h index 5f416e4..2759c42 100644 --- a/src/sphere.h +++ b/src/sphere.h @@ -7,12 +7,15 @@ #include +#include "time.h" + struct Sphere { vec4 cr; vec3 albedo; }; +void makeSpheres(struct Sphere *spheres, int count); struct Sphere makeSphere(vec3 center, float radius, vec3 albedo); void updateSphereUniform(GLuint shaderProgram, struct Sphere sphere); void updateSphereUniforms(GLuint shaderProgram, struct Sphere *spheres, int count); diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..f08b2a7 --- /dev/null +++ b/src/time.c @@ -0,0 +1,6 @@ +#include "time.h" + +float now() +{ + return (float)SDL_GetTicks() / 1000.0; +} diff --git a/src/time.h b/src/time.h new file mode 100644 index 0000000..c7786c5 --- /dev/null +++ b/src/time.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +// seconds since program start +float now(); diff --git a/todo.md b/todo.md index 0952e9c..270d329 100644 --- a/todo.md +++ b/todo.md @@ -17,11 +17,11 @@ * [ ] do a 'fract * [-] depth * [x] acquire value - * [ ] depth texture * [ ] acquire randomness * [ ] acceleration time ! - * [ ] auxiliary textures: g buffer - * [ ] frame blending + * [ ] auxiliary textures + * [ ] depth buffer + * [ ] frame blending * [ ] maybe do some fractals * [ ] mandelbrot * [ ] julia