Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

GLSL: Bump mapping

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

ok, I finally got all the kinks out of my shaders and I'm getting some results, I'm using a different approach for bump mapping but it's still not coming out right. here's what I have:
<vertex>
uniform vec3 camPosition;

attribute vec3 rm_Tangent;   

varying vec2 v2TexCoord;
varying vec3 v3TanLightDir;
varying vec3 v3TanViewDir;

void main(void)
{
	// compute worldspace eye and light vectors
	vec3 v3TempLightDir = vec3(1.0, -1.0, 0.0);
	vec3 v3TempViewDir = camPosition - gl_Vertex.xyz;

	// matrix operation to transform 
	// both vectors into tangent space
	vec3 v3Binormal = cross( gl_Normal, rm_Tangent );

	v3TanLightDir.x = dot(rm_Tangent, v3TempLightDir);
	v3TanLightDir.y = dot(v3Binormal, v3TempLightDir);
	v3TanLightDir.z = dot(gl_Normal, v3TempLightDir);

	v3TanViewDir.x = dot(rm_Tangent, v3TempViewDir);
	v3TanViewDir.y = dot(v3Binormal, v3TempViewDir);
	v3TanViewDir.z = dot(gl_Normal, v3TempViewDir);

	// fetch tex coord
	v2TexCoord = (gl_MultiTexCoord0 * gl_TextureMatrix[0]).xy;

	// mvt
	gl_Position = ftransform();
}

<fragment>
uniform sampler2D TextureColor;
uniform sampler2D TextureNormBump;

varying vec2 v2TexCoord;
varying vec3 v3TanLightDir;
varying vec3 v3TanViewDir;

const float BUMP_HEIGHT_SCALE = 4.0;
const float BUMP_HEIGHT_BIAS = -2.0;

const float PARALLAX_SCALE = 0.08;
const float PARALLAX_BIAS = -0.04;

void main(void)
{
	// normalize the interpolated tangent space light vector and eye vector
	vec3 v3NormLightDir = normalize(v3TanLightDir);
	vec3 v3NormViewDir = normalize(v3TanViewDir);

	// calculate a suitable height value for this texel
	float fHeight = texture2D(TextureNormBump, v2TexCoord).w * PARALLAX_SCALE + PARALLAX_BIAS;

	// perturb the texture coordinate
	vec2 v2NewTexCoord = v2TexCoord + (fHeight * v3NormViewDir.xy) * texture2D(TextureNormBump, v2TexCoord).z;

	// retrieve the surface normal at this new texture coordinate
	vec3 v3BumpNormal = texture2D(TextureNormBump, v2NewTexCoord).xyz * BUMP_HEIGHT_SCALE + BUMP_HEIGHT_BIAS;

	// the dot3 operation - calculate diffuse intensity
	float fDiffuseIntensity = clamp( dot( v3NormLightDir, v3BumpNormal ), 0.0, 1.0);
	
	// fetch the color
	gl_FragColor = fDiffuseIntensity * texture2D( TextureColor, v2NewTexCoord );
	gl_FragColor.a = 1.0;
}

both the diffuse and bump are the same texture, they way the texture it, it will work, it's the Earth texture. but yea, it's definitely not working right.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by BloodLust666
...it's still not coming out right.
...it's definitely not working right.

What's not right about it? A picture's worth a thousand words with these issues ;)

BTW, I assume your texture has an alpha-channel with the height-map information?

Quote:
Original post by BloodLust666
both the diffuse and bump are the same texture, they way the texture it, it will work, it's the Earth texture.

A regular diffuse texture will *not* work as a normal map, in almost every circumstance...

Share this post


Link to post
Share on other sites
No. A diffuse map is just colour information, but a normal map contains mathematical vectors. RGB become XYZ. If XYZ don't form a normalized vector then you're gonna get weird results.
i.e. after decoding X,Y and Z, the length (X*X + Y*Y + Z*Z) should equal 1

If you've got photoshop, use NVIDIAs photoshop plugins to make a proper normal map, otherwise use the crazy bump beta.

Share this post


Link to post
Share on other sites
Not in the above code you posted:

// retrieve the surface normal at this new texture coordinate
vec3 v3BumpNormal = texture2D(TextureNormBump, v2NewTexCoord).xyz * BUMP_HEIGHT_SCALE + BUMP_HEIGHT_BIAS;

// the dot3 operation - calculate diffuse intensity
float fDiffuseIntensity = clamp( dot( v3NormLightDir, v3BumpNormal ), 0.0, 1.0);



Anyway, even if you were normalizing it, the values you would be getting would be completely arbitrary in respect to the height-map, and would not "look right".

Share this post


Link to post
Share on other sites
wow, that crazy bump program IS pretty crazy! hah But now I have a normal map (tangent space) and my bump shader is really not working... there's no bumps at all and where the light isn't shining, the model is just black.. are there any other errors you see in my shader?

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!