Switched from Canvas to OpenGL, need help scaling screen

Problems with Canvas, OpenGL, etc...

Switched from Canvas to OpenGL, need help scaling screen

Postby jgibbs » Sat Apr 23, 2011 10:21 am

Hi all, firstly so as I don't break what seems to be tradition in this forum, I'm pretty new to OpenGL, so...

I've just finished switching some code from using a Canvas to using OpenGL to get performance up to an acceptable level (everything is 2D using sprites).

I've almost completed the task, but am left with one final hurdle which I can't seem to uncover an easy answer to, how to scale the screen?

The original code was built using 800x480 as the reference resolution, and in the onDraw function of the Canvas I simply called canvas.scale(sx, sy) to accommodate different devices.

The new code is using a similar method of drawing by using glDrawTexfOES to output the sprites onto the GL surface where I want them. I'm now stuck with what I have to do to support different display types. Is there an all-encompassing call that I can make to simply translate my fixed 800x480 based target coordinates into the actual display, or do I have to divide down my coords and sprite sizes during my onDraw call?

Any and all help on this problem would be greatly appreciated.
jgibbs
Freshman
Freshman
 
Posts: 4
Joined: Sat Apr 23, 2011 10:07 am

Top

Re: Switched from Canvas to OpenGL, need help scaling screen

Postby Sweep88 » Wed Apr 27, 2011 10:19 am

Just try this:

Code: Select all
int preferredWidth = 480;
int preferredHeight = 800;

int targetWidth = 320;
int targetHeight = 480;
gl.glScalef(targetWidth / (float) preferredWidth, targetHeight / (float) preferredHeight, 1);


Ive just wrote it down without testing it, but it should work.

Good Luck!

// Edit:
But I never used this function to draw Sprites, i think the given coordinates are not affected by the modelview matrix, so the glScalef way can't work... You can try to draw textured Quads, this would be the easiest way.
Sweep88
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 12, 2011 3:23 pm
Location: Germany

Re: Switched from Canvas to OpenGL, need help scaling screen

Postby jgibbs » Sat Apr 30, 2011 6:59 am

Thanks for that idea, but I've been through those transformation attempts and it seems that the way I have things set up it doesn't want to work that way.

I've ended up simply scaling the textures as they are loaded to the current display resolution, and they are positioned in the world based on the same scale. Doesn't appear to have had any performance impact, since the heavy lifting is done at load time.
jgibbs
Freshman
Freshman
 
Posts: 4
Joined: Sat Apr 23, 2011 10:07 am

Re: Switched from Canvas to OpenGL, need help scaling screen

Postby bxm » Tue May 10, 2011 3:08 am

If you are simply using the glDrawTex* extension, all you need to do is change the width and height of the image drawn. So for your game, set your preferred scaling for all images as such:

float scaleX = screenWidth / 800;
float scaleY = screenHeight / 480;

and every time you call to draw an image, you can just:

glDrawTex*(... imageWidth * scaleX, imageHeight * scaleY);

Though I would store the scaled modified drawing width/height in the sprite object to avoid all that float math while drawing.
bxm
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue May 10, 2011 2:51 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 6 guests