Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:21:56+00:00 2026-05-24T05:21:56+00:00

I wish to capture an image I rendered rendered in openGL. I use glReadPixels

  • 0

I wish to capture an image I rendered rendered in openGL. I use glReadPixels and then save the image with CImg. Unfortunately, the result is wrong. See below. The image on the left is correct. I captured it with GadWin PrintScreen. The image on the right is incorrect. I created it with glReadPixels and CImg:

enter image description here
enter image description here

I’ve done a lot of Web research on what might be wrong, but I’m out of avenues to pursue. Here the code the captures the image:

void snapshot() {
    int width = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);
    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
    glPixelStorei(GL_PACK_ALIGNMENT, 1);
    int bytes = width*height*3; //Color space is RGB
    GLubyte *buffer = (GLubyte *)malloc(bytes);

    glFinish();
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
    glFinish();

    CImg<GLubyte> img(buffer,width,height,1,3,false);
    img.save("ScreenShot.ppm");
    exit(0);
}

Here is where I call the snapshot method:

void display(void) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        drawIndividual(0);
        snapshot();
        glutSwapBuffers();
    }

Following up on a comment, I grabbed the bit depth and printed to the console. Here are the results:

redbits=8
greenbits=8
bluebits=8
depthbits=24
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:21:58+00:00Added an answer on May 24, 2026 at 5:21 am

    CImg does not interleave its colors. That is, 3 red, green, blue pixels would be stored linearly as:

    R1, R2, R3, …, G1, G2, G3, …, B1, B2, B3, …

    However, OpenGL’s glReadPixel and glDrawPixel expect interleaved color components like this:

    R1, G1, B1, R2, G2, B2, …

    Additionally, OpenGL puts the origin (0,0) at the lower left corner of an image. CImg uses the more approach where the origin is at the top left of the image.

    Here is a routine I wrote convert the interleaved colors to CImg’s channel-oriented format.

    void convertToNonInterleaved(int w, int h, unsigned char* tangled, unsigned char* untangled) {
        //Take string in format R1 G1 B1 R2 G2 B2... and re-write it 
        //in the format format R1 R2 G1 G2 B1 B2... 
        //Assume 8 bit values for red, green and blue color channels.
        //Assume there are no other channels
        //tangled is a pointer to the input string and untangled 
        //is a pointer to the output string. This method assumes that 
        //memory has already been allocated for the output string.
    
        int numPixels = w*h;
        int numColors = 3;
        for(int i=0; i<numPixels; ++i) {
            int indexIntoInterleavedTuple = numColors*i;
            //Red
            untangled[i] = tangled[indexIntoInterleavedTuple];
            //Green
            untangled[numPixels+i] = tangled[indexIntoInterleavedTuple+1];
            //Blue
            untangled[2*numPixels+i] = tangled[indexIntoInterleavedTuple+2];
        }
    }
    

    I should have probably added the code to account for the change in origin, but I felt lazy and decided to use CImg to do that. Once this routine is run, this code creates the image object:

    unsigned char* p = (unsigned char *)malloc(bytes);
    convertToNonInterleaved(width, height, buffer, p);
    CImg<unsigned char> img(p, width, height,1,3);
    free(p);
    img.mirror('y');
    CImgDisplay main_disp(img,"Snapshot");
    while (!main_disp.is_closed() ) {
        main_disp.wait();
    }
    

    And as far as I can tell, I do not have to set any packing parameters. Here is the code I use to grab the pixels from the OpenGL render target:

    bytes = width*height*3; //Color space is RGB
    if(buffer) 
        free(buffer);
    buffer = (GLubyte *)malloc(bytes);
    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to capture data from the views in the activity, then pause it,
I wish to use xml and xsl to generate controls on an asp.net page.
I would like to capture an image from the camera on Android, and send
I'm capturing images using this code #pragma mark - image capture // Create and
I wish to use Google custom search and in particular would like to have
I have been working on this regex: {link=([^|{}]+)\||([^|{}]+)\||([^|{}]+)} I wish to capture any non-pipe
I wish to write a small tool that will capture a global event when
I wish the UIPIckerView would allow you to scroll then select but since it
My problem is that i wish to capture the text that is displayed in
I wish Subversion had a better way of moving tags. The only way that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.