Skip to content

Commit

Permalink
Implement Phong shading in the raytracer
Browse files Browse the repository at this point in the history
  • Loading branch information
mansen420 committed Mar 17, 2024
1 parent 84c5541 commit c1ff447
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
69 changes: 56 additions & 13 deletions shaders/raytracer.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#version 450 core
uniform float rand;
uniform float time;

out vec4 frag_output;
in vec2 texture_coordinates;
in vec2 pos_coords;


struct sphere
{
vec3 center;
float radius;
vec3 center;
float radius;
vec3 color;
bool emissive;
};
struct light
{
vec3 pos;
vec3 color;
};

struct ray
{
vec3 origin;
Expand Down Expand Up @@ -78,6 +87,7 @@ ray project_ray_perspective(camera_space cam_space, float focal_length)
return ray(cam_space.origin,
normalize(cam_space.origin - cam_space.w*focal_length + cam_space.u*pos_coords.x + cam_space.v*pos_coords.y));
}
light LIGHT = light(vec3(1), vec3(1));
vec3 trace(in ray r, in sphere[2] s, in int depth)
{
float t_min = 0, t_max = 10.0;
Expand All @@ -87,28 +97,61 @@ vec3 trace(in ray r, in sphere[2] s, in int depth)
ray r_in = r;
while(count++ < depth)
{
bool hit_object = false;
float closest_intersection = t_max;
sphere obj;
for(int i = 0; i < 2; ++i)
{
bool intersect;
float root = ray_sphere_intersection_root(r_in, s[i], t_min, t_max, intersect);
float root = ray_sphere_intersection_root(r_in, s[i], t_min, closest_intersection, intersect);
if(intersect)
{
vec3 P = extend_ray(r_in, root);
vec3 view_vec = cam_space.origin - P;
vec3 normal = P - s[i].center;
vec3 reflected_vec = normalize(reflect(view_vec, normal));
r_in = ray(r_in.origin, normalize( vec3(1.0, 1.0, 0.0) - P));
result = 0.5*normalize(normal)+0.5;
closest_intersection = root;
hit_object = true;
obj = s[i];
}
}
if(hit_object)
{
if(obj.emissive)
{
result *= 1.0;
break;
}
else
{
vec3 P = extend_ray(r_in, closest_intersection);
vec3 n = normalize(P - obj.center);
result *= obj.color;
float intensity = dot(n, normalize(LIGHT.pos - P));
result *= intensity;
break;
}
//vec3 view_vec = P - cam_space.origin;
//vec3 r = normalize(reflect(-view_vec, n));
//r_in.direction = normalize(r);
}
else
{
result = vec3(0.5, 0.8, 1.0);
//result *= mix(vec3(1.0), vec3(0.2, 0.5, 1.0), 0.5*normalize(r_in.direction).y+0.5);
//result *= 0;
break;
}
}
return result;
}
void main()
{
const float time_arg = 2*degrees(time);
const float time_var = (0.5*sin(2*degrees(time))+0.5);
const float time_neg = sin(time_arg);
ray r = project_ray_perspective(cam_space, 1.0);
sphere my_s1 = sphere(vec3(0.5, 0, -1.5), 0.8);
sphere my_s2 = sphere(vec3(0, 0, -2), 0.8);

sphere my_s1 = sphere(vec3(1, 0.2, -4), 0.8, vec3(1), false);
sphere my_s2 = sphere(vec3(-0.5, 0.0, -2.0), 0.8, vec3(1,0,0), false);
my_s1.center += 2*vec3(0, time_neg,0);
sphere s[2] = {my_s2, my_s1};
frag_output = vec4(trace(r, s, 10), 1.0);

frag_output = vec4(trace(r, s, 1), 1.0);
}
2 changes: 1 addition & 1 deletion src/engine_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace renderer
PP_PASS_ENBLD = true;
SHADOW_PASS_ENBLD = true;

RENDER_W = 1920, RENDER_H = 1080;
RENDER_W = 1000, RENDER_H = 1000;
RENDER_AR = 16.0/9.0;
SHADOW_MAP_H = SHADOW_MAP_W = 1024;
NEAR_PLANE = 0.1f, FAR_PLANE = 100.0f;
Expand Down
10 changes: 9 additions & 1 deletion src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ namespace renderer

glUniform3f(glGetUniformLocation(object_shader_program_ptr->get_ID(), "light_pos"),
internal_state.LIGHT_POS.x, internal_state.LIGHT_POS.y, internal_state.LIGHT_POS.z);

glUniform3f(glGetUniformLocation(object_shader_program_ptr->get_ID(), "view_vector"), camera::POS.x, camera::POS.y, camera::POS.z);
glUniformMatrix4fv(glGetUniformLocation(object_shader_program_ptr->get_ID(), "projection_transform"), 1, GL_FALSE,
glm::value_ptr(perspective_transform));
Expand Down Expand Up @@ -362,13 +361,22 @@ namespace renderer
glBindVertexArray(screen_quad_vao_ID);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void send_raytracing_uniforms()
{
using namespace glm;
glUniform1f(glGetUniformLocation(raytracing_shader_program_ptr->get_ID(), "rand"), float(rand())/RAND_MAX);
glUniform1f(glGetUniformLocation(raytracing_shader_program_ptr->get_ID(), "time"), float(radians(glfwGetTime())));
}
void raytracing_pass()
{
glViewport(OPENGL_VIEWPORT_X, OPENGL_VIEWPORT_Y, internal_state.RENDER_W, internal_state.RENDER_H);
glBindFramebuffer(GL_FRAMEBUFFER, raytracing_framebuffer_ID);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);

glUseProgram(raytracing_shader_program_ptr->get_ID());

send_raytracing_uniforms();
glBindVertexArray(screen_quad_vao_ID);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
Expand Down

0 comments on commit c1ff447

Please sign in to comment.