# OpenGL A beginner with many questions

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

## Recommended Posts

I've got a 2D image renderer in OpenGL with SDL. However, it doesn't run as fast as I'd like. I'm currently running it in immediate mode, but since I'm using sprite sheets, I can't use display lists. I don't want to use vertex buffer objects for hardware compatibility reasons. This would seem to leave me with vertex arrays. Are there any other options that I haven't considered? Also, are the hardware incompatibilities of VBO's generally considered a problem, or should I use those instead? Aside from moving out of immediate mode, how could I best speed up the renderer? My code:
    int Num;
float Left, Right, Top, Bottom, Width, Height;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for ( int k = RenderJobs.size()-1; k >= 0; k--){
Num = RenderJobs[k].TexNum;
Left = float(RenderJobs[k].ClipRect.x)/float(Textures[Num].GetSize().w);
Right = float(RenderJobs[k].ClipRect.x+RenderJobs[k].ClipRect.w)/float(Textures[Num].GetSize().w);
Top = float(RenderJobs[k].ClipRect.y)/float(Textures[Num].GetSize().h);
Bottom = float(RenderJobs[k].ClipRect.y+RenderJobs[k].ClipRect.h)/float(Textures[Num].GetSize().h);
Width = 0.5*RenderJobs[k].BlitRect.w;
Height = 0.5*RenderJobs[k].BlitRect.h;
glTranslatef(RenderJobs[k].BlitRect.x + Width, RenderJobs[k].BlitRect.y + Height, 0);
glRotatef(RenderJobs[k].Rotation,0.0f,0.0f,1.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, Textures[Num].GetTexID());
glTexCoord2f(Left, Top);
glVertex3f(-Width, -Height, 0);
glTexCoord2f(Right, Top);
glVertex3f(Width, -Height,  0);
glTexCoord2f(Right, Bottom);
glVertex3f(Width, Height, 0);
glTexCoord2f(Left, Bottom);
glVertex3f(-Width, Height, 0);
glEnd();
}
RenderJobs.clear();
SDL_GL_SwapBuffers();

RenderJobs is a vector containing the various data on which part of the texture should be displayed, where and how large, the rotation, etc. The reason I assigned the various texture vertexes to the Left, Right etc is because it saved me some typing and it seemed reasonable that only doing it once per render and saving it to a variable would save some CPU time over doing it multiple times. If I were trying to make a console-style menu for a game (ie, up/down to highlight selection, then, say, enter to select it, as well as an options menu), would a GUI library be appropriate for this, or should I try to create my own system for it? I really haven't had any luck thinking of a system to manage this. Are there any simple ways to do full screen antialiasing (hopefully ones that don't hurt performance too much)? Where could I find resources on implementing this? I saw the one on NeHe, but it mentioned destroying and recreating the window, and I'm not entirely sure how that would translate to an SDL window rather than an OpenGL or Win32 one.

##### Share on other sites
I find it hard to believe that you have performance problems in 2D. There are always optimizations but to get an extra 4 frames when your getting like 300 or more (which you should be getting).

When I used immediate mode I could draw tens of thousands of sprites. Maybe your framerate is fine but you just want to make your code better?

##### Share on other sites
Quote:
 Original post by redattack34Also, are the hardware incompatibilities of VBO's generally considered a problem, or should I use those instead?
You have to consider VBOs. They're supported since GeForce1. Besides issues related to broken driver installs, I've never had a serious problem with them.
Quote:
 Original post by redattack34If I were trying to make a console-style menu for a game (ie, up/down to highlight selection, then, say, enter to select it, as well as an options menu), would a GUI library be appropriate for this, or should I try to create my own system for it? I really haven't had any luck thinking of a system to manage this.
I've seen a few people pointing out console libraries. It's something I personally don't reccomand. If you have issues in writing a console link, it's likely you have to check your design.
Quote:
 Original post by redattack34Are there any simple ways to do full screen antialiasing (hopefully ones that don't hurt performance too much)? Where could I find resources on implementing this? I saw the one on NeHe, but it mentioned destroying and recreating the window, and I'm not entirely sure how that would translate to an SDL window rather than an OpenGL or Win32 one.
The other possibility is to use FBOs but this is possibly a problem for you considering your approach to VBOs.

##### Share on other sites
I believe the poster ment console as in gameconsole, Xbox,PS etc.

Any type of game/application do require a gui of some kind. If you're not happy with making your own, you could always try some of the various freeware libraries that are available. :)

Your biggest problem in the code you displayed is the amount of glBegin()/glEnd() calls. You should restructure so that you position vertices where they should be, i.e (pseudo)

glBindTexture(GL_TEXTURE_2D,someBigTextureAtlas.id);glBegin(GL_QUADS);   for all sprites:      calc pos for each vertex.      glTexCoord... glVertex3f...      glTexCoord... glVertex3f...      glTexCoord... glVertex3f...      glTexCoord... glVertex3f...glEnd();or better yet, preprocess the sprites and then send all the vertices in one go.

Cheers,

/Robert

##### Share on other sites
Quote:
 Original post by dpadam450I find it hard to believe that you have performance problems in 2D. There are always optimizations but to get an extra 4 frames when your getting like 300 or more (which you should be getting).When I used immediate mode I could draw tens of thousands of sprites. Maybe your framerate is fine but you just want to make your code better?

On my computer (with a Radeon X700) I get just below 60FPS rendering 2000 images per frame (it doesn't seem to matter which images or what size of images), but that drops to 10 FPS or so on the other, older machines I test it on. I'd like to have it run well on the older hardware as well, and I am anticipating the possibility of having to render large numbers of images per frame. Better code would be nice too, though.

Quote:
 You have to consider VBOs. They're supported since GeForce1. Besides issues related to broken driver installs, I've never had a serious problem with them.

Ah, OK. I wasn't sure just how new the hardware would need to be in order to support VBO's, thanks.

Quote:
 I believe the poster ment console as in gameconsole, Xbox,PS etc.

Yes, I should have clarified that. The main menus in the Megaman series are a good example.

Quote:
 Your biggest problem in the code you displayed is the amount of glBegin()/glEnd() calls. You should restructure so that you position vertices where they should be, i.e (pseudo)

Not sure I know what you mean. I should calculate and store all of the vertex data at once, then send it all in one glBegin/End block? I'll try that, thanks.

[Edited by - redattack34 on March 20, 2008 1:54:45 PM]

##### Share on other sites
OK, I've tried it. It doubles the framerate, but now I've got a new problem. glTranslatef and glRotatef don't seem to be working inside glBegin/End (the textures are displayed at the top left corner of the screen, rather than where they should be). If I move them outside of glBegin/End, then I have to also move the for loop outside glBegin/End (to keep updating the position and rotation), and then I'm back where I started. If I were to restructure the program such that Render() is called once for every image, I could get away without the for loop, but it would still call glBegin/End many times.

Now, I think I could skip the use of glTranslatef altogether by incorporating that data into the quad vertexes, but what about rotation? I could calculate rotated vertex positions, but that seems unnecessarily complex, and I'm sure there's a simpler way to do it.

glBindTexture also doesn't work in glBegin/End, which is a pain, but I could get around that with one huge spritesheet texture rather than multiple smaller ones, if I have to.

##### Share on other sites
The simplest thing is to calculate the rotated and translated vertex positions by yourself. glRotate does much more than the couple of trig functions you need to rotate a 2D sprite.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002140
×