Sign in to follow this  
steven78

How can I optimize this code?

Recommended Posts

int Draw (Zone zone)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
	glTranslatef(0.0f,0.0f,-zoom);
	
	glPushMatrix();
	glMultMatrixf(Transform.M);
	glColor3f(0.5f, 0.6f, 0.7f);
	glBegin(GL_TRIANGLES);
	for (int i = 0; i < zone.Triangles.size(); i++)
	{
		double x_1 = zone.Triangles[i].P1.X();
		double y_1 = zone.Triangles[i].P1.Y();
		double z_1 = zone.Triangles[i].P1.Z();
		double x_2 = zone.Triangles[i].P2.X();
		double y_2 = zone.Triangles[i].P2.Y();
		double z_2 = zone.Triangles[i].P2.Z();
		double x_3 = zone.Triangles[i].P3.X();
		double y_3 = zone.Triangles[i].P3.Y();
		double z_3 = zone.Triangles[i].P3.Z();
		glVertex3d(x_1,y_1,z_1);
		glVertex3d(x_2,y_2,z_2);
		glVertex3d(x_3,y_3,z_3);
	}
	glEnd();
	glPopMatrix();

	glFlush ();

	return TRUE;
}
While doing some Mouse-manipulation (zoom, rotation ...) the redrawing of the triangles is much to slow, because I have more than 100000 triangles. What can I do? By the way: How can I mark the code as code?

Share this post


Link to post
Share on other sites
Use [ source] [/ source] tags (without the spaces of course).

You will want to look into Vertex Arrays for passing your data to OpenGL. Specifically, look into the Vertex Buffer Object extension.

Share this post


Link to post
Share on other sites
1) Easy way - use less triangles.
2) Find a better way to draw them, such as vertex arrays. It looks like the data is already contiguous, so it shouldn't be too hard.
3) Less triangles.
4) Take the VA stuff one step further and use VBOs. More faster, faster good.
5) Less triangles.

Also:
  • Zone should be passed as a const reference,
    int Draw(const Zone& zone)
    . Currently you're copying the entire thing every time you call Draw.
  • Why are X(), Y(), and Z() functions? Do you really need anything more than 'float X, Y, Z;'? And if they really need to be functions, make sure that they're inlined, and inlining is enabled (it probably won't be in debug builds)


The tags are
[ source ] ... [ /source ]



and
[code] ... [/code]

Share this post


Link to post
Share on other sites
Thanks at all!
The NeHe Lesson seems to be quit good, some month ago I've started at lesson 1. :-)

@uavfun:
to do it with less triangles seems to be quit hard, cause the data is fix and come from a comprehensive computation...
But you are right, the data is contiguous, it describes a hugh surface, divided in triangles.

Share this post


Link to post
Share on other sites
If it's contiguous, you should be using indexed vertices, rather than a collection of triangles. As is, you're sending every vertex multiple times as part of different triangles. Using indices, you'd send all the vertices, then tell it which ones are part of each triangle. (This can be done with vertex arrays, although you can use the same mechanism with just a plain list of vertices).

You may also be able to use a triangle strip instead of a list, depending on the data. This works quite well with terrain - it's usually a nice grid, so you can turn it into a single strip that weaves back and forth.

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