Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to openGl, i have one task to render some in openGl Offscreen and write the same into JPG or Bmp image here i dont want to show the render data into a openGl window i just need to write into an Image(jpg or BMP).

I google it but unable to find the right solution... i am strucked on this any one please help me on this task...

Thanks in advance.
Posted
Updated 18-Sep-13 21:51pm
v2

I hope image data available in a FrameBuffer, then you can use glReadPixels() to read pixels of the frame buffer( RGB data from the pixel buffer).

glReadPixels( Left, Top, Width, Height, GL_BGR_EXT, GL_UNSIGNED_BYTE, pbyData ); // pbyData should be allocated with 3 * Width * Height to receive RGB data from frame buffer.

Then save this buffer to a file with BMP header.

Here is a sample code, in this application frame buffer contents are saved to a BMP file.
http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=236394
Please refer BMPLoader::SaveBMP() and ZoomInterpolationDlg::OnBnClickedButtonSave()
 
Share this answer
 
Comments
Member 10283916 20-Sep-13 5:59am    
The image data is not available in a FrameBuffer. Can you please provide some small example on the same and the farme buffer object is avilable in which version of the openGl
Santhosh G_ 20-Sep-13 10:04am    
" I have one task to render some in openGl Offscreen", here i can understand that the image is in a framebuffer. Offscreen rendering can be performed with a frame buffer. We can access frame buffer with opengl extensions.
Here is a sample code and details of offscren rendering. http://www.songho.ca/opengl/gl_fbo.html
C++
#include <stdlib.h> 
#include <iostream>
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include 
#include 
#else
#include 
#endif
#include "bitmap_image.hpp"
using namespace std;


//Draws the 3D scene
void drawScene()
{
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective	

	glEnable(GL_DEPTH_TEST);

	static unsigned char texture[3 * 400 * 400];
	static unsigned int texture_id;

	// Texture setting
	glEnable(GL_TEXTURE_2D);
	glGenTextures(1, &texture_id);
	glBindTexture(GL_TEXTURE_2D, texture_id);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 400, 400, 0, GL_RGB,	GL_UNSIGNED_BYTE, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

	glLoadIdentity();
	glTranslatef(0, 0, -10);	

	/* Define a view-port adapted to the texture */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(20, 1, 5, 15);
	glViewport(0, 0, 400, 400);
	glMatrixMode(GL_MODELVIEW);

	/* Render to buffer */
	glClearColor(1, 1, 1, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	
	glColor3f(1.0,0.0,0.0);

	glBegin(GL_TRIANGLES);
	
	glVertex3f( 0.0f, 1.0f, 0.0f);             

	glVertex3f(-1.0f,-1.0f, 0.0f);             

	glVertex3f( 1.0f,-1.0f, 0.0f);             

	glEnd();  
	///glFlush();
	// Image Writing	
	unsigned char* imageData = (unsigned char *)malloc((int)(400*400*(3)));
	glReadPixels(0, 0, 400, 400, GL_RGB, GL_UNSIGNED_BYTE, imageData);
	//write 
	bitmap_image image(400,400);
	image_drawer draw(image);

	for (unsigned int i = 0; i < image.width(); ++i)
	{
		for (unsigned int j = 0; j < image.height(); ++j)
		{
			image.set_pixel(i,j,*(++imageData),*(++imageData),*(++imageData));		
		}
	}

	image.save_image("Trangle_image.bmp");
	///glutSwapBuffers(); 
}

int main(int argc, char** argv)
{
	//Initialize GLUT
	//glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowSize(400, 400); //Set the window size
	//Create the window
	glutCreateWindow("Test");	
	drawScene();
	return 0; //This line is never reached
}</iostream></stdlib.h>



Finally i achived the task, which is working fine.
 
Share this answer
 
v2
Comments
Member 13056933 27-Sep-18 21:52pm    
How can I save images of the loaded buffer, with different Camera position??? Help me please!
you can use copy of screen and paste in to body of jpg or bmp format with GDI+.
 
Share this answer
 
Comments
Member 10283916 19-Sep-13 5:11am    
i dont want to show the wimdow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900