• Advertisement


This topic is now archived and is closed to further replies.

Rendering to a texture

This topic is 5289 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been going thru the tutorial on GameTutorials for using glCopyTexImage2D() to capture data rendered to the backbuffer, into a GL texture for later texture mapping. However, the tutorial indicates doing this realtime in the main renderloop. I''m trying to get this working in such a fashion that the textures are generated once at startup time, then are used statically throughout the lifetime of the application, and I can''t quite get my head around the concept. So far I''ve come up with the following solution:

unsigned int iTexture = 0;
unsigned int iTextureSize = 256;
unsigned int g_iScreenWidth = 1024;
unsigned int g_iScreenHeight = 768;


void PreRender(void)

	// Allocate and upload a texture to memory

	const int iWidth = iTextureSize;
	const int iChannels = 4;
	unsigned char* lpTexture = new unsigned char[iWidth * iWidth * iChannels];
	glGenTextures(1, &iTexture);
	glBindTexture(GL_TEXTURE_2D, iTexture);
	glTexImage2D(GL_TEXTURE_2D, 0, iChannels, iWidth, iWidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpTexture);
	delete [] lpTexture;

	// The mini-''RenderScene'' functionality

	float iViewport = iTextureSize;

	// Set the camera

	gluLookAt(0, 0, 5,	0, 0, 0,	0, 1, 0);

	// Store the current transform on matrix stack


		float x, y, z;
		x = 1;
		y = 0;
		z = 0;

		// Resize the viewport

		glViewport(0, 0, iViewport, iViewport);

		// Render a simple quad

		glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
			glVertex3f(0, 0, z);
			glVertex3f(0, y, z);
			glVertex3f(x, y, z);
			glVertex3f(x, 0, z);

		// Use the premade texture

		glBindTexture(GL_TEXTURE_2D, iTexture);

		// Store the rendered data into the texture

		glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, iViewport, iViewport, 0);

		// Now clear the background to medium grey

		glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

		// Return the viewport to normal size

		glViewport(0, 0, g_iScreenWidth, g_iScreenHeight);




void RenderScene(void)

	// Clear the screen

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	// Reset the matrix stack


	// Set the camera

	gluLookAt(0, 0, 10,	0, 0, 0,	0, 1, 0);

	// Set the prerendered texture

	glBindTexture(GL_TEXTURE_2D, iTexture);

	// Draw a quad

		glTexCoord2f(0, 0);
		glVertex3f(0, 0, 0);
		glTexCoord2f(0, 1);
		glVertex3f(0, 1, 0);
		glTexCoord2f(1, 1);
		glVertex3f(1, 1, 0);
		glTexCoord2f(1, 0);
		glVertex3f(1, 0, 0);

	// Update the screen




The z''s might be a little off, but you get the idea. Am I on the right track? Any help is appreciated. This code is untested, I''ll give it a go when I get home, but any ideas are appreciated. On a side note, using extensions and Windows-specific functions are not an option, as I''m trying to use the same code on Mac OS 8/9/X and Windows. MatrixCubed

Share this post

Link to post
Share on other sites
I''ve only quickly read the code but it seems correct.
- Render your scene at initialisation time, copy it to your texture.
- After this, no glCopy* calls are needed in the render loop, you just use the previously generated texture.

Share this post

Link to post
Share on other sites

  • Advertisement