# OpenGL A beginner with many questions

## 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978797
• ### Similar Content

• Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
So, here's what the plan is so far as far as loading goes:
Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!

• I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.

• 10
• 9
• 21
• 14
• 12