Skip to main content
GameDev.net gamedev.net
🔒 Locked

Adding textures with a class problem

Started by shaDoze Nov 21, 2003 at 4:52 AM 6 replies 650+ views
Original Post
shaDoze
shaDoze
Hi, I was working on a small scenegraph and got stuck with textures for some reason. Basicly, when I define my texture object in the main class and set it to a pyramid model, it works. When I define this texture object in my pyramid model class (with the same code), it does not work. Both compile, just the latter won''t show any textures. I have a small class "MyPyramidModel" that renders a basic colored pyramid with a texture applied on it as follows (this does not show the textures):
CMyPyramidModel :: CMyPyramidModel()
{
	CTexture * texture = new CTexture();
	texture->LoadFromFile( "../textures/text.rgb", TEX_FILE_TYPE_RGB );
	texture->SetWrap( GL_REPEAT, GL_REPEAT );
	texture->SetFilter( GL_LINEAR, GL_LINEAR );

	CPart * side = new CPart( GL_TRIANGLES, 3 );	// front

	side->SetColor( 1.0, 0.0, 0.0 );				// red

	side->SetTexture( texture );
	side->Add( CVector( 0.0, 1.0, 0.0 ),	TexCoordType( 0.5, 1.0 ) );
	side->Add( CVector( 1.0, -1.0, 1.0 ),	TexCoordType( 1.0, 0.0 ) );
	side->Add( CVector( -1.0, -1.0, 1.0 ),	TexCoordType( 0.0, 0.0 ) );
	side->SetDefaultNormal();
	this->Add( side );

...
However, if I adjust the previous code with the code below and pass a texture to it, it does work:
CMyPyramidModel :: CMyPyramidModel( CTexture * texture )
{
	CPart * side = new CPart( GL_TRIANGLES, 3 );	// front

	side->SetColor( 1.0, 0.0, 0.0 );				// red

	side->SetTexture( texture );
	side->Add( CVector( 0.0, 1.0, 0.0 ),	TexCoordType( 0.5, 1.0 ) );
	side->Add( CVector( 1.0, -1.0, 1.0 ),	TexCoordType( 1.0, 0.0 ) );
	side->Add( CVector( -1.0, -1.0, 1.0 ),	TexCoordType( 0.0, 0.0 ) );
	side->SetDefaultNormal();
	this->Add( side );

...
Any ideas?
MelvinElvin
MelvinElvin
have u stepped over it to see if it''s reading in the texture fine?
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
shaDoze
shaDoze
Yep, texture seems to load fine.

Debugging showed graphics data, correct size, format, etc in both cases.
Trienco
Trienco
hmm... your object doesnt happen to fly around as a global object? creating opengl textures in constructors is dangerous. make it global and it will screw up because without opengl being initialized it just cant do it. option would be a flag to indicate if opengl is initialized and inside your texture loading check for it. if not, just call it here.
f@dzhttp://festini.device-zero.de
nev
nev
i guess i''m in pretty much the same situation....
i''m sure that opengl is initialized and the texture is loaded correctly, but only standard color on my quads.
Shane I Am
Shane I Am
Yes a lot of people new to openGL run into the problem of trying to create a texture inside a class before openGL is initialized. Either create your object after openGL is initialized (if it creates the texture in the constructor) or set up another method that will create the texture and call it after openGL is initialized.
shaDoze
shaDoze
Heh, that was exactly it. It was created globally. Works now. Thanks guys.
nev
nev
i''m 100% sure i do everything in the correct order(initgl, loadtexture, bind).
it doesn''t even work when i (correctly!) load the texture in the same function where i''m using it. (function is static...)

could the static be the same problem?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.