View Single Post
  #3  
Old 10-01-2013, 11:14 AM
TopazFrost TopazFrost is offline
Member
 
Join Date: Oct 2009
Posts: 23
Ok....The built in bump mapping still isn't working, but I found a work around. I made my own bump shader. For those looking for a solution here is the code. This uses 2 hard coded directional light positions, colors, and ambient light. That was all I needed.

Attaching the shader:
Code:
norm = viz.add(textureFilePath)
norm.wrap(viz.WRAP_T, viz.REPEAT)
norm.wrap(viz.WRAP_S, viz.REPEAT)
			
node.texture(norm,unit=1)

shader = viz.addShader( vert='bump.vert', frag='bump.frag')
node.apply(shader)
node.apply(viz.addUniformInt('normalMap',1))
The vertex shader (bump.vert):
Code:
attribute float lightNum1;
attribute float lightNum2;

varying vec3 lightVec;
varying vec3 lightVec2;
varying vec3 vPosition;
varying vec3 eyeVec;
varying vec2 texCoord;

void main(void)  
{     
	//lights positions and color  <----------------------Change this  code block if you want to use GL specified lights
        vec3 LightLocation = vec3(2,2,1);
	vec3 LightLocation2 = vec3(-1,2,-2);

	//put vertex in final render position, and grab texture coordinate for later use in fragment shader
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	texCoord = gl_MultiTexCoord0.xy;

	//get everything normalized and in place for later
	vPosition = normalize(vec3(gl_ModelViewMatrix *  gl_Vertex));
	lightVec = normalize(gl_NormalMatrix * LightLocation); 	    //since these are directional light, we only need the orientation
	lightVec2 = normalize(gl_NormalMatrix * LightLocation2); 	

	//the rest of this is getting the space ready for bump mapping which requires a different transform space

	// Building the transform matrix Eye Space (camera) -> Tangent Space (bump map)
        mat3 CameraToTangent;
	vec3 c1 = cross( gl_NormalMatrix * gl_Normal, vec3(0.0, 0.0, 1.0) ); 
	vec3 c2 = cross( gl_NormalMatrix * gl_Normal, vec3(0.0, 1.0, 0.0) ); 
	if( length(c1)>length(c2) )
	{
		CameraToTangent[0] = normalize(c1);
	}
	else
	{
		CameraToTangent[0] = normalize(c2);
	}
	CameraToTangent[2] = normalize (gl_NormalMatrix * gl_Normal);
	CameraToTangent[1] = cross(CameraToTangent[2],CameraToTangent[0]);
	
	// transform light and eye position vectors into tangent/bump space 
	eyeVec = -normalize(vPosition * CameraToTangent);
	lightVec = normalize (lightVec * CameraToTangent);
	lightVec2 = normalize (lightVec2 * CameraToTangent);
}
The fragment shader (bump.frag):
Code:
uniform sampler2D texMap;
uniform sampler2D normalMap;

varying vec3 lightVec;
varying vec3 lightVec2;
varying vec3 vPosition;
varying vec3 eyeVec;
varying vec2 texCoord;

void main (void) 
{ 
	//lights positions and color <------------------Change this  code block if you want to use GL specified lights
	float I = .8;
	float I2 = .6;
	vec4 LightColor = vec4(I,I,I, 1);
        vec4 LightColor2 = vec4(I2,I2,I2, 1);

	gl_FragColor = vec4(0.3,0.3,0.3,1.0);	//add in the ambient right away

	vec3 bump = normalize( texture2D(normalMap, texCoord).xyz * 2.0 - 1.0 );		//the bump mapped normal vector
	
	//calculate the diffuse color for each light (dot for the percentage applied and max to make sure it isn't negative)
	vec4 diffuse = gl_FrontMaterial.diffuse * LightColor *  max (dot (lightVec, bump), 0.0) ; 
	vec4 diffuse2 = gl_FrontMaterial.diffuse * LightColor2 *  max (dot (lightVec2, bump), 0.0) ; 

	//get the specular values for each light
	float specular = pow(clamp(dot(-reflect(lightVec, bump), eyeVec), 0.0, 1.0),  gl_FrontMaterial.shininess );
	float specular2 = pow(clamp(dot(-reflect(lightVec2, bump), eyeVec), 0.0, 1.0),  gl_FrontMaterial.shininess );
	vec4 spec =  gl_FrontMaterial.specular * LightColor * specular;
	vec4 spec2 = gl_FrontMaterial.specular * LightColor2 * specular2;

	//sum all light sources together
	gl_FragColor += diffuse + diffuse2 ;			
	gl_FragColor += spec + spec2;					
	
	gl_FragColor *= texture2D(texMap, texCoord);	//multiply in the texture
	gl_FragColor = clamp(gl_FragColor, 0.0, 1.0);
}
Reply With Quote