facebookresearch / DeepSDF

Learning Continuous Signed Distance Functions for Shape Representation
MIT License
1.41k stars 259 forks source link

NaN values for several points & normals during preprocessing of mesh #26

Closed no-materials closed 5 years ago

no-materials commented 5 years ago

Greetings, some of the vertice coords and normal coords fetched after the virtual rendering of the mesh preprocessing executable have the value of (NaN, NaN, NaN).

Is this expected due to not enough 'watertightness' of mesh?

I am starting to think that my #version 120 implementation of the shaders are faulty:

@start vertex
#version 120

attribute vec3 vertex;
varying vec4 position_world;
varying vec4 position_camera;
varying vec3 viewDirection_camera;

uniform mat4 MVP;
uniform mat4 V;

void main(){

    // Projected image coordinate
    gl_Position =  MVP * vec4(vertex,1.0);

    // world coordinate location of the vertex
    position_world = vec4(vertex,1.0);
    position_camera = V * vec4(vertex, 1.0);

    viewDirection_camera = normalize(vec3(0.0,0.0,0.0) - position_camera.xyz);
}

@start geometry
#version 120
#extension GL_EXT_geometry_shader4 : enable

varying vec4 position_world[];
varying vec3 viewDirection_camera[];

varying vec3 normal_camera;
varying vec3 normal_world;
varying vec4 xyz_world;
varying vec3 viewDirection_cam;
varying vec4 xyz_camera;
varying float primitiveID;

uniform mat4 V;

void main() {
    vec3 A = position_world[1].xyz - position_world[0].xyz;
    vec3 B = position_world[2].xyz - position_world[0].xyz;
    vec3 normal = normalize(cross(A,B));
    vec3 normal_cam = (V * vec4(normal,0.0)).xyz;

    gl_Position = gl_PositionIn[0];
    normal_camera = normal_cam;
    normal_world = normal;
    xyz_world = position_world[0];
    xyz_camera = V * xyz_world;
    viewDirection_cam = viewDirection_camera[0];
    primitiveID = gl_PrimitiveIDIn;
    EmitVertex();

    gl_Position = gl_PositionIn[1];
    normal_camera = normal_cam;
    normal_world = normal;
    xyz_world = position_world[1];
    xyz_camera = V * xyz_world;
    viewDirection_cam = viewDirection_camera[1];
    primitiveID = gl_PrimitiveIDIn;

    EmitVertex();

    gl_Position = gl_PositionIn[2];
    normal_camera = normal_cam;
    normal_world = normal;
    xyz_world = position_world[2];
    xyz_camera = V * xyz_world;
    viewDirection_cam = viewDirection_camera[2];
    primitiveID = gl_PrimitiveIDIn;

    EmitVertex();
    EndPrimitive();
}

@start fragment
#version 120

varying vec3 viewDirection_cam;
varying vec3 normal_world;
varying vec3 normal_camera;
varying vec4 xyz_world;
varying vec4 xyz_camera;
varying float primitiveID;

uniform vec2 slant_thr;
varying vec4 ttt;
uniform mat4 V;
uniform mat4 ToWorld;

bool isnan( float val )
{
  return ( val < 0.0 || 0.0 < val || val == 0.0 ) ? false : true;
  // important: some nVidias failed to cope with version below.
  // Probably wrong optimization.
  /*return ( val <= 0.0 || 0.0 <= val ) ? false : true;*/
}

void main(){
    vec3 view_vector = vec3(0.0,0.0,1.0);
//    vec3 view_vector = normalize(vec3(0.0,0.0,1.0) - xyz_camera.xyz);
    vec4 test = vec4(0.0,0.0,0.0,1.0);

    // Check if we need to flip the normal.
    vec3 normal_world_cor;// = normal_world;
    float d = dot(normalize(normal_camera), normalize(view_vector));

    if (abs(d) < 0.001) {
        gl_FragData[0] = vec4(0.0,0.0,0.0,0.0);
        gl_FragData[1] = vec4(0.0,0.0,0.0,0.0);
        gl_FragData[2] = vec4(0.0,0.0,0.0,0.0);
        return;
    } else {
        if (d < 0) {
            test = vec4(0,1,0,1);
            normal_world_cor = -normal_world;
        } else {
            normal_world_cor = normal_world;
        }

        gl_FragData[0] = xyz_world;
        gl_FragData[0].w = primitiveID + 1.0f;

        gl_FragData[1] = vec4(normalize(normal_world_cor),1);
        gl_FragData[1].w = primitiveID + 1.0f;

    }

}

Any help would be appreciated.

no-materials commented 5 years ago

I re-implemented the preprocessMesh application integrating glfw & glew in order to be able to use #version 330 core for my shaders. Works as expected.