I want to project a texture on to a wall or any object. I modified this tutorial's shaders as follows based on this Cg projective texture mapping tutorial (ctrl-f "9.3") but I get a blank, white canvas. I'm new to shaders and not quite sure how to debug this(no JS errors), any tips?
Vertex shader:
uniform mat4 u_modelViewProjMatrix;
uniform mat4 u_normalMatrix;
uniform vec3 lightDir;
attribute vec3 vNormal;
attribute vec4 vTexCoord;
attrib开发者_如何学Cute vec4 vPosition;
varying float v_Dot;
varying vec2 v_texCoord;
void main()
{
gl_Position = u_modelViewProjMatrix * vPosition;
v_texCoord = vTexCoord.st * vPosition;
vec4 transNormal = u_normalMatrix * vec4(vNormal, 1);
v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
}
Fragment shader:
uniform sampler2D sampler2d;
varying float v_Dot;
varying vec2 v_texCoord;
void main()
{
vec4 color = texture2DProj(sampler2d,v_texCoord);
gl_FragColor = vec4(color.xyz * v_Dot, color.a);
}
If there's a problem in your code, I think it's the odd way you work out v_texCoord. Are you sure you don't want to take a matrix in place of vTexCoord? Putting that aside, contrary to my expectations, texture2DProj
is defined in GLSL ES 1.0 and it seems otherwise to be syntactically correct, with matched varying names.
Debugging GLSL when you've no step debugger available tends to involve outputting intermediate values as fragment colours. So, your tests could be:
- adjust your fragment shader to output a constant colour, like bright red. If your geometry doesn't change to bright red then your program isn't correctly linking and you should check the log for compile errors.
- try setting gl_FragColor to a suitably adapted version of v_texCoord. Try loading v_texCoord with different intermediate values from the vertex shader. Watch the output to make sure you're getting the right sort of numbers.
- try texturing without the projective step to make sure you're uploading the texture and setting the appropriate uniform correctly.
Plus any other variation you can think of along those lines.
精彩评论