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 8138213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:25:16+00:00 2026-06-06T11:25:16+00:00

glViewport ( 0, 0, 320, 480 ); byte* memory glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT,

  • 0
glViewport ( 0, 0, 320, 480 );
byte* memory 
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 1 );
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, memory);

I want to convert the byte array, which is read from the frame buffer using glreadpixel(), to any image of PNG/JPEG:

The printf statement shows that glreadpixel() is reading the memory correctly.
In what way can I write the image into to a file, e.g: “C:\image.jpg”?

  • 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-06-06T11:25:18+00:00Added an answer on June 6, 2026 at 11:25 am

    OpenGL (ES included) accepts only the RAW texture data (that is, RGB triples, RGBA quads or some other predefined format) or some data with s3tc (S3 Texture Compression), but not the file formats. Same with the output.

    EDIT:

    To be sure that the elements actually appear in the framebuffer, glFlush() call must be done right before the glReadPixels() call. It will force the update of the buffer.

    Addition:

    If all you need is to dump the buffer contents to the file, I would suggest you to use the .bmp file format. It’s only a 54-byte header at the beginning and then the data which is read directly from the OpenGL (watch for GL_UNPACK_ALIGNMENT though).

    Here’s a WriteBMP() function for you:

    void WriteBMP(const char *fname, int w,int h,unsigned char *img)
    {
        FILE *f = fopen(fname,"wb");
    
        unsigned char bfh[54] = {0x42, 0x4d,
        /* bfSize [2]*/ 54, 0, 0, 0, /**/
        /* reserved [6]*/ 0, 0, 0, 0, /**/
        /* biOffBits [10]*/ 54, 0, 0, 0, /**/
        /* biSize [14]*/ 40, 0, 0, 0, /**/
        /* width [18]*/ 0, 0, 0, 0, /**/
        /* height [22]*/ 0, 0, 0, 0, /**/
        /* planes [26]*/ 1, 0, /**/
        /* bitcount [28]*/ 24, 0,/**/
        /* compression [30]*/ 0, 0, 0, 0, /**/
        /* size image [34]*/ 0, 0, 0, 0, /**/
        /* xpermeter [38]*/ 0, 0, 0, 0, /**/
        /* ypermeter [42]*/ 0, 0, 0, 0, /**/
        /* clrused [46]*/ 0, 0, 0, 0, /**/
        /* clrimportant [50]*/ 0, 0, 0, 0 /**/};
        int realw = w * 3, rem = w % 4, isz = (realw + rem) * h, fsz = isz + 54;
        //bfh.bfSize = fsz;
        bfh[2] = (fsz & 0xFF); bfh[3] = (fsz >> 8) & 0xFF; bfh[4] = (fsz >> 16) & 0xFF; bfh[5] = (fsz >> 24) & 0xFF;
        //bfh.biSize = isz
        bfh[34] = (isz & 0xFF); bfh[35] = (isz >> 8) & 0xFF; bfh[36] = (isz >> 16) & 0xFF; bfh[37] = (isz >> 24) & 0xFF;
        //bfh.biWidth = w;
        bfh[18] = (w & 0xFF); bfh[19] = (w >> 8) & 0xFF; bfh[20] = (w >> 16) & 0xFF; bfh[21] = (w >> 24) & 0xFF;
        //bfh.biHeight = h;
        bfh[22] = (h & 0xFF); bfh[23] = (h >> 8) & 0xFF; bfh[24] = (h >> 16) & 0xFF; bfh[25] = (h >> 24) & 0xFF;
    
        // xpels/ypels
        // bfh[38] = 19; bfh[39] = 11;
        // bfh[42] = 19; bfh[43] = 11;
    
        fwrite((void*)bfh, 54, 1, f);
    
        unsigned char* bstr = new unsigned char[realw], *remstr = 0; 
        if(rem != 0) { remstr = new unsigned char[rem]; memset(remstr,0,rem); }
    
        for(int j = h-1 ; j > -1 ; j--){
                for(int i = 0 ; i < w ; i++)
                        for(int k = 0 ; k < 3 ; k++) { bstr[i*3+k] = img[(j*realw+i*3)+(2-k)]; }
                fwrite(bstr,realw,1,f); if (rem != 0) { fwrite(remstr,rem,1,f); }
        }
    
        delete [] bstr; if(remstr) delete [] remstr;
    
        fclose(f);
    }
    

    Then just do the call:

    WriteBMP("C:\\image.bmp", 320, 480, memory);
    

    If you really need the .jpg/.png file, you might convert the resulting .bmp file or use the FreeImage library or the libjpng/libpng themselves. They all deal with specific file formats.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got my openGL view at a size of (lets say..) 800(width)x600(height). Then i
I'm getting some strange values for the width/height in the following code, resulting in
When I issue glViewport with my new resolution it does not seem to update
I'm wanted to convert some of my python code to C++ for speed but
I have an orthogonal perspective which I initialize like so: gl.glViewport(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
I seem to have a curious problem. I set up OpenGL like this: glViewport(0,
I have a problem with glViewport. In my liitle programm i have two viewports.
I set up a scene using: glViewport(0,0,screen->w,screen->h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); const float wh = (float)screen->w/(float)screen->h;
On OpenGL-ES i'm confused on what the difference is between setting glOrthof() glViewPort() GLU.gluOrtho2D()
So I am trying to learn how to use glViewport(), MULTIPLE TIMES. Below is

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.