Advertisement

Adapting Lesson 36 (Rendering to a Texture)

Started by October 20, 2004 02:53 AM
0 comments, last by DJKaotica 20 years, 1 month ago
Oiy, been having so many issues with this. (sorry, couldn't resist using a bit of UML...learning it in University right now...hated when I started, starting to see the uses now, all of this is coded in C++ in Visual Studio though) So I create a class:

GLTexture
{
   //attributes
   textureid //given by OpenGL
   width
   height

   //operations
   Draw(x: int, y: int, ...)
   CopytoTexture()
   New(width: int, height: int)
   LoadBMP(filename: char*)
}
LoadBMP is working just fine, uses code from Lesson 6, tested with the NeHe Texture from lesson 6. Draw is also working just fine, tested after loading the texture. Draw has been tested to draw the same texture at numerous points, this also works fine. Draw draws a quad using 4 glVertex2i's with the x, x+width, y, and y+height values. Will use the default values or take width, height values, and 4 floats for the coords on the texture to use. Everything seems to draw just fine, however when I try to CopytoTexture(), the texture copied to is just black...or empty.
void GLTexture::CopytoTexture()
{
		glBindTexture(GL_TEXTURE_2D, ID());
		glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
}
Here's my DrawGLScene function, direct from NeHe's code. tex = GLTexure with NeHe.bmp image loaded testdtt = GLTexture created with New(x, y) to write a texture to (256x256).
int DrawGLScene(GLvoid)
{

	glLoadIdentity();
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glViewport(0,0,256,256);
	tex.Draw(10, 10); //draws the NeHe.bmp logo, full size, at 10,10 (from bottom left)
	glFlush();
	testdtt.CopytoTexture();
	glViewport(0,0,640,480);

	//This is the second glClear function
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	testdtt.Draw(100,0);

	glFlush();

	return TRUE;										// Everything Went OK
}
Now, if I comment out the second glClear function, and the glViewport functions, the image is drawn fine. If I leave the glViewport functions, the image is squished when the viewport is changed from 256x256 to 640x480, and drawn like that (seems logical to me). However, testdtt doesn't seem to be Copying the data correctly. Help? Here's my Init function. Somehow, I think the problem is here, as the code has been thrown together from a few lessons. (Orthographic, Texture, and Drawing to Texture Lessons)
int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
{
	if (!tex.LoadBMP("NeHe.bmp")) return FALSE;
	if (!testdtt.New(256, 256)) return FALSE;


	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	return TRUE;										// Initialization Went OK
}
Thanks for any help you can give! (It's probably something really simple that I'm not noticing...)
---Kaos, the mass confusion...
Quote: Original post by DJKaotica
(It's probably something really simple that I'm not noticing...)



Oh my....I should've given you guys the entire section of code.

void GLTexture::CopytoTexture(){	glBindTexture(GL_TEXTURE_2D, ID());	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);}


glCopyTexImage2D gets angry when the given width/height values are 0.

My New(int w, int h) function created a Zeroed array of unsigned ints to set the data, but it never set the class attributes width and height, so they were their default values of 0.

Whoops!

--DJKaotica
---Kaos, the mass confusion...

This topic is closed to new replies.

Advertisement