I am trying to write a GPGPU program, by just copying contents of a texture to an output texture attached to FBO. however, I see that the input texture data is not being loaded properly/ not binded properly. I couldnt understand whats wrong.
here is my code :
void render()
{
int i;
float * result, *in;
//allocating and filling data in the input texture
result = (float * ) malloc(4*32*32*sizeof(float));
in = (float *) malloc(4*32*32*sizeof(float));
for(i=0;i<32*32*4; i++)
{
in[i]=1.0f;
}
makeBuffers(); //makes the vertex and element buffers
makeTexture(in); //makes the input textures
makeShaders(); //makes the vertex n pixel shaders
makeProgram(); //makes the program
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, res.vertexBuffer);
glVertexAttribPointer(res.position, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, (void*)0);
glEnableVertexAttribArray(res.position);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, res.elementBuffer);
res.texture= glGetUniformLocation(res.shaderProgram, "intexture");
/* bind texture */ makefbo(); // making the offscreen frame buffer object
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, res.texture);
glUniform1i(res.texture, 0);
glDrawElements(GL_TRIANGLE_STRIP,4, GL_UNSIGNED_SHORT, (void*)0);
// glFlush(); // i think drawing would be enough now.
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); //reading back data from the framebuffer
glReadPixels(0, 0, 32, 32,GL_RGBA,GL_FLOAT,result);
printf(" float values \n");
for(i=0; i< 32*4 ; i++)
printf(" %f ", result[i]);
printf(" \n");
}
and here is my maketexture() function
glGenTextures (1, &res.texture);
glBindTexture(GL_TEXTURE_2D,res.texture);
// set texture parameters
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP);
// define texture with floating point format
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32F,
texSize,texSize,0,GL_RGBA,GL_FLOAT,0);
// transfer data to texture
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,texSize,texSize,
GL_RGBA,GL_FLOAT,data);
and here is my framebufferoject preparation and output texture biding :
glGenFramebuffers(1, &res.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, res.fbo);
glViewport(0, 0, 32, 32);
glGenTextures(1, &res.tex);
glBindTexture(GL_TEXTURE_2D, res.fbo);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
32, 32, 0, GL_RGBA, GL_FLOAT, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, res.tex, 0);
the data does not seem to be getting read properly. I tried to attach the input texture to a framebuffer object, and try to read back the texture data immediately, and the data gets displayed fine then. but when I try to render to an offscreen texture, it doesnot give correct output. It gives just a bunch of zeros at the end of pixel shader execution.
pixel shader takes the input texture and copies the data into the output texture : outColor = texture2D (intexture, texcoord );
could somebody see any problem with this?