# OpenGL A beginner with many questions

## Recommended Posts

redattack34    138
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
Krohm    5030
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
redattack34    138
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
redattack34    138
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
Fingers_    410
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

• ### Similar Content

• By Zaphyk
I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?

• I'm trying to get some legacy OpenGL code to run with a shader pipeline,
The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
I've got a version 330 vertex shader to somewhat work:
#version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
Question:
What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?

Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.

• Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.

• By KarimIO
Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!

• Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
I'm available for a good conversation about Game Engine / Graphics Programming

• 13
• 10
• 15
• 10
• 18