Author: JonNeal
Date: 2008-07-15 20:26:40 -0400 (Tue, 15 Jul 2008)
New Revision: 1320

Modified:
   trunk/pysoy/include/math.pxd
   trunk/pysoy/include/soy.scenes.pxd
   trunk/pysoy/src/scenes/Landscape.pym
Log:
Ticket #929 :
  * started work on norms


Modified: trunk/pysoy/include/math.pxd
===================================================================
--- trunk/pysoy/include/math.pxd        2008-07-15 21:50:03 UTC (rev 1319)
+++ trunk/pysoy/include/math.pxd        2008-07-16 00:26:40 UTC (rev 1320)
@@ -22,3 +22,4 @@
   long int         lroundf   ( float )
   double           fabs      ( double )
   float            fabsf     ( float )
+  double           sqrt      ( double )

Modified: trunk/pysoy/include/soy.scenes.pxd
===================================================================
--- trunk/pysoy/include/soy.scenes.pxd  2008-07-15 21:50:03 UTC (rev 1319)
+++ trunk/pysoy/include/soy.scenes.pxd  2008-07-16 00:26:40 UTC (rev 1320)
@@ -21,6 +21,7 @@
 cimport glib
 cimport ode
 cimport py
+cimport math
 cimport soy._internals
 cimport soy._datatypes
 cimport soy.colors

Modified: trunk/pysoy/src/scenes/Landscape.pym
===================================================================
--- trunk/pysoy/src/scenes/Landscape.pym        2008-07-15 21:50:03 UTC (rev 
1319)
+++ trunk/pysoy/src/scenes/Landscape.pym        2008-07-16 00:26:40 UTC (rev 
1320)
@@ -87,6 +87,7 @@
   cdef void _createArrays(self) :
     cdef int _i, _j, _currentLoop, _offset
     cdef unsigned short _a, _b, _c, _d
+    cdef double _v1[3], _v2[3], _normal[3], _normal2[3], _length
     #
     # Calculate positions and texcoords first
     for _i from 0 <= _i < self._heightmapTex._height :
@@ -106,14 +107,84 @@
     # Normals and tangents calculated second because they depend on position
     for _i from 0 <= _i < self._heightmapTex._height :
       for _j from 0 <= _j < self._heightmapTex._width :
+        _normal[0] = 0
+        _normal[1] = 0
+        _normal[2] = 0
+        _normal2[0] = 0
+        _normal2[1] = 0
+        _normal2[2] = 0
         _offset = self._heightmapTex._width*_i+_j
-        self._vertArray[_offset].nx = 0
-        self._vertArray[_offset].ny = 1 # Up
-        self._vertArray[_offset].nz = 0
+        # 
+        # for code cleanlyness, get the four verticies first as:
+        # 
+        # c-b  Note CCW winding order.
+        # |\|   vector1 (b) = cb 
+        # a     vector2 (a) = ca
+        # 
+        _a = _offset+self._heightmapTex._width
+        _b = _offset+1
+        _c = _offset
+        #
+        # check if we are at either of two sides which only have 3 verts to 
calc
+        if _j+1 != self._heightmapTex._width and _i+1 != 
self._heightmapTex._height :
+          # subtract the vertices to get vectors
+          _v1[0] = self._vertArray[_c].px-self._vertArray[_b].px
+          _v2[0] = self._vertArray[_c].px-self._vertArray[_a].px
+          _v1[1] = self._vertArray[_c].py-self._vertArray[_b].py
+          _v2[1] = self._vertArray[_c].py-self._vertArray[_a].py
+          _v1[2] = self._vertArray[_c].pz-self._vertArray[_b].pz
+          _v2[2] = self._vertArray[_c].pz-self._vertArray[_a].pz
+          # perform cross products on the two vectors
+          _normal[0] = _v1[1]*_v2[2]-_v1[2]*_v2[1] #Calculate the x component 
of the normal
+          _normal[1] = _v1[2]*_v2[0]-_v1[0]*_v2[2] #Calculate the y component 
of the normal
+          _normal[2] = _v1[0]*_v2[1]-_v1[1]*_v2[0] #Calculate the z component 
of the normal
+        
+        # check if we are at either of two sides which only have 3 verts to 
calc
+        if _j == 0 or _i == 0 :
+          #   a 
+          # |\|   vector1 (b) = cb 
+          # b c   vector2 (a) = ca
+          # 
+          _a = _offset-self._heightmapTex._width
+          _b = _offset-1
+          _c = _offset
+          # subtract the vertices to get vectors
+          _v1[0] = self._vertArray[_b].px-self._vertArray[_c].px
+          _v2[0] = self._vertArray[_a].px-self._vertArray[_c].px
+          _v1[1] = self._vertArray[_b].py-self._vertArray[_c].py
+          _v2[1] = self._vertArray[_a].py-self._vertArray[_c].py
+          _v1[2] = self._vertArray[_b].pz-self._vertArray[_c].pz
+          _v2[2] = self._vertArray[_a].pz-self._vertArray[_c].pz
+          # perform cross products on the two vectors
+          _normal2[0] = _v1[1]*_v2[2]-_v1[2]*_v2[1] #Calculate the x component 
of the normal
+          _normal2[1] = _v1[2]*_v2[0]-_v1[0]*_v2[2] #Calculate the y component 
of the normal
+          _normal2[2] = _v1[0]*_v2[1]-_v1[1]*_v2[0] #Calculate the z component 
of the normal
+        
+        # last thing of all, average the two together and set them as the 
normals!
+        if _normal[0] == 0 and _normal[1] == 0 and _normal[2] == 0 :
+          _normal[0] = _normal2[0]
+          _normal[1] = _normal2[1]
+          _normal[2] = _normal2[2]
+        elif _normal2[0] != 0 and _normal2[1] != 0 and _normal2[2] != 0 :
+          _normal[0] += _normal2[0]
+          _normal[1] += _normal2[1]
+          _normal[2] += _normal2[2]
+          _normal[0] /= 2
+          _normal[1] /= 2
+          _normal[2] /= 2
+        _length = 
math.sqrt(_normal[0]*_normal[0]+_normal[1]*_normal[1]+_normal[2]*_normal[2])
+        _normal[0] /= _length
+        _normal[1] /= _length
+        _normal[2] /= _length
+        stdio.printf("vert %d {nx: %f, ny: %f, nz: %f}\n", _offset, 
_normal[0], _normal[1], _normal[2]);
+        self._vertArray[_offset].nx = _normal[0]
+        self._vertArray[_offset].ny = _normal[1]
+        self._vertArray[_offset].nz = _normal[2]
         self._vertArray[_offset].ux = 1 # Up
         self._vertArray[_offset].uy = 0
         self._vertArray[_offset].uz = 0
-    #clear vars for next loops!
+    
+    #and finally, set up the face array to make all of the triangles
     _currentLoop = 0
     #loop through all of the quads in the grid
     for _i from 0 <= _i < self._heightmapTex._height-1 :

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to