Compare commits

...

2 Commits

Author SHA1 Message Date
1de2dae223 feat: added normal calc 2025-08-27 16:00:02 +02:00
fcbc6632e7 fix: removed sensless draw-call 2025-08-27 15:59:30 +02:00
2 changed files with 60 additions and 31 deletions

View File

@@ -111,7 +111,6 @@ int main(int argc, char **argv) {
glUseProgram(shaderProgramm);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(fsq.getVAO());
glDrawArrays(GL_TRIANGLES, 0, 6);

90
sdf.fs
View File

@@ -4,10 +4,10 @@ R"###(
out vec4 FragColor;
in vec2 texCoords;
vec3 center = vec3(0.0f,0.0f,20.0f);
float radius = 5.0f;
vec3 center = vec3(0.0,0.0,20.0);
float radius = 5.0;
float planeHeight = -7.0f;
float planeHeight = -7.0;
struct Ray {
vec3 dir;
@@ -25,52 +25,82 @@ Ray applyMin(Ray ray) {
}
Ray planeMin(Ray ray){
ray.min = ray.pos.y - planeHeight;
ray.color = vec3(1.0,1.0,1.0);
return applyMin(ray);
float planeMin(vec3 pos){
return pos.y - planeHeight;
}
Ray sphereMin(Ray ray){
ray.min = length(ray.pos - center) - radius;
ray.color = vec3(1.0,0.0,0.0);
return applyMin(ray);
float sphereMin(vec3 pos){
return length(pos - center) - radius;
}
Ray minFn(Ray ray){
Ray sp = sphereMin(ray);
Ray pl = planeMin(ray);
float sp = sphereMin(ray.pos);
float pl = planeMin(ray.pos);
if (sp.min < pl.min){
if (sp < pl){
ray.color = vec3(1.0, 0.5, 0.2);
ray.min = sp;
} else {
ray.color = vec3(1.,1.,1.);
ray.min = pl;
}
return applyMin(ray);
}
float minFn(vec3 pos){
float sp = sphereMin(pos);
float pl = planeMin(pos);
if (sp < pl){
return sp;
} else {
return pl;
}
}
Ray march(Ray ray){
//ray.min = minFn(ray.pos);
ray = minFn(ray);
int c = 0;
while(( c < 200) && (ray.min < 100.0f) && (ray.min > 0.1f) ) {
ray = minFn(ray);
c++;
}
return ray;
}
vec3 calcNormal(Ray ray){
float h = 0.001;
float m = sphereMin(ray.pos);
vec3 n = vec3(
minFn(ray.pos + vec3( h,.0,.0)) - minFn(ray.pos - vec3( h,.0,.0)),
minFn(ray.pos + vec3(.0, h,.0)) - minFn(ray.pos - vec3(.0, h,.0)),
minFn(ray.pos + vec3(.0,.0, h)) - minFn(ray.pos - vec3(.0,.0, h))
);
return normalize(n);
}
void main()
{
vec3 dir = vec3(texCoords.x, texCoords.y,2.0f);
dir = (dir - 0.5f) * 2;
vec3 dir = vec3((texCoords - 0.5)*2,1.0);
vec3 pos = dir;
Ray ray = Ray(pos,normalize(dir),.0,.0,vec3(.0));
ray = march(ray);
vec3 normal = calcNormal(ray);
vec3 lighDir = normalize(vec3(.5,-1.0,0.5));
float light = (.5 - min(dot(normal,lighDir),0.0))*2.;
ray = minFn(ray);
while( (ray.min < 100.0f) && (ray.min > 0.1f) ) {
//ray.dir = normalize(ray.dir);
ray = minFn(ray);
//vec3 off = ray.dir * min;
//ray.pos = ray.pos + off;
}
if(ray.min <= 0.2f){
FragColor = vec4(ray.color, 1.0f);
if(ray.min <= 0.2){
FragColor = vec4(ray.color * light, 1.0);
} else {
FragColor = vec4(.0,0.0f,0.0f,1.0f);
FragColor = vec4(.0,0.0,0.0,1.0);
}
}