def look(_, up): forward = _v.normalize(_v.sub(_.lookAt, _.position)) side = _v.normalize(_v.cross(forward, up)) up = _v.cross(side, forward) matrix = [[side[0], side[1], side[2], _.position[0]], [up[0], up[1], up[2], _.position[1]], [-forward[0], -forward[1], -forward[2], _.position[2]], [0, 0, 0, 1]] return matrix
def IndexedNormals(V, F): ''' Keep the face index, and provide one normal per vertex ''' # normals = np.ndarray((len(V)*3,3), dtype=np.float32) normals = [[] for x in range(len(V))] A = [[] for x in range(len(V))] # normals = np.ndarray((len(F)*3,3), dtype=np.float32) x = 0 for f in F: p0, p1, p2 = [V[f[x]] for x in range(3)] n = _v.cross(_v.vector(p0, p1), _v.vector(p0, p2)) for i in range(3): # in triangle # print (f[i] , n) # normals[x].append(n) # append the face normal # normals[x].append(f[i]) # keep the vertex index normals[f[i]].append(n) # would index the normal by face # compute the adjacency list while we are in A[f[i]] += [fa for fa in f if fa != f[i] and fa not in A[f[i]]] x += 1 for i, n in enumerate(normals): # print(n,len(n),_v.sum(*n), _v.div(float(len(n)),_v.sum(*n))) # weighted by the number of face = wrong normals[i] = _v.div(float(len(n)), _v.sum(*n)) # print('>',normals[i]) # print('#',A) # print('//',normals, len(normals)) return normals
def lookB(_, phi, theta, psi): ''' spherical coordinates only time with euler angles, promise ''' _.lookAt = ( sin(psi) * cos(theta), sin(psi) * sin(theta), # =x tan(theta) cos(theta)) forward = _v.normalize(_v.sub(_.lookAt, _.position)) side = _v.normalize(_v.cross(forward, _.up)) up = _v.cross(side, forward) matrix = [[side[0], side[1], side[2], _.position[0]], [up[0], up[1], up[2], _.position[1]], [-forward[0], -forward[1], -forward[2], _.position[2]], [0, 0, 0, 1]] return matrix
def target_orientation(_): up = (sin(roll * DEGREE_TO_RADIAN), cos(roll * DEGREE_TO_RADIAN), 0) ''' * the following section of code is the sgl version of * * gluLookAt( position.x, position.y, position.z, * lookAt.x, lookAt.y, lookAt.z, * up.u, up.v, up.w ) ''' forward = _v.normalize(_v.sub(_.lookAt, _.position)) side = _v.normalize(_v.cross(forward, up)) up = _v.cross(side, forward) _.matrix = [[side[0], side[1], side[2], position.x], [up[0], up[1], up[2], position.y], [-forward[0], -forward[1], -forward[2], position.z], [0, 0, 0, 1]]
def vertexAndNormalsList(V, F): ''' one normal per faces = one normal for each distinct vertices no shared verts. Gouraud Shading, diamond sharp edges the unormalized normals list, indexed on the vertex list unormalized = relative to the surface of the face. check the influence of the face size on the illumination model ''' vertices = np.ndarray((len(F) * 3, 3), dtype=np.float32) normals = np.ndarray((len(F) * 3, 3), dtype=np.float32) x = 0 for f in F: p0, p1, p2 = [V[f[y]] for y in range(3)] n = _v.cross(_v.vector(p0, p1), _v.vector(p0, p2)) for i in range(3): vertices[x] = V[f[i]] # flatten / recopy the vertex # the three vertex hold the same normal value normals[x] = _v.div(_v.mag(n), n) x += 1 return vertices, normals
[4, 5, 0, 1], ] def rgb(x, y, z): return x / 2 + .5, y / 2 + .5, z / 2 + .5 sizes = [] # noinspection SpellCheckingInspection verticies, normals, colors = [], [], [] for indexes in faces: sizes.append(len(indexes)) p0, p1, p2 = [points[indexes[i]] for i in range(3)] normal = _v.cross(_v.vector(p0, p1), _v.vector(p0, p2)) for index in indexes: vertex = points[index] verticies.append(vertex) normals.append(normal) colors.append(rgb(*vertex)) # noinspection SpellCheckingInspection tex_coords = verticies # noinspection SpellCheckingInspection indicies = list(range(sum(sizes))) # noinspection SpellCheckingInspection __all__ = [ "sizes", "indicies",