Thread: Simple Color Picker with glReadPixel

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Simple Color Picker with glReadPixel

    I'm trying to read the pixel at the user's cursor when they right click on a triangular gradient, but glReadPixel is only returning black. If I make it a flat color instead of a gradient, it works, so I don't know what's wrong.

    Code:
    glBegin(GL_TRIANGLES);
            glColor3f(1.0f, 0.0f, 0.0f);
            glVertex2i(0, 0);
            
            glColor3f(0.0f, 1.0f, 0.0f);
            glVertex2i(128, 0);
            
            glColor3f(0.0f, 0.0f, 1.0f);
            glVertex2i(64, 128);
        glEnd();
    glColor3f(1.0f, 1.0f, 1.0f);
    //This next part only happens when they right click, but I removed that stuff for now for simplicity. This happens right after the triangle is drawn, though
    GLubyte pixel[4];
    glReadPixels(mPos[0], cheight - mPos[1], 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
    I have lighting and depth_test disabled. I'm using GL_Blend for the shade model and changing it to GL_Flat doesn't fix it. I also tried both glReadBuffer(GL_Front) and glReadBuffer(GL_Back). This project is using SDL_GL but I doubt that's the problem since flat colors get picked just fine.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The only way this will work is if you are using an orthographic projection matrix. If you want to do this in 3D you must fire a ray into 3 space based on the click position of the mouse. Then you can check for ray/triangle collisions in world space or local space. The only important part of this is that the ray and triangle must be in the same space. Once you determine which triangle was hit you can then find the barycentric coordinates of the hit and use these coordinates to address the texture that is being used on the primitive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Week Selection from Date Time Picker
    By gaurav_13191 in forum Windows Programming
    Replies: 2
    Last Post: 10-31-2011, 12:57 PM
  2. How to call a particular random number picker?
    By anon in forum General Discussions
    Replies: 4
    Last Post: 09-06-2011, 03:22 PM
  3. do while loop and modulus question - lotto picker
    By bos1234 in forum C Programming
    Replies: 3
    Last Post: 01-25-2011, 10:23 AM
  4. How to read from date and time picker?
    By vikernes in forum Windows Programming
    Replies: 3
    Last Post: 06-17-2006, 07:10 PM
  5. simple background color question.
    By ... in forum C++ Programming
    Replies: 9
    Last Post: 10-29-2002, 05:12 PM

Tags for this Thread