Sign in to follow this  
billconan

help me with glReadPixels

Recommended Posts

hello guys, i try to render the scene on a texture through glReadPixels to read the frame buffer to the texture. but it turns out that the texture is solid white. i just don't know why. please help. here is the code:
//the buffer used to store the frame buffer
unsigned char *bmp=new unsigned char[1024*768*3];
//set camera
this->mainCamera->updataCamera();
//render something here
glEnable(GL_TEXTURE_2D);
//read the frame buffer, this seems to be right, cause i printed the data that 
//is read in to the buffer bmp
glReadPixels(0,0,1024,768,GL_RGB,GL_UNSIGNED_BYTE,bmp);
//generate the texture
glBindTexture( GL_TEXTURE_2D, render_texture );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_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 );

//read the date from buffer bmp to texture
	 glTexImage2D(GL_TEXTURE_2D,0,3, 1024,768, 0,   GL_RGB,  GL_UNSIGNED_BYTE, bmp);

glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);

		
glColor4ub(255,255,255,255);
//select the texture
glBindTexture(GL_TEXTURE_2D,this->render_texture);
//render the quad with the texture
			glBegin(GL_QUADS);
			
				glTexCoord2i(0,0);
				glVertex3i(500,-500,500);
				glTexCoord2i(1,0);
				glVertex3i(-500,-500,500);
				glTexCoord2i(1,1);
				glVertex3i(-500,-500,-500);
				glTexCoord2i(0,1);
				glVertex3i(500,-500,-500);
				glEnd();

//disable the texture
glBindTexture( GL_TEXTURE_2D, 0 );
		glDisable(GL_TEXTURE_2D);
//delete the buffer
delete bmp;
the scene is a solid white quad without any texture.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This code may be usefull:

AUX_RGBImageRec *LoadBMP(char *fileName) {
FILE *File = NULL;
if (!fileName) {
return NULL;
}
File=fopen(fileName,"r");
if (File) {
fclose(File);
return auxDIBImageLoad(fileName);
}
return NULL;
}

int GLbitmap::loadPicture(string str) {
int Status = FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage, 0, sizeof(void *)*1);
char fileName[str.length()];
for (int i=0; isizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
if (TextureImage[0]) {
if (TextureImage[0]->data) {
free(TextureImage[0]->data);
}
}
return Status;
}

on the other hand, I use glColor4f. glColor4ub have caused some problems to me.

Share this post


Link to post
Share on other sites
1024 x 768 isn't power of two. Do you have NPOT hardware? Otherwise what you read in is not a valid texture and OpenGL turns off texturing -> solid white.

By the way, you can use glCopyTexImage2D for this same purpose and it should be a lot faster.

Share this post


Link to post
Share on other sites
Along with the texture dimensions as deavik mentioned, you're also setting the minification filter to GL_LINEAR_MIPMAP_LINEAR but you aren't supplying any mipmap levels, so the texture will not be complete. Either set the minification filter to GL_LINEAR or GL_NEAREST or supply all levels of the mipmap chain.

And yes, glCopyTexImage2D will be a lot faster because you would be copying from video ram to video ram instead of pulling from the video ram to system ram, then sending it back to video ram.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalidor
And yes, glCopyTexImage2D will be a lot faster because you would be copying from video ram to video ram instead of pulling from the video ram to system ram, then sending it back to video ram.

And glCopyTexSubImage could be even faster, as it will update existing texture with new data instead of (re)creating a new one. And then I haven't even mentioned any of the newer render to texture technique...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this