| 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] |