glTranslate, how exactly does it work?
Posted
by
mykk
on Game Development
See other posts from Game Development
or by mykk
Published on 2012-06-08T11:57:15Z
Indexed on
2012/06/08
16:50 UTC
Read the original article
Hit count: 251
I have some trouble understanding how does glTranslate work. At first I thought it would just simply add values to axis to do the transformation.
However then I have created two objects that would load bitmaps, one has matrix set to GL_TEXTURE:
public class Background
{
float[] vertices = new float[]
{ 0f, -1f, 0.0f,
4f, -1f, 0.0f,
0f, 1f, 0.0f,
4f, 1f, 0.0f };
....
private float backgroundScrolled = 0;
public void scrollBackground(GL10 gl)
{
gl.glLoadIdentity();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glTranslatef(0f, 0f, 0f);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glTranslatef(backgroundScrolled, 0.0f, 0.0f);
gl.glPushMatrix();
this.draw(gl);
gl.glPopMatrix();
backgroundScrolled += 0.01f;
gl.glLoadIdentity();
}
}
and another to GL_MODELVIEW:
public class Box
{
float[] vertices = new float[]
{ 0.5f, 0f, 0.0f,
1f, 0f, 0.0f,
0.5f, 0.5f, 0.0f,
1f, 0.5f, 0.0f };
....
private float boxScrolled = 0;
public void scrollBackground(GL10 gl)
{
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0f, 0f, 0f);
gl.glPushMatrix();
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(boxScrolled, 0.0f, 0.0f);
gl.glPushMatrix();
this.draw(gl);
gl.glPopMatrix();
boxScrolled+= 0.01f;
gl.glLoadIdentity();
}
}
Now they are both drawn in Renderer.OnDraw. However background moves exactly 5 times faster. If I multiply boxScrolled by 5 they will be in sinc and will move together. If I modify backgrounds vertices to be
float[] vertices = new float[]
{ 1f, -1f, 0.0f,
0f, -1f, 0.0f,
1f, 1f, 0.0f,
0f, 1f, 0.0f };
It will also be in sinc with the box. So, what is going under glTranslate?
© Game Development or respective owner