Page 1 of 1

OpenGL Textures are not rendering

Posted: Tue Aug 30, 2011 10:13 am
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);

..

}

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 2:23 am
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.

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 3:15 am
by Teser0ck
Thanks for the tip, unfortunately this didn't solved the problem :|

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 8:12 am
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.

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 10:18 am
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
         ....
}

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 10:39 am
by N64vSNES
You you completely and totally sure your textures are getting loaded correctly?

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 10:52 am
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 )

Re: OpenGL Textures are not rendering

Posted: Wed Aug 31, 2011 10:43 pm
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.

Re: OpenGL Textures are not rendering

Posted: Thu Sep 01, 2011 4:22 am
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.

Re: OpenGL Textures are not rendering

Posted: Thu Sep 01, 2011 4:24 am
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:

Re: OpenGL Textures are not rendering

Posted: Thu Sep 01, 2011 5:22 am
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 :)

Re: OpenGL Textures are not rendering

Posted: Thu Sep 01, 2011 6:12 am
by Teser0ck
Yeah, thanks. At last I am gonna post some videos about the game :)