Jump to content
  • Advertisement
Sign in to follow this  
mynameisnafe

Check My Normal Calculations functions? Please?

This topic is 1967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'll just get down to it.. this is right, right?

 

/*
    Get Normals..
*/

class nVectorMath {
    //
    //
public:
    static void  Subtract( Vertex3D& ret, Vertex3D v1, Vertex3D v2 )
    {    
        ret.x = v1.x - v2.x;
        ret.y = v1.y - v2.y;
        ret.z = v1.z - v2.z;    
    }

    static void  Add( Vertex3D& ret, Vertex3D v )
    {
        ret.x += v.x;
        ret.y += v.y;
        ret.z += v.z;
    }

    //
    //

    static float Dot( Vertex3D v1, Vertex3D v2 )
    {
        return    v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
    }

    static void  Cross( Vertex3D& ret, Vertex3D v1, Vertex3D v2 )
    {    
        ret.x = v1.y* v2.z - v1.z* v2.y;
        ret.y = v1.z* v2.x - v1.x* v2.z;
        ret.z = v1.x* v2.y - v1.y* v2.x;    
    }

    //
    //

    static void  Normalize( Vertex3D& v )
    {
        float len = sqrtf(Dot(v,v));

        if(len==0)    return;
    
        float invL = 1.0f/len;

        v.x = v.x * invL;
        v.y = v.y * invL;
        v.z = v.z * invL;
    }

    static void  GetNormal( Vertex3D& n, Vertex3D a, Vertex3D b, Vertex3D c )
    {
        Vertex3D e1, e2;

        Subtract( e1, a, b );

        Subtract( e2, a, c );

        Cross( n, e1, e2 );

        Normalize( n );
    }
};

Share this post


Link to post
Share on other sites
Advertisement

Okay!

 

So I just rewrite as..

 

 

    static void  GetNormal( Vertex3D& n, Vertex3D a, Vertex3D b, Vertex3D c )
    {
        Vertex3D e1, e2;

        Subtract( e1, b, a);

        Subtract( e2, c, a );

        Cross( n, e1, e2 );

        Normalize( n );
    }

 

?

 

Theres nothing else I need to change? And that's given vertices A, B, and C for some given triangle in a triangle list?

Edited by mynameisnafe

Share this post


Link to post
Share on other sites

Awesome news.. the bad news.. my shader must be bust. I can hardcode a colour but if I try any inverse-transposing specular and diffusing business my mesh comes out black.. and flat-shaded.:/

Share this post


Link to post
Share on other sites

Brother Bob seems to have covered your immediate query, but I wanted to mention that your use of a class containing only static functions is a little odd.

 

Usually one would use a namespace for this purpose, instead.

Share this post


Link to post
Share on other sites

Brother Bob seems to have covered your immediate query, but I wanted to mention that your use of a class containing only static functions is a little odd.

 

Usually one would use a namespace for this purpose, instead.

I know it's stupid.. not used namespaces in C++ before.. off I go searching.. all procrastination from my actual problems with .3ds..

It's been one of those weeks!

 

Thanks :)

Share this post


Link to post
Share on other sites

I know it's stupid.. not used namespaces in C++ before.. off I go searching..

 

Well that seems simple enough, sweet!

 

My loader now just holds an instance of an nVectorMath, now renamed VectorMagic lol

So if I'm generating these normals correctly, and I'm confident I'm putting them on the GPU correctly, and I've got one directional white light, and I set the colour of whatever is being shaded to bright green, what's the most likely cause of my completely black mesh?

 

Heres what happens / what I know for phong shading
 - vertex position and vertex normal are sent to the vertex shader

 - here they are transformed to world space and sent to the fragment shader

 - in the fragment shader work out all the lighting razzmataz

 - blend specular and diffuse colours, sum them, and output the final colour

 

So here we go, theres a lot of code here, which does the process above and gives me a black mesh..

 

Get the Inverse Transpose Model Matrix

 

glm::mat4 GLModel::GetInvTransModelMatrix()
{
    return glm::transpose( glm::inverse( modelMatrix ) );
}

 

... Everything gets put into the pipleline..

    glm::vec4 cPos = glm::vec4( mainCam->GetPosition(), 1.0 );

    glm::vec4 slDir( 0.0f, 1.0f, 0.0f, 0.0f );
    glm::vec4 slDif( 1.0f, 1.0f, 1.0f, 1.0f );
    glm::vec4 slSpe( 0.07f, 0.8f, 0.9f, 1.0f );
                                                                  // cam, direction, diffuse, specular, falloff/attenuation, inverse transpose model matrix
    theBeast->m_Shader.SetLightUniforms( cPos, slDir, slDif, slSpe, 0.8f, theBeast->GetInvTransModelMatrix() );
                                 // world mx,          view mx,                                proj mx,                                   scale,      viewmode
    theBeast->Render( glm::mat4(1.0), mainCam->GetViewMatrix(), m_ogl->GetProjectionMatrix(), 0.07f, GetViewMode() ); // for wireframe and what not. Using fill mode atm
 

 

then in the vertex shader..

 

#version 330

uniform mat4  worldMx;
uniform mat4  viewMx;
uniform mat4  projMx;

uniform mat4  modelMatrix;
uniform mat4  invtrans_modelMatrix;

uniform float scale_xyz;
 
layout (location=0) in vec4 vertexPos;
layout (location=1) in vec3 vertexNormal;

out vec3 colour;
out vec3 normalWorld;
out vec4 positWorld;

void main(void)
{
    vec4 v = vertexPos;

    v.x *= scale_xyz;
    v.y *= scale_xyz;
    v.z *= scale_xyz;

    //-----------------------------------------------

    colour = vec3( 0.3, 0.4, 0.7 ); // pale blue

    // vertex position in world coordinate space - for fragment shader
    positWorld = modelMatrix * v;

    // normal transformed to world coordinate space - again for fragment shader
    normalWorld = ( invtrans_modelMatrix * vec4(vertexNormal, 0.0) ).xyz;

    gl_Position = projMx * viewMx * worldMx * modelMatrix * v;
}

 

And finally the Pixel shader..

 

#version 330

uniform vec4  cameraPos;
uniform vec4  lightDirection;
uniform vec4  lightDiffuse;
uniform vec4  lightSpecular;
uniform float lightFalloff;

in vec3 colour;
in vec3 normalWorld;
in vec4 positWorld;

layout (location=0) out vec4 fragColour;

void main(void)
{
    // make sure light direction vector is unit length (store in L)
    
    vec4 L = normalize( lightDirection );
    
    // important to normalise length of normal otherwise shading artefacts occur
    
    vec3 N = normalize( normalWorld );
    
    // calculate lambertian term
    
    float lambertian = clamp( dot( L.xyz, N ), 0.0, 1.0 );

    //
    // calculate diffuse light colour
    //

    vec3 diffuseColour = colour.rgb * lightDiffuse.rgb * lambertian; // input colour actually diffuse colour
    
    //
    // calculate specular light colour
    //

    // vector from point on object surface in world coords to camera

    vec3 E = cameraPos.xyz - positWorld.xyz;

    E = normalize(E);    
    
    // reflected light vector about normal N
    vec3 R = reflect(-L.xyz, N);                    

    float specularIntensity = pow( max( dot(R, E), 0.0 ), lightFalloff );
    vec3  specularColour = vec3(1.0f, 1.0f, 1.0f) * lightSpecular.rgb * specularIntensity * lambertian;

    //
    // combine colour components to get final pixel / fragment colour
    //
    
    vec3 rgbColour = diffuseColour + specularColour;

    // output final colour to framebuffer

    fragColour = vec4( rgbColour, 1.0 );
//    fragColour = vec4( colour, 1.0 );
}
 

Share this post


Link to post
Share on other sites

im a lazy guy, so i haven't read the entire code section, but if stuff turns up black when it shouldn't, go slow at first.

this means:

  • first, directly output all your inputs as color (in this case, esp. the normals) and check if they have sane values (represented as colors in the framebuffer) you might want to pack them before outputting them (something like (normal+1)/*0,5), so you get the full range.
  • after that, output every step of your lighting, and do a check if it is as expected
  • also, don't forget to check constants, that can mess up things too
  • have a very simple model handy for this, where results are easily predictable (i use cubes and spheres)

you can also always use something like nsight or so for error checking, but i got kinda used not to use those, because they don't like me and have more issues with my code than me (especially with stream output/transform feedback, they love failing on those).

 

hope this helps,

    tasche

Share this post


Link to post
Share on other sites

im a lazy guy, so i haven't read the entire code section, but if stuff turns up black when it shouldn't, go slow at first.

this means:

  • first, directly output all your inputs as color (in this case, esp. the normals) and check if they have sane values (represented as colors in the framebuffer) you might want to pack them before outputting them (something like (normal+1)/*0,5), so you get the full range.
  • after that, output every step of your lighting, and do a check if it is as expected
  • also, don't forget to check constants, that can mess up things too
  • have a very simple model handy for this, where results are easily predictable (i use cubes and spheres)

you can also always use something like nsight or so for error checking, but i got kinda used not to use those, because they don't like me and have more issues with my code than me (especially with stream output/transform feedback, they love failing on those).

 

hope this helps,

    tasche

 

Thats some pretty sound advice dude, ta muchly. :) However you lost me a bit with "first, directly output all your inputs as color (in this case, esp. the normals) and check if they have sane values (represented as colors in the framebuffer) you might want to pack them before outputting them
(something like (normal+1)/*0,5), so you get the full range."

 

So you mean use my normals as colours? frag_colour = normal.xyz?

 

Also, what's this doing ( x) / * 0  ?

 

Thanks again dude :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!