Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: OpenGL running slow as !@#$%  (Read 1146 times)

0 Members and 1 Guest are viewing this topic.
OpenGL running slow as !@#$%
« on: February 15, 2009, 06:33:09 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Whoevers been following Sword of Divination lately knows I've moved over to C++ with openGL.  So I implemented some tiling capabilities..and when a bunch of tiles are drawn on the screen, the program runs slow as !@#$%.  Now wait a minute, from what I understood, applying a texture map to a quad and drawing the quad was supposed to be fast, so why is this slowing down so damn much? 

This is what's on the screen:


And it's not like I've loaded a bunch of images up, I just loaded the entire tileset and split it into coordinates.

Code: [Select]
void HVSTGFX::CTileSheet::createTile(int tileID, float x, float y)
{

float widthAdd = (tiles[tileID].pixWidth/2)*WIDTHSCALE;
float heightAdd = (tiles[tileID].pixHeight/2)*HEIGHTSCALE;

glBindTexture(GL_TEXTURE_2D, sheet.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sheet.bitmapInfoHeader.biWidth, sheet.bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, sheet.sprite);

glColor3f(1.0f,1.0f,1.0f );
glBegin(GL_QUADS);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY);                                                  glVertex3f(x, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY);                          glVertex3f(x + widthAdd, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY + tiles[tileID].glHeight); glVertex3f(x + widthAdd, y + heightAdd, 0.0f);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY + tiles[tileID].glHeight);                         glVertex3f(x, y + heightAdd, 0.0f);
glEnd();


}
« Last Edit: February 15, 2009, 05:33:02 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: OpenGL running slow as !@#$%
« Reply #1 on: February 15, 2009, 07:48:38 am »
  • If not now, when?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 520
I don't know your project so perhaps I am way off. You seem to be calling CTileSheet::createTile to display your sprites. Why, exactly, are you also loading the exact same texture each time you want to draw a sprite?

My suggestion is you move the texture loading code somewhere else, where it will only be called once:
Code: [Select]
glBindTexture(GL_TEXTURE_2D, sheet.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sheet.bitmapInfoHeader.biWidth, sheet.bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, sheet.sprite);

And then change your function to simply be this:
Code: [Select]
void HVSTGFX::CTileSheet::createTile(int tileID, float x, float y)
{

float widthAdd = (tiles[tileID].pixWidth/2)*WIDTHSCALE;
float heightAdd = (tiles[tileID].pixHeight/2)*HEIGHTSCALE;

glBindTexture(GL_TEXTURE_2D, sheet.texture);
glColor3f(1.0f,1.0f,1.0f );
glBegin(GL_QUADS);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY);                                                  glVertex3f(x, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY);                          glVertex3f(x + widthAdd, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY + tiles[tileID].glHeight); glVertex3f(x + widthAdd, y + heightAdd, 0.0f);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY + tiles[tileID].glHeight);                         glVertex3f(x, y + heightAdd, 0.0f);
glEnd();


}

If you are still having problems, consider Display Lists.
Logged
Re: OpenGL running slow as !@#$%
« Reply #2 on: February 15, 2009, 04:11:49 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
Ah..that would probably make sense.  Alrighty, lemme get on that.

EDIT:  Being that I'm lazy, I changed the function around to look like this:

Code: [Select]
void HVSTGFX::CTileSheet::createTile(int tileID, float x, float y)
{

float widthAdd = (tiles[tileID].pixWidth/2)*WIDTHSCALE;
float heightAdd = (tiles[tileID].pixHeight/2)*HEIGHTSCALE;


if (!sheet.loaded)
{
sheet.loaded = true;
glBindTexture(GL_TEXTURE_2D, sheet.texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, sheet.bitmapInfoHeader.biWidth, sheet.bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, sheet.sprite);
}
else
glBindTexture(GL_TEXTURE_2D, sheet.texture);

glColor3f(1.0f,1.0f,1.0f );
glBegin(GL_QUADS);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY);                                                  glVertex3f(x, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY);                          glVertex3f(x + widthAdd, y, 0.0f);
glTexCoord2f(tiles[tileID].glX + tiles[tileID].glWidth, tiles[tileID].glY + tiles[tileID].glHeight); glVertex3f(x + widthAdd, y + heightAdd, 0.0f);
glTexCoord2f(tiles[tileID].glX, tiles[tileID].glY + tiles[tileID].glHeight);                         glVertex3f(x, y + heightAdd, 0.0f);
glEnd();

}



Except now all the quads are coming out white.  So, the textures aren't even being applied now.

I also tried moving the code around for binding and such to a seperate function, but I get the same results.

EDIT:  Fix'd.  For some reason the CSprite constructor wasn't doing its job of intializing the loaded variable.  It's all working now, thanks for your help!
« Last Edit: February 15, 2009, 09:10:36 pm by MG-Zero »
Logged



i love big weenies and i cannot lie
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.309 seconds with 44 queries.

anything