It is currently Wed Sep 08, 2010 6:08 am

All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: slow texture render first time
PostPosted: Sat Jul 31, 2010 1:20 am 
Joined: Sat Jul 31, 2010 12:56 am
Posts: 6

hi! whenever i try to draw a texture that hadn't been drawn before the method SDL_GL_SwapBuffers() freeze for about 1 or 2 seconds, but once its draw once the method works fast.

somebody knows why this happen?

thanks


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Sat Jul 31, 2010 5:54 pm 
User avatar
Joined: Tue Apr 06, 2010 4:54 pm
Posts: 25

Hi juakob,

Do you mind posting your code that is responsible for rendering your texture(s)?

Also, are you saying this happens with textures drawn after SDL_GL_SwapBuffers()? And how do you know this call is responsible for the freeze?

_________________
- NordCoder


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Sun Aug 01, 2010 1:11 pm 
Joined: Sat Jul 31, 2010 12:56 am
Posts: 6

here its how i create the texture, sTexture its just a struct that contains info about the texture
Code:
uint cTextureManager::LoadTextureFromMemory(SDL_Surface *surface, std::string file){
   GLuint texture;

   glPixelStorei(GL_UNPACK_ALIGNMENT,4);
   glGenTextures(1,&texture);
   glBindTexture(GL_TEXTURE_2D,texture);

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

   SDL_PixelFormat *fmt = surface->format;
   //if there is alpha
   if (fmt->Amask)
   {
      gluBuild2DMipmaps(GL_TEXTURE_2D,4,surface->w,surface->h,GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);
   }
   else // no alpha
   {
      gluBuild2DMipmaps(GL_TEXTURE_2D,3,surface->w,surface->h,GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
   }
   sTexture Tex;
   Tex.nombreArchivo=file;
   Tex.texture=texture;

   Tex.height=surface->h;
   Tex.width=surface->w;
   SDL_FreeSurface(surface);

   uint place;

    if( m_texture.empty() )
    {
        place = 1;
    }
    else
    {
        place = m_texture.size() + 1;
    }
   //set the map accordingly and return its id/place
   m_texture[ place ] = Tex;

    return place;
}

Here its how i create the plane with the texture
Code:
void cTextureManager::DrawTexture(uint TextureId, GLfloat x, GLfloat y, GLfloat scale, GLfloat rotation , GLfloat red, GLfloat green , GLfloat blue,GLfloat alpha ){
   if(CurrentTexture!=TextureId){
      glBindTexture(GL_TEXTURE_2D,m_texture[TextureId].texture);
      CurrentTexture=TextureId;
   }
   GLfloat width = m_texture[TextureId].width*scale;
   GLfloat height =m_texture[TextureId].height*scale;
   
   glLoadIdentity();
   glTranslatef(x,y,0.0f);
   glScalef(scale,scale,0.0f);
   glRotatef(rotation,0.0f,0.0f,1.0f);
   glBegin(GL_QUADS);

      glColor4f(red,green,blue,alpha);
      //Top-left vertex (corner)
      glTexCoord2f(0, 0);
      glVertex2f(0, 0);

      //Bottom-left vertex (corner)
      glTexCoord2f(0, 1);
      glVertex2f(0, height);

      //Bottom-right vertex (corner)
      glTexCoord2f(1, 1);
      glVertex2f(width, height);

      //Top-right vertex (corner)
      glTexCoord2f(1, 0);
      glVertex2f(width, 0);
   glEnd();


}

and then i just call
SDL_GL_SwapBuffers()

I know it freeze because i use breakpoints and when i run that line its freeze for a second or 2


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Mon Aug 02, 2010 9:19 am 
Site Admin
Joined: Tue Oct 28, 2008 9:55 am
Posts: 185
Location: Indiana

Just to confirm - is your texture a power of 2 texture?


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Mon Aug 02, 2010 6:26 pm 
Joined: Sat Jul 31, 2010 12:56 am
Posts: 6

i load 3 textures one of 128x128 and two of 512x512.
The strange thing its why only the first time it render it cause that anomaly.
Im really new to opengl so i dont really know when the texture is copied to video card memory, it the only thing i can think about opengl that can be so expensive to do.


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Wed Aug 04, 2010 1:45 am 
Joined: Sat Jul 31, 2010 12:56 am
Posts: 6

I found this only happen with images that have alpha channel.
maybe i made a mistake in this line
Code:
gluBuild2DMipmaps(GL_TEXTURE_2D,4,surface->w,surface->h,GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);


or something else...


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Thu Aug 05, 2010 12:51 am 
Joined: Sat Jul 31, 2010 12:56 am
Posts: 6

i found that if you compile a release and run the exe this problem dosent happen, but if you run inside visual studio you the freez.

the solution i made was to add this line when you create a texture
Code:
#ifdef _DEBUG
   DrawTexture(texture,0,0,0,0,1,1,1,0);
#endif

that way you render when you load and you dont have to worry about a future freez


Offline
 Profile  
 Post subject: Re: slow texture render first time
PostPosted: Sun Aug 08, 2010 8:44 pm 
Site Admin
Joined: Tue Oct 28, 2008 9:55 am
Posts: 185
Location: Indiana

Perhaps it has to do something with the debugger. Not really sure.


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: