Sign in to follow this  
afkboard

OpenGL OpenGL ES setPosition

Recommended Posts

Hi,

I have simple 2D textured rects on which I want to put other textured rect as button. I tried to write something like:


void setPosition(float x, float y)



method, so that when the user sets the points as:


mButton1.setPosition(240, 120);



the button to be drawn at this point. Now, setting x and y in the opengl world via gluUnProject never works correctly, there are always some incorrect values. Any other simpler way to do this? I tried this: http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/no8

Thanks

Share this post


Link to post
Share on other sites
You should not need to use gluUnProject for this.

What projection matrix are you using? If you want to work on a pixel level, than you should just use an orthographic projection matrix that matches your screen dimensions.

glOrtho(0,WIDTH, 0,HEIGHT, -1, 1);

Then your vertex positions will match your pixel coordinates. Then if you want to draw a button at 240, 120, you just transform it right 240 units and up/down 120 units.

Share this post


Link to post
Share on other sites
Quote:
Original post by karwosts
You should not need to use gluUnProject for this.

What projection matrix are you using? If you want to work on a pixel level, than you should just use an orthographic projection matrix that matches your screen dimensions.

glOrtho(0,WIDTH, 0,HEIGHT, -1, 1);

Then your vertex positions will match your pixel coordinates. Then if you want to draw a button at 240, 120, you just transform it right 240 units and up/down 120 units.


But what when I have 3D ? glOrtho AFAIK is for 2D only, isn't it?

Share this post


Link to post
Share on other sites
But you're asking about buttons, which is a 2D GUI, I would imagine. If you have a GUI you should be rendering it with glOrtho on top of your 3D world.

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
But you're asking about buttons, which is a 2D GUI, I would imagine. If you have a GUI you should be rendering it with glOrtho on top of your 3D world.

-me


Well I want to make it general, buttons currently are 2D - yes, but then, there is a Z-rotated plane which I should make something similar for it too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
But you're asking about buttons, which is a 2D GUI, I would imagine. If you have a GUI you should be rendering it with glOrtho on top of your 3D world.

-me


Hm. Ok. Is there any example for using glOrtho ? I.e. I am drawing a button like this:


glPushMatrix()
glTranslate(x, y, 0);
glScale(.5, .5, 0);
mButton.draw(gl);
glPopMatrix();



What should I do to make the matrix translate to (i.e.) 240, 120 ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this