Jump to content
  • Advertisement
Sign in to follow this  

OpenGL GLSL BumpMap Math error

This topic is 2149 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 have a problem with OpenGL and GLSL to implement Bump Map.

Vertex shader:

#version 330 core

layout (std140) uniform Matrices {

mat4 projViewModelMatrix;

mat3 normalMatrix;

mat4 modelViewMatrix;


uniform mat4 View;

uniform vec3 LightPosition;

uniform vec3 CameraPosition;

in vec3 position;

in vec3 normal;

in vec2 texCoord;

out vec2 TexCoord;

out vec3 Normal;



out vec3 lightVec;

out vec3 eyeVec;

in vec3 vTangent;

in vec3 bTangent;

void main()


Normal = normalize(normalMatrix * normal);

gl_Position = projViewModelMatrix * vec4(position, 1.0);

TexCoord = vec2(texCoord);


vec3 n = normalize(normalMatrix * normal);

vec3 t = normalize(normalMatrix * vTangent);

vec3 b = normalize(normalMatrix * bTangent);

vec3 vVertex = vec3(modelViewMatrix * vec4(position, 1.0));

// vec3 tmpVec = LightPosition.xyz - vVertex.xyz;

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;

lightVec.x = dot(tmpVec, t);

lightVec.y = dot(tmpVec, b);

lightVec.z = dot(tmpVec, n);

tmpVec = -vVertex;

eyeVec.x = dot(tmpVec, t);

eyeVec.y = dot(tmpVec, b);

eyeVec.z = dot(tmpVec, n);



#version 330
layout (std140) uniform Material {

vec4 diffuse;

vec4 ambient;

vec4 specular;

vec4 emissive;

float shininess;

int texCount;


uniform sampler2D texUnit;

in vec3 Normal;

in vec2 TexCoord;

out vec4 outputF;

//BUMP MAPPING ///////////////////////////////////////////

in vec3 lightVec;

in vec3 eyeVec;

uniform sampler2D normalMap;

uniform vec4 diffuseLight;

uniform vec4 ambientLight;

uniform vec4 specularLight;



vec3 N2 = normalize(texture(normalMap, TexCoord).xyz * 2.0 - 1.0);

vec3 L2 = normalize(lightVec);

vec3 V2 = normalize(eyeVec);

vec3 R2 = normalize(-reflect(L2, N2));

float NdotL = max(0.0, dot(N2, L2));

float RdotV = max(0.0, dot(R2, V2));

// Compute final colours.

vec4 ambient = ambientLight * ambient;

vec4 diffuse = diffuseLight * diffuse * NdotL;

vec4 specular2 = specularLight * specular * (pow(RdotV, shininess) * 1);

vec4 base = texture2D(texUnit, TexCoord);

// Final colour.

outputF = base * ambient + base * diffuse + specular2;


There is some error there? I feel that the calculation of the direction of the light is wrong. Could it be?


The line:

vec3 tmpVec = (View * vec4(LightPosition, 1.0)).xyz - vVertex.xyz;

is OK?

the line:

tmpVec = -vVertex;


OK? or I need change to:

tmpVec = (View * vec4(CameraPosition, 1.0)).xyz - vVertex.xyz;



tmpVec = (CameraPosition).xyz - vVertex.xyz;


Please help me.

Thank you, best regards.

Edited by swiftcoder
Please use [code] [/code] tags in future.

Share this post

Link to post
Share on other sites

Come on guys.

Be a bit more patient, most people have jobs, education, family and lives to attend to beside roaming forums.


EDIT: Unfortunately, I can't help you with this.

Edited by assainator

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!