Jump to content
  • Advertisement
Sign in to follow this  
grahek

This time the real question :-)

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! Here's the question...I have to click on the object, that is drawn with quads, and the object must move in the specific direction (like x-axis) for some distance. And then I have to click it again and the object must move again, but this time in the opposite direction. I already did the code for my table (office desk with four drawers) and in my code I can spin the table around by clicking the first button on the mouse and I can optionally choose the number of the drawers (from 1 – 4) and even move them by clicking the specific keys on the keyboard. So where can I find some examples, that contain my kind of problem (clicking on objects and then they move). Thanks to all who will help me, Vito.

Share this post


Link to post
Share on other sites
Advertisement
Tutorial on opengl selection buffer here. Also covers Alpha Blending and Testing.

There are many other ways to solve your problem (I'm assuming you want a GL solution though).

HTH

Share this post


Link to post
Share on other sites
Uf...I'll try to review this tutoril but I guess this will be a little too advanced for me :-)

Share this post


Link to post
Share on other sites
Just go through it slowly and pick out the sources you need - it ain't all that hard ;)
The selection buffer basically works like this:
-set up a selection buffer
-Initialize the so called "name stack"
-define a picking area (in your case: everything within a small radius around the mouse cursor)
-switch to selection buffer render mode
-render your scene like you normally do and assign a "name" to each pickable object. When you are in selection buffer render mode, everything you render won't appear on the screen, instead it is rendered into a buffer
-Switch back to normal render mode. The returned integer will tell how many objects were within your previously defined picking region
-if more than one hit: loop through all objects and remember the one closest to the "camera"
-do what you need to do with the selected object
-render your scene again, but this time to the screen

The pure selection algorithm for example could look like this:
(let's assume, all your rendering is within a function called RenderScene();)

// The Size Of The Viewport. [0] Is <x>, [1] Is <y>, [2] Is <length>, [3] Is <width>
GLint viewport[4];
// This Sets The Array <viewport> To The Size And Location Of The Screen Relative To The Window
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, buffer); // Tell OpenGL To Use Our Array For Selection (selection buffer)
// Puts OpenGL In Selection Mode. Nothing Will Be Drawn. Object ID's and Extents Are Stored In The Buffer.
(void) glRenderMode(GL_SELECT);
glInitNames(); // Initializes The Name Stack
glPushName(0); // Push 0 (At Least One Entry) Onto The Stack
glMatrixMode(GL_PROJECTION); // Selects The Projection Matrix
glPushMatrix(); // Push The Projection Matrix
glLoadIdentity(); // Resets The Matrix
// This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, where The Mouse Is.
gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport);
// Apply The Perspective Matrix
gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]),
0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
RenderScene(); // Render your Scene
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Pop The Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
hits=glRenderMode(GL_RENDER); // Switch To Render Mode, Find Out How Many
if (hits > 0) // If There Were More Than 0 Hits
{
int choose = buffer[3]; // Make Our Selection The First Object
int depth = buffer[1]; // Store How Far Away It Is
for (int loop = 1; loop < hits; loop++) // Loop Through All The Detected Hits
{
// If This Object Is Closer To Us Than The One We Have Selected
if (buffer[loop*4+1] < GLuint(depth))
{
choose = buffer[loop*4+3]; // Select The Closer Object
depth = buffer[loop*4+1]; // Store How Far Away It Is
}
}



Within RenderScene() you'd have to assign an ID (name) to each object, for example:

glLoadName(1); //everything that is rendered after this command will be known as object 1
glBegin(GL_QUADS);
//vertices for drawer 1
glEnd();
glLoadName(2); //everything that is rendered after this command will be known as object 2
glBegin(GL_QUADS);
//vertices for drawer 2
glEnd();
//and so on



When your mousecursor is floating over drawer 2, glRenderMode(GL_RENDER); (within the selection routine) will most likely return 1 (for one hit) and choose will be 2 - that's the name you assigned in RenderScene().
You then know, you will have to do something with drawer2 - like moving it around.

Share this post


Link to post
Share on other sites
OK, this was helpfull. I'll try this later and see how it works. So, I guess the code for moving objects should be something like: if the result is drawer 1, than translate it for dx right?

Share this post


Link to post
Share on other sites
That's correct ;)
But I'd suggest you read through the previously posted tutorial anyway - at least the parts that deal with the selection buffer. It will make you understand how selection (picking) works in detail.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!