質問

I want to select 2d object in opengl, but don't know how. I want it like in 3d with gluPickMatrix. This is what i tried:

void initDraw2D(){
    GLuint buff[BUFSIZE];                
    GLint hits, view[4];
    glSelectBuffer(BUFSIZE, buff);
    glGetIntegerv(GL_VIEWPORT, view);
    glRenderMode(GL_SELECT);
    glInitNames();
    glPushName(0);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
        glLoadIdentity();
        gluPickMatrix(mouseX, view[3] - mouseY, 1.0, 1.0, view);
        glMatrixMode(GL_MODELVIEW);
        Draw();
        glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    hits = glRenderMode(GL_RENDER);
    if (onSelect){
    processHits(hits, buff);
    onSelect = false;
    }
    Draw();
    glutPostRedisplay();
}

But it don't select when i click on it.

役に立ちましたか?

解決

One easy method is to render each object with different color. Write a function that returns a 3 dimensional array(vector), and if it hasn't picked yet, select it as the selection color of the object, add to the list of the selecting colors. So now each object has a different color, and you can check the color of the pixel at the cursor's location. Use framebuffers or pbo-s for this. Then make a lookup in the selection list, and return a pointer to the object.(or do whatever you want with it).

Of course it don't have to be rendered on the screen.

it looks like this:(pseudocode)

object* object1 = new object();
object1->createSelectColor();
object1->addColorToList();
...


objectRenderer->renderColoredObjects(/*to the fbo or texture for example*/);
objectRenderer->pickColorAtCursorPos();
objectRenderer->lookUpColorInList(/*objectRenderer->selectedcolor*/);
objectRenderer->setTarget(/*objectRenderer->selectedobject*/);

This is geometry independent. The colors scale from 0-255 in R, G, and B. So that's 255*255*255=16581375 different colors, one for each object.

You can create maps for the color lookups, indexing the objects and colors, create clever color picking function that makes the lookup easy...ect.

This method can be found in the book: Chris Seddon - OpenGL Game Development which is a pretty nice to start.

他のヒント

Can't you just check if mouse cursor is inside bounding rectangle?

class Rectangle
{
    int x, y, w, h;

    bool IsPicked(Point mousePos)
    {
       return ((mousePos.x >= x) && (mousePos.x <= x + w)) 
              && 
              ((mousePos.y >= y) && (mousePos.y <= y + h));
    }
};

(written by heart, not tested =) )

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top