Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

shading #15

Merged
merged 12 commits into from
Dec 10, 2023
Merged
Prev Previous commit
Next Next commit
normals fixed again?
theolundqvist committed Dec 10, 2023

Verified

This commit was signed with the committer’s verified signature.
theolundqvist Theodor Lundqvist
commit ea8b8ee675d3f6f26bb90464cf5f742bf50294f5
156 changes: 85 additions & 71 deletions shaders/EDAN35/voxel.frag
Original file line number Diff line number Diff line change
@@ -22,15 +22,15 @@ out vec4 fColor;

float isInside(vec3 pos){
// this works but did not notice performance difference
/*
return step(0.0, pos.x)*step(pos.x, 1.0) *
step(0.0, pos.y)*step(pos.y, 1.0) *
step(0.0, pos.z)*step(pos.z, 1.0);
/*
if (pos.x < 0.0 || pos.x > 1.0) return false;
if (pos.y < 0.0 || pos.y > 1.0) return false;
if (pos.z < 0.0 || pos.z > 1.0) return false;
return true;
*/
*/
if (pos.x < 0.0 || pos.x > 1.0) return 0.0;
if (pos.y < 0.0 || pos.y > 1.0) return 0.0;
if (pos.z < 0.0 || pos.z > 1.0) return 0.0;
return 1.0;
}

struct start_t{
@@ -56,21 +56,25 @@ vec3 uvw_to_normal(vec3 uvw){
}

vec2 uvw_to_uv(vec3 uvw){
// Variables to hold the UV coordinates on the cube's face
vec2 uv;
// Determine which component is dominant (furthest from the cube's center)
float maxComponent = max(max(abs(uvw.x), abs(uvw.y)), abs(uvw.z));
if (maxComponent == uvw.x) {
uv = uvw.zy;
if (uvw.x < 0.5) uv.x = 1.0 - uv.x;
} else if (maxComponent == uvw.y) {
uv = uvw.xz;
//if (uvw.y < 0.5) uv.x = 1.0 - uv.x;
if(uvw.x == 0.0 && uvw.y == 0.0 && uvw.z == 0.0) return vec2(0.0, 0.0);
if(uvw.x == 1.0 && uvw.y == 1.0 && uvw.z == 1.0) return vec2(1.0, 1.0);
if(uvw.x == 0.0 && uvw.y == 1.0 && uvw.z == 1.0) return vec2(0.0, 1.0);
if(uvw.x == 1.0 && uvw.y == 0.0 && uvw.z == 1.0) return vec2(1.0, 0.0);
if(uvw.x == 1.0 && uvw.y == 1.0 && uvw.z == 0.0) return vec2(1.0, 0.0);
if(uvw.x == 0.0 && uvw.y == 0.0 && uvw.z == 1.0) return vec2(0.0, 1.0);
vec3 tangents = vec3(1) - abs(uvw_to_normal(uvw));
vec3 a, b;
if (tangents.x == 0.0f) {
a = vec3(0, 1, 0);
b = vec3(0, 0, 1);
} else if (tangents.y == 0.0f) {
a = vec3(1, 0, 0);
b = vec3(0, 0, 1);
} else {
uv = uvw.xy;
//if (uvw.z < 0.5) uv.x = 1.0 - uv.x;
a = vec3(1, 0, 0);
b = vec3(0, 1, 0);
}
return uv;
return vec2(dot(b, uvw), dot(a, uvw));
}

// Work In Progress
@@ -109,22 +113,29 @@ start_t findStartPos(){
}
return start_t(near, uvw_to_normal(near));
}

struct hit_t {
vec3 voxel;
vec3 position;
float depth;
vec3 voxel_pos;
vec3 pixel_pos;
vec3 uvw;
vec2 uv;
vec3 normal;
int material;
};

hit_t fixed_step(){
vec3 V = normalize(fV) * voxel_size/15;// fixed step
vec3 P = findStartPos().pos;// P is in 0-1.0 space
start_t start = findStartPos();// P is in 0-1.0 space
vec3 P = start.pos;
int max_step = int(15*15/voxel_size);
for (int i = 0; i < max_step; i++){
if (isInside(P) < 0.5) discard;
int material = int(round(texture(volume, P).r*255));
if (material != 0) return hit_t(P, P, vec3(0), material);
if (material != 0) {
float t = length(P - start.pos);
vec3 index = floor(P/voxel_size);
return hit_t(t, index, P, (P - vec3(index))/voxel_size, uvw_to_uv((P - vec3(index))/voxel_size), start.normal, material);
}
P += V;
}
discard;
@@ -144,15 +155,11 @@ vec3 reinhard_jodie(vec3 v)
vec3 shade(hit_t hit){
vec3 V=normalize(fV);
vec3 N=normalize(hit.normal);
if (isInside(hit.voxel - 0.01 * hit.normal * voxel_size) < 0.5){
discard;
}
vec3 L=normalize(light_direction);

float diffuse_co= 1.7f * max(dot(L, N), 0);
float specular_co = 0.0;
bool blinn = true;
if(blinn)
if (blinn)
{
vec3 halfwayDir = normalize(L -V);
specular_co = pow(max(dot(N, halfwayDir), 0.0), 4.0);
@@ -192,28 +199,29 @@ hit_t fvta_step(){
vec2 uv = uvw_to_uv(uvw);

for (int i = 0; i < max_steps; i++){
int mat = int(round(texture(volume, pos).r*255));
if (mat > 0){
return hit_t(pos, vec3(uv, 1), normal, mat);
}
if (isInside(pos) < 0.5) {
//return hit_t(t, voxel_pos, pos, uvw, uv, vec3(1,0,0), 0);
discard;
}
/*
if (sideDist.x < sideDist.y && sideDist.x < sideDist.z) {
// X-axis traversal.
normal = -vec3(1, 0, 0) * rs;
t = sideDist.x * rd;
} else if (sideDist.y < sideDist.z) {
// Y-axis traversal.
normal = -vec3(0, 1, 0) * rs;
t = sideDist.y * rd;
} else {
// Z-axis traversal.
normal = -vec3(0, 0, 1) * rs;
t = sideDist.z * rd;
int mat = int(round(texture(volume, pos).r*255));
if (mat > 0){
return hit_t(t, voxel_pos, pos, uvw, uv, normal, mat);
}
*/
/*
if (sideDist.x < sideDist.y && sideDist.x < sideDist.z) {
// X-axis traversal.
normal = -vec3(1, 0, 0) * rs;
t = sideDist.x * rd;
} else if (sideDist.y < sideDist.z) {
// Y-axis traversal.
normal = -vec3(0, 1, 0) * rs;
t = sideDist.y * rd;
} else {
// Z-axis traversal.
normal = -vec3(0, 0, 1) * rs;
t = sideDist.z * rd;
}
*/
// black magic compare between sideDist.x < sideDist.y && sideDist.x < sideDist.z etc
vec3 mm = step(sideDist.xyz, sideDist.yxy) * step(sideDist.xyz, sideDist.zzx);
normal = -mm * rs;
@@ -222,7 +230,7 @@ hit_t fvta_step(){

// other stuff that is nice to know
vec3 mini = ((voxel_pos-ro)/voxel_size + 0.5 - 0.5*vec3(rs))*deltaDist;
t = max ( mini.x, max ( mini.y, mini.z ) );
t = max (mini.x, max (mini.y, mini.z));
pos = ro + rd * (t + Epsilon);
uvw = (pos - voxel_pos)/voxel_size;
uv = vec2(dot(mm.yzx, uvw), dot(mm.zxy, uvw));
@@ -231,40 +239,40 @@ hit_t fvta_step(){
}
discard;
}
mat4 rotationX( in float angle ) {
return mat4( 1.0, 0, 0, 0,
0, cos(angle), -sin(angle), 0,
0, sin(angle), cos(angle), 0,
0, 0, 0, 1);
mat4 rotationX(in float angle) {
return mat4(1.0, 0, 0, 0,
0, cos(angle), -sin(angle), 0,
0, sin(angle), cos(angle), 0,
0, 0, 0, 1);
}

mat4 rotationY( in float angle ) {
return mat4( cos(angle), 0, sin(angle), 0,
0, 1.0, 0, 0,
-sin(angle), 0, cos(angle), 0,
0, 0, 0, 1);
mat4 rotationY(in float angle) {
return mat4(cos(angle), 0, sin(angle), 0,
0, 1.0, 0, 0,
-sin(angle), 0, cos(angle), 0,
0, 0, 0, 1);
}

mat4 rotationZ( in float angle ) {
return mat4( cos(angle), -sin(angle), 0, 0,
sin(angle), cos(angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
mat4 rotationZ(in float angle) {
return mat4(cos(angle), -sin(angle), 0, 0,
sin(angle), cos(angle), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
float ao(hit_t hit){
vec3 N=normalize(hit.normal);
vec3 P = hit.position;
vec3 P = hit.pixel_pos;
float ao_inv = 1.0;
int nbr_samples = 10;
float r = voxel_size * 0.2;
vec3 sphere_center = P + N * r;
vec3 bitangent = cross(N, vec3(0.5, 0.5, 0.5));
for(int i = 0; i < nbr_samples; i++){
for (int i = 0; i < nbr_samples; i++){
// rotate bitangent
float angle = 2.0 * 3.14159265359 * float(i)/float(nbr_samples);
bitangent = normalize((rotationZ(angle * N.z) * rotationX(angle * N.x) * rotationY(angle * N.y) * vec4(bitangent, 1.0)).xyz);
vec3 sample_point = sphere_center + bitangent * r*0.5;
if(isInside(sample_point) < 0.5) continue;
if (isInside(sample_point) < 0.5) continue;
float material = texture(volume, sample_point).r * 255.0;
if (material > 0.0){
ao_inv -= 1.0/(nbr_samples+1.0);
@@ -276,19 +284,25 @@ float ao(hit_t hit){
void main()
{
// custom front face culling to do it based on cam pos
if (face_dot_v < 0.0) discard;
//if (face_dot_v < 0.0) discard;

//vec3 color = findStartPos();
hit_t hit;
//hit = fixed_step();
hit = fvta_step();

if (isInside(hit.pixel_pos - 0.01 * hit.normal * voxel_size) < 0.5){
discard;
}
vec3 color = vec3(hit.material/255.0, 0, 0);
color = shade(hit);
//color *= ao(hit);
color = hit.position;
//color = normalize(hit.normal * 0.5f + 0.5f);
//color = normalize(hit.normal);
//color = hit.pixel_pos;
//color = hit.voxel_pos;
//color = normalize(hit.normal) * 0.5 + 0.5;
//color = hit.uvw;
//color = vec3(hit.uv, 1.0);
//color = vec3(hit.depth);


fColor = vec4(color, 1.0);
}
4 changes: 2 additions & 2 deletions src/EDAN35/project.cpp
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ void edan35::Project::run() {


glClearDepthf(1.0f);
glClearColor(1.0f, 1.0f, 0.94f, 1.0f);
glClearColor(0.85f, 0.85f, 0.74f, 1.0f);
glEnable(GL_DEPTH_TEST);

auto lastTime = std::chrono::high_resolution_clock::now();
@@ -190,7 +190,7 @@ void edan35::Project::run() {

mWindowManager.NewImGuiFrame();

glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// RENDER
app->render(show_basis, basis_length_scale, basis_thickness_scale, dt);
6 changes: 3 additions & 3 deletions src/EDAN35/project/VoxelVolume.cpp
Original file line number Diff line number Diff line change
@@ -227,9 +227,9 @@ class VoxelVolume {
setUniforms(tf, world_to_clip, cam_pos);

// render
//glEnable(GL_CULL_FACE);
//glFrontFace(GL_CW);
//glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_FRONT);
renderMesh(bounding_box);

// Unbind texture and shader program