PDA

View Full Version : Interesting 3D Article



matthew
01-05-2008, 18:15
Here's (http://msdn.microsoft.com/en-us/library/bb324491.aspx) an interesting article by Microsoft on 'Face & Vertex Normal Vectors'. :)

Petr Schreiber
01-05-2008, 18:56
Hi Matthew,

thanks for nice link.
The interpolation of 2 normals to simulate smooth shading is basically what TBGL_NORMAL_SMOOTH style does in TBGL.

Interesting could be to interpolate, but with respect to size of polygons - bigger polygons would have more influence on final normal vector than smaller ones. Will try to experiment with it during the summer.


Thanks and please post even more interesting sources!,
Petr

ReneMiner
06-11-2012, 01:28
...Interesting could be to interpolate, but with respect to size of polygons - bigger polygons would have more influence on final normal vector than smaller ones. Will try to experiment with it during the summer....

ok, this seems the reason for my problem, which i have today. If i calculate vertex-normals by adding up all faces normals and dividing by count of vertices that share this position it always looks like this (see picture). There are lines visible that are not supposed to be there.

now I just wanted to know: did you find some algorithm in the meantime? do i "just" have to multiply the normal-vector with surface (square-units) to retrieve "its weight of influence" on that corner of a face? what factor should I use - i won't turn them up too high nor too low.

I'm dealing here with display-list-entities, so the m15-method to recalc normals is no option in this case. or have i overseen some similar method to entities/display-lists? is there one?
or is there a possibility to push the display-list-data into a m15-model, let this do the interpolation due recalcNormals and read out calculated normal-values from m15?

Petr Schreiber
06-11-2012, 11:25
I think the reason is mainly in fact TBGL performs the light calculation on per-vertex basis.

For best results, you could use native OpenGL and pixel shaders, which can help making light calculations on per-pixel basis. Mastering the shaders is rewarding yet time intensive task, I have created some examples here (http://www.thinbasic.com/community/showthread.php?8218-Bonus-for-tB-1-4-0-1-9th-of-July-release&highlight=glsl). Learning shaders is not a "weekend challenge", but more likely a long time project.


Petr

ReneMiner
06-11-2012, 13:14
thanks for the link Petr, interesting examples, I did not know that there's sooo much inside tbgl. but they do not really solve my problem. I want to save a user-drawn 3d-picture and therefor i want to calculate the vertex-normals. the result (see picture) shows that my kind of calculating them is not good and i don't know any other way.

I tried to calculate some vertex-normals, where i imagine some reference-face where I know the exact normals like flat a square with one unit length and width (open edges) to re-normalize so I use factor 1.0 of light on 1.0 square-unit of surface to multiply the normals before adding them up, it gets much too bright because most of the faces have more than one square-unit, was the wrong way...
Next I added up all surfaces square-units (of course just them where the vertex is member) and divided the total of surface by that current triangles share to have a factor how much this vertex-normal should get of the face-value. the thinner corners of the triangle get too bright- now I was fed up with trying, went another way:

I save my picture as m15, just having face-normals applied and reload the model with plain, straight tbgl_method TBGL_m15LoadModel and perform just TBGL_m15RecalcNormals with %TBGL_NORMAL_SMOOTH-switch, it looks great, thats the result I wanted to see!

So I won't crank my brain on calculating that myself -the method is already built in there somewhere- I want just peek out the already calculated vertex-normals from the tbgl-engine. But where can I find them?
I tried already all kinds of combinations like TBGL_m15GetNormal,TBGL_m15GetNormals with x, nX , underscores (my hope was, you forgot to mention them in help file), tried to find something with hex-editor and dll-viewer inside tbgl.dll but no success :(

Petr Schreiber
06-11-2012, 19:34
The approach for %TBGL_NORMAL_SMOOTH works on this principle:

#1 Calculate normal for each triangle
You can use TBGL_m15GetVertexXYZ to retrieve the vertices, TBGL_m15GetVertexPStop to find out if it is the last vertex in polygon.

The normal for single triangle is calculated as:


ux = vertexB.x - vertexA.x
uy = vertexB.y - vertexA.y
uz = vertexB.z - vertexA.z

vx = vertexC.x - vertexA.x
vy = vertexC.y - vertexA.y
vz = vertexC.z - vertexA.z

normalX = (uy * vz - uz * vy)
normalY = -(ux * vz - uz * vx)
normalZ = (ux * vy - uy * vx)

normalLength = SQR(normalX*normalX + normalY*normalY + normalZ*normalZ)

normalX /= normalLength
normalY /= normalLength
normalZ /= normalLength


#2 For each vertex find all triangles which share it

#3 The final normal composed from all the normals, calculated like


N = number of triangles which share the same vertex

FinalNormal.X = ( Normal(1).X + Normal(2).X + ... + Normal(N).X ) / N
FinalNormal.Y = ( Normal(1).Y + Normal(2).Y + ... + Normal(N).Y ) / N
FinalNormal.Z = ( Normal(1).Z + Normal(2).Z + ... + Normal(N).Z ) / N

Petr

ReneMiner
06-11-2012, 21:54
nice that you point it out exactly the way i described above...at least i got the formula right.

How comes, the m15-mesh (same vertices) looks so much better? is there some (hardware)-shading or smoothing enabled by default when using m15? Does it have different light?
Is my Entity-Spotlight (0.5 above cam-Y, centered XZ between camera and object to look at) the bad guy in this issue? I'm gonna try right now...

Edit: tried... directional light looks better. not perfect but better.