Sign in to follow this  
L. Spiro

OpenGL Normal-Mapping Woes

Recommended Posts

L. Spiro    25638
This is my shader:

float mix( in float _fX, in float _fY, in float _fA ) { return _fX + _fA * (_fY - _fX); }
float2 mix( in float2 _fX, in float2 _fY, in float _fA ) { return _fX + _fA * (_fY - _fX); }
float3 mix( in float3 _fX, in float3 _fY, in float _fA ) { return _fX + _fA * (_fY - _fX); }
float4 mix( in float4 _fX, in float4 _fY, in float _fA ) { return _fX + _fA * (_fY - _fX); }
float2 mix( in float2 _fX, in float2 _fY, in float2 _fA ) { return _fX + _fA * (_fY - _fX); }
float3 mix( in float3 _fX, in float3 _fY, in float3 _fA ) { return _fX + _fA * (_fY - _fX); }
float4 mix( in float4 _fX, in float4 _fY, in float4 _fA ) { return _fX + _fA * (_fY - _fX); }

matrix<float, 4, 4> g_mViewMatrix;
matrix<float, 3, 3> g_mNormalMatrix;
vector<float, 4> g_vDiffuseMaterial;
vector<float, 4> g_vAmbientMaterial;
vector<float, 4> g_vEmissiveMaterial;
vector<float, 4> g_vSpecularMaterial;
float g_fPower;
int g_iTotalDirLights;
int g_iTotalPointLights;
vector<float, 4> g_vLightVectors[8];
vector<float, 4> g_vLightHalfVectors[8];
vector<float, 4> g_vLightAmbients[8];
vector<float, 4> g_vLightDiffuses[8];
vector<float, 4> g_vLightSpeculars[8];
vector<float, 4> g_vLightAttens[8];
vector<float, 4> g_vLightRanges[8];
vector<float, 3> g_vSpotLightDirs[8];
vector<float, 4> g_vSkyColor;
vector<float, 4> g_vGroundColor;
sampler2D g_sSampler2dTex0;
sampler2D g_sSampler2dTex1;
sampler2D g_sSampler2dTex2;
sampler2D g_sSampler2dTex3;
sampler2D g_sSampler2dTex4;
sampler2D g_sSampler2dTex5;
sampler2D g_sSampler2dTex6;
sampler2D g_sSampler2dTex7;

vector<float, 4> cDiffuse;
vector<float, 4> cSpecular;

LSE_COLOR_PAIR GetDirLightColors(vector<float, 3> _vNormalInViewSpace, vector<float, 4> _vPosVector, int _iIndex){
// Removed to save space.

void Main(in vector<float, 2> _vIn2dTex0:TEXCOORD2, in vector<float, 3> _vInNormal:NORMAL0, in vector<float, 3> _vInTangent:TANGENT1, in vector<float, 3> _vInBiNormal:BINORMAL1, in vector<float, 4> _vInPos:POSITION0, in vector<float, 4> _vInEyePos:TEXCOORD1, out vector<float, 4> _vOutColor:COLOR0){
// Determine the normal. Read from the normal map and offset the input normal by that amount.
vector<float, 3> vNormalizedNormal = tex2D(g_sSampler2dTex1, _vIn2dTex0).xyz;
vNormalizedNormal = ((vNormalizedNormal*2.0)-1.0);
vNormalizedNormal = normalize(((normalize(_vInNormal) + (vNormalizedNormal.x*_vInTangent)) + (vNormalizedNormal.y*_vInBiNormal)));

// For calculating the specular component of lighting.
vector<float, 4> vViewPosToEye = -normalize(_vInEyePos);

LSE_COLOR_PAIR cpLightColors = {
vector<float, 4>(0.0, 0.0, 0.0, 0.0),
vector<float, 4>(0.0, 0.0, 0.0, 0.0)
for(int I = 0; I < g_iTotalDirLights; I++) {
LSE_COLOR_PAIR cpThis = GetDirLightColors(vNormalizedNormal, vViewPosToEye, I);
cpLightColors.cDiffuse += cpThis.cDiffuse;
cpLightColors.cSpecular += cpThis.cSpecular;

// Apply hemisphere ambient lighting.
vector<float, 3> vUp = mul(g_mViewMatrix, vector<float, 4>(0.0, 1.0, 0.0, 0.0)).xyz; += mix(,, ((dot(vUp, vNormalizedNormal) * 0.5) + 0.5));

// Sample the diffuse color from the texture.
_vOutColor = vector<float, 4>(tex2D(g_sSampler2dTex0, _vIn2dTex0).xyz, g_vDiffuseMaterial.w); *=;

// Apply lighting. = (( * + (g_vSpecularMaterial*cpLightColors.cSpecular).xyz);

In this form it produces this incorrect result:

This is how it should look:

The good image is taken from the OpenGL version of my engine, which uses exactly the same shader for rendering (but translated to GLSL, not HLSL). For the nay-sayers, here is the GLSL version of the exact same shader:

#version 130
precision mediump float;
out vec4 _vOutColor;
in vec4 LSG_VERT_OUT_PIXEL_IN_369_1/*_vInEyePos*/;
in vec4 LSG_VERT_OUT_PIXEL_IN_365_0/*_vInPos*/;
in vec3 LSG_VERT_OUT_PIXEL_IN_360_1/*_vInBiNormal*/;
in vec3 LSG_VERT_OUT_PIXEL_IN_368_1/*_vInTangent*/;
in vec3 LSG_VERT_OUT_PIXEL_IN_364_0/*_vInNormal*/;
in vec2 LSG_VERT_OUT_PIXEL_IN_369_2/*_vIn2dTex0*/;

uniform mat4x4 g_mViewMatrix;
uniform mat3x3 g_mNormalMatrix;
uniform vec4 g_vDiffuseMaterial;
uniform vec4 g_vAmbientMaterial;
uniform vec4 g_vEmissiveMaterial;
uniform vec4 g_vSpecularMaterial;
uniform float g_fPower;
uniform int g_iTotalDirLights;
uniform int g_iTotalPointLights;
uniform vec4 g_vLightVectors[8];
uniform vec4 g_vLightHalfVectors[8];
uniform vec4 g_vLightAmbients[8];
uniform vec4 g_vLightDiffuses[8];
uniform vec4 g_vLightSpeculars[8];
uniform vec4 g_vLightAttens[8];
uniform vec4 g_vLightRanges[8];
uniform vec3 g_vSpotLightDirs[8];
uniform vec4 g_vSkyColor;
uniform vec4 g_vGroundColor;
uniform sampler2D g_sSampler2dTex0;
uniform sampler2D g_sSampler2dTex1;
uniform sampler2D g_sSampler2dTex2;
uniform sampler2D g_sSampler2dTex3;
uniform sampler2D g_sSampler2dTex4;
uniform sampler2D g_sSampler2dTex5;
uniform sampler2D g_sSampler2dTex6;
uniform sampler2D g_sSampler2dTex7;

vec4 cDiffuse;
vec4 cSpecular;

LSE_COLOR_PAIR GetDirLightColors(in vec3 _vNormalInViewSpace, in vec4 _vPosVector, in int _iIndex){
// Removed to save space.

void main(){
// Determine the normal. Read from the normal map and offset the input normal by that amount.
vec3 vNormalizedNormal = vec3(texture(g_sSampler2dTex1, LSG_VERT_OUT_PIXEL_IN_369_2/*_vIn2dTex0*/).xyz);
vNormalizedNormal = ((vNormalizedNormal*2.0)-1.0);
vNormalizedNormal = normalize(((normalize(LSG_VERT_OUT_PIXEL_IN_364_0/*_vInNormal*/) + (vNormalizedNormal.x*LSG_VERT_OUT_PIXEL_IN_368_1/*_vInTangent*/)) + (vNormalizedNormal.y*LSG_VERT_OUT_PIXEL_IN_360_1/*_vInBiNormal*/)));

// For calculating the specular component of lighting.
vec4 vViewPosToEye = vec4(-normalize(LSG_VERT_OUT_PIXEL_IN_369_1/*_vInEyePos*/));

LSE_COLOR_PAIR cpLightColors = LSE_COLOR_PAIR( vec4(0.0, 0.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 0.0) );

for(int I = 0; I < g_iTotalDirLights; I++){
LSE_COLOR_PAIR cpThis = GetDirLightColors(vNormalizedNormal, vViewPosToEye, I);
cpLightColors.cDiffuse += cpThis.cDiffuse;
cpLightColors.cSpecular += cpThis.cSpecular;

// Apply hemisphere ambient lighting.
vec3 vUp = vec3(((g_mViewMatrix) * (vec4(0.0, 1.0, 0.0, 0.0))).xyz); += mix(,, ((dot(vUp, vNormalizedNormal) * 0.5) + 0.5));

// Sample the diffuse color from the texture.
_vOutColor = vec4(texture(g_sSampler2dTex0, LSG_VERT_OUT_PIXEL_IN_369_2/*_vIn2dTex0*/).xyz, g_vDiffuseMaterial.w); *=;

// Apply lighting. = (( * + (g_vSpecularMaterial*cpLightColors.cSpecular).xyz);

So indeed the shaders match in logic exactly.
So I checked my inputs.
I exported all of my OpenGL vertex buffers and then added some temporary code to load them into the Direct3D 9 vertex buffers, which ensured that the two sets of vertex buffers were all exactly alike. The result was the same.

So I changed the out color to show me tangents, binormals, input normals, everything.
Input normals matched but the tangent and binormal were wrong.

This is how I transform the tangent (and likewise with the binormal) in the vertex shader:
[code]_vOutTangent = normalize(mul(g_mNormalMatrix, _vInTangent));[/code] HLSL
[code]LSG_VERT_OUT_PIXEL_IN_360_1/*_vOutBiNormal*/ = normalize(((g_mNormalMatrix) * (_vInBiNormal)));[/code] GLSL
If I change it to this:
[code]_vOutTangent = _vInTangent;[/code]
…and then change the output color in the pixel shader to the tangent value, the result is the same in both HLSL and GLSL. That means initially the input is the same. The data coming in from the vertex buffers is fine. It gets lost when transformed.

The input normals are transformed the same way, and they are always a match between GLSL and HLSL.
Of course, the order of my matrix multiplication is correct, or else I would never be able to get vertices, normals, etc. to match between them.
I have tried transposing the matrices before transforming the tangent and binormal, and also reversing the order of the multiply. The result is the same.

Any ideas?
Yogurt Emperor

Made the code more readable for those among us who may be human.

Share this post

Link to post
Share on other sites
L. Spiro    25638
The code for both languages is generated from LSSL (L. Spiro Shader Language).
So whitespace and comments were stripped. I added back some of it for posting here (and removed all of the #line directives).

So that is the reason for the lack of whitespace and comments.
I will edit a bit more into the code.

Yogurt Emperor

Share this post

Link to post
Share on other sites
L. Spiro    25638
No ideas?
The point of corruption should be the matrix multiply, but it works fine on the input normal. Why would it not work equally well on the tangent and binormal?

Yogurt Emperor

Share this post

Link to post
Share on other sites
L. Spiro    25638
Just posting the solution here in case it can help someone else.

This problem went away when I aligned my vertex buffer elements to every 32 bytes.
By changing only the stride of each element, this bug has gone away. Most likely this is a Direct3D bug.

L. Spiro

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
  • Popular Now