//(C) 2022 Victor Suarez Rovere //Inspired in https://fabiensanglard.net/rayTracing_back_of_business_card/ #ifndef SHADER #include "tr.h" full_state_t state; #else #define int16_t int #define BIG_FLOAT 1.e23 #define SPHERE_RADIUS 4.5 #define is_negative(v) ((v)<0.) #define color_basic_t vec3 #define fixed_type float #define color_type float #define screen_coord_t float #define static #define round16(v) int16_t(round(v)) #endif struct hitout { float t; vec3 n; int16_t m; }; hitout T(vec3 o,vec3 d){ hitout r; r.t=BIG_FLOAT; r.m=0; float p=-o.z/d.z; if(.01.01) { // So far this is the minimum distance, save it. And also // compute the bouncing ray vector into 'n' r.t=s; r.n=normalize(pv+d*r.t); r.m=2; } } } return r; } // (S)ample the world and return the pixel color for // a ray passing by point o (Origin) and d (Direction) color_basic_t S0(vec3 o,vec3 d){ float t; vec3 n; color_basic_t col = {.7,.6,1.}; //sky color //Search for an intersection ray Vs World. hitout r1 = T(o,d); int16_t m=r1.m; t = r1.t; n = r1.n; if(m==0) { //No sphere found and the ray goes upward: Generate a sky color fixed_type u = fixed_type(1.)-fixed_type(d.z); u = u*u; col = col*u*u; } else { //A sphere was maybe hit. vec3 h=o+d*t; // h = intersection coordinate /*static const*/ vec3 ld = {9.,9.,16.}; vec3 l=normalize(ld+h*-1.); // 'l' = direction to light (random delta for shadows). vec3 r=d+n*(dot(n,d)*-2.); // r = The half-vector //Calculated the lambertian factor float b=dot(l,n); //Calculate illumination factor (lambertian coefficient > 0 or in shadow)? hitout r2 = T(h,l); if(is_negative(b) || (r2.m != 0)) { b=0.; } // Calculate the color 'p' with diffuse and specular component color_type p=0.; if(b>0.) { p=color_type(dot(l,r)); p=p*p; p=p*p; } if(m==1){ h=h*.2; //No sphere was hit and the ray was going downward: Generate a floor color static const color_basic_t c1 = {1.,.3,.3}; static const color_basic_t c2 = {1.,1.,1.}; bool ct = bool((round16(h.x)^round16(h.y)) & 1); color_basic_t fcolor = ct?c1:c2; fixed_type bc = (b*.5+.1); col = fcolor*bc; } else { //m == 2 A sphere was hit. Cast an ray bouncing from the sphere surface. color_basic_t pcol = {p,p,p}; col = pcol; } } return col; } // (S)ample the world and return the pixel color for // a ray passing by point o (Origin) and d (Direction) color_basic_t S(vec3 o,vec3 d){ float t; vec3 n; color_basic_t col = {.7,.6,1.}; //sky color //Search for an intersection ray Vs World. hitout r1 = T(o,d); int16_t m=r1.m; t = r1.t; n = r1.n; if(m==0) { //No sphere found and the ray goes upward: Generate a sky color fixed_type u = fixed_type(1.)-fixed_type(d.z); u = u*u; col = col*u*u; } else { //A sphere was maybe hit. vec3 h=o+d*t; // h = intersection coordinate /*static const*/ vec3 ld = {9.,9.,16.}; vec3 l=normalize(ld+h*-1.); // 'l' = direction to light (random delta for shadows). vec3 r=d+n*(dot(n,d)*-2.); // r = The half-vector //Calculated the lambertian factor float b=dot(l,n); //Calculate illumination factor (lambertian coefficient > 0 or in shadow)? hitout r2 = T(h,l); if(is_negative(b)|| (r2.m != 0)) { b=0.; } // Calculate the color 'p' with diffuse and specular component color_type p=0.; if(b>0.) { p=color_type(dot(l,r)); p=p*p; p=p*p; } if(m==1){ h=h*.2; //No sphere was hit and the ray was going downward: Generate a floor color static const color_basic_t c1 = {1.,.3,.3}; static const color_basic_t c2 = {1.,1.,1.}; bool ct = bool((round16(h.x)^round16(h.y)) & 1); color_basic_t fcolor = ct?c1:c2; fixed_type bc = (b*.5+.1); col = fcolor*bc; } else { //m == 2 A sphere was hit. Cast an ray bouncing from the sphere surface. color_basic_t pcol = {p,p,p}; col = pcol+S0(h,r)*fixed_type(.5); } } return col; } color_basic_t render_pixel_internal(screen_coord_t x, screen_coord_t y, float t) { vec3 orig = {10., -20., 10.}; float camera_pos = 1.; #if 0 vec3 camera_dir = {float(x), camera_pos, float(y)}; return S(orig, normalize(camera_dir)); #else vec3 c = vec3(0.); int i; for(i=0; i= 256) ? uint8_t(255):uint8_t(r); pix.g = (g >= 256) ? uint8_t(255):uint8_t(g); pix.b = (b >= 256) ? uint8_t(255):uint8_t(b); } return pix; } full_state_t full_update(INOUT(full_state_t) state, bool reset, bool button_state) { state.scene.frame = state.scene.frame + 1; if(reset) state.scene.frame = 0; return state; } #else //shader vec3 frag_render(float t, float x, float y, float mx, float my) { #if 0 float a = x+y; for(int n=0; n<10000; ++n) a+=.0001; int i = int(a*8.); a = float(i&5)/8.; vec3 color = vec3(a, my, fract(t)); return color; #else return render_pixel_internal(x-.5, y-.5, t); #endif } out vec4 outColor; void main() { vec3 c = frag_render(u_time, gl_FragCoord.x/u_resolution.x, gl_FragCoord.y/u_resolution.y, u_mouse.x/u_resolution.x, u_mouse.y/u_resolution.y); outColor = vec4(c, 1.0); } #endif //SHADER