PDA

View Full Version : FBO



kewuwsi
02-04-2008, 12:32 PM
following is my frame buffer obj testing code:
got confused why the outputs are different? can anybody give a clue? tks alot.

// FBO1.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <GL/glew.h>
#include <GL/glut.h>

int main(int argc, char **argv) {
// declare texture size, the actual data will be a vector
// of size texSize*texSize*4
int texWidth = 2;
int texHeight = 4;
// create test data
unsigned char* data = (unsigned char*)malloc(texWidth*texHeight*sizeof(unsigned char));
unsigned char* result = (unsigned char*)malloc(4*texWidth*texHeight*sizeof(unsigned char));
for (int i=0; i<texWidth*texHeight; i++)
data[i] = (unsigned char)(i+1);
// set up glut to get valid GL context and
// get extension entry points
glutInit (&argc, argv);
glutCreateWindow("TEST1");
glewInit();
// viewport transform for 1:1 pixel=texel=data mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,texWidth,0.0,texHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,texWidth,texHeight);
// create FBO and bind it (that is, use offscreen render target)
GLuint fb;
glGenFramebuffersEXT(1,&fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fb);
// create texture
GLuint tex;
glGenTextures (1, &tex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,tex);
// set texture parameters
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,
GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,
GL_TEXTURE_WRAP_T, GL_CLAMP);
// define texture with floating point format
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_RGBA,
texWidth,texHeight,0,GL_LUMINANCE,GL_UNSIGNED_BYTE ,0);
// attach texture
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_RECTANGLE_ARB,tex,0);

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
assert(status == GL_FRAMEBUFFER_COMPLETE_EXT);
// transfer data to texture
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,0,0,0,tex Width,texHeight,
GL_LUMINANCE,GL_UNSIGNED_BYTE,data);
// and read back
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glReadPixels(0, 0, texWidth, texHeight,GL_LUMINANCE,GL_UNSIGNED_BYTE,result);
// print out results
printf("Data before roundtrip:\n");
for (int i=0; i<texWidth*texHeight; i++)
printf("%u\n",data[i]);
printf("Data after roundtrip:\n");
for (int i=0; i<texWidth*texHeight; i++)
printf("%u\n",result[i]);
// clean up
free(data);
free(result);
glDeleteFramebuffersEXT (1,&fb);
glDeleteTextures (1,&tex);
return 0;
}

-NiCo-
02-04-2008, 01:25 PM
First of all, try putting



glPixelStorei(GL_PACK_ALIGNMENT,1);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);


right after glewInit();

The values in result should be the same as the values in data multiplied by 3. this is because the luminance values get replicated to the R,G and B channels when calling glTexSubImage and the R,G and B values are summed when retrieving luminance values with readpixels.

N.

kewuwsi
02-04-2008, 02:45 PM
great! exactly as you predicted: