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

## Recommended Posts

I'm creating a "Object3D" class, used to manipulate objects in the scene... every object can have a scaling, a position and a rotation... to render such objects, I should call glTranslatef(...) glRotatef(...) glScalef(...) but what if translation is (0,0,0), rotation is 0° and scaling is 1? Is it better to check these values like this (pseudo-code) if(position!=(0,0,0)) glTranslatef(...) if(rotation.angle!=0) glRotatef(...) if(scaling!=(1,1,1)) glScalef(...) or calling functions without any assumptio/check? I suppose the last is better, since I can avoid 3 checks... moreover cases of null translation/rotation/scaling are logically less than other cases, so I should check these values to allow functions calling, even if it's necessary... what do you think about it?

##### Share on other sites
I definatly wouldnt bother with it per-frame, you would probably just end up stalling the CPU pipeline. The call overhead is unlikely to be the source of a bottleneck unless you're rendering ALOT of these; if you are CPU branch prediction could improve performance (maybe). Your best bet is to collapse transformations where you can, eg. if you have several objects which share the same transformations, do your own matrix math on one matrix and use that matrix for all the other objects etc.

there are much better ways to improve performance at the algorithm level than removing simple redundant function calls, and you may be optimizing too early (which is the root of all that is Evil generally regarded under the BadIdea™ tag line)

FYI, glRotate is the most expensive function out of those 3 (the other two just write 3 floats in a 16 float array) if you want you can "roll" your own glRotatef and use sin and cos lookup tables rather than the actual functions (which should be faster) but also remember NVIDIA and ATI spend alot of time, effort and manpower optimizing their drivers and its probably safe to assume those functions are about as fast as they can be.

Cheers
-Danu

##### Share on other sites
Quote:
 Original post by BladeWiseI'm creating a "Object3D" class, used to manipulate objects in the scene... every object can have a scaling, a position and a rotation... to render such objects, I should callglTranslatef(...)glRotatef(...)glScalef(...)but what if translation is (0,0,0), rotation is 0° and scaling is 1? Is it better to check these values like this (pseudo-code)if(position!=(0,0,0)) glTranslatef(...)if(rotation.angle!=0) glRotatef(...)if(scaling!=(1,1,1)) glScalef(...)or calling functions without any assumptio/check? I suppose the last is better, since I can avoid 3 checks... moreover cases of null translation/rotation/scaling are logically less than other cases, so I should check these values to allow functions calling, even if it's necessary... what do you think about it?

Checking at that low level will probably be inefficient. A better strategy would be to classify your geometry as static or dynamic (dynamic meaning that its position will change). So if something is fully static, it is never expected to move and thus you can do the following

if (dynamic){  glTranslate, glRotate, glScale, etc}drawGeometry();

This will never be the bottleneck in your application but using a strategy like the above in my own engine did give me a little speed boost. Depends what exactly you are rendering (scene type).

HTH

• 16
• 9
• 13
• 41
• 15