Results 1 to 5 of 5

Thread: Picking

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    16

    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:
    Code :
     
        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

  2. #2
    Senior Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    555

    Re: Picking

    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/.../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.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    16

    Re: Picking

    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?

  4. #4
    Senior Member Regular Contributor
    Join Date
    Dec 2008
    Location
    USA
    Posts
    134

    Re: Picking

    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).

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2009
    Posts
    16

    Re: Picking

    Ok, thanks todayman

Similar Threads

  1. Z-buffer picking, gluUnProject picking not working
    By Icetigris in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-29-2008, 10:38 AM
  2. Picking in 3D - plz help
    By beautiful world in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-30-2002, 10:39 AM
  3. Picking
    By imported_Dominik in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-13-2002, 01:03 PM
  4. picking again.....
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-15-2002, 06:34 AM
  5. picking
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-14-2002, 05:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean