Jump to content
  • Advertisement

Archived

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

MatrixCubed

Rendering to a texture

This topic is 5478 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);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	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

	glPushMatrix();

		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);
		glBegin(GL_QUADS);
			glVertex3f(0, 0, z);
			glVertex3f(0, y, z);
			glVertex3f(x, y, z);
			glVertex3f(x, 0, z);
		glEnd();

		// 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);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Return the viewport to normal size

		glViewport(0, 0, g_iScreenWidth, g_iScreenHeight);

	glPopMatrix();

}

////////////////////////////////////////////////////////////////////////////////


void RenderScene(void)
{

	// Clear the screen

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Reset the matrix stack

	glLoadIdentity();

	// 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

	glBegin(GL_QUADS);
		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);
	glEnd();

	// Update the screen

	glutSwapBuffers();

}

////////////////////////////////////////////////////////////////////////////////


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
http://MatrixCubed.cjb.net

Share this post


Link to post
Share on other sites
Advertisement
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!