Placing many different 2D elements in a window

Hey all,

I have a question about placing many 2D elements in an openGL window. I am doing that in my system a lot and so far I was using different glViewPort calls for placing each element in the window and then drawing it according to its own coordinates. It works fine like that but this leads me to the question of how heavy a glViewPort call actually is. I would like to know that since my code is getting full of it due to the number of 2D elements and the fact that they can have children elements so in a hierarchy of children imagine that glViewPort is called for each one in the hierarchy.

From a code point of view using glViewPort is easy and nice but how heavy is it? As an alternative I could have one viewport only for the main window and after that placing all the elements by scaling and translating. I have implemented both drawing methods but I can’t seem to determine how heavy each one is. That’s why I am turning to the community for help.

Might be making a huge mistake of course by considering one of these methods but since I don’t know if I am, I would like your help for that. The way I see it:

glViewPort advantages:
[ul][li] Easy to use, just set the viewport to the x,y pos of the 2D Element and the width/height to its width/height and you are done[*]Speed = ???[/ul] [/li]

Scaling and translating advantages:
[ul][li] Allows for floating point coordinates of the elements[*]Speed = ???[/li]Requires one translation and one scaling for each element instead of a glViewPort – which I can’t say if it’s better or worse[/ul]

Thanks in advance!

Scaling and translating is in the end only one matrix multiplication per vertex which the hardware is very good at. It’s also more flexible as you can also do stretching, shering, rotations etc. with no additional costs (all boild down to one combined matrix). I would do it this way.

Yes it is as you say, and the added flexibility is a big plus for any system and for the moment this is the implementation I have switched to.

My only concern is that I don’t have a comparison metric with glViewport since I don’t know how it operates under the hood.

In the end glViewport is just another matrix that transforms coordinates from the unit-cube (-1…1, -1…1) to your screen coordinates (e.g. 0…1919, 0…1079) before the geometry gets rasterized. But it’s hard to tell what other stuff could go on in the driver. I’d guess that glViewport can’t be faster but might not be much slower.

glViewport() works for fixed-pipeline GLs all the way to 4.2 and if that is an issue it might be a better solution. I don’t think you will notice much of a speed difference between the two methods, but please bench and share your results with us.