extract time to file

This commit is contained in:
ktyl 2021-08-07 03:44:08 +01:00
parent 5cc2c3d92b
commit 4700579877
7 changed files with 65 additions and 63 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -7,12 +7,15 @@
#include <cglm/vec3.h>
#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);

6
src/time.c Normal file
View File

@ -0,0 +1,6 @@
#include "time.h"
float now()
{
return (float)SDL_GetTicks() / 1000.0;
}

6
src/time.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <SDL2/SDL.h>
// seconds since program start
float now();

View File

@ -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