OpenGL Textures are not rendering

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

OpenGL Textures are not rendering

Post by Teser0ck »

As the title says, the problem is that my textures are not rendering ( I am using SDL to load them ) . I am 95% sure that the files are loading right, however similar problem happened to me yet, in my previous game. Some guys didn't see spites, just blank white screen, but another guys saw it normally..
Here's some code :

Rendering system stuff

Code: Select all


RenderSystem::RenderSystem()
{
      
	PlD = new Vector2( 20, 40 );// Size of the image
	PlTex = this->LoadIMG( "yay.bmp" );

}

void RenderSystem::BindTexture( GLuint Texture )
{
	   glBindTexture(GL_TEXTURE_2D, Texture);

}

void RenderSystem::PrepareScene( )
{
	   glEnable(GL_TEXTURE_2D);

	   glPushMatrix();   
	   glColor3f( 1.0, 1.0, 1.0 );
	   glBegin (GL_QUADS);

}

void RenderSystem::RenderScene( Rect *POS, Rect *SRC )
{

	   glTexCoord2f( SRC->x / SRC->w, SRC->y / SRC->h);
	   glVertex2f(POS->x,POS->y);

      glTexCoord2f( ( SRC->x + SRC->w ) / SRC->w, SRC->y / SRC->h);
	   glVertex2f(POS->x + POS->w, POS->y);

      glTexCoord2f( ( SRC->x + SRC->w ) / SRC->w, ( SRC->y + SRC->h ) / SRC->h);
	   glVertex2f(POS->x + POS->w, POS->y + POS->h);

      glTexCoord2f( SRC->x / SRC->w, ( SRC->y + SRC->h ) / SRC->h);
	   glVertex2f(POS->x, POS->y + POS->h);
   

}

void RenderSystem::EndScene( )
{

	   glEnd();
	   glPopMatrix();

}


GLuint RenderSystem::LoadIMG( std::string Name )
{


	glEnable(GL_TEXTURE_2D);

    SDL_Surface *Surface = NULL;
    GLenum texture_format = NULL;
    GLuint tex = 0;
    GLint  nOfColors = 0;

   if ( (Surface = IMG_Load(Name.c_str())) ) { 
   



		if ( !isPowerOfTwo(Surface->w) ) {
			printf("image.bmp's width is not a power of 2\n");
		}
	 

		if ( !isPowerOfTwo(Surface->h) ) {
			printf("image.bmp's height is not a power of 2\n");
		}
	
			nOfColors = Surface->format->BytesPerPixel;
			if (nOfColors == 4)     // contains an alpha channel
			{
					if (Surface->format->Rmask == 0x000000ff)
							texture_format = GL_RGBA;
					else
							texture_format = GL_BGRA;
			} else if (nOfColors == 3)     // no alpha channel
			{
					if (Surface->format->Rmask == 0x000000ff)
							texture_format = GL_RGB;
					else
							texture_format = GL_BGR;
			} 


	 

			glGenTextures( 1, &tex );
			glBindTexture( GL_TEXTURE_2D, tex );
	 

	 

			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

			glEnable(GL_TEXTURE_2D);

	 

		glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, Surface->w, Surface->h, 0,
						  texture_format, GL_UNSIGNED_BYTE, Surface->pixels );
	} 
   else
   {
      std::cout<<"\nNOT LOADED\n";
   }
    SDL_FreeSurface(Surface);


	return tex;
}
Sprite.cpp

Code: Select all


Sprite::Sprite( GLuint Tex, Vector2 *Dims, Vector2 *SoC )
{

	M_Tex  = Tex; // texture
	M_Dims = Dims; // dimensions of my texture
	M_SoC  = SoC; // Size of one clip

	Box = new Rect( 0.0, 0.0, M_SoC->x, M_SoC->y );

}

void Sprite::Draw( int Part )
{
   
   AssetManager::GetRSInstance()->BindTexture( M_Tex );
   
   CBox = new Rect( Part * M_SoC->x,  M_SoC->y, Part * M_SoC->x + M_SoC->x, M_SoC->y );
   
   AssetManager::GetRSInstance()->PrepareScene();
   AssetManager::GetRSInstance()->RenderScene( Box, CBox );
   AssetManager::GetRSInstance()->EndScene();

}

Player

Code: Select all

Player::Player()
{


RS = AssetManager::GetRSInstance();// RenderSystem

...

m_SoC = new Vector2( 16, 16 ); // Size of one Clip

SPR = new Sprite( RS->PlTex, RS->PlD, m_SoC );

...

}

void Player::Show(SDL_Rect camera,int MAX_X,int MAX_Y,int LAYER,int xOff,int yOff)
{
     
..

SPR->Draw(0);

..

}
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Textures are not rendering

Post by N64vSNES »

Just glanced through your code but I quickly noticed this:

Code: Select all

void Sprite::Draw( int Part )
{
   
   AssetManager::GetRSInstance()->BindTexture( M_Tex );
   
   CBox = new Rect( Part * M_SoC->x,  M_SoC->y, Part * M_SoC->x + M_SoC->x, M_SoC->y );
   
   AssetManager::GetRSInstance()->PrepareScene();
   AssetManager::GetRSInstance()->RenderScene( Box, CBox );
   AssetManager::GetRSInstance()->EndScene();

}
And here:

Code: Select all

void RenderSystem::PrepareScene( )
{
      glEnable(GL_TEXTURE_2D);

      glPushMatrix();   
      glColor3f( 1.0, 1.0, 1.0 );
      glBegin (GL_QUADS);

}
You bind the texture before calling PrepareScene() and you enable texturing in PrepareScene(), so try switching it the other way around.
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

Thanks for the tip, unfortunately this didn't solved the problem :|
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Textures are not rendering

Post by N64vSNES »

Teser0ck wrote:Thanks for the tip, unfortunately this didn't solved the problem :|
Well I don't see anything else wrong with the code you've provided.

Try posting your image loading code and your initialization code.
Perhaps there is something wrong there.
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

Image loading code is in Render.cpp code, here's my initialization code :

( main.cpp )

Code: Select all


void Init()
{

..

           SDL_Init(SDL_INIT_EVERYTHING);
	   SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL);
           // ^ I changed these two while ago and screen changed color from white to black 

	   glEnable( GL_TEXTURE_2D );


	   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	   glMatrixMode(GL_PROJECTION);
	   glLoadIdentity();

	   glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);

	   glMatrixMode(GL_MODELVIEW);
	   glLoadIdentity();

    
...

}

int main( int argc, char* args[] )//--------------------------INT MAIN----------------------
{	 

	 Init();// initialize everything
         ....
}
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Textures are not rendering

Post by N64vSNES »

You you completely and totally sure your textures are getting loaded correctly?
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

Yeah, I am pretty sure it's getting loaded right. I have done some tests. Only thing I found is that my GLuint Texture is still equal to zero ( after loading an image )
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: OpenGL Textures are not rendering

Post by Ginto8 »

If the texture names are 0, that means that the texture has not loaded correctly. Move your "glEnable(GL_TEXTURE_2D);" to before "glGenTextures(...);", and make sure an OpenGL context is present when the textures are generated.

If only certain people are saying it's white, it may be an issue with non-power of 2 textures. I remember a similar problem, but I don't know if it applies here.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

Yes, my texture's width and height is power of two. I tried to make a function Load instead of loading textures right in constructor and I put it in main after Init(), so now it says the value of Texture is 1, but it still is not rendering.
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

SOLVED !!!
After making the function Load and putting function BindTexture after PrepareScene when rendering it finally works !
Thanky you all for help :bow:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL Textures are not rendering

Post by N64vSNES »

Teser0ck wrote:SOLVED !!!
After making the function Load and putting function BindTexture after PrepareScene when rendering it finally works !
Thanky you all for help :bow:
Glad you've solved it :)
User avatar
Teser0ck
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 35
Joined: Mon Aug 29, 2011 12:59 pm
Current Project: Platformer game, Map editor, Particle system
Programming Language of Choice: C++
Location: Czech Republic

Re: OpenGL Textures are not rendering

Post by Teser0ck »

Yeah, thanks. At last I am gonna post some videos about the game :)
Post Reply