Sign in to follow this  
thekilla1234

OpenGL SDL/OpenGL - Slow Rendering With Textures Close Up

Recommended Posts

thekilla1234    100
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: http://img716.imageshack.us/img716/1228/brick.png http://img97.imageshack.us/img97/570/slate.png 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: http://img30.imageshack.us/img30/6469/housesmall.png 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: http://img41.imageshack.us/img41/4859/houselarge.png 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): http://img203.imageshack.us/img203/1290/profilem.png 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()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) != CGE_SDL_INIT_SUCCESSFUL)
		exit(0);

	GLuint renderFlags;
	
	if(mWindowMode == CGE_WINDOWED)
		renderFlags = SDL_OPENGL;
	else
		renderFlags = SDL_OPENGL | SDL_FULLSCREEN;
	
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, CGE_DOUBLE_BUFFERING_ENABLE);

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




cgeScene::initialize() sets up OpenGL as follows.
void cgeScene::initialize() const
{
	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_LIGHTING);
	glEnable(GL_COLOR_MATERIAL);

	glFrontFace(GL_CW);
	glShadeModel(GL_SMOOTH);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glClearColor(mClearColor.mRed, mClearColor.mGreen, mClearColor.mBlue, mClearColor.mAlpha);
	disableAllLights();
	mCamera.initialize();
}




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()));

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// 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);

	glMatrixMode(GL_MODELVIEW);
}




The main rendering function. Called once per loop of the infinite loop in main.
void cgeScene::render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	cgeMatrix cameraTranslationMatrix;
	cameraTranslationMatrix.setTranslation(-mCamera.mPosition);
	glMultMatrixf((cameraTranslationMatrix * mCamera.mRotationMatrix).mMatrix); 

	for(GLuint modelInstance = 0; modelInstance < mModelInstances.size(); modelInstance++)
	{
		glPushMatrix();
			//setupLighting(mModelInstances[modelInstance].mPosition);
			mModelInstances[modelInstance].draw();
		glPopMatrix();
	}

	SDL_GL_SwapBuffers();
}




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;
	modelTranslationMatrix.setTranslation(mPosition);
	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
		if(!mModel->getModelParts()[i].mTexture)
			glBindTexture(GL_TEXTURE_2D, CGE_NO_TEXTURE);
		else
			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
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);




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
jyk    2094
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
thekilla1234    100
Thanks for the tips, I've updated it. I never noticed the links being a problem since I have a plugin for Firefox to link them up automatically. They should be fine now though.

Share this post


Link to post
Share on other sites
Ohforf sake    2052
Quote:
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
thekilla1234    100
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
HuntsMan    368
Quote:
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
thekilla1234    100
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  

  • Similar Content

    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
    • By dpadam450
      FINALLY, upgrading my engine to openGL 4. I was having some trouble so I started with a stripped down application and was wondering if VAO's are required, because I have a sample working, but if I remove the VAO then it doesn't seem to like drawing my triangle.
  • Popular Now