GLSL: Bump mapping

Started by
6 comments, last by BloodLust666 16 years, 1 month ago
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.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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...
I'm using the diffuse map as the normal map. can't that work?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
well, I am normalizing the color so it does equal 1 when I'm using it
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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".
oh wow... I was normalizing it in my old shader I was using. But if that won't work either, I'll look into those links. Thanks :)
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

This topic is closed to new replies.

Advertisement