
  type vector3f
  single x
  single y
  single z
  end type

  type triangle9f
  vector3f p1
  vector3f p2
  vector3f p3
  end type

'#recordof vector3f

  '-----------------------------
  sub ScaleVector(n as vector3f)
  '=============================

  'scale to unit length
  '--------------------
  
  double s=1/sqr (n.x*n.x + n.y*n.y + n.z*n.z)
  n.x *=s
  n.y *=s
  n.z *=s
  end sub


  'http://www.mvps.org/directx/articles/math/dot/index.htm
  'http://en.wikipedia.org/wiki/Dot_product

  '----------------------------------------------------------
  function DotProduct(u as vector3f, v as vector3f) as single
  '==========================================================
  return u.x*v.x + u.y*v.y + u.z*v.z
  end function



/*
Surface Normals
===============

These are vectors perpendicular to a surface.
They are mostly used to calculate light reflected from 3d surfaces.

http://www.opengl.org/wiki/Calculating_a_Surface_Normal
discussion on use of normals:
http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/


Pseudocode
----------

Begin Function CalculateSurfaceNormal (Input Triangle) Returns Vector

	Set Vector U to (Triangle.p2 minus Triangle.p1)
	Set Vector V to (Triangle.p3 minus Triangle.p1)

	Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y)
	Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z)
	Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x)

	Returning Normal

End Function
*/


  '------------------------------------------
  sub normal(t as triangle9f, n as vector3f)
  '==========================================

  'assumes points are defined anticlockwise

  vector3f u,v

  'set Vector U to (Triangle.p2 minus Triangle.p1)
  'set Vector V to (Triangle.p3 minus Triangle.p1)

  u.x=t.p2.x-t.p1.x
  u.y=t.p2.y-t.p1.y
  u.z=t.p2.z-t.p1.z
  v.x=t.p3.x-t.p1.x
  v.y=t.p3.y-t.p1.y
  v.z=t.p3.z-t.p1.z



  'Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y)

  n.x= (u.y*v.z)-(u.z*v.y)

  'Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z)

  n.y=(u.z*v.x)-(u.x*v.z)

  'Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x)

  n.z=(u.x*v.y)-(u.y*v.x)

  ScaleVector n

  end sub

  '=========

  triangle9f t
  vector3f   n
  
  
  t<=(-1,0,0, 1,0,0, 0,1,0)
  normal t,n
  string cm=","
  print "Normal=" n.x cm n.y cm n.z



