camera lookat

This commit is contained in:
ktyl 2021-08-02 23:32:47 +01:00
parent ff945c29d8
commit 9a14ab8283
3 changed files with 58 additions and 26 deletions

View File

@ -1,20 +1,23 @@
#version 430 #version 430
layout (location = 1) uniform vec4 t; // TODO: do i actually need explicit location descriptors?
layout (location = 1) uniform vec4 _t;
layout (location = 2) uniform vec3 _w; // view space axes layout (location = 2) uniform vec3 _w; // view space axes
layout (location = 3) uniform vec3 _u; layout (location = 3) uniform vec3 _u;
layout (location = 4) uniform vec3 _v; layout (location = 4) uniform vec3 _v;
layout (location = 5) uniform mat4 _cameraInverseProjection; layout (location = 5) uniform mat4 _cameraInverseProjection;
layout (location = 6) uniform vec3 _camh; layout (location = 6) uniform vec3 _camh;
layout (location = 7) uniform vec3 _camv; layout (location = 7) uniform vec3 _camv;
layout (location = 8) uniform vec3 _camll; layout (location = 8) uniform vec3 _camll;
layout (location = 9) uniform vec3 _cpos;
layout (location = 10) uniform vec3 _tpos; // target
layout(local_size_x = 1, local_size_y = 1) in; // size of local work group - 1 pixel 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 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;
#include sphere.glsl #include sphere.glsl
@ -57,6 +60,8 @@ Ray createCameraRay(vec2 uv)
uv = uv*0.5+0.5; uv = uv*0.5+0.5;
//uv.x=1-uv.x; //uv.x=1-uv.x;
vec3 target = vec3(0,0,0);
// transform camera origin to world space // transform camera origin to world space
// TODO: c2w matrix!! for now we just assume the camera is at the origin // TODO: c2w matrix!! for now we just assume the camera is at the origin
// float3 origin = mul(_CameraToWorld, float4(0.0,0.0,0.0,1.0)).xyz; // float3 origin = mul(_CameraToWorld, float4(0.0,0.0,0.0,1.0)).xyz;
@ -79,11 +84,11 @@ Ray createCameraRay(vec2 uv)
float max_y = 5.0; float max_y = 5.0;
Ray ray; Ray ray;
ray.origin = vec3(0.0,0.0,0.0); ray.origin = _cpos;
ray.direction = dir; ray.direction = dir;
return ray; return ray;
} };
void main() void main()
{ {
@ -106,26 +111,30 @@ void main()
hit.normal = vec3(0.0,0.0,0.0); hit.normal = vec3(0.0,0.0,0.0);
hit.albedo = vec3(0.0,0.0,0.0); hit.albedo = vec3(0.0,0.0,0.0);
vec3 spheresCenter = _w*-10.0; vec3 spheresCenter = vec3(0.0,0.0,0.0);
float t = _t.x;
Sphere s1; Sphere s1;
s1.center = spheresCenter+vec3(sin(t.x),0.0,cos(t.x))*2.5; s1.center = spheresCenter+vec3(sin(t),0.0,cos(t))*2.5;
s1.radius = 2.0; s1.radius = 2.0;
s1.albedo = vec3(1.0,0.0,0.0); s1.albedo = vec3(1.0,0.0,0.0);
t+=3.1415/1.5;
Sphere s2; Sphere s2;
s2.center = spheresCenter-vec3(sin(t.x),0.0,cos(t.x))*2.5; s2.center = spheresCenter+vec3(sin(t),0.0,cos(t))*2.5;
s2.radius = 2.0; s2.radius = 2.0;
s2.albedo = vec3(0.0,0.0,1.0); s2.albedo = vec3(0.0,1.0,0.0);
Sphere sphere; t+=3.1415/1.5;
sphere.center = _w*-10.0; Sphere s3;
sphere.center += vec3(0.0,0.0,t.y); s3.center = spheresCenter+vec3(sin(t),0.0,cos(t))*2.5;
sphere.radius = 4.0; s3.radius = 2.0;
s3.albedo = vec3(0.0,0.0,1.0);
// ray-sphere intersection // ray-sphere intersection
intersectSphere(ray, hit, s1); intersectSphere(ray, hit, s1);
intersectSphere(ray, hit, s2); intersectSphere(ray, hit, s2);
intersectSphere(ray, hit, s3);
// TODO: write depth to texture // TODO: write depth to texture
float depth = hit.distance/INF; float depth = hit.distance/INF;

View File

@ -66,7 +66,7 @@ void updateUniforms(GLuint shaderProgram)
{ {
float t = time(); // time float t = time(); // time
float sin_t = sin(t); float sin_t = sin(t);
int tLocation = glGetUniformLocation(shaderProgram, "t"); int tLocation = glGetUniformLocation(shaderProgram, "_t");
glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f); glUniform4f(tLocation, t, sin_t, (1.0 + sin_t)*0.5, 0.0f);
updateCameraUniforms(shaderProgram); updateCameraUniforms(shaderProgram);
@ -85,10 +85,31 @@ void updateCameraUniforms(GLuint shaderProgram)
int inverseProjLocation = glGetUniformLocation(shaderProgram, "_cameraInverseProjection"); int inverseProjLocation = glGetUniformLocation(shaderProgram, "_cameraInverseProjection");
glUniformMatrix4fv(inverseProjLocation, 1, GL_FALSE, proji[0]); glUniformMatrix4fv(inverseProjLocation, 1, GL_FALSE, proji[0]);
float t = time();
vec3 cdir, cright, cup;
vec3 up = {0.1*sin(t),1.0,0.2*cos(t)}; // world up
glm_vec3_normalize(up);
// lookat vector and view matrix
float d = 10.0 + sin(t);
vec3 cpos = {sin(-t)*d,cos(0.5*t)*5.0,cos(-t)*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)
glm_vec3_normalize(cdir);
glm_vec3_cross(up,cdir,cright); // camera right
glm_vec3_normalize(cright);
glm_vec3_cross(cdir,cright,cup); // camera up
glm_vec3_normalize(cup);
mat4 view;
glm_lookat(cpos,tpos,cup,view);
int cposLocation = glGetUniformLocation(shaderProgram, "_cpos");
glUniform3f(cposLocation, cpos[0],cpos[1],cpos[2]);
// form view space axes // form view space axes
vec3 u,v; vec3 w,u,v;
vec3 up = {0.0,1.0,0.0}; glm_vec3_copy(cdir,w);
vec3 w = {0.0,0.0,-1.0};
glm_vec3_norm(w); glm_vec3_norm(w);
glm_vec3_cross(up,w,u); glm_vec3_cross(up,w,u);
glm_vec3_norm(u); glm_vec3_norm(u);
@ -98,8 +119,9 @@ void updateCameraUniforms(GLuint shaderProgram)
glUniform3f(wLocation+1, u[0], u[1], u[2]); // u glUniform3f(wLocation+1, u[0], u[1], u[2]); // u
glUniform3f(wLocation+2, v[0], v[1], v[2]); // v glUniform3f(wLocation+2, v[0], v[1], v[2]); // v
// get camera properties // get camera properties
float theta = glm_rad(fovy); // convert fovy to radians float theta = glm_rad(fovy); // convert fov to radians
float h = tan(theta*0.5); float h = tan(theta*0.5);
float vph = 2.0*h; // viewport height float vph = 2.0*h; // viewport height
float vpw = aspect*vph; // viewport width float vpw = aspect*vph; // viewport width

View File

@ -10,6 +10,7 @@
* [x] pass uniforms to texture to animate it * [x] pass uniforms to texture to animate it
* [-] ray tracing time * [-] ray tracing time
* [x] perspective * [x] perspective
* [x] camera lookat
* [-] depth * [-] depth
* [x] acquire value * [x] acquire value
* [ ] depth texture * [ ] depth texture