# How to scale openGL textures by a ratio based on height/width (or alternative)?

I have been able to successfully allow my UI to extend over edges of my game on higher resolutions by using the Android Guide and by implementing this code on the GL10 object:

gl.glTranslatef(GTEngine.get_SCREEN_RATIO(), 0f, 0f);


Where the first parameter is the ratio calculated by dividing the width by the height.

My confusion arises in the ability to use :

gl.glScalef(n,n, 1f);


... to scale the image by using the same screen ratio in order to account for the "growth" or "shrinkage" that comes with different resolutions. Does anyone have a solution for this n variable? I cannot seem to make up some mathematically solution that works, and am worried that I may be forced to build a case by case logic (>.>)

• What's the end result you're looking for with the scaling? Can you give an example? – MichaelHouse Jul 30 '13 at 18:12
• I am looking for a solution that results in an a Density Independent GL10 Object. – Justin Jul 30 '13 at 18:16
• 'if(densityRatio > 1.666f){ gl.glScalef(n,n, 1f)}' – Justin Jul 30 '13 at 18:19

If I'm understanding you correctly you want an object to scale and appear the same size /ratio (relative) on whichever device you are using? (i.e., so it doesn't appear stretched in either the X or Y direction)?

If so, the way I worked around this was to decide the size I wanted my object to appear on my development device, so on my Nexus 10 which has a res of 2560 x 1504 (Ratio of 1.7) , my sprites are 102 x 100 in size.

Therefore, I said:

102/2560 = 0.03984375f

100/1504 = 0.0664893617021277f

And then, in my code just just did:

(pseudo code follows)

sprite width = (0.03984375 * current Viewport Width)
sprite height =(0.0664893617021277 * current Viewport Height)


I'm not using GLScalef to scale my objects though, I'm manipulating the vertices directly. But this results in objects of the same relative size and ratio on all screens which (I think) is what you're after?

• Why are manipulating the vertices directly? – Justin Aug 9 '13 at 15:14
• It was just the way I did things @Justin, no specific reason, I researched the different ways to scale and move my sprites and just decided on that way of doing things :-) – BungleBonce Aug 9 '13 at 17:43