home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2309 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: newsfeed.internetmci.com!xmission!xmission!not-for-mail
  2. From: butlerm@xmission.xmission.com (Mark David Butler)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: NormalVectors-How to compare angles?
  5. Date: 29 Jan 1996 22:24:48 -0700
  6. Organization: XMission Internet (801 539 0900)
  7. Message-ID: <4eka30$meh@xmission.xmission.com>
  8. References: <310CC190.78DF@gih.no>
  9. NNTP-Posting-Host: xmission.xmission.com
  10.  
  11. There is absolutly no reason to calculate angles and trigonometric functions
  12. when you can use dot products instead.
  13.  
  14. Given 3d vector points A,B,C in order such that (B-A) cross (C-A) is outward
  15. and L a unit vector that points toward the light source P.
  16.  
  17. D = B - A  
  18. E = C - A 
  19.  
  20. N = V1 cross V2 
  21.  
  22. Intensity = ambient_intensity + cos^2(angle between L and N) * factor
  23.  
  24. now the cos(angle between L and N) = L * N / N * N  so
  25.  
  26. Intensity = ambient_intensity + factor * ((L*N) / (N*N))^2
  27.         where N = (B-A) cross (C-A)
  28.  
  29. You can do this with nine subtractions, twelve additions, 14 multiplications,
  30. and one division per polygon.  You might have to throw in some
  31. left shifts to handle fixed point arithmetic properly.
  32.  
  33. Note if A,B are 3d vectors  A*B = Ax*Bx + Ay*By + Az*Bz
  34.  
  35. also mag(N) * mag(N) = N*N
  36.  
  37. also  A cross B = [ Ay*Bz - By*Az, Az*Bx - Bz*Ax, Ax*By-Bx*Ay ]
  38.  
  39. If you want to speed this up even more, precalculate N*N.  It is proportional
  40. to the area of the polygon and (obviously) invariant under rotation.
  41.  
  42.  
  43. In article <310CC190.78DF@gih.no> you write:
  44. >I'm trying to do gouroud shading in assem, and I need a fast way of comparing the vectors,
  45. >so I can calculate the colors. Is there a faster way of doing this then using normalvectors and
  46. >comparing
  47. >them to the vector from the lightsource (angles between vectors are a shitty job to do (roots
  48. >and sines)
  49. >especially in asm).
  50. >I can fake it if all the surfaces are the same size, but thats not what I want to do...
  51. >
  52. >I need some serious help here.......
  53. >PLEASE PLEASE PLEASE PLEASE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  54. >
  55. >
  56. >email me or ship it here...
  57. >
  58. >e-mail:haa_jako@hig.no
  59.  
  60.  
  61. -- 
  62. Mark David Butler         ( butlerm @ xmission.com )
  63.  
  64. -- 
  65. Mark David Butler         ( butlerm @ xmission.com )
  66.  
  67.