Advertisement

can you push/pop glTranslatef variables?

Started by December 07, 2001 08:32 PM
9 comments, last by TnA SlasheR 23 years, 2 months ago
Can anyone please help me? My problem is this. i have created a fairly nice 3d engine that loads off a data file. this is fine, but the format of each polygon includes not the absolute translate value, but rather the relative value to translate from one polygon to the next. this is bodgy! it makes creating levels harder and adding additional features more confusing. i can place a glLoadIdentity() before in my loop for adding polygons, but then i am unable to move. does anyone have any ideas?
Pushing and popping transformation matrices is done using glPushMatrix(); and glPopMatrix();
Now that might work a few times, but don''t forget the matrix stack is most of the time limited to 16 (or is it 32?), so you can''t do that all the time.
One method would be to replace glPushMatrix(); and glPopMatrix(); by simply reversing your translation... glTranslatef(X,Y,Z); is cancelled by glTranslatef(-X,-Y,-Z);
So you could use a recursive function call like :
void DrawPolygon(Polygon *Poly){  if(Poly==NULL) return;  glTranslatef(Poly->X,Poly->Y,Poly->Z);  /*Draw polygon Poly here*/  DrawPolygon(Poly->Next);      glTranslatef(-Poly->X,-Poly->Y,-Poly->Z);} 

(in that example each polygon would be stored in a structure containing its coordinates relatively to its parent X Y Z, and a pointer to the next polygon would be stored in Next.)

It would work ok, but would no way be the most efficient way.
I would suggest either adding the values on the fly at load time, or writing a little utility that would convert a file in that format to one with absolute coordinates.

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
Advertisement
I wouldnt do that, while it would work. Translate*() calls are expensive in resources, and you should try to make as few as possable.
www.EberKain.comThere it is, Television, Look Listen Kneel Pray.
thanx heaps guys, you''ve both shed enough light on the topic for me to figure it out.

CHEERS
quote:
Original post by Eber Kain
I wouldnt do that, while it would work. Translate*() calls are expensive in resources, and you should try to make as few as possable.

I said I didn''t recommend it either, but I don''t think Translate is THAT expensive... it''s only 3 fpu additions after all.

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
...I said I didn''t recommend it either, but I don''t think Translate is THAT expensive... it''s only 3 fpu additions after all...
Actual NO. It is MUCH more expensive than 3 additions... Anything that changing modelwiev matrix is quite expensive (this includes translate/rotate/push/pop/..). Don''t even try doing push/pop per triangle. this thing will kill your framerate in no-time. It''s much faster to compute abs coordinats on CPU and sent them to OpenGL as vertex array (or VAR).

My point is that it''s MUCH better to change your data so something like VA on load and then use it then doing any push/pop operations per triangles

and about push/pop stack : it''s depth is usualy 16/32 but you can call much more times but all calls about this limit are software implemented( a bit slower )

There are more worlds than the one that you hold in your hand...
You should never let your fears become the boundaries of your dreams.
Advertisement
Anyway, I agree that my first method was stupid was just one way that would work.

I agree that the best method is computing all the absolute coordinates, and that Translating per vertex shouldn''t be done, too

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
quote:

...only 3 fpu additions after all...


Like _DarkWing_ said, it involves A LOT more than just calculating the resulting matrix. But in fact, a translate is not even only 3 addisions, it''s a whole matrix/matrix multiplication. Even though most parts of the translation matrix is known, and the operation can be optimized a little, it will still be more than 3 additions.
Damn... of course, it has to multiply the matrix... how can I be so stupid? lol

--
New feature - Windows users, press ALT-F4 to launch the IQ test.
--New feature - Windows users, press ALT-F4 to launch the IQ test.
yes.. but if you wnat to make this optimization you have to code it yourself.. and the problem is that you have to get the matrix, change it, and then send it back to opengl and that is WAY slower that letting opengl do it itself...

There are more worlds than the one that you hold in your hand...
You should never let your fears become the boundaries of your dreams.

This topic is closed to new replies.

Advertisement