Ticket #1987: normal.diff

File normal.diff, 2.7 KB (added by sanderd17, 11 years ago)
  • source/graphics/Terrain.cpp

     
    144144{
    145145    CVector3D left, right, up, down;
    146146
    147     // Calculate normals of the four half-tile triangles surrounding this vertex:
     147    // Use the cross product of the vectors cutting this position
    148148
    149     // get position of vertex where normal is being evaluated
    150     CVector3D basepos;
    151     CalcPosition(i, j, basepos);
     149    CalcPosition(i-1, j, left);
     150    CalcPosition(i+1, j, right);
     151    CalcPosition(i, j-1, up);
     152    CalcPosition(i, j+1, down);
    152153
    153     if (i > 0) {
    154         CalcPosition(i-1, j, left);
    155         left -= basepos;
    156         left.Normalize();
    157     }
     154    // calculate cross product, leave out zero terms
     155    normal = CVector3D(left.Y-right.Y, 2*TERRAIN_TILE_SIZE, up.Y-down.Y);
    158156
    159     if (i < m_MapSize-1) {
    160         CalcPosition(i+1, j, right);
    161         right -= basepos;
    162         right.Normalize();
    163     }
    164 
    165     if (j > 0) {
    166         CalcPosition(i, j-1, up);
    167         up -= basepos;
    168         up.Normalize();
    169     }
    170 
    171     if (j < m_MapSize-1) {
    172         CalcPosition(i, j+1, down);
    173         down -= basepos;
    174         down.Normalize();
    175     }
    176 
    177     CVector3D n0 = up.Cross(left);
    178     CVector3D n1 = left.Cross(down);
    179     CVector3D n2 = down.Cross(right);
    180     CVector3D n3 = right.Cross(up);
    181 
    182     // Compute the mean of the normals
    183     normal = n0 + n1 + n2 + n3;
     157    // Normalize
    184158    float nlen=normal.Length();
    185159    if (nlen>0.00001f) normal*=1.0f/nlen;
    186160}
     
    191165{
    192166    CFixedVector3D left, right, up, down;
    193167
    194     // Calculate normals of the four half-tile triangles surrounding this vertex:
     168    // Use the cross product of the vectors cutting this position
    195169
    196     // get position of vertex where normal is being evaluated
    197     CFixedVector3D basepos;
    198     CalcPositionFixed(i, j, basepos);
     170    CalcPositionFixed(i-1, j, left);
     171    CalcPositionFixed(i+1, j, right);
     172    CalcPositionFixed(i, j-1, up);
     173    CalcPositionFixed(i, j+1, down);
    199174
    200     if (i > 0) {
    201         CalcPositionFixed(i-1, j, left);
    202         left -= basepos;
    203         left.Normalize();
    204     }
     175    // calculate cross product, leave out zero terms
     176    normal = CFixedVector3D(left.Y-right.Y,fixed::FromInt(2*TERRAIN_TILE_SIZE), up.Y-down.Y);
    205177
    206     if (i < m_MapSize-1) {
    207         CalcPositionFixed(i+1, j, right);
    208         right -= basepos;
    209         right.Normalize();
    210     }
    211 
    212     if (j > 0) {
    213         CalcPositionFixed(i, j-1, up);
    214         up -= basepos;
    215         up.Normalize();
    216     }
    217 
    218     if (j < m_MapSize-1) {
    219         CalcPositionFixed(i, j+1, down);
    220         down -= basepos;
    221         down.Normalize();
    222     }
    223 
    224     CFixedVector3D n0 = up.Cross(left);
    225     CFixedVector3D n1 = left.Cross(down);
    226     CFixedVector3D n2 = down.Cross(right);
    227     CFixedVector3D n3 = right.Cross(up);
    228 
    229     // Compute the mean of the normals
    230     normal = n0 + n1 + n2 + n3;
     178    // Normalize
    231179    normal.Normalize();
    232180}
    233181