1

how to handle clik on specific 3d or 2d object in opengl, for example i have the following code

void Widget::drawCircle(float radius) {
    glBegin(GL_TRIANGLE_FAN);
    for (int i = 0; i < 360; i++) {
        float degInRad = i*DEG2RAD;
        glVertex2f(cos(degInRad) * radius, sin(degInRad) * radius);
    }
    glEnd();
}

So i need to handle click on this circle, is there any solutions for this problem?

KillianDS
  • 16,936
  • 4
  • 61
  • 70
user1280078
  • 71
  • 1
  • 7

2 Answers2

2

When I need to detect clicks, I usually do my ordinary draw loop, but instead of drawing the objects with texturing, lighting and other effects enabled, I draw each of them with flat/no shading, each in a different color. I then check the color on the pixel the mouse is on, and map backwards from the color returned from the framebuffer to the object that I drew with that color.

Perhaps this technique is useful for you, too.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • Yes, i thout about this solution but i need to have same color for all objects, thank – user1280078 Mar 26 '12 at 08:06
  • 1
    @user1280078: Why? You don't have to actually *show* the user the picture with the colored objects. You render it so that you can read the pixel, then clear the screen (without swapping buffers) and render what you want the user to see. – Nicol Bolas Mar 26 '12 at 08:09
1

Take a look into this nehe tutorial item. It is very complex, but it shows how opengl picking works. In my opinion, if you need it, you are better with some game engine then with opengl.

Here is another (similar) way of selecting items in opengl.

opengl mouse raytracing will provide you with all details how to select items in opengl. This thread even provides the code how it is done :

Vector3 World::projectedMouse(float mx, float my){

    GLdouble model_view[16];
    GLint viewport[4];
    GLdouble projection[16];

    GLfloat winX, winY, winZ;
    GLdouble dx, dy, dz;

    glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    winX = (float)mx;
    winY = (float)viewport[3] - (float)my;

    glReadPixels ((int)mx, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); 
    gluUnProject(winX, winY, 0, model_view, projection, viewport, &bx, &by, &bz);
    Vector3 pr2 = Vector3(bx, by, bz);

    glColor3f(1,0,0);
    glBegin (GL_LINE_LOOP); 
    glVertex3f(player->getPosition().x, player->getPosition().y + 100, player->getPosition().z); // 0
    glVertex3f(pr.x,pr.y,pr.z); // 1
    glVertex3f(player->getPosition().x, player->getPosition().y, player->getPosition().z); // 0
    glEnd();

    return pr;
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • i've seen this tutorial http://schabby.de/picking-opengl-ray-tracing/, and i think it's that thing what i'm searchin do you have source code of this example? – user1280078 Mar 26 '12 at 08:27
  • @user1280078 I don't have source code, but you should also take a look into [this question](http://stackoverflow.com/questions/2093096/implementing-ray-picking) – BЈовић Mar 26 '12 at 08:34