Advertisement

Vertex Normals Issue

Started by July 18, 2004 05:06 PM
13 comments, last by Technoid 20 years, 4 months ago
The snippet was simply to show some basic logic, I have my data arranged like this:

typedef struct{

float vert x,y,z;

}vert;

typedef struct{

int face a,b,c;

}poly;

typedef struct{

float u,v;

}uv;

typedef struct{

vert *verts;
poly *polys;
uv *uvs;

}mesh;



I use the face indices to reference the correct verts and uv's, nothing fancy.
(On a side note, is it possible to use vertex arrays with a data structure like this, or would I

have to arrange my data differently?)
It can be to in 2 passes. But you'll need bunch of temp structures.

Pass 1: Reindexing faces
For each vertex you need to store it's index in array. At start initialize all indexes at thier curent number (index=i).
Then walk trough array finding duplicated vertices. When you find a duplicate you have to change it's index(index[duplicate]=index[original]). Now all indexes should point to unique vertices, so go over face array and update face info (face.a =index[face.a];)

Pass 2: Removing unused vertices
This is a tricky part. Each vertex must know its new index. First loop once over all vertices and calculate their new index (by simply skipping duplicate indexes). Then go over face array and update their info once again. Now one more loop over vertices and this time removing duplicates.

I strongly sugest using STL for some dirty parts to get tings running.

Notes:
-This is very unoptimized and will run dog slow. There are bunch of things to optimize, but first get it running.
-As far as I know it could be done in O(numFaces+numVertices)
You should never let your fears become the boundaries of your dreams.
Advertisement
if its only on start-up though... he should only need to do the "dog-slow" stuff once, or am I way off in left field here?

also, very interested in STL, I'm technically not a software guy, I graduated in electrical engineering. Point me in a direction ? I wanna learn some stuff to make my life easier :)

AP: I wrote "dog-slow" becouse original algoritem runs in O(num_vert^2). I've implemented simmilar thing before and it took like 2 minutes for mesh with 60k vertices. After implementing sorting and some range limits it went down to a few seconds. As for STL (Standard Template Library) there are number of tutorials about it. You only need some basic stuff to implement this. You can optimize further later on.
You should never let your fears become the boundaries of your dreams.
Thanks DarkWing, I'll try that out.

This topic is closed to new replies.

Advertisement