Sign in to follow this  

OpenGL SDL/OpenGL - Slow Rendering With Textures Close Up

Recommended Posts

I have had this problem for a while now and I've searched everywhere for an answer to no avail. Maybe I'm terrible at using Google, enlighten me if you please. Anyway, on with the problem. Basically, I have a really low polygon house, which is rendered as triangles using glDrawElements(). It is actually drawn with two glDrawElements() calls, since the model is split into two different parts to draw the brick and slate textures on the seperate parts of the house. The textures used are 256 x 256 in size, as shown below: Quick PC spec: Windows 7 2.4GHz Intel Q6600 Quad Core NVidia GeForce 9600GT 512MB GDDR3 3GB Memory Compiler: Visual C++ 2008 Express The application renders really smoothly when the house is at a distance, such as in the screenshot below: However, once the house gets closer, and the triangles take up more of the screen, the application runs slower and slower until the triangles fill the window, in which it starts running at about 10fps. The screenshot below ran at about 12.5fps: When textures are enabled, this problem really slows the application down. There is a very small slow down in the close up situation with no textures, but it is a lot smaller. Also, I should note that if part of the window is placed part way off the screen while the house is really close, the application speeds up to almost full speed. Therefore, there is something wrong with SDL_GL_SwapBuffers() when OpenGL is required to render pixels that take up most of the window. Below is a quick profile result (Time units are milliseconds): Disregard the large 'new'/'delete' count, they are done during calling reserve() on a vector in setupLighting(), which is a function that makes no difference to the problem (I have tried removing it during certain tests). The highlighted row is the SDL_GL_SwapBuffers() command, which is executing at an average of 81 milliseconds, or 12.5fps. Of course, I wouldn't believe SDL_GL_SwapBuffers() is the problem, it will be somewhere in my own code, but I can't think for my life what it is. Maybe blazingly obvious, who knows, heres some code anyway, see what you can find. All classes I have created have the 'cge' prefix, just for the record. Here is my window creation member function, which simply sets up the SDL window ready for OpenGL rendering
void cgeRenderWindow::initialize()

	GLuint renderFlags;
	if(mWindowMode == CGE_WINDOWED)
		renderFlags = SDL_OPENGL;

	mRenderSurface = SDL_SetVideoMode(mWindowWidth, mWindowHeight, mWindowMode, renderFlags);

cgeScene::initialize() sets up OpenGL as follows.
void cgeScene::initialize() const



	glClearColor(mClearColor.mRed, mClearColor.mGreen, mClearColor.mBlue, mClearColor.mAlpha);

Commenting out glEnable(GL_TEXTURE_2D) here vastly improves the speed of the close up geometry, though it still runs slower than at a distance. mCamera.initialize() calls cgeCamera::initialize(), which sets up the viewport. It uses a rectangle class I made which returns floats, that explains the casts made, though that matters not.
void cgeCamera::initialize() const
	// Rectangle coordinates are floats, glViewport takes ints (GLsizei = int), convert appropriately
	glViewport(static_cast<GLint>(mViewportBoundary.mBottomLeftCorner.mX), static_cast<GLint>(mViewportBoundary.mBottomLeftCorner.mY), static_cast<GLint>(mViewportBoundary.getWidth()), static_cast<GLint>(mViewportBoundary.getHeight()));


	// Division by 0 avoidance
	GLfloat aspectRatio(mViewportBoundary.getWidth() / (mViewportBoundary.getHeight() == 0 ? 1 : mViewportBoundary.getHeight()));

	// Creates a projection matrix using the corresponding parameter values
	gluPerspective(mFieldOfView, aspectRatio, mNearPlane, mFarPlane);


The main rendering function. Called once per loop of the infinite loop in main.
void cgeScene::render()

	cgeMatrix cameraTranslationMatrix;
	glMultMatrixf((cameraTranslationMatrix * mCamera.mRotationMatrix).mMatrix); 

	for(GLuint modelInstance = 0; modelInstance < mModelInstances.size(); modelInstance++)


setupLighting() makes a very small difference to the speed, but I commented it out just to make sure. All matrix multiplications make very little difference, I have tried commenting those out also in previous tests. That leaves cgeModelInstances::draw(). Only one model instance is drawn, which is the house shown. The model parts contain triangle information used in the glPointer() functions, each model part corresponding to a certain material (in this case, this is 2 model parts for the 2 textures). Here is cgeModelInstances::draw().
void cgeModelInstance::draw()
	cgeMatrix modelTranslationMatrix;
	glMultMatrixf((modelTranslationMatrix * mRotationMatrix).mMatrix);

	// Draw each model part
	for(GLuint i = 0; i < mModel->getModelParts().size(); i++)
		glVertexPointer(CGE_VERTEX_DIMENSIONS, GL_FLOAT, 0, &(mModel->getModelParts()[i].getVertecies()[0]));
		glNormalPointer(GL_FLOAT, 0, &(mModel->getModelParts()[i].getNormals()[0]));
		glTexCoordPointer(CGE_TEXTURE_DIMENSIONS, GL_FLOAT, 0, &(mModel->getModelParts()[i].getTextureCoordinates()[0]));
		//glColor4f(mColor.mRed, mColor.mGreen, mColor.mBlue, mColor.mAlpha);

		// If there is no texture, unbind
			glBindTexture(GL_TEXTURE_2D, CGE_NO_TEXTURE);
			glBindTexture(GL_TEXTURE_2D, mModel->getModelParts()[i].mTexture->getIdentifier());
		glDrawElements(GL_TRIANGLES, mModel->getModelParts()[i].getIndecies().size(), GL_UNSIGNED_INT, &(mModel->getModelParts()[i].getIndecies()[0]));

Again, the matrix multiplication makes very little difference to the speed. My textures are created as follows.
	// Generate a new texture object and bind to it
	glGenTextures(1, &mIdentifier);
	glBindTexture(GL_TEXTURE_2D, mIdentifier);

	// Create the texture from the pixel data (located at the address of pixelData[0])
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_BGRA, GL_UNSIGNED_BYTE, &pixelData[0]);

	// Set the parameters for this new texture

Note that I have used the 256 constant here to make sure they are 256 x 256 textures, though they are anyway, so this shouldn't make any difference. I was wondering if anyone else has had this close up texture problem before, whether it's a common problem or if it's my code. If you need more information, feel free to ask. [Edited by - thekilla1234 on March 16, 2010 9:31:49 AM]

Share this post

Link to post
Share on other sites
The long lines are making your post really wide (on my monitor, at least). Could you possibly edit your post and use [ source ] tags rather than [ code ] tags? (You can also use HTML to make your images clickable, which will probably make people more likely to check them out.)

Share this post

Link to post
Share on other sites
The highlighted row is the SDL_GL_SwapBuffers() command, which is executing at an average of 81 milliseconds, or 12.5fps. Of course, I wouldn't believe SDL_GL_SwapBuffers() is the problem, it will be somewhere in my own code, but I can't think for my life what it is.

Calling swapBuffers makes the driver flush the command queue. Basically the command blocks until the GPU actually completed all rendering and has swapped the buffers, that's why it is showing up in the profiler.

This might sound silly, but can you use glGetString with GL_VENDOR to determine if you are for whatever reason using the old Microsoft software OpenGL implementation?

Share this post

Link to post
Share on other sites
It does seem to be running from Microsoft's implementation indeed. I didn't think this would be a problem for such a small mesh. Are extensions the solution to this, or is it something along the lines of the .dll I have? I was planning extensions later once I had the basics down, so if that is the problem, I shall add them now.

Share this post

Link to post
Share on other sites
Original post by thekilla1234
It does seem to be running from Microsoft's implementation indeed. I didn't think this would be a problem for such a small mesh. Are extensions the solution to this, or is it something along the lines of the .dll I have? I was planning extensions later once I had the basics down, so if that is the problem, I shall add them now.

No, the solution is to use a hardware OpenGL implementation. You have problems creating your GL context, and it's fallbacking to a software implementation.

Share this post

Link to post
Share on other sites
Hah, figured out the problem, and an embarassing one at that. I had a copy of Microsoft's opengl32.dll in my executable's local directory. Deleted it and the FPS shot way up.

Thanks for pointing me in the right direction.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
  • Popular Now