Hi,
I am trying to write a shader to do bump mapping using OSG and OpenGL ES 2.0 in
a mobile platform (iOS).
My 3d models are saved as OBJs. The bump map is embedded in the .MTL and
declared in lines of the type:
Code:
newmtl brick2
Kd 0.800000 0.800000 0.800000
illum 2
Ns 50.781250
map_Kd BRICKTAN.jpg
bump BRICKBMP.jpg
the fragment shader is:
Code:
precision mediump float;
uniform sampler2D BumpTex; //The bump-map
uniform sampler2D DecalTex; //The texture
varying vec4 passcolor; //Receiving the vertex color from the vertex shader
varying vec3 LightDir; //Receiving the transformed light direction
varying vec2 tex_coord0;
varying vec2 bump_coord0;
void main()
{
//Get the color of the bump-map
vec3 BumpNorm = vec3(texture2D(BumpTex, bump_coord0));
//Get the color of the texture
vec3 DecalCol = vec3(texture2D(DecalTex, tex_coord0));
//Expand the bump-map into a normalized signed vector
BumpNorm = (BumpNorm -0.5) * 2.0;
//Find the dot product between the light direction and the normal
float NdotL = max(dot(BumpNorm, LightDir), 0.0);
//Calculate the final color gl_FragColor
vec3 diffuse = NdotL * passcolor.xyz * DecalCol;
//Set the color of the fragment
gl_FragColor = vec4(diffuse, passcolor.w);
}
the vertex shader is
Code:
precision mediump float;
varying vec2 tex_coord0;
varying vec2 bump_coord0;
varying vec4 passcolor; //The vertex color passed
varying vec3 LightDir; //The transformed light direction, to pass to the
fragment shader
attribute vec3 tangent; //The inverse tangent to the geometry
attribute vec3 binormal; //The inverse binormal to the geometry
uniform vec4 u_LightPos; //The position of the light
void main()
{
tex_coord0 = gl_MultiTexCoord0.xy;
bump_coord0 = gl_MultiTexCoord1.xy;
// the vertex position
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
// light dir
vec3 lightDirection = vec3(u_LightPos.xyz - vVertex);
//Put the color in a varying variable
passcolor = gl_Color;
//Construct a 3x3 matrix from the geometry’s inverse tangent, binormal, and
normal
mat3 rotmat = mat3(tangent,binormal,gl_Normal);
//Rotate the light into tangent space
LightDir = rotmat * normalize(lightDirection);
//Normalize the light
normalize(LightDir);
//Use the first set of texture coordinates in the fragment shader
//Put the vertex in the position passed
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
I thought I should link three uniforms. One for the bump texture and one for
the normal texture. I assigned the value 0 to DecalTex and 1 to BumpTex.
Code:
osg::Uniform* textureUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D,
"DecalTex");
textureUniform->set(0);
_oggettoObj->getOrCreateStateSet()->addUniform(textureUniform);
osg::Uniform* bumpUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D,
"BumpTex");
bumpUniform->set(1);
_oggettoObj->getOrCreateStateSet()->addUniform(bumpUniform);
_root->addChild(_oggettoObj.get());
Then there is light position which is placed at (0,0,0) and can be moved with
some controls in my application.
The output of the application seems normal. The shader does not fail to compile
and the images are loaded in memory.
The problem is that this is not working. I am sure that I am accessing the
wrong texture for the bump mapping. The problem is I don't know how to select
the correct one from the shader. Can you help me to understand, please?
Thank you!
Cheers,
John
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=49187#49187
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org