Jump to content
  • Advertisement
Sign in to follow this  
phishtree

OpenGL OpenGL + SDL 2D optimization help

This topic is 3353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello gentlemen, I'm a newb when it comes to OpenGL. Recently, I've ported my 2D game engine from Win32 API to OpenGL (for rendering) and SDL (window handling). However, my frame rate dropped significantly from 200 FPS to about 35 FPS. Since it's just a port of the graphics API, it most def is the rendering that is causing this slowdown. My engine renders a scene with about 200 tiles (each tile = 16x16) which is handled by my TileManager class. The tile images are pulled from a few tilesheets. When I render, TileManager goes through each tile in the scene and it renders each Tile using this code:
glBindTexture( GL_TEXTURE_2D, m_texID );

GLfloat portionXstart, portionYstart;
GLfloat portionXend, portionYend;

// This part finds the area of the tile image in the tilesheet
portionXstart = (GLfloat)imgOffsetX / m_imageWidth;
portionYstart = (GLfloat)imgOffsetY / m_imageHeight;
portionXend = (GLfloat)(imgOffsetX+cropWidth) / m_imageWidth;
portionYend = (GLfloat)(imgOffsetY+cropHeight) / m_imageHeight;

glBegin(GL_QUADS);

	glTexCoord2f(portionXstart, portionYstart); 
	glVertex2i(posX, posY);

	glTexCoord2f( portionXend, portionYstart);
	glVertex2i(posX + cropWidth, posY);

	glTexCoord2f(portionXend, portionYend); 
	glVertex2i(posX + cropWidth, posY+ cropHeight);

	glTexCoord2f(portionXstart, portionYend); 
	glVertex2i(posX, posY + cropHeight);

glEnd();


Not sure if it helps, but I set up the window using this code:
SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_HWSURFACE );
glViewport(0, 0, width, height);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, height, 0.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glClearDepth(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


I've tried commenting out the glBindTexture and just sticking to one texture, but to no avail. :( I've been reading up on vertex buffers objects/vertex arrays, will those help my cause? Any help would be greatly appreciated. Thanks! :)

Share this post


Link to post
Share on other sites
Advertisement
I tried doing glBegin/glEnd once, but it doesn't seem be binding the texture correctly. I see a weird hodgepodge of tiles only from only 1 tilesheet.

Share this post


Link to post
Share on other sites
Right, sorry, you can't call glBindTexture inside a glBegin/glEnd pair.

You should definitely look into VBOs. Also, if your tiles use different textures, you should sort them by texture to minimize texture changes.

Share this post


Link to post
Share on other sites
Are you not drawing objects that are not seen? (ie, culling)

There are better options than using inmediate mode (glbegin/glend)
For instance VBOs, display lists (less obsolete than inmediate mode)

Put
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, height, 0.0, 0.0, 100.0);
in your resize code


In you render code put:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draaw whatever...

Remove those lines from your init code, it does not make sense.

Are you using always the same texture atlas? In this case,
glBindTexture( GL_TEXTURE_2D, m_texID );
should be done only once.

This is my SDL init code

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
#ifndef TARGET_WINDOWS_XP
SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
#endif

#ifdef TARGET_WINDOWS_XP
m_surface=SDL_SetVideoMode(480, 320, 0, SDL_OPENGL);
if (m_surface==NULL) {
g_log->WriteInfo("SDL could not set the video mode. Error: %s.\n", SDL_GetError());
return false;
}
SDL_WM_SetCaption("Level Loader", NULL);

Share this post


Link to post
Share on other sites
vertex arrays wont help at all.
Im not sure what the problem is, 35fps (on what hardware/resolution)
does seem very low for 200 quads (unless they are all fullscreen quads) esp if youre not changing textures etc

though 35fps doesnt sound slow enuf for software opengl (then again u may have a fast cpu)

If u put up an .exe I might have a look at how it performs on my machine

Share this post


Link to post
Share on other sites
It sounds as though perhaps you use OpenGL to draw to an SDL system memory surface or something like that, which will cause the image to be copied to system memory unnecessarily. Try creating a minimal application using your set-up code, which draws a single triangle, and see if it still happens. If so, post the code for that application.

200 tiles is nothing, you should easily be able to draw thousands without that slowdown, even when changing textures and using immediate mode. Some other questions that can be relevant:
What OS/hardware/drivers/programming environment are you using?
Do you compile in debug mode?
Do other OpenGL applications work fine?

Share this post


Link to post
Share on other sites
Wow, I am a total newb. My friend suggested that I try upgrade my drivers (NVIDIA). So I did. When I ran the app, it rendered at 60fps. After turning VSYNC off, I got 500FPS...

Pwned. :(

I'm a bit frustrated that I've been spending a few days trying to fix this thing. I guess it's my punishment for not expecting it was a driver issue. Anyhow, I greatly appreciated all the help! Thanks guys in aiding me in this fruitless endeavor! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!