This time the real question :-)

Started by
5 comments, last by grahek 18 years, 8 months ago
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.
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
Uf...I'll try to review this tutoril but I guess this will be a little too advanced for me :-)
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 WindowglGetIntegerv(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 StackglPushName(0); // Push 0 (At Least One Entry) Onto The StackglMatrixMode(GL_PROJECTION); // Selects The Projection MatrixglPushMatrix(); // Push The Projection MatrixglLoadIdentity(); // 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 MatrixgluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]),0.1f, 100.0f);glMatrixMode(GL_MODELVIEW); // Select The Modelview MatrixRenderScene(); // Render your SceneglMatrixMode(GL_PROJECTION); // Select The Projection MatrixglPopMatrix(); // Pop The Projection MatrixglMatrixMode(GL_MODELVIEW); // Select The Modelview Matrixhits=glRenderMode(GL_RENDER); // Switch To Render Mode, Find Out How Manyif (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 1glBegin(GL_QUADS);//vertices for drawer 1glEnd();glLoadName(2);  //everything that is rendered after this command will be known as object 2glBegin(GL_QUADS);//vertices for drawer 2glEnd();//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.
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?
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.
I'll do that...well, at least I'll try to :-)

This topic is closed to new replies.

Advertisement