//this is our main function, it executes every frame for every pixel on screen
void fragment() {
// defines the color to mitigate null visual error.
vec4 Color = vec4(vec3(0.1), 1.0);;
//calculate the scrren cordinates shifted such that the origin is in the center of the screen
vec2 uv = UV-0.5;
//scales the uv coordinates to avoid having weird FOV distortion based on screen ratio
uv.y = uv.y * SCREEN_PIXEL_SIZE.x/SCREEN_PIXEL_SIZE.y;
//generates the primary direction vector for thisfragment's ray.
vec4 RayDirection = MakeRayDirection(uv, FOV) * RotationXYZ(CameraRotation);
//gets the first hit object in the scene thanks to the "raycast" function, all the parameterzs are eithe calculated above or are uniforms / consts at the top (see bookmarks)
hit rayhit = raycast(CameraPosition + RayDirection * camera_safety_distance, RayDirection, MaxIteration, MaximumDistance, DistanceThreshold);
//colors in the pixel if the rayhit actually hit something.
if (rayhit.Distance <= DistanceThreshold){
Color = vec4(material_list[rayhit.ClosestSDF.material_ID].albedo, 1.0);
// a bunch of light/shadow calculation need to go here + post processing (look at obsidian notes for further details).
light lighthits[10];
for (int I; I <= mylight.length(); I++){
float distance_to_light = distance(mylight[I].position.xyz, rayhit.position.xyz);
switch (mylight[I].type){
case 0:
case 1:
case 2:
if (dot(mylight[I].direction.xyz, rayhit.normal.xyz) <= 0.0){
lighthits[I] = mylight[I];
}
}
}
}
//below are alternative color schemes, should be able to get rid of them in the future.
//Color = abs(rayhit.normal);
//Color = vec4(vec3(float(rayhit.iterations)/float(MaxIteration)), 1.0);
//Color = vec4(vec3(rayhit.depth/MaximumDistance), 1.0);
//Color = abs(rayhit.position)/10.0;
//finally outputs the color of the pixel.
COLOR = Color;
}