Original Post
Hello everyone
On my tablet i render my scene to a depth buffer and read a pixel from it to determine my current position.
- I visualized the depth buffer -> all is fine, vertex shader:
[source lang="cpp"]varying float camDistance;
void main() {
vec4 tmpPos = vec4(vPosition0.xyz, 1.0);
vec4 posCamSpace = matProj * matView * tmpPos;
camDistance = (posCamSpace.z - 0.1) / (800.0 - 0.1);
gl_Position = posCamSpace;
}[/source]
i hardcoded the values just to be sure!
- I visualized the depth read from the buffer -> It is correct for the nearest points and for the points far away, thats the code i use:
[source lang="java"] ibuff.position(0);
GLES20.glReadPixels((int)touchPos.x, (int)touchPos.y, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ibuff);
ibuff.position(0);
int color = ibuff.getInt() & 0x000000FF;
float depth = color / 256.0f;[/source]
- When i check my coordinates i see that it uses nearly the position of the camera for all 3 components and when i move my finger shifts the coorect coordinates but like 0.001 per pixel like so it seems that it unprojects the point no matter what depth i enter to the near plane.
- If I use depth 1 every time unprojection is done nicely and works like a charm if i move my finger, all coordinates are correct and change correct.
I used gluUnProject to unproject my values as well as my own implementation. With gluUnProject:
[source lang="java"] GLU.gluUnProject(touchPos.x, touchPos.y, depth,
getActiveCamera().getMatView().getFloats(), 0,
PerspectiveMatrix.getFloats(), 0, viewport, 0, coords, 0);[/source]
touchPos.y of course is transformed with the viewport (touchPos.y = viewport[3] - touchPos.y).
Thats my own implementation:
[source lang="java"] public static Vector3 unproject(Vector3 v, Matrix invView, Matrix invProj, int[] viewport) {
Matrix matCombo = Matrix.multiply(invView, invProj);
float vx = viewport[0];
float vy = viewport[1];
float vw = viewport[2];
float vh = viewport[3];
float[] rhsVec = {
((2 * (v.x - vx)) / vw) - 1,
((2 * (v.y - vy)) / vh) - 1,
2 * v.z - 1,
1
};
float[] result = new float[4];
android.opengl.Matrix.multiplyMV(result, 0, matCombo.getFloats(), 0, rhsVec, 0);
float d = 1 / result[3];
return new Vector3(result[0] * d, result[1] * d, result[2] * d);
}[/source]
What did i forget? I used gluUnProject on my Desktop with glReadPixels (but GL_DEPTH_COMPONENT which is not available on OpenGL ES) and it worked there!
Thank you for any help
Plerion
On my tablet i render my scene to a depth buffer and read a pixel from it to determine my current position.
- I visualized the depth buffer -> all is fine, vertex shader:
[source lang="cpp"]varying float camDistance;
void main() {
vec4 tmpPos = vec4(vPosition0.xyz, 1.0);
vec4 posCamSpace = matProj * matView * tmpPos;
camDistance = (posCamSpace.z - 0.1) / (800.0 - 0.1);
gl_Position = posCamSpace;
}[/source]
i hardcoded the values just to be sure!
- I visualized the depth read from the buffer -> It is correct for the nearest points and for the points far away, thats the code i use:
[source lang="java"] ibuff.position(0);
GLES20.glReadPixels((int)touchPos.x, (int)touchPos.y, 1, 1, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ibuff);
ibuff.position(0);
int color = ibuff.getInt() & 0x000000FF;
float depth = color / 256.0f;[/source]
- When i check my coordinates i see that it uses nearly the position of the camera for all 3 components and when i move my finger shifts the coorect coordinates but like 0.001 per pixel like so it seems that it unprojects the point no matter what depth i enter to the near plane.
- If I use depth 1 every time unprojection is done nicely and works like a charm if i move my finger, all coordinates are correct and change correct.
I used gluUnProject to unproject my values as well as my own implementation. With gluUnProject:
[source lang="java"] GLU.gluUnProject(touchPos.x, touchPos.y, depth,
getActiveCamera().getMatView().getFloats(), 0,
PerspectiveMatrix.getFloats(), 0, viewport, 0, coords, 0);[/source]
touchPos.y of course is transformed with the viewport (touchPos.y = viewport[3] - touchPos.y).
Thats my own implementation:
[source lang="java"] public static Vector3 unproject(Vector3 v, Matrix invView, Matrix invProj, int[] viewport) {
Matrix matCombo = Matrix.multiply(invView, invProj);
float vx = viewport[0];
float vy = viewport[1];
float vw = viewport[2];
float vh = viewport[3];
float[] rhsVec = {
((2 * (v.x - vx)) / vw) - 1,
((2 * (v.y - vy)) / vh) - 1,
2 * v.z - 1,
1
};
float[] result = new float[4];
android.opengl.Matrix.multiplyMV(result, 0, matCombo.getFloats(), 0, rhsVec, 0);
float d = 1 / result[3];
return new Vector3(result[0] * d, result[1] * d, result[2] * d);
}[/source]
What did i forget? I used gluUnProject on my Desktop with glReadPixels (but GL_DEPTH_COMPONENT which is not available on OpenGL ES) and it worked there!
Thank you for any help
Plerion



