• FEATURED

View more

View more

View more

\$25

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

## Check My Normal Calculations functions? Please?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1mynameisnafe  Members

Posted 20 February 2013 - 09:28 AM

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 );
}
};


### #2Brother Bob  Moderators

Posted 20 February 2013 - 11:06 AM

The vector from a to b is b-a, but you're calculating a-b. Similarly for a and c. This will result in a negated normal.

### #3mynameisnafe  Members

Posted 20 February 2013 - 11:09 AM

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, 20 February 2013 - 11:30 AM.

### #4Brother Bob  Moderators

Posted 20 February 2013 - 11:24 AM

Yes, that will make two vectors from a to b and c, respectively. The cross product looks to be all right as well.

### #5mynameisnafe  Members

Posted 20 February 2013 - 11:32 AM

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.:/

### #6swiftcoder  Senior Moderators

Posted 20 February 2013 - 12:27 PM

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.

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #7mynameisnafe  Members

Posted 20 February 2013 - 12:46 PM

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

### #8mynameisnafe  Members

Posted 20 February 2013 - 02:00 PM

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



#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;
}


#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 );
}



### #9Tasche  Members

Posted 24 February 2013 - 03:55 AM

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

### #10mynameisnafe  Members

Posted 25 February 2013 - 07:18 AM

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

### #11Tasche  Members

Posted 25 February 2013 - 10:03 AM

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

yep, exactly. something along the lines of:

cbuffer
{
float4 something;
}

struct vsIn
{
float4 position;
float4 normal;
float2 tex;
}

struct psIn
{
float4 position;
float4 normal;
float2 tex;
}

psIn vsmain(vsIn input)
{
psIn output;
...some vertex manipulations...
output.position = finalPos;
output.normal = rotatedNormal;
output.tex = input.tex;
}

float4 psmain : SV_TARGET
{
float4 ret;
...some cool lighting done here...
return ret;
}



now just before you 'return ret;' you insert:

ret = input.normal;

for example. if that is already messed up, pass the normal from the vertex shader untouched (may have to add an extra var using the texcoord semantic in psIn), and output that first.

psIn vsmain(vsIn input)
{
psIn output;
...some vertex manipulations...
output.position = finalPos;
output.normal = input.normal;     //<----change to this
output.tex = input.tex;
}



use this for quick and dirty 'judged-by-eye' tests

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

that was just another way to show you how typos work. was meant to be (normal+1)*0.5. this moves normal component values from -1,1 range to 0,1 which can be viewed as rgb.

Edited by Tasche, 25 February 2013 - 10:04 AM.

### #12mynameisnafe  Members

Posted 27 February 2013 - 08:29 AM

Thanks

I agree I should be doing this in DirectX ;)

that was just another way to show you how typos work. was meant to be (normal+1)*0.5. this moves normal component values from -1,1 range to 0,1 which can be viewed as rgb.

I see

Okay, onwards!

Thanks again

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.