Archived

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

Authustian

Tuxtures handled within class

Recommended Posts

Authustian    125
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

Share this post


Link to post
Share on other sites
Authustian    125
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

Share this post


Link to post
Share on other sites
dadio    122
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.

Share this post


Link to post
Share on other sites
lunasol    122
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

Share this post


Link to post
Share on other sites
Lukerd    122
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"

Share this post


Link to post
Share on other sites
Authustian    125
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 :-)

Share this post


Link to post
Share on other sites
TheMummy    122
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 ..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites