Changeset 1320

Show
Ignore:
Timestamp:
07/15/08 20:26:40 (5 months ago)
Author:
JonNeal
Message:

Ticket #929 :

  • started work on norms
Location:
trunk/pysoy
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pysoy/include/math.pxd

    r1299 r1320  
    2323  double           fabs      ( double ) 
    2424  float            fabsf     ( float ) 
     25  double           sqrt      ( double ) 
  • trunk/pysoy/include/soy.scenes.pxd

    r1317 r1320  
    2222cimport ode 
    2323cimport py 
     24cimport math 
    2425cimport soy._internals 
    2526cimport soy._datatypes 
  • trunk/pysoy/src/scenes/Landscape.pym

    r1319 r1320  
    8888    cdef int _i, _j, _currentLoop, _offset 
    8989    cdef unsigned short _a, _b, _c, _d 
     90    cdef double _v1[3], _v2[3], _normal[3], _normal2[3], _length 
    9091    # 
    9192    # Calculate positions and texcoords first 
     
    107108    for _i from 0 <= _i < self._heightmapTex._height : 
    108109      for _j from 0 <= _j < self._heightmapTex._width : 
     110        _normal[0] = 0 
     111        _normal[1] = 0 
     112        _normal[2] = 0 
     113        _normal2[0] = 0 
     114        _normal2[1] = 0 
     115        _normal2[2] = 0 
    109116        _offset = self._heightmapTex._width*_i+_j 
    110         self._vertArray[_offset].nx = 0 
    111         self._vertArray[_offset].ny = 1 # Up 
    112         self._vertArray[_offset].nz = 0 
     117        #  
     118        # for code cleanlyness, get the four verticies first as: 
     119        #  
     120        # c-b  Note CCW winding order. 
     121        # |\|   vector1 (b) = cb  
     122        # a     vector2 (a) = ca 
     123        #  
     124        _a = _offset+self._heightmapTex._width 
     125        _b = _offset+1 
     126        _c = _offset 
     127        # 
     128        # check if we are at either of two sides which only have 3 verts to calc 
     129        if _j+1 != self._heightmapTex._width and _i+1 != self._heightmapTex._height : 
     130          # subtract the vertices to get vectors 
     131          _v1[0] = self._vertArray[_c].px-self._vertArray[_b].px 
     132          _v2[0] = self._vertArray[_c].px-self._vertArray[_a].px 
     133          _v1[1] = self._vertArray[_c].py-self._vertArray[_b].py 
     134          _v2[1] = self._vertArray[_c].py-self._vertArray[_a].py 
     135          _v1[2] = self._vertArray[_c].pz-self._vertArray[_b].pz 
     136          _v2[2] = self._vertArray[_c].pz-self._vertArray[_a].pz 
     137          # perform cross products on the two vectors 
     138          _normal[0] = _v1[1]*_v2[2]-_v1[2]*_v2[1] #Calculate the x component of the normal 
     139          _normal[1] = _v1[2]*_v2[0]-_v1[0]*_v2[2] #Calculate the y component of the normal 
     140          _normal[2] = _v1[0]*_v2[1]-_v1[1]*_v2[0] #Calculate the z component of the normal 
     141         
     142        # check if we are at either of two sides which only have 3 verts to calc 
     143        if _j == 0 or _i == 0 : 
     144          #   a  
     145          # |\|   vector1 (b) = cb  
     146          # b c   vector2 (a) = ca 
     147          #  
     148          _a = _offset-self._heightmapTex._width 
     149          _b = _offset-1 
     150          _c = _offset 
     151          # subtract the vertices to get vectors 
     152          _v1[0] = self._vertArray[_b].px-self._vertArray[_c].px 
     153          _v2[0] = self._vertArray[_a].px-self._vertArray[_c].px 
     154          _v1[1] = self._vertArray[_b].py-self._vertArray[_c].py 
     155          _v2[1] = self._vertArray[_a].py-self._vertArray[_c].py 
     156          _v1[2] = self._vertArray[_b].pz-self._vertArray[_c].pz 
     157          _v2[2] = self._vertArray[_a].pz-self._vertArray[_c].pz 
     158          # perform cross products on the two vectors 
     159          _normal2[0] = _v1[1]*_v2[2]-_v1[2]*_v2[1] #Calculate the x component of the normal 
     160          _normal2[1] = _v1[2]*_v2[0]-_v1[0]*_v2[2] #Calculate the y component of the normal 
     161          _normal2[2] = _v1[0]*_v2[1]-_v1[1]*_v2[0] #Calculate the z component of the normal 
     162         
     163        # last thing of all, average the two together and set them as the normals! 
     164        if _normal[0] == 0 and _normal[1] == 0 and _normal[2] == 0 : 
     165          _normal[0] = _normal2[0] 
     166          _normal[1] = _normal2[1] 
     167          _normal[2] = _normal2[2] 
     168        elif _normal2[0] != 0 and _normal2[1] != 0 and _normal2[2] != 0 : 
     169          _normal[0] += _normal2[0] 
     170          _normal[1] += _normal2[1] 
     171          _normal[2] += _normal2[2] 
     172          _normal[0] /= 2 
     173          _normal[1] /= 2 
     174          _normal[2] /= 2 
     175        _length = math.sqrt(_normal[0]*_normal[0]+_normal[1]*_normal[1]+_normal[2]*_normal[2]) 
     176        _normal[0] /= _length 
     177        _normal[1] /= _length 
     178        _normal[2] /= _length 
     179        stdio.printf("vert %d {nx: %f, ny: %f, nz: %f}\n", _offset, _normal[0], _normal[1], _normal[2]); 
     180        self._vertArray[_offset].nx = _normal[0] 
     181        self._vertArray[_offset].ny = _normal[1] 
     182        self._vertArray[_offset].nz = _normal[2] 
    113183        self._vertArray[_offset].ux = 1 # Up 
    114184        self._vertArray[_offset].uy = 0 
    115185        self._vertArray[_offset].uz = 0 
    116     #clear vars for next loops! 
     186     
     187    #and finally, set up the face array to make all of the triangles 
    117188    _currentLoop = 0 
    118189    #loop through all of the quads in the grid