Results 1 to 8 of 8

Thread: Screenshot of opengl window

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    5

    Screenshot of opengl window

    Need to take simple screenshot of the opengl window and save it to file.
    Please, post example code in c++.

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

    Re: Screenshot of opengl window

    just use glReadPixels to retrieve raw data.
    http://www.opengl.org/sdk/docs/man/x...ReadPixels.xml

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    5

    Re: Screenshot of opengl window

    Ok, i get the data. Now how to save it to file? I'm sorry, i'm absolutely zero in c++.

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

    Re: Screenshot of opengl window

    I use this method to write a PPM file, maybe it can help you

    Code :
    void Image::writePPM( std::ostream& out ) {
    	// Escreve o cabeçalho do arquivo
    	out << "P6" << endl;
    	out << width << ' ' << height << endl;
    	out << "255" << endl;
     
    	// Escreve os valores ajustados para o intervalo [0,255]
    	// O valor do canal aplha é ignorado
    	for ( unsigned int idx = 0; idx < this->getNumberOfPixels(); idx++ ) {
     
    		unsigned int iRed = (unsigned int) ( 256 * raster[idx].r );
    		unsigned int iGreen = (unsigned int) ( 256 * raster[idx].g );
    		unsigned int iBlue = (unsigned int) ( 256 * raster[idx].b );
     
    		if ( iRed > 255 )
    			iRed = 255;
    		if ( iGreen > 255 )
    			iGreen = 255;
    		if ( iBlue > 255 )
    			iBlue = 255;
     
    		unsigned char red = (unsigned char) iRed;
    		unsigned char green = (unsigned char) iGreen;
    		unsigned char blue = (unsigned char) iBlue;
     
    		out.put( red );
    		out.put( green );
    		out.put( blue );
    	}
    }

    Comments are in portuguese, but you can take the idea

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    5

    Re: Screenshot of opengl window

    I succeeded make a screenshot with SDL library:

    Code :
    SDL_Surface * image = SDL_CreateRGBSurface(SDL_SWSURFACE, current_w, current_h, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
     
    glReadBuffer(GL_FRONT);
    glReadPixels(0, 0, current_w, current_h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
     
    SDL_SaveBMP(image, "pic.bmp");
    SDL_FreeSurface(image);

    But it was all black))) My size but all black. Why and how to fix it?

    UPD: I found - need to remove glReadBuffer(GL_FRONT). But now screenshot is upside down... How to fix IT?)

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

    Re: Screenshot of opengl window

    The system's origin of an OpenGL window is in the bottom left corner, while a bitmap has its origin in the upper left corner.

    All you need to do is to invert the y coords of your pixels.
    How this can be done with SDL? I have no idea.

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    5

    Re: Screenshot of opengl window

    I found:

    Code :
    int index;
    void* temp_row;
    int height_div_2;
     
    temp_row = (void *)malloc(image->pitch);
    if(NULL == temp_row)
    {
    	SDL_SetError("Not enough memory for image inversion");
    }
    height_div_2 = (int) (image->h * .5);
    for(index = 0; index < height_div_2; index++)    
    {
    	memcpy((Uint8 *)temp_row,(Uint8 *)(image->pixels) + image->pitch * index, image->pitch);
    	memcpy((Uint8 *)(image->pixels) + image->pitch * index, (Uint8 *)(image->pixels) + image->pitch * (image->h - index-1), image->pitch);
    	memcpy((Uint8 *)(image->pixels) + image->pitch * (image->h - index-1), temp_row, image->pitch);
    }
    free(temp_row);

    Now all works fine. Thanks for help, McLeary.

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

    Re: Screenshot of opengl window

    No problem

Similar Threads

  1. Need help to take screenshot of openGL app in other app (C++)
    By Alex-cat in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-27-2015, 03:37 PM
  2. Screenshot with OpenGL
    By WolfG in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-22-2012, 01:19 AM
  3. Vista Opengl Screenshot
    By gocca in forum OpenGL: Windows
    Replies: 6
    Last Post: 12-02-2009, 04:46 AM
  4. Taking a screenshot of a moving object and antialias the screenshot
    By pmatos in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 11-24-2003, 06:43 AM
  5. Screenshot from an OpenGL Window
    By stryx in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-01-2002, 03:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean