Tuxtures handled within class

Started by
8 comments, last by Authustian 22 years, 6 months ago
ok, i started of by tring to make a texture class, you know, pass in the file name in it''s constructor and there ya go, have all your texture stuff encapsulated so yo don''t mess with it. It didn''t work, all i got were white cubes, so i caved in and just added the functions normaly. now i try to make a cube class. I''m working on a game that uses alot of cubes, so i figured a class would be a good addition to the engine. Now i run into the same problem. i think it has something to do with textures being stored in the class, but i have no idea why it doesn''t work. I''m useing Nehe''s code (of course) for LoadGLTexture. here''s the code if you want to check it out.
  
class CCube
{

public:
	CCube(float fsize, char *Filename)
	{
		glEnable(GL_TEXTURE_2D);
		LoadGLTexture(texture, Filename);
		MySize = fsize;
		Location.x = 0.0f;
		Location.y = 0.0f;
		Location.z = 0.0f;
		SphereRad = (float)2*sqrt(MySize*MySize + MySize*MySize);		//calcultae spherical volume for fustrum culling

	};

	~CCube(){};

	void SetLoc(CVector newLoc)
	{
		Location.x = newLoc.x;
		Location.y = newLoc.y;
		Location.z = newLoc.z;
	};
	
	void SetLoc(float newx, float newy, float newz)
	{
		Location.x = newx;
		Location.y = newy;
		Location.z = newz;
	};

	void Draw()
	{	
		glBindTexture(GL_TEXTURE_2D, texture[0] );
		glBegin(GL_QUADS);
			// Front Face

			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x + -MySize, Location.y +  -MySize, Location.z + MySize);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y +  -MySize, Location.z + MySize);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize,  Location.z + MySize);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x + -MySize, Location.y +  MySize,  Location.z + MySize);
			// Back Face

			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x + -MySize, Location.y + -MySize, Location.z + -MySize);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x + -MySize, Location.y +  MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y + -MySize, Location.z + -MySize);
			// Top Face

			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x + -MySize, Location.y +  MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x + -MySize, Location.y +  MySize, Location.z +  MySize);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize, Location.z +  MySize);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize, Location.z + -MySize);
			// Bottom Face

			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x + -MySize, Location.y + -MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y + -MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y + -MySize, Location.z +  MySize);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x + -MySize, Location.y + -MySize, Location.z +  MySize);
			// Right face

			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y + -MySize, Location.z + -MySize);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize, Location.z + -MySize);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x +  MySize, Location.y +  MySize, Location.z +  MySize);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x +  MySize, Location.y + -MySize, Location.z +  MySize);
			// Left Face

			glTexCoord2f(0.0f, 0.0f); glVertex3f(Location.x +  -MySize, Location.y + -MySize, Location.z + -MySize);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(Location.x +  -MySize, Location.y + -MySize, Location.z +  MySize);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(Location.x +  -MySize, Location.y +  MySize, Location.z +  MySize);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(Location.x +  -MySize, Location.y +  MySize, Location.z + -MySize);
		glEnd();		// Done Drawing Quads

	};

private:
	GLuint	texture[1];
	float MySize;
	CVector Location;
	float SphereRad;

};
  
Thanx in advance for any help. ------------------------------------------------- Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
Advertisement
I realy want to know what i''m doing wrong. Anyone?

-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
your class seems ok.
I would check the LoadGLTexture(texture, Filename);
since you created texture[1], are you sure you sould send texture to your LoadGLTexture? maby send texture[0] or check to make sure the LoadGLTexture function returnes correctly.

since you only have one texture, don''t define an array. just a simple GLuint texture; will do.

Also make sure your texture is the correct size (power of 2).

And last thing, make sure your consructor is accessed at all...

enjoy, and good luck.
few weeks ago, i ran into a similar problem. Look at

http://www.gamedev.net/community/forums/topic.asp?topic_id=60592

hope that help

lunasol
I had the same problem. The textures wouldn''t load in the constructor they only load in the initGL function. Its very strange.


"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"
well, i made a CCube::LoadTex() function that runs all the same texture loading functions and everything, and i call it in the init function, and it all seems to work fine... i don''t get it but as long as it works i''m happy

-------------------------------------------------
Don''t take life too seriously, you''''ll never get out of it alive. -Bugs Bunny
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
Ages and ages ago (Well ok, last Christmas ;-)) I wrote a demo for the NeHe christmas challenge that never got posted because I wrote it in Borland C++ Builder 4 and Jeff didn''t have the runtime libraries on his computer and couldn''t get it to run, however, the demo is available on my site (http://go.to/eyescream) along with full source code (Even if the demo does actually suck :-)). I was working on a library of OpenGL classes I called CGLX before I wrote the demo and the demo uses the CGLX library (In a beta state). Amongst the code is a class you might find useful called CTexture. It does exactly what you want, and it works nicely :-) If you can''t just rip out the code and use it in MSVC just let me know (E-Mail me so you can be sure I get the message :-)) and I will modify the class and post it up on my site.

These days, I''m working on the Infinite.Scream engine (An open-source 3D game engine) in MSVC6 + SP5. The OpenGL texture wrapper class is massively improved. I wont explain exactly what it can do because it will be demonstrated in my contribution to the Apocalypse demo contest (And I dont want to give away any of my ideas to the opposition ;-)), however you''ll get to download the code and see for yourself :-)
The Texture loading will never work when you try to load textures into gl before you have a running OpenGL context/window .... And you even get no glError codes ....
So befor initialize your classes ensure that your context has been initialized by your main module ..
I''ve run into the same problem in the past, and it drove me crazy because my texture object returned was zero. If you look in the OpenGL documentation, it states that zero is never returned! Anyway, TheMummy''s right, you can''t initialize textures or anything until after you open your window. Your texture class was probably working just fine, I suggest going back to that rather than the function call.
Is a call to glGenTextures required?

This topic is closed to new replies.

Advertisement