Skip to main content
GameDev.net gamedev.net
🔒 Locked

help me with glReadPixels

Started by billconan Apr 23, 2006 at 12:04 AM 5 replies 1k views
Original Post
billconan
billconan
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.
falledAngel
falledAngel
well I forget login xD

I had problems using glColorub(...) because it showed me only the textures with the last valid configuration color.
I spent most of my money on booze, women and cars. The rest I just wasted. George Best
billconan
billconan
i don't get u
deavik
deavik
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.
Kalidor
Kalidor
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.
Brother Bob
Brother Bob
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...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.