Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Converting colors

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    4

    Converting colors

    Update: I'm getting close. Now, the output colors are exactly half what they should be... If i put in
    glColorf(90/255f, 40/255f, 133/255f)

    It outputs R: 45 G: 20 B: 66

    Now my code looks like this:

    Code :
    ByteBuffer pRGB = ByteBuffer.allocateDirect(12);
     
     
    GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_BYTE, pRGB);
     
    pRGB.rewind();
     
     
    System.out.print("R:");
    System.out.print((pRGB.get()  & 0xff  ));
    System.out.print("G:");
    System.out.print((pRGB.get() & 0xff));
    System.out.print("B:");
    System.out.print((pRGB.get()  & 0xff));


    OLD:
    So I'm still working on color picking. Now the problem comes down to a color conversion problem. I set each primitive to a unique color using:

    glColor3f(glColor[0]/255f,glColor[1]/255f, glColor[2]/255f) where glColor is an array of ints with each cell between 0 and 255.

    Getting the pixel data looks like this:

    Code :
    //Disable anything that could affect color
    GL11.glDisable(GL11.GL_TEXTURE_2D);
    GL11.glDisable(GL11.GL_FOG);
    GL11.glDisable(GL11.GL_LIGHTING);
    GL11.glDisable (GL11.GL_BLEND);
    GL11.glDisable (GL11.GL_DITHER);
    GL11.glDisable (GL11.GL_TEXTURE_1D);
    GL11.glShadeModel (GL11.GL_FLAT);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
     
     
    //Set up buffer to store pixel data
    ByteBuffer pRGB = ByteBuffer.allocateDirect(12);
    FloatBuffer fRGB = pRGB.asFloatBuffer();
     
     
    //Render primitives with unique color
    RenderColors()
     
     
     
     
    //Read pixel
    GL11.glReadPixels( WindowWidth,WindowHeight-y, 1, 1, GL11.GL_RGB, GL11.GL_FLOAT, fRGB);
     
    //Set buffer back to start 			
    pRGB.rewind();
    fRGB.rewind();
     
    //Output pixel color    			
    System.out.print("R:");
    System.out.print((int)(pRGB.get() & 0xff));
    System.out.print("G:");
    System.out.print((int)(pRGB.get() & 0xff));
    System.out.print("B:");
    System.out.print((int)(pRGB.get() & 0xff));

    Help?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Nov 2010
    Location
    Brazil, Rio de Janeiro
    Posts
    147

    Re: Coverting colors

    I'm not sure if I got the point but, why are you using GL_FLOAT on glReadPixels if your color are set up using unsigned byte?

  3. #3
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    965

    Re: Coverting colors

    Shouldn't this:
    Code :
    GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_BYTE, pRGB);
    Actually be:
    Code :
    GL11.glReadPixels(x, 600-y, 1, 1, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, pRGB);
    ?

    By the way, if performance is important for this you shouldn't be using GL_RGB for your glReadPixels. Use GL_BGRA instead.

  4. #4
    Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    250

    Re: Coverting colors

    glColorf(90/255f, 40/255f, 133/255f)

    You could just pass
    glColor3uf(90,40,133);

  5. #5
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Coverting colors

    you meant glColor3ub (instead of glColor3uf).

  6. #6
    Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    250

    Re: Coverting colors

    yeah !

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2011
    Posts
    4

    Re: Coverting colors

    Any other way besides using unsigned? I'm using Java, that isn't an option.

  8. #8
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: Coverting colors

    just convert to signed :
    Code :
    int unsignedByteValue = 255;
    byte signedByteValue;
    if (unsignedByteValue >= 128 ) {
     signedByteValue = unsignedByteValue - 256;
    } else {
     signedByteValue = (byte) unsignedByteValue;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •