Jump to content
  • Advertisement
Sign in to follow this  
justinrwalsh

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
so where does the brush default to when you push a matrix?
or load identity?

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

Share this post


Link to post
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 this post


Link to post
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.

instead of:

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:

glLoadIdentity();

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:

glLoadIdentity();
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:

glLoadIdentity();
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:

glLoadIdentity();
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 this post


Link to post
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:
glLoadIdentity();
glTranslatef(0.375,0.375);

See http://216.239.59.104/search?q=cache:Xd-lr0sHIdsJ:fly.cc.fer.hr/~unreal/theredbook/appendixh.html+glTranslatef(0.375,0.375)&hl=en for more info.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!