PDA

View Full Version : Problem when moving object in Z coord...



lm2s
11-27-2010, 07:57 PM
Hello!
I'm starting to play around with OpenGL ES 2 and when trying to move an object in the Z coord it just either disappears or doesn't do anything.
It moves well in X and Y but not Z.
To move in Z I use the gl_Position.z in the shader.

The source of the project is in: http://cl.ly/24240x2D1t2A3I0c1l1P
It's a XCode project for iOS, the setup code can be found in EAGLView.m @ createFramebuffer, and the draw code can be found in TranslateSinZ.m @ drawView.

I believe the problem should be in the setup code but I can't figure out what's wrong...

Anyone can help me out?
Thank you!

Xmas
11-30-2010, 04:45 AM
gl_Position is the position of the vertex in homogeneous clip space. Its Z component is only used for clipping against the near and far planes as well as for the depth test. It does not affect the position of the vertex in X and Y on screen.

I suspect that what you actually want is to translate the vertices in view space Z, before applying a projection transformation.

lm2s
11-30-2010, 12:37 PM
I thought just changing the value in "gl_Position.z" would work since it works for "gl_Position.x" and "gl_Position.y".
So to translate the rectangle in Z, how can I achieve that?
"Translate the vertices in view space Z" can you explain in another fashion?
Thank you for your time.

BKB
04-23-2011, 03:44 AM
Indeed, changing the value of gl_Position.z wont work because the rendered image is on a 2d plane with no depth :)

What you need to do is add back in the model, view and projection matrices that got deprecated in the move to programmable shaders, so my vertex shader looks a little something like:


attribute lowp vec4 v_position;
attribute lowp vec2 v_texture;

uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_projection;

varying vec2 textureVarying;

void main()
{
textureVarying = v_texture;
gl_Position = m_projection * m_view * m_model * v_position;
}

Here, translations and rotations for a given model are loaded into the model matrix, m_model , the camera setup is loaded into the view matrix, m_view and the projection (ortho or perspective) is loaded into the projection matrix m_projection .