OpenGL ES setPosition

Started by
4 comments, last by afkboard 13 years, 5 months ago
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
Advertisement
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.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
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?
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
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.
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 ?

This topic is closed to new replies.

Advertisement