Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Neat trick in figuring out if vertex indices are worth it on your terrain

Author
Message
Sid Sinister
21
Years of Service
User Offline
Joined: 10th Jul 2005
Location:
Posted: 15th Jun 2010 11:08
Whenever you create a mesh there will always be shared vertices. More often that not, creating indices to eliminate those shared vertices will give you a performance boost, especially on large terrain.

However, there are cases in which using indices can create a negative effect on performance. Think about 5 triangles that never share a vertex. Without indices you would have to use 15 vertices. Using indices you have define 15 vertices because they are all unique, and what's more is that you still have to define 15 indices, each referencing it's own vertex. Sending those extra bytes to the graphics card will down your game.

But there's a time saving trick to help you figure out which is better to use. Take the number of unique vertices you have and divide it by the number of triangles. If no vertices are shared among the triangles the value should be 3. The more vertices that can be shared, the smaller your number will be. And the lower the number, the more performance you will gain by using indices.

For example:



Take this 4 faced (8 triangles) mesh, with it's inner edges forming a diamond. If you weren't to use indices, you would have 24 vertices (8 triangles * 3 vertices per triangle). If you used indices, you would only have 9 unique vertices saving you 15 vertices in memory, bandwidth and vertex processing power.

The math? 9 Unique Vertices / 8 Triangles = 1.125 (A great performance gain!)

I know the majority of you use DBP here, but I'd still highly recommend picking up the book XNA 3.0 Game Programming Recipes on Amazon for a good price. The information in there has been invaluable to me and can translate over to DBP pretty well (XNA is C#... not that hard to read). Anyway, that's where I picked up this great tip. Hope you enjoyed it!

"If I have seen a little further it is by standing on the shoulders of Giants" - Isaac Newton
Current Project: http://strewnfield.wordpress.com/ (Last updated 06/11/09)
tiresius
23
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 16th Jun 2010 00:12
That's cool.

So how do we add/change indices on a mesh? Via memblock trickery?


A 3D marble platformer using Newton physics.
Sid Sinister
21
Years of Service
User Offline
Joined: 10th Jul 2005
Location:
Posted: 16th Jun 2010 00:41
Well, maybe someone can provide you with DBP code, but for starter's here's what it is in C#:



int[] means an integer array. So the function itself is of type int[] and is called CreateTerrainIndices.

heightData is an Vector2 array holding the x and y coordinates of the heights of a height map.

I'm then creating a new array the size I need.

Then the fancy math, which, if you want, I'll draw a picture for later explaining it.

Then it returns the incdices back to the int[] that called it.

"If I have seen a little further it is by standing on the shoulders of Giants" - Isaac Newton
Current Project: http://strewnfield.wordpress.com/ (Last updated 06/11/09)
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 16th Jun 2010 22:24
Quote: "So how do we add/change indices on a mesh? Via memblock trickery?"


I think DBP memblocks only allows vertices. You can, however, change index data by using the vertexdata commands (Get/Set indexdata). I have never tried 'adding' indices this way, but I don't think it would work.

Cheers!
Sven B

Sid Sinister
21
Years of Service
User Offline
Joined: 10th Jul 2005
Location:
Posted: 17th Jun 2010 00:05
Indices are just an array that you create after sorting through all non-unique vertices and grouping them. I don't see why you wouldn't be able to feed that into a memblock afterwards, before you even create it. If I had the time, I'd give it a shot right now. If no one checks, I might give this a shot in a week when classes are out.

"If I have seen a little further it is by standing on the shoulders of Giants" - Isaac Newton
Current Project: http://strewnfield.wordpress.com/ (Last updated 06/11/09)
James H
19
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 17th Jun 2010 00:29
Does SET GLOBAL OBJECT CREATION Creation Mode do this for us? ie is it the same? I thought perhaps that if it does, then it is also hardware dependent as per the info from helpfiles;

Parameters:
Creation Mode

Integer
A Creation Mode of 0 is the default, whereas 1 will force the objects to share vertex buffers, 2+3 stops texture sorting, 4+5 uses sort by number and 6+7 sorts by depth.

Description:

A Creation Mode of zero is the default, whereas a value of one will force the objects to share vertex buffers where possible. Vertex buffers are the internal memory resource the engine uses to store all geometry data before it is rendered to the screen. By sharing vertex buffers, you can gain performance on some 3D cards, but not others. By default vertex buffers are not shared for the sake of widest compatibility, better generic performance and less prone to buffer overruns. Additionally, objects that share vertex buffers take longer to delete as they must be disentangled from its buffer on removal.
Sid Sinister
21
Years of Service
User Offline
Joined: 10th Jul 2005
Location:
Posted: 17th Jun 2010 02:15
No, from what I gather, it's doing the following:

Each time you draw an object your vertices are transferred from your RAM to your graphics card. Usually, the majority of this data remains the same frame to frame, meaning the same data is transferred every time.

You can speed this process up by storing those vertices in the video RAM. Vertices can be transferred much faster from video ram than system RAM.

What I believe that Set Global Object Creation does is group all this info together instead of sending individual pieces of data. A lump sum so to speak.

"If I have seen a little further it is by standing on the shoulders of Giants" - Isaac Newton
Current Project: http://strewnfield.wordpress.com/ (Last updated 06/11/09)
Latch
20
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 17th Jun 2010 17:07
Just to throw something in the mix to think about: UV coordinates, at least in a Direct X file I think, are tied to the vertices I believe in a 1:1 ratio. If you build faces using the same index of the same vertices, if you try to position or size the UVs for individual faces, you will inadvertently affect the neightbors that share the same vertex index. I don't think you can have independent UVs that don't belong to a vertex. I think that's why you'll often see primatives like a cube using 24 vertices and 24 UVs so each face can handle textures independently.

Enjoy your day.
James H
19
Years of Service
User Offline
Joined: 21st Apr 2007
Location: St Helens
Posted: 18th Jun 2010 02:24
Sid: thanks for saving me some time

Latch: I had been considering this; if the object was for terrain then a shader taking control of uvs would suit this method best? I am not aware of exactly how shaders can do this but I`m pretty sure VanB provided an example of this in terrsculpt? Just thoughts really. Perhaps it would be best for a developer to decide how important uv control is to there developement.
Sid Sinister
21
Years of Service
User Offline
Joined: 10th Jul 2005
Location:
Posted: 18th Jun 2010 20:06
I'm unsure about the UV coordinate question, but I wouldn't jump to conclusions about it. I have never seen, read, or heard a downside to using indices other than what I posted in here - that if you have too many unique vertices, it's not worth it. I'm 99% sure you still can, it's just a matter of how. Once I get there, I'll let you know

"If I have seen a little further it is by standing on the shoulders of Giants" - Isaac Newton
Current Project: http://strewnfield.wordpress.com/ (Last updated 06/11/09)

Login to post a reply

Server time is: 2026-07-25 08:13:39
Your offset time is: 2026-07-25 08:13:39