Click to See Complete Forum and Search --> : Simple GUI System, Coords question mainly


Grofit
October 8th, 2008, 03:16 AM
Hey,

Im just thinking about how you logically go about making a simple GUI in a simple 3d based app, such as a simple OpenGL app.

The part im wondering is how do you work out when you have clicked on an element of the GUI? as the mouse click will have an X/Y position from windows or your input system I assume, then from there do you have to convert it into worldspace to work out where in the scene you have clicked, also how do you take from your world to local space? (while we are on the subject)

I made a really simple menu system for an old 2D game a while back, but as it was 2d i didnt need to do any translations or anything on the coord. I think i just made a simple tree hierarchy to specify what menu elements sat where and it just checked down the tree to work out what was clicked...

Im just curious as i may need to write a simple 3d menu system in the future and any info to give me a headstart would be great!

victor2008
October 11th, 2008, 01:01 AM
It is a way of communicating with a computer that uses visual feedback to the user as much as possible. Features of a GUI include the use of icons to represent commands and options, pull-down menus that appear when called for and then disappear when no longer needed, and the use of a mouse to move a pointer around the screen. By pointing to the appropriate icons or menu items and clicking a mouse button, various commands can be activated. It is also possible to use the mouse in drawing programs.
________________________________________
Flower Delivery Service (http://www.florist-flowers-roses-delivery.com/illinois/chicago_il.html) 25000 loans (http://www.cheaponline-loans.co.uk/help/loans-for-25000.php) empleo vilafranca del penedes (http://www.vilafrancadelpenedes.com/-1/posts/2_Feina/0/) download ringtones (http://www.myringtoneshub.com/)

AntiBNI
October 11th, 2008, 08:32 AM
It is a way of communicating with a computer that uses visual feedback to the user as much as possible. Features of a GUI include the use of icons to represent commands and options, pull-down menus that appear when called for and then disappear when no longer needed, and the use of a mouse to move a pointer around the screen. By pointing to the appropriate icons or menu items and clicking a mouse button, various commands can be activated. It is also possible to use the mouse in drawing programs.

You didn't even read the question didn't you?

Hes not asking what is a GUI.


Hey,

Im just thinking about how you logically go about making a simple GUI in a simple 3d based app, such as a simple OpenGL app.

The part im wondering is how do you work out when you have clicked on an element of the GUI? as the mouse click will have an X/Y position from windows or your input system I assume, then from there do you have to convert it into worldspace to work out where in the scene you have clicked, also how do you take from your world to local space? (while we are on the subject)

I made a really simple menu system for an old 2D game a while back, but as it was 2d i didnt need to do any translations or anything on the coord. I think i just made a simple tree hierarchy to specify what menu elements sat where and it just checked down the tree to work out what was clicked...

Im just curious as i may need to write a simple 3d menu system in the future and any info to give me a headstart would be great!


I guess you should ask in "http://www.gamedev.net/community/forums/"since the whole forum is based on 2D and 3D Stuff.


*Edit*

If your question gets answered,please put the link here cuz me and maybe some other people have the same question as you and will find this helpful.

bitshifter420
October 11th, 2008, 10:38 PM
Is there any particular reason why the menu(s) must be rendered in 3D?
Usually i setup a perspective projection matrix to render 3D stuff and then switch to an orthographic projection matrix to render the 2D stuff.
Here is some sample code on how to mix 2D and 3D rendering/hit testing.

Setup perspective: (done once during initialization)

glViewport(...);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(...);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


Prepare buffers and preserve modelview identity:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();


...Render/Hit test 3D stuff here...

Push orthographic:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(...);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();


...Render/Hit test 2D stuff here...

Pop orthographic:

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();


Restore modelview identity and swap buffers:

glPopMatrix();
SwapBuffers(hdc);

mokbol
October 13th, 2008, 06:05 AM
The part im wondering is how do you work out when you have clicked on an element of the GUI? as the mouse click will have an X/Y position from windows or your input system I assume, then from there do you have to convert it into worldspace to work out where in the scene you have clicked, also how do you take from your world to local space? (while we are on the subject)

bitshifter420
October 13th, 2008, 09:31 PM
If you are in 2D (using orthographic projection) then the relationship between the cursor position the screen and the viewport are 1:1.
If you are in 3D (using perspective projection) then the scene must be squashed into 2D before any hit testing can be done.
When i wrote a chess game i had done the menu(s) in 2D so they were easy to hit test, but when i needed to hit test the board and the pieces i was forced to find a solution for 3D hit testing.
3D hit testing is expensive and i would try to avoid it if possible.
What i will do is punch out a simple demo which implements 2D/3D hit testing. then i will post it here for you to study, just give me a little time to do it.

TheCPUWizard
October 13th, 2008, 09:41 PM
There are two different approaches.

1) As mentioned, "smash" the 3d down to a flat @d, and simply use that. This is the "cheapest" in many resepects.

2) Consider the screen as a plane in the 3D space. When you click on the screen, you generate a normal ray from the position on the plane, and perfom interceptions.

bitshifter420
October 14th, 2008, 05:49 AM
Ok, maybe you can grasp the concept of 3D hit testing without a full blown demo.
This is the general idea on how to perform selection testing using the feedback buffer.


POINT cursor;
GetCursorPos(&cursor);

int viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

int width = viewport[2] - viewport[0];
int height = viewport[3] - viewport[1];

unsigned buffer[512];
glSelectBuffer(512, buffer);

glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPickMatrix((double)cursor.x, (double)(viewport[3] - cursor.y), 1.0, 1.0, viewport);

float aspect = 1.0;

if(height)
aspect = (float)(width / height);

// TODO: set *** values to match your perspective.
gluPerspective(***, aspect, ***, ***);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// TODO: render object here...
// (including local transforms)

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

INT hits = glRenderMode(GL_RENDER);

if(hits > 0)
{
// TODO: handle situation here.
// buffer now contains data about object.
// see glSelectBuffer for details.
}


Go ahead and toy with this code to make it fit in your app.
If you are not farmilliar with some of these functions, look them up on msdn or in the redbook. (the redbook and bluebook are nice to have around)