# glTranslatef question...

This topic is 5443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK well i am doing all my drawing in an orthographic projection, and using direct access mode. I am going to be switching to display lists soon, to speed up performance, and learn how to use them. And the only thing i dont understand is how to draw the objects to the screen. Right now i can give a point based on pixels... i.e. screen size is 640x480..so i say glVertex2i(0,10); glVertex2i(10,10); glVertex2i(10,0); glVertex2i(0,0); for a box 10x10 pixels wide, then move it around by adding x or y to the base numbers. works but I dont understand how you can compile the object, with display lists, then call glTranslatef to make it appear where it needs to be. If anyone can break this down for me, on exactly how the function works, and how one would store x,y corodinates for an object that will use translate as it's means of being drawn. TIA, justin

##### Share on other sites
It's pretty simple really, as long as you know how to handle matrices. If you don't I suggest you read the red book and learn:

void InitLists(){glNewList(list,GL_COMPILE)//build object here...glEndList();}void Render(){...glPushMatrix();glTranslatef(object.x,object.y,object.z);glCallList(list);glPopMatrix();...}

##### Share on other sites
so where does the brush default to when you push a matrix?

and what values in the object.x object.y would you have to put in for a point to be at 200,200?

##### Share on other sites
As I said, read the RedBook. It explains it better than I ever could. I can't teach you OpenGL in a forum.

##### Share on other sites
It's actually really simple if all you are using is translate. Basicly translate does exactly what you were doing by adding x and y to each vertex, but it lets you do it for every vertex at once.

glVertex2i(0+x,10+y);
glVertex2i(10+x,10+y);
glVertex2i(10+x,0+y);
glVertex2i(0+x,0+y);

you can do

glTranslate2i(x,y);
glVertex2i(0,10);
glVertex2i(10,10);
glVertex2i(10,0);
glVertex2i(0,0);

OGL will handle adding x and y for you. These translations are cumulative, meaning that:

glTranslate(2,0);
glTranslate(3,0);

is the same as:

glTranslate(5,0);

This means that you may want to reset the translation to 0 before each call. You can do this with:

Adding display lists is pretty simple, just wrap your vertex calls in a newList/endList block to create the list. Then do something like this:

glTranslate2i(x,y);
glCallList(list);

Mike was using the matrix stack in his example. This is often used to apply multiple transforms to a group of objects. If you had 3 objects you wanted to move together you could do:

glTranslate2i(x,y);
glCallList(list1);
glCallList(list2);
glCallList(list3);

And they would all be moved by x,y. Lets say you also wanted to move them relitive to each other:

glTranslate2i(x,y);
glPushMatrix();
glTranslate2i(x1,y1);
glCallList(list1);
glPopMatrix();
glPushMatrix();
glTranslate2i(x2,y2);
glCallList(list2);
glPopMatrix();
glPushMatrix();
glTranslate2i(x3,y3);
glCallList(list3);
glPopMatrix();

Push stores the current transformation on the stack, pop restores the matrix to the last push.

##### Share on other sites
Also, assuming you use some ortho projection so that your coordinates match the pixel coordinates (glOrtho(0,winwidth,0,winheight)) then instead of using only glLoadIdentity() consider to use:
glTranslatef(0.375,0.375);

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 14
• 40
• 63