I am trying to implement a textured projection based directly on this example on oZone3d . My environment is OpenGL ES 2.0 on Android 2.2.
Matrix math has never been my forte, and I suspect that my problem lies somewhere in the texture matrix matrix (texGenMatrix). I tried everything I could imagine to get this work, but the projection is not working correctly. I would really appreciate some additional views on my code. Thank you
My geometry is not shown here (horizontal plane through the origin), as well as some other plumbing, such as a shader shape / attribute.
Camera setup:
Matrix.frustumM(cameraProjMatrix, 0,
-nearPlaneSize.x, nearPlaneSize.x,
-nearPlaneSize.y, nearPlaneSize.y,
nearPlaneDistance, farPlaneDistance
);
Matrix.setLookAtM(cameraViewMatrix, 0,
0, 100, 100,
0, 0, 0,
0, 1, 0
);
Matrix.translateM(modelMatrix, 0, x, y, z);
Matrix.multiplyMM(modelViewMatrix, 0, cameraViewMatrix, 0, modelMatrix, 0);
Matrix.invertM(inverseCameraViewMatrix, 0, cameraViewMatrix, 0);
Texture projection setting:
Matrix.frustumM(texProjMatrix, 0,
-nearPlaneSize.x, nearPlaneSize.x,
-nearPlaneSize.y, nearPlaneSize.y,
nearPlaneDistance, farPlaneDistance
);
Matrix.setLookAtM(texViewMatrix, 0,
0, 100, 10,
0, 0, 0,
0, 1, 0
);
float[] scaleBiasMatrix = new float[] { 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f
};
Matrix.multiplyMM(texGenMatrix, 0, scaleBiasMatrix, 0, texProjMatrix, 0);
Matrix.multiplyMM(texGenMatrix, 0, texGenMatrix, 0, texViewMatrix, 0);
Vertex Shader:
attribute vec3 aPosition; // vertex x y z
uniform mat4 texGenMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 cameraProjMatrix;
varying vec4 vProjCoord;
void main() {
gl_Position = cameraProjMatrix * modelViewMatrix * aPosition;
vec4 posEye = modelViewMatrix * aPosition;
vec4 posWorld = inverseCameraViewMatrix * posEye;
vProjCoord = texGenMatrix * posWorld;
}
Fragment Shader:
uniform sampler2D projectionMap;
varying vec4 vProjCoord;
void main() {
vec3 finalColor = vec3( 0.5, 0.5, 0.5);
if (vProjCoord.q > 0.0) {
vec4 ProjMapColor = texture2DProj(projectionMap, vProjCoord);
finalColor += ProjMapColor.rgb;
}
gl_FragColor = vec4(finalColor, 1.0);
}