Jump to content
  • Advertisement
Sign in to follow this  
jeff_rowa

OpenGL Bump Mapping problem

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

Hello People, I'm trying to implement a bump mapping with per pixel lighting using GLSL and OpenGL, my shader program is perfectly fine and when I run it in RenderMonkey I get the expected result, but when I use the same shader in my C++ app, I get completely different result, I think I'm passing all the uniform variables corretly, so could anyone please tell me what could be the problem. Correct Result in RenderMonkey: Correct Image Incorrect Result in C++ Wrong Result Many tnx in advance

Share this post


Link to post
Share on other sites
Advertisement
well I'm setting the light pos value the same value that I'm setting in render monkey, even though when I move it farther away it look a little better but no luck!!. and I'm sure that my shader program is working fine,

P.S I'm not setting about light in my OpenGL (C++) app like glEnable(GL_LIGHTING), should I do it?

Share this post


Link to post
Share on other sites
Perhaps you are computing your tangent frame incorrectly (assuming you are doing the normal offset in tangent space)?

But yeah, you should post your shader code and your C++ code for setting the input parameters.

Share this post


Link to post
Share on other sites
tnx guyz here is my shader code:

// ************************ Vertex Shader
uniform vec3 lightPosition;
uniform vec4 eyePosition;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;

//attribute vec3 rm_Binormal;
//attribute vec3 rm_Tangent;
uniform vec3 rm_Tangent;

void main( void )
{
gl_Position = ftransform();
Texcoord = gl_MultiTexCoord0.xy;

vec4 fvObjectPosition = gl_ModelViewMatrix * gl_Vertex;

vec3 fvViewDirection = eyePosition.xyz - fvObjectPosition.xyz;
vec3 fvLightDirection = lightPosition - fvObjectPosition.xyz;

vec3 fvNormal = gl_NormalMatrix * gl_Normal;
//vec3 fvBinormal = gl_NormalMatrix * rm_Binormal;
vec3 fvTangent = gl_NormalMatrix * rm_Tangent;
vec3 fvBinormal = fvNormal * fvTangent;


ViewDirection.x = dot( fvTangent, fvViewDirection );
ViewDirection.y = dot( fvBinormal, fvViewDirection );
ViewDirection.z = dot( fvNormal, fvViewDirection );

LightDirection.x = dot( fvTangent, fvLightDirection.xyz );
LightDirection.y = dot( fvBinormal, fvLightDirection.xyz );
LightDirection.z = dot( fvNormal, fvLightDirection.xyz );

}




// ************************ Fragment Shader
uniform vec4 ambientMaterial;
uniform vec4 specularMaterial;
uniform vec4 diffuseMaterial;
uniform float specularPower;

uniform sampler2D baseMap;
uniform sampler2D bumpMap;
uniform sampler2D heightMap;

varying vec2 Texcoord;
varying vec3 ViewDirection;
varying vec3 LightDirection;
uniform float scale;
uniform float bias0;


void main( void )
{
vec3 fvLightDirection = normalize( LightDirection );
vec3 fvViewDirection = normalize( ViewDirection );

float Height = scale*texture2D(heightMap, Texcoord).x - bias0;

// compute new texture coordinates
vec2 TexCorrected = Height * fvViewDirection.xy + Texcoord;

float stepSize=0.01;
//Iteration 1:
float newHeight = texture2D(heightMap, TexCorrected).x;
if (newHeight>Height)
TexCorrected = TexCorrected + stepSize*newHeight * fvViewDirection.xy;
else
TexCorrected = TexCorrected - stepSize*newHeight * fvViewDirection.xy;



vec3 fvNormal = normalize( ( texture2D( bumpMap, TexCorrected ).xyz * 2.0 ) - 1.0 );
float fNDotL = dot( fvNormal, fvLightDirection );
vec3 fvReflection = normalize( ( ( 2.0 * fvNormal ) * fNDotL ) - fvLightDirection );
float fRDotV = max( 0.0, dot( fvReflection, fvViewDirection ) );

vec4 fvBaseColor = texture2D( baseMap, TexCorrected );

vec4 fvTotalAmbient = ambientMaterial * fvBaseColor;
vec4 fvTotalDiffuse = diffuseMaterial * fNDotL * fvBaseColor;
vec4 fvTotalSpecular = specularMaterial * ( pow( fRDotV, specularPower ) );

gl_FragColor = ( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular );

}



BTW I'm not enabling lighting in C++ or setting any light properties.

[Edited by - jeff_rowa on June 21, 2007 6:08:43 PM]

Share this post


Link to post
Share on other sites
When computing binormal, you should do:

vec3 fvBinormal = cross(fvNormal, fvTangent);

The '*' operator just does a component-wise multiplication.

Share this post


Link to post
Share on other sites
tnx you DrTypo I changed the shader code but still the result is not good.
One more thing guyz, as you can see I'm not passing normal vector to the shader, does it automatically reads the value from OpenGL or should I do something about it?

Share this post


Link to post
Share on other sites
Well, you should provide normal vectors using glNormal*() (in immediate mode) or glNormalPointer() (vertex arrays).

Do you also provide tangent vectors?

Share this post


Link to post
Share on other sites
well this is the code from my C++ app:

glUseProgramObjectARB( m_shaderProgram );
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, m_normalMap);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_heightMap);

glActiveTexture(GL_TEXTURE0);

// Set Shader variables
glUniform1iARB( m_testTextureLocation, 0);
glUniform1fARB( m_normalMapLocation, 2);
glUniform1iARB( m_hightTextureLcation, 1);

glUniform4fARB( m_eyeLocation, 0.0f, 0.0f, m_z, 1.0f );
glUniform4fARB( m_lightPosLocation, 0.0f, 1.0f, m_lightZ, 1.0f );
glUniform3fARB( m_tangentLocation, 1.0f, 0.0f, 0.0f);
glUniform4fARB(m_ambientMaterialLocation, 0.36, 0.36, 0.36, 1.0);
//..... and the rest,

glBindTexture(GL_TEXTURE_2D, m_texture);
glInterleavedArrays( GL_T2F_C3F_V3F, 0, g_quadVertices );
glDrawArrays( GL_QUADS, 0, 4 );

glUseProgramObjectARB( NULL );



I haven't defined the normal as an attribute and I'm using gl_Normal in vertex shader as you can see in the first post. should I add normal vector in my array (g_quadVertices) ??

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!