Jump to content
  • Advertisement
Sign in to follow this  
rampster

Normalmap shader

This topic is 2649 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

Hi,


I am trying to draw a cube with a bump map using normal maps but I really cant see any bump effect on a spinning cube. My unit cube is imported out of maya as a collada file with geometric tangents option (and they seem right).
My worldspace light direction is (0.0, 0.0, 1.0).
My camera position is (0.0, 0.0, 5.0).
I am rotating my cube across Y axis.

Am I missing anything in my shader that could cause a problem?

Thanks in advance!


My vertex shader:

// Structs
struct DirectionalLight
{
vec3 direction;
vec3 color;
};

// Uniforms
uniform DirectionalLight u_directionalLight;
uniform mat4 u_worldViewProjectionMatrix;

// Inputs
attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord;
attribute vec3 a_tangent;
attribute vec3 a_binormal;

// Outputs
varying vec3 v_lightDirectionTangentSpace;
varying vec2 v_texCoord;

void main()
{
gl_Position = u_worldViewProjectionMatrix * a_position;

// Create a transform to convert a vector to tangent space.
mat3 tangentSpaceTransform = mat3(a_tangent, a_binormal, a_normal);

// Transform light direction to tangent space.
v_lightDirectionTangentSpace = u_directionalLight.direction * tangentSpaceTransform;

v_texCoord = a_texCoord;
}


Fragement shader


precision highp float;

// Structs
struct DirectionalLight
{
vec3 direction; // Direction of the light
vec3 color; // RGB color of the light
};

// Uniforms
uniform DirectionalLight u_directionalLight; // Directional Light
uniform vec3 u_ambientColor; // Ambient color
uniform sampler2D u_diffuseTexture;
uniform sampler2D u_normalMapTexture;

// Inputs
varying vec3 v_lightDirectionTangentSpace;
varying vec2 v_texCoord;

void main()
{
vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).xyz * 2.0 - 1.0);
vec4 baseColor = texture2D(u_diffuseTexture, v_texCoord);
vec4 ambientColor = vec4(u_ambientColor, 1.0) * baseColor;

// Diffuse
float diffuseIntensity = max(0.0, dot(normalVector, v_lightDirectionTangentSpace));
vec4 diffuseColor = (vec4(u_directionalLight.color, 1.0) * diffuseIntensity) * baseColor;


gl_FragColor = ambientColor + diffuseColor;
}




Share this post


Link to post
Share on other sites
Advertisement

Hi,


I am trying to draw a cube with a bump map using normal maps but I really cant see any bump effect on a spinning cube. My unit cube is imported out of maya as a collada file with geometric tangents option (and they seem right).
My worldspace light direction is (0.0, 0.0, 1.0).
My camera position is (0.0, 0.0, 5.0).
I am rotating my cube across Y axis.

Am I missing anything in my shader that could cause a problem?

Thanks in advance!


My vertex shader:

// Structs
struct DirectionalLight
{
vec3 direction;
vec3 color;
};

// Uniforms
uniform DirectionalLight u_directionalLight;
uniform mat4 u_worldViewProjectionMatrix;

// Inputs
attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord;
attribute vec3 a_tangent;
attribute vec3 a_binormal;

// Outputs
varying vec3 v_lightDirectionTangentSpace;
varying vec2 v_texCoord;

void main()
{
gl_Position = u_worldViewProjectionMatrix * a_position;

// Create a transform to convert a vector to tangent space.
mat3 tangentSpaceTransform = mat3(a_tangent, a_binormal, a_normal);

// Transform light direction to tangent space.
v_lightDirectionTangentSpace = u_directionalLight.direction * tangentSpaceTransform;

v_texCoord = a_texCoord;
}


Fragement shader


precision highp float;

// Structs
struct DirectionalLight
{
vec3 direction; // Direction of the light
vec3 color; // RGB color of the light
};

// Uniforms
uniform DirectionalLight u_directionalLight; // Directional Light
uniform vec3 u_ambientColor; // Ambient color
uniform sampler2D u_diffuseTexture;
uniform sampler2D u_normalMapTexture;

// Inputs
varying vec3 v_lightDirectionTangentSpace;
varying vec2 v_texCoord;

void main()
{
vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).xyz * 2.0 - 1.0);
vec4 baseColor = texture2D(u_diffuseTexture, v_texCoord);
vec4 ambientColor = vec4(u_ambientColor, 1.0) * baseColor;

// Diffuse
float diffuseIntensity = max(0.0, dot(normalVector, v_lightDirectionTangentSpace));
vec4 diffuseColor = (vec4(u_directionalLight.color, 1.0) * diffuseIntensity) * baseColor;


gl_FragColor = ambientColor + diffuseColor;
}


Are you able to bump map a single quad? Doing a quad is easier than doing a cube. Doing a static cube is easier than doing a rotating cube. Generally, I find that if I use a bottom up I approach, it's much easier to figure out what's wrong.

Share this post


Link to post
Share on other sites
[color="#1C2837"]
[color="#1C2837"]solved!
[color="#1C2837"] "v_lightDirectionTangentSpace = u_directionalLight.direction * tangentSpaceTransform;"
[color="#1C2837"]
[color="#1C2837"]must have been
[color="#1C2837"]
[color="#1C2837"]v_lightDirectionTangentSpace = [color="#1C2837"]tangentSpaceTransform * [color="#1C2837"]u_directionalLight.direction;
[color="#1C2837"]
[color="#1C2837"]This was messing up the light direction and hence the lighting calculations!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!