DGL
https://delphigl.com/forum/

smooth normals
https://delphigl.com/forum/viewtopic.php?f=19&t=2807
Seite 1 von 1

Autor:  noeska [ So Mai 02, 2004 19:46 ]
Betreff des Beitrags:  smooth normals

I tried adding calculation of smoothed normals to gl3ds. But i have the feeling i did something wrong.

The following is what i added to the calcvnormals procedure:
Code:
  1.   //real smooth normals...
  2.   vnormal.x:=0.0;
  3.   vnormal.y:=0.0;
  4.   vnormal.z:=0.0;
  5.   for m := 0 to FNumMeshes - 1 do
  6.   begin
  7.     if FMesh[m].FNumIndices > 0 then
  8.     begin
  9.       f := 0;
  10.       while f < FMesh[m].FNumIndices - 1 do // go through all vertexes and
  11.       begin
  12.        Find:=FMesh[m].FVertex[FMesh[m].FIndices[f]];
  13.        for J:=0 to FMesh[m].FNumIndices-1 do
  14.         if (FMesh[m].FVertex[FMesh[m].FIndices[j]].x=Find.x) or (FMesh[m].FVertex[FMesh[m].FIndices[j]].y=Find.y) or (FMesh[m].FVertex[FMesh[m].FIndices[j]].z=Find.z) then
  15.        begin
  16.          vnormal.x:=vnormal.x+FMesh[m].FVNormal[FMesh[m].FIndices[j]].x;
  17.          vnormal.y:=vnormal.y+FMesh[m].FVNormal[FMesh[m].FIndices[j]].y;
  18.          vnormal.z:=vnormal.z+FMesh[m].FVNormal[FMesh[m].FIndices[j]].z;
  19.          Inc(Shared);
  20.        end;
  21.  
  22.         vertexn.x:=vnormal.x / shared;
  23.         vertexn.y:=vnormal.y / shared;
  24.         vertexn.z:=vnormal.z / shared;
  25.  
  26.         L:=sqrt(sqr (vertexn.x) + sqr(vertexn.y) + sqr(vertexn.z));
  27.         if L>0 then
  28.         begin
  29.          vertexn.x:=vertexn.x/L;
  30.          vertexn.y:=vertexn.y/L;
  31.          vertexn.z:=vertexn.z/L;
  32.          FMesh[m].FVnormal[FMesh[m].FIndices[f]] := vertexn;
  33.         end;
  34.  
  35.         Shared:=0;
  36.  
  37.         vnormal.x:=0;
  38.         vnormal.y:=0;
  39.         vnormal.z:=0;
  40.  
  41.         f := f + 1;
  42.       end;
  43.     end;
  44.   end;


Dateianhänge:
Dateikommentar: test version with smoothed normals calculation
gl3ds.zip [17.18 KiB]
331-mal heruntergeladen

Autor:  Magellan [ So Mai 02, 2004 20:56 ]
Betreff des Beitrags: 

I couldn’t find any mistake in your calculations. But you forgot setting “Shared” to zero at the beginning. So “Shared” isn’t defined in your first loop, where you increase “Shared”.
This could be a reason for wrong calculations.

Autor:  Mars [ Mo Mai 03, 2004 08:27 ]
Betreff des Beitrags: 

Magellan is right, and since you normalize your normal vectors anyway, you can skip these lines
Code:
  1.         vertexn.x:=vnormal.x / shared;
  2.         vertexn.y:=vnormal.y / shared;
  3.         vertexn.z:=vnormal.z / shared;


since they have no effect (they don't change the direction of the vector), alternatively you can skip the normalizing step, if you *know* that the original plane normals are unit length (but I would not count on it).

Autor:  Sascha Willems [ Mo Mai 03, 2004 17:46 ]
Betreff des Beitrags: 

Sry for being a bit OT, but don't just do a plain smooth normals-function, cause it won't give you usefull results on many models (it actually only works with continues models, that are models without hard egdes like terrains or spheres). Just imagine a cube, where your smoothing routine just would blindly take all vertices into account without caring about hard edges. This is how a cube is supposed to look like with correct normals :
Bild
If you just smooth blindly without accounting hard edges, your cube would (lit) look like this :
Bild

So if you want really correct smooth normals, you'll have to implement something like an edge-tolerance. (take a look at this link to get more infos and some source on the matter)

In the end, I would leave the work for the 3D-Modeller you're using. 3DSMax for example has a modifier called "smooth" that has a checkbox (Autosmooth) that let's 3DS generate totally correct normals.

Autor:  noeska [ Mo Mai 03, 2004 18:55 ]
Betreff des Beitrags: 

ah that could be why the result looks ok on one mesh, but bad on another. It is a pitty i can't find the normals inside the 3ds files.

Autor:  Sascha Willems [ Mo Mai 03, 2004 18:59 ]
Betreff des Beitrags: 

What do you mean by that? Your loader correctly loads and sets the normals inside any 3DS-Files I've used until now. So maybe it's the fault of the app you generate your 3DS-Meshes with.

Autor:  noeska [ Mo Mai 03, 2004 19:21 ]
Betreff des Beitrags: 

inside the 3ds file the normal data is not stored so i have to calculate it. So i cannot leave the work to the 3ds editor as the normals are not stored inside the 3ds file.

I always calculated them per face and stored it 3 times (for each vertex) for each face. Now i added smoothing them with the idea that the meshes would look better. But that is not always the case.

Autor:  Sascha Willems [ Mo Mai 03, 2004 19:42 ]
Betreff des Beitrags: 

Ah, sorry, I didn't saw that you calculate them by yourself for now. But there seems to be a way to get the normals out of a 3DS-File. That should work easily for meshes that don't use smoothing groups :

Frome some info-file on the 3DS-Format hat geschrieben:
The order has a purpose: to give the direction for the normal
of each face.
If you turn a screw (standard screw) in the way the vertices
indicate you will find the normal.
If vertices given in order are A B C:

C
^
|
A-----B

This means unscrewing = the normal points out of the screen.


So you can get a faces normal out of the winding of it's vertices. Sadly there aren't more infos on that topic, and all other docs on 3DS I found until now just ignore the fact that there's a way to derive the normals from a 3DS-File.

Edit : Just found this document that seems to explain how to get normals for 3DS-Files. Hope it helps, I'll also try to figure out how to do it and let you know.

Autor:  Sascha Willems [ Mo Mai 10, 2004 14:59 ]
Betreff des Beitrags: 

Since I've now also wanted to get totally correct normals, I've dug a little deeper into the 3DS-format and tried to implement smoothing groups. Info on if a face belongs to such a smoothinggroup and in which smoothing group it is is stored in the chunk with the ID $4150. But documentation on 3DS is either incomplete or wrong, so implementing smoothing groups isn't very easy at all. The two docs I have on the 3DS-fileformat include wrong informations on what is stored in that chunk and I had to take a look at another loader to see how to get it right. Right now it's kind of working on some meshes as you can see here :
Bild Bild
(left = no smoothinggroups, right = with smoothinggroups, note that normals look wrong, that's cause I'm applying kind of a gammafunction to make them easier to see)

As you can see, especially the pillar on the left shot has no really smooth normals, whereas the right one has due to the use of smoothinggroups. But as you can also see, some other parts of the mesh have totally wrong normals when using smoothinggroups. But as said above, documentation on that matter is really bad (or better said non-present at all, all docs I have only say how the chunk looks, not how it's used), so I'm right now trying to figure out by myself how it works. I'll let you know if I'll (when ever) get it working...

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/