Author Topic: 3d normal calculations  (Read 3245 times)

0 Members and 1 Guest are viewing this topic.

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
3d normal calculations
« on: February 15, 2009 »
Is the following a correct way of calculating the normal of a triangle surface?

Code: [Select]
1
|\
| \
|  \
|___\
2    3

Local ax:Float = x2-x1
Local ay:Float = y2-y1
Local az:Float = z2-z1
Local bx:Float = x3-x1
Local by:Float = y3-y1
Local bz:Float = z3-z1
Local nx:Float = (ay*bz) - (az*by)
Local ny:Float = (az*bx) - (ax*bz)
Local nz:Float = (ax*by) - (ay*bx)
Local length:Float = Sqr(nx*nx + ny*ny + nz*nz)
nx :/ length
ny :/ length
nz :/ length

x1-x3, y1-y2, z1-z3 are the position vectors of the three points of the triangle. Nx,ny,nz are the final normals (hopefully).

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: 3d normal calculations
« Reply #1 on: February 15, 2009 »
Looks fine.
See cross-product:

Some sort of oop-structure for 3d vectors usually helps to get such things running straight.
Challenge Trophies Won:

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: 3d normal calculations
« Reply #2 on: February 15, 2009 »
I had made a question to why a light was shining wrong, but it turned out, that it was me not understanding that a directional light, its not a position but a vector of the direction of the light that it gets. So I got the light to behave as I wanted. One less thing to worry about :)
« Last Edit: February 15, 2009 by zawran »

Online Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17407
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: 3d normal calculations
« Reply #3 on: February 17, 2009 »
It's nice when things resolve themselves like that :)
Shockwave ^ Codigos
Challenge Trophies Won: