PDA

View Full Version : Offscreen texture rendering to bitmap



Tyrahell
04-01-2010, 07:42 AM
Regards,

I'm trying to create an offscreen rendering on my OpenGL app. To get an offscreen rendering, i store my render into a texture and i wish to export this texture to an image. After one day reading tutorial about FBO, DIB, Offscreen rendering & Render to texture, im stuck at the "texture to image" step. Please, can you help me to save my offscreen rendered texture to a bitmap ?




///////////////////////////////////////////////////////////////////////////////
// Define static variable
static unsigned char texture[3 * SIZE * SIZE];
static unsigned int texture_id;
static int window_width = 512;
static int window_height = 512;

// OpenGL settings
glEnable(GL_DEPTH_TEST);

// Texture setting
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 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);
glRotatef(30, 1, 0, 0);

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

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

//DrawCube
glBegin(GL_LINE_LOOP);

glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 0); glVertex3f(-1, 1, -1);

glTexCoord2i(0, 0); glVertex3f( 1, -1, -1);
glTexCoord2i(0, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);

glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, -1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, -1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

glTexCoord2i(0, 0); glVertex3f(-1, 1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, 1, -1);

glTexCoord2i(0, 0); glVertex3f(-1, -1, -1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, -1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, -1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, -1);

glTexCoord2i(0, 0); glVertex3f(-1, -1, 1);
glTexCoord2i(0, 1); glVertex3f(-1, 1, 1);
glTexCoord2i(1, 1); glVertex3f( 1, 1, 1);
glTexCoord2i(1, 0); glVertex3f( 1, -1, 1);

glEnd();
//Ending texture
glFlush();

/* Copy buffer to texture */
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 5, 5, 0, 0, SIZE - 10, SIZE - 10);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

This code create texture like this (render on the different side of my cube) :

http://j.imagehost.org/0367/texture.jpg

How can i export this texture to a bitmap ?

++Tyra :sorrow:

ZbuffeR
04-01-2010, 08:28 AM
Either glReadPixels from a framebuffer/FBO, or glGetTexture if you already have built a texture from your fbo.
http://www.opengl.org/sdk/docs/man/xhtml/glGetTexImage.xml