简体   繁体   中英

opengl glReadPixels

I want to get color of a pixel. The pixel is mouse position. I use glReadPixels but i can't

    POINT pt;
    GetCursorPos(&pt);
    unsigned char pixel[3];
    glReadPixels(pt.x, pt.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

After this codes value of pixel is: 'Ì' any idea?

204 is CC in hex representation. This value is often used to fill non-initialized memory. If you'll initialize pixel with zero (for example)

 unsigned char pixel[3] = {0};

99,(9)% you'll see zero after call to glReadPixels . Depending on documentation glReadPixels

If an error is generated, no change is made to the contents of data.

that is your data in pixel was not changed because of error. Follow @OlegTitov's fourth advice (look for what glGetError(); will tell you)

Upd: If you want to get a pixel value from the main screen using only glReadPixels , and if you didn't create any GLFrameBuffer, I'm not sure, but I think you'll fail. I'll repeat - I'm not sure, but I think, that glReadPixels can read pixel values only from frame buffers, that was previously created by gl-functions

  1. When outputting char to console, compiler will print as a symbol, not as letter. This is done for C style strings could be printed normal way. To print integer value first cast variable to integer type.

  2. When reading from screen: Note that openGL's coordinate origin is bottom left corner while window systems use upper left corner, so you need to convert from one coordinate system to another

    glReadPixels(pt.x, window_height - pt.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

  3. If you experience further problems make sure that correct pixel buffer is bound as read beffer. For window output:

    glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

  4. If still have some problems start checking your code with glGetError();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM