Picking

Hi guys!

I’d like to render a red triangle which should change its color to green if I click on it with the left mousebutton.

I want to generate the triangle in a method called drawObjects().

The method looks like this at the moment:



    private static void drawObjects(int mode)
    {
        float[] red = { 1.0f, 0.0f, 0.0f, 1.0f };
        float[] blue = { 0.0f, 0.0f, 1.0f, 1.0f };
              
        Gl.glMatrixMode(Gl.GL_MODELVIEW);
        Gl.glLoadIdentity();
        Gl.glInitNames();
        Gl.glPushName(0);

        Gl.glPushMatrix();
            /*
            insert object names for selection
            set colors for rendering (active var)

            ....
            */

        ///red triangle
        Gl.glColor3f(1, 0, 0);
        Gl.glLoadName(0);
        Gl.glTranslatef(0.0f, 0.0f, -10.0f);
        Gl.glBegin(Gl.GL_TRIANGLES);
            Gl.glVertex3f(-0.5f, 1f, 0f);
            Gl.glVertex3f(0.5f, 1f, 0f);
            Gl.glVertex3f(0f, 2f, 0f);
        Gl.glEnd();

        ///green triangle
        Gl.glColor3f(0, 1, 0);
        Gl.glLoadName(1);
        Gl.glBegin(Gl.GL_TRIANGLES);
        Gl.glVertex3f(-0.5f, 1f, 0f);
        Gl.glVertex3f(0.5f, 1f, 0f);
        Gl.glVertex3f(0f, 2f, 0f);
        Gl.glEnd();

        Gl.glPopMatrix();
        Glut.glutSwapBuffers();
    }

I’d like to know if it is right to generate two different triangles with different names this situation.

Cheers,

enne

A better method overall would be to have one triangle and change it’s colour when you click on it. Overall in any 3D system less is more. So if you can get away with drawing one set of geometry instead of two without any loss of image quality you should certainly do it IMO.

With regards to picking I actually don’t use it any more. Picking is deprecated AFAIK in OpenGL3.x, and these days the overhead associated with picking on the GPU is far heavier than doing ray-triangle intersections on the CPU with your geometry and gluUnProject… I also think it’s quite a lot more complex as you have to setup modes for your drawing routine, or have two different drawing routines, and all of this increases data transfer between you and the GPU to effectively interrogate data you already have to hand on the client side!

So using my method you’d only be “drawing” your scene once, and doing some maths on it for collision once. All in all you’d be drawing 1 triangle.

Using your current method you’d be “drawing” two triangles twice. Once to draw them and once again to pick with them!

Hence my personal preference for doing it on the CPU…

This is a handy reference:
http://www.opengl.org/resources/faq/technical/selection.htm

I only say all this because you are working your way through this now and learning what I feel is a bad method, so it might be better to change tack at this point and learn a more flexible method instead.

Hey scratt!

Thanks for your help. Your recommendation sounds good. The problem is that I have to study picking for university, so I’m forced to learn it with the worse alternative.

So can you tell me please how I can change the color by drawing only one triangle?

I would keep a boolean (or other useful variable) to track whether the triangle should be red or green. If the variable indicates red, use gl.glColor4fv(red). If green, use gl.glColor4vf(green).

Ok, thanks todayman :slight_smile: