0
\$\begingroup\$

My world space normals appear fine for most of a mesh. However, some of the normals are black. I was wondering if anyone had any ideas why this is. This happens for any mesh, not just this sphere. I'm using a right-handed coordinate system with z up (+x = right, +y = forward, +z = up)enter image description here

The following image is showing the mesh with no camera or object rotation and with the normals scaled from the [-1,1] to [0,1] range.

enter image description here

^^Green appears on the other side of the sphere (+y)

The Following Is The GBuffer Generation

Vertex Shader:

#version 430


////////////////////////////
//This Is A GBuffer Shader.
//GLSL 4.3
////////////////////////////


#define PLATFORM_WINDOWS_DESKTOP       //Define Windows Desktop Platform For SnAPI.ShaderLib.
#define SHADING_LANGUAGE_GLSL          //Define GLSL Shading Language For SnAPI.ShaderLib.
#pragma include "SnAPIShaderLibrary.h" //Include SnAPI's Shader Library (SnAPI.ShaderLib).


//This Shader Expects To Be Given These Attributes From The Application.
in vec3 In_VertPos;
in vec3 In_VertNormal;
in vec2 In_VertTexCoord;


//This Shader Expects To Be Given The Following Variabls From The Application. 
uniform mat4 World;
uniform mat4 WorldView;
uniform mat4 WorldViewProjection;
uniform mat4 WorldInverseTranspose;
uniform mat4 NormalMatrix;


//This Shader Exports The Following Attributes To Be Consumed By The Next Shader.
layout(location = 0) out vec3 Out_VertPos;
layout(location = 1) out vec3 Out_VertNormal;
layout(location = 2) out vec2 Out_VertTexCoord;


//The Vertex Shader Entry Point.
void main()
{
    //Write Out The Vertex Position In Clip Space.
    gl_Position = WorldViewProjection * vec4(In_VertPos, 1);

    //Write Out The Vertex Position In Eye Space.
    //Out_VertPos = ObjectPositionToEyePosition( WorldView, In_VertPos );

    //Write Out The Vertex Position In Eye Space.
    Out_VertPos = ( World * vec4( In_VertPos, 1 ) ).xyz;

    //Write Out The Vertex Normal In Eye Space.
    //Out_VertNormal = ObjectNormalToEyeNormal( NormalMatrix, In_VertNormal );

    //Write Out The Normal Direction In World Space.
    Out_VertNormal = ( World * vec4( In_VertNormal, 0.0 ) ).xyz;

    //Write Out The Vertex Texture Coordinate.
    Out_VertTexCoord = In_VertTexCoord;
}

Fragment Shader:

#version 430


////////////////////////////
//This Is A GBuffer Shader.
//GLSL 4.3
////////////////////////////


//This Shader Expects To Be Given These Attributes From The Previous Shader.
layout(location = 0) in vec3 In_VertPos;
layout(location = 1) in vec3 In_VertNormal;
layout(location = 2) in vec2 In_VertTexCoord;


//This Shader Supports 1 Diffuse Texture.
uniform sampler2D DiffuseTexture;


//This Shader Expects The Following FragData To Be Exported.
layout(location = 0) out vec4 Out_Pos;
layout(location = 1) out vec4 Out_Normal;
layout(location = 2) out vec4 Out_TexCoord;
layout(location = 3) out vec4 Out_Diffuse;//Vertex Color


//Specifies Whether This Instance Has A Diffuse Texture.
bool bHasDiffuse = false;


//The Fragment Shader Entry Point.
void main()
{
    //Write Position Out To Texture.
    Out_Pos = vec4( In_VertPos, 1 );

    //Write Normal Out To Texture.
    Out_Normal = vec4( normalize( In_VertNormal ), 0 );

    //Write Texture Coord Out To Texture.
    Out_TexCoord = vec4( In_VertTexCoord, 1, 1 );

    //Write Diffuse Out To Texture.
    if( bHasDiffuse )
        Out_Diffuse = texture( DiffuseTexture, In_VertTexCoord );
    else
        Out_Diffuse = vec4( 0.4, 0.4, 0.4, 1 );//Dark Gray Color.
}

The Following Is The Full Screen Quad Rendering:

Vertex Shader:

#version 430


/////////////////////////////////////////
//This Is A Shading And Lighting Shader.
//GLSL 4.3
/////////////////////////////////////////


//This Shader Expects To Be Given These Attributes From The Application.
in vec3 In_VertPos;//The Clip-Space Vertex Position.
in vec2 In_VertTexCoord;


//This Shader Exports The Following Attributes To Be Consumed By The Next Shader.
layout(location = 0) out vec2 Out_VertTexCoord;


//The Vertex Shader Entry Point.
void main()
{
    //Write Out The Vertex Position.
    gl_Position = vec4( In_VertPos, 1 );

    //Write Out The Vertex Texture Coordinate.
    Out_VertTexCoord = In_VertTexCoord;
}

Fragment Shader:

#version 430


/////////////////////////////////////////
//This Is A Shading And Lighting Shader.
//GLSL 4.3
/////////////////////////////////////////


#define PLATFORM_WINDOWS_DESKTOP       //Define Windows Desktop Platform For SnAPI.ShaderLib.
#define SHADING_LANGUAGE_GLSL          //Define GLSL Shading Language For SnAPI.ShaderLib.
#pragma include "SnAPIShaderLibrary.h" //Include SnAPI's Shader Library (SnAPI.ShaderLib).


//This Shader Expects To Be Given These Attributes From The Previous Shader.
layout(location = 0) in vec2 In_VertTexCoord;


//This Shader Expects To Be Given The Following GBuffer Textures From The Application.
uniform sampler2D GPositionTexture;
uniform sampler2D GNormalTexture;
uniform sampler2D GDepthTexture;
uniform sampler2D GTexCoordTexture;
uniform sampler2D GDiffuseTexture;


//This Shader Expects The Following FragData To Be Exported.
layout(location = 0) out vec4 Out_FragColor;


//The Fragment Shader.
void main()
{
    //Pixel Albedo.
    vec4 Color = texture( GDiffuseTexture, In_VertTexCoord );

    //Pixel Position In World Space.
    vec3 PixelPos = texture( GPositionTexture, In_VertTexCoord ).xyz;

    //Pixel Normal In World Space
    vec3 Normal = normalize( texture( GNormalTexture, In_VertTexCoord ).xyz );

    //The Lighting Factor (The Ammount Of Lighting To Apply).
    float Lighting = dot( vec3( 1, 0, 0 ) /*Light Direction*/, Normal );

    //Clamp Lighting Factor Between 0-1.
    Lighting = max( Lighting, 0 );

    //Calculate Final Shading/Lighting.
    //Out_FragColor =  Lighting * Color;

    //Preview Normals.
    Out_FragColor =  vec4( Normal, 1 );
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ black means they are (0,0,0) ... \$\endgroup\$ – The Light Spark Dec 3 '15 at 9:17
  • \$\begingroup\$ does this output look accurate for world space normals? should there normally be these black values? \$\endgroup\$ – Richmar1 Dec 3 '15 at 9:20
  • \$\begingroup\$ @Richmar1 No, I'm pretty sure it should look something like this codinglabs.net/public/contents/tutorial_simple_def_rendering/… . Could you provide your vertex- and fragment-shader? \$\endgroup\$ – Christer Dec 3 '15 at 9:23
  • \$\begingroup\$ absolutely, here. \$\endgroup\$ – Richmar1 Dec 3 '15 at 9:29
  • \$\begingroup\$ @Richmar1 I don't see your shaders. \$\endgroup\$ – Christer Dec 3 '15 at 9:42
1
\$\begingroup\$

It seems to be working fine. I don't know where the x,y,z-axis is pointing, so I can't really tell. But if you apply no rotation before viewing the geometry it should be easier to determine if it's working correctly as the world space and eye space axis line up. Then the object should be red on the right side (positive x-axis), green on the up side (positive y-axis), and blue toward you/out of the screen (positive z-axis). I'm assuming you are using a right handed coordinate system.

The black is probably the result of all the values becoming less than 0, so no color. If you want it to always have color you could map the values [-1, 1] to [0, 1] before previewing them.

Out_FragColor = vec4( 0.5*(Normal + vec3(1.0, 1.0, 1.0)), 1 )

This should be doing this mapping operation x' = 0.5(x+1) for all x,y,z.

\$\endgroup\$
10
  • \$\begingroup\$ Thank you for your feedback. unfortunately, mapping from 0-1 results in the same black spot, exactly the same. So my code is correct then? it must be an issue with the models normals imported from 3ds max? \$\endgroup\$ – Richmar1 Dec 3 '15 at 10:55
  • \$\begingroup\$ From 0-1? You were suppose to map from [-1,1]->[0,1]. Could you include an image with this mapping and with no view rotation? \$\endgroup\$ – Christer Dec 3 '15 at 10:58
  • \$\begingroup\$ yeah, i mapped from [-1,1]->[0,1]. An image showing this new mapping has been added to the Original Post. \$\endgroup\$ – Richmar1 Dec 3 '15 at 11:02
  • \$\begingroup\$ @Richmar1 I'm so sorry, you were in fact mapping [0,1] -> [-1,1]. I goofed up, fixed my answer now. My fault. \$\endgroup\$ – Christer Dec 3 '15 at 11:04
  • \$\begingroup\$ not a problem my friend. i've uploaded the requested image in the original post :) \$\endgroup\$ – Richmar1 Dec 3 '15 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.