Jump to content
  • Advertisement
Sign in to follow this  

How can I optimize this code?

This topic is 4553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

int Draw (Zone zone)
	glColor3f(0.5f, 0.6f, 0.7f);
	for (int i = 0; i < zone.Triangles.size(); i++)
		double x_1 = zone.Triangles.P1.X();
		double y_1 = zone.Triangles.P1.Y();
		double z_1 = zone.Triangles.P1.Z();
		double x_2 = zone.Triangles.P2.X();
		double y_2 = zone.Triangles.P2.Y();
		double z_2 = zone.Triangles.P2.Z();
		double x_3 = zone.Triangles.P3.X();
		double y_3 = zone.Triangles.P3.Y();
		double z_3 = zone.Triangles.P3.Z();

	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.

  • 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 ]

[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. :-)

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!