Compare commits

..

2 Commits

3 changed files with 50 additions and 33 deletions

View File

View File

@@ -4,11 +4,11 @@
#include <cstdio> #include <cstdio>
const char *vertexShaderSource = const char *vertexShaderSource =
#include "fsq.vs" #include "fsq.glsl"
; ;
const char *fragmentShaderSource = const char *fragmentShaderSource =
#include "sdf.fs" #include "sdf.glsl"
; ;
/*void framebuffer_size_callback(GLFWwindow* window, int width, int height) /*void framebuffer_size_callback(GLFWwindow* window, int width, int height)

View File

@@ -6,23 +6,46 @@ in vec2 texCoords;
uniform float aspect; uniform float aspect;
vec3 center = vec3(0.0,0.0,20.0); vec3 center = vec3(0.0,-4.0,20.0);
float radius = 5.0; float radius = 5.0;
float planeHeight = -7.0; float planeHeight = -7.0;
struct SmallHit {
float min;
float len;
bool firstHP;
};
struct Ray { struct Ray {
vec3 dir; vec3 dir;
vec3 pos; vec3 pos;
float min; float min;
float len; float len;
SmallHit small;
vec3 color; vec3 color;
}; };
Ray applyMin(Ray ray) { Ray createRay(vec3 dir, vec3 pos){
vec3 off = ray.dir * ray.min; return Ray(dir, pos, .0, .0, SmallHit(80.,0.,false), vec3(.0));
}
Ray applyMin(Ray ray, float min) {
vec3 off = ray.dir * min;
ray.pos = ray.pos + off; ray.pos = ray.pos + off;
ray.len = ray.len + ray.min; ray.len = ray.len + min;
if( (min < ray.min) && !ray.small.firstHP ){
ray.small.firstHP = true;
}
ray.min = min;
if((ray.min < ray.small.min) && ray.small.firstHP){
ray.small.min = ray.min;
ray.small.len = ray.len;
}
return ray; return ray;
} }
@@ -38,18 +61,15 @@ float sphereMin(vec3 pos){
Ray minFn(Ray ray){ Ray minFn(Ray ray){
float sp = sphereMin(ray.pos); float sp = sphereMin(ray.pos);
float pl = planeMin(ray.pos); float pl = planeMin(ray.pos);
float min;
if (sp < pl){ if (sp < pl){
ray.color = vec3(1.0, 0.5, 0.2); ray.color = vec3(1.0, 0.5, 0.2);
ray.min = sp; min = sp;
} else { } else {
ray.color = vec3(0.2, 0.3, 0.3); ray.color = vec3(0.2, 0.3, 0.3);
ray.min = pl; min = pl;
} }
return applyMin(ray); return applyMin(ray,min);
} }
float minFn(vec3 pos){ float minFn(vec3 pos){
@@ -67,7 +87,7 @@ Ray march(Ray ray){
//ray.min = minFn(ray.pos); //ray.min = minFn(ray.pos);
ray = minFn(ray); ray = minFn(ray);
int c = 0; int c = 0;
while(( c < 200) && (ray.min < 100.0f) && (ray.min > 0.1f) ) { while(( c < 200) && (ray.min < 100.0f) && (ray.min > 0.01f) ) {
ray = minFn(ray); ray = minFn(ray);
c++; c++;
} }
@@ -93,8 +113,7 @@ void main()
vec3 dir = vec3((texCoords - 0.5)*2,1.0); vec3 dir = vec3((texCoords - 0.5)*2,1.0);
dir.x *= aspect; dir.x *= aspect;
vec3 pos = dir; vec3 pos = dir;
Ray ray = Ray(normalize(dir),pos,.0,.0,vec3(.0)); Ray ray = createRay(normalize(dir),pos);
ray = march(ray); ray = march(ray);
vec3 normal = calcNormal(ray); vec3 normal = calcNormal(ray);
@@ -104,28 +123,26 @@ void main()
float shLeve = 1.0; float shLeve = 1.0;
vec2 offsets[8] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( 0.94201624, 0.39906216 ),
vec2( -0.94558609, 0.76890725 ),
vec2( 0.094184101, 0.92938870 ),
vec2( -0.34495938, -0.29387760 )
);
for(int i = 0; i < 8; i++){ Ray shRay = createRay(lighDir * (-1.0), ray.pos + (-0.5)*lighDir);
Ray shRay = Ray(lighDir * (-1.0) + vec3(offsets[i].x,.0,offsets[i].y)*0.05, ray.pos + 0.5*normal, .0,.0, vec3(.0));
shRay = march(shRay); shRay = march(shRay);
if( (dot(lighDir,normal) < 0.0) && (shRay.len <20)){
shLeve -= 0.125*max(1.0-shRay.len*0.07 , 0.0);
}
}
light *= shLeve * .8; if( dot(lighDir,normal) < 0. ){
if( shRay.len <80){
shLeve = .0;//shRay.len*0.02 , 0.0;
}
else {
shLeve = shRay.small.min / shRay.small.len;
shLeve *= 9.0;
//shLeve = pow(shLeve*10.0,0.5);
}
}
shLeve = min(max(shLeve, 0.0),1.0);
light *= shLeve;
if(ray.min <= 0.2){ if(ray.min <= 0.2){
//FragColor = vec4(vec3(shRay.small.min)*0.1,1.0);
FragColor = vec4(ray.color * (light+0.3), 1.0); FragColor = vec4(ray.color * (light+0.3), 1.0);
} else { } else {
FragColor = vec4(.0,0.0,0.0,1.0); FragColor = vec4(.0,0.0,0.0,1.0);