def _gl_look_at(self, pos, target, up) -> numpy.ndarray: """The standard lookAt method. Args: pos: current position target: target position to look at up: direction up Returns: numpy.ndarray: The matrix """ z = vector.normalise(pos - target) x = vector.normalise(vector3.cross(vector.normalise(up), z)) y = vector3.cross(z, x) translate = matrix44.create_identity() translate[3][0] = -pos.x translate[3][1] = -pos.y translate[3][2] = -pos.z rotate = matrix44.create_identity() rotate[0][0] = x[0] # -- X rotate[1][0] = x[1] rotate[2][0] = x[2] rotate[0][1] = y[0] # -- Y rotate[1][1] = y[1] rotate[2][1] = y[2] rotate[0][2] = z[0] # -- Z rotate[1][2] = z[1] rotate[2][2] = z[2] return matrix44.multiply(translate, rotate)
def mouseMovimiento(self, xoffset, yoffset, detector=True): # Valores del mouse xoffset = xoffset * self.sensibilidad yoffset = yoffset * self.sensibilidad self.dataX = self.dataX + xoffset self.dataY = self.dataY + yoffset # Si detecta un mouse que tome los siguientes datos if detector: # Si el dato es mayor o menor al dato en y que tome los datos if (self.dataY > 45.0): self.dataY = 45.0 if (self.dataY < -45.0): self.dataY = -45.0 # Vector para la camara frontal front = Vector3([0.0, 0.0, 0.0]) # Encontramos cada uno de los datos de la camara front tanto en x, y e z front.x = cos(radians(self.dataX)) * cos(radians(self.dataY)) front.y = sin(radians(self.dataY)) front.z = sin(radians(self.dataX)) * cos(radians(self.dataY)) # Normalizamos el vector, para obtener datos cercanos # debug ---> print(self.frontal) self.frontal = vector.normalise(front) # debug ---> print(self.derecha) # debug ---> print(self.arriba) self.derecha = vector.normalise( vector3.cross(self.frontal, Vector3([0.0, 1.0, 0.0]))) self.arriba = vector.normalise( vector3.cross(self.derecha, self.frontal))
def move_camera(self, direction): current_frame = time.get_ticks() delta_time = current_frame - self.last_frame camera_speed = delta_time / 20000 if direction == "UP": self.camera_position[1] += camera_speed self.last_cam_y = self.camera_position[1] if direction == "DOWN": self.camera_position[1] -= camera_speed self.last_cam_y = self.camera_position[1] if direction == "FORWARD": self.camera_position += camera_speed * self.camera_front if direction == "BACK": self.camera_position -= camera_speed * self.camera_front if direction == "LEFT": self.camera_position -= vector.normalise( vector3.cross(np.array(self.camera_front, dtype=np.float32), np.array([0., 1., 0.], dtype=np.float32))) * camera_speed if direction == "RIGHT": self.camera_position += vector.normalise( vector3.cross(np.array(self.camera_front, dtype=np.float32), np.array([0., 1., 0.], dtype=np.float32))) * camera_speed self.camera_position[1] = self.last_cam_y
def look_at(self, position, target, world_up): # 1.Position = known # 2.Calculate cameraDirection zaxis = vector.normalise(position - target) # 3.Get positive right axis vector xaxis = vector.normalise( vector3.cross(vector.normalise(world_up), zaxis)) # 4.Calculate the camera up vector yaxis = vector3.cross(zaxis, xaxis) # create translation and rotation matrix translation = Matrix44.identity() translation[3][0] = -position.x translation[3][1] = -position.y translation[3][2] = -position.z rotation = Matrix44.identity() rotation[0][0] = xaxis[0] rotation[1][0] = xaxis[1] rotation[2][0] = xaxis[2] rotation[0][1] = yaxis[0] rotation[1][1] = yaxis[1] rotation[2][1] = yaxis[2] rotation[0][2] = zaxis[0] rotation[1][2] = zaxis[1] rotation[2][2] = zaxis[2] return translation * rotation
def update_camera_vectors(self): front = Vector3([0.0, 0.0, 0.0]) front.x = cos(radians(self.yaw)) * cos(radians(self.pitch)) front.y = sin(radians(self.pitch)) front.z = sin(radians(self.yaw)) * cos(radians(self.pitch)) self.camera_front = vector.normalise(front) self.camera_right = vector.normalise(vector3.cross(self.camera_front, Vector3([0.0, 1.0, 0.0]))) self.camera_up = vector.normalise(vector3.cross(self.camera_right, self.camera_front))
def _update_yaw_and_pitch(self) -> None: """Updates the camera vectors based on the current yaw and pitch""" front = Vector3([0.0, 0.0, 0.0]) front.x = cos(radians(self.yaw)) * cos(radians(self.pitch)) front.y = sin(radians(self.pitch)) front.z = sin(radians(self.yaw)) * cos(radians(self.pitch)) self.dir = vector.normalise(front) self.right = vector.normalise(vector3.cross(self.dir, self._up)) self.up = vector.normalise(vector3.cross(self.right, self.dir))
def update_camera_vectors(self): front = glm.vec3(0.0, 0.0, 0.0) front.x = cos(radians(self.jaw)) * cos(radians(self.pitch)) front.y = sin(radians(self.pitch)) front.z = sin(radians(self.jaw)) * cos(radians(self.pitch)) self.camera_front = vector.normalise(front) self.camera_right = vector.normalise( vector3.cross(self.camera_front, glm.vec3(0.0, 1.0, 0.0))) self.camera_up = vector.normalise( vector3.cross(self.camera_right, self.camera_front))
def Axis_from_jaw_pitch(jaw, pitch): front = Vector3([0.0, 0.0, 0.0]) # jaw, pitch are spherical coordinates that define a unit vector front.x = cos(radians(jaw)) * cos(radians(pitch)) front.z = sin(radians(pitch)) front.y = sin(radians(jaw)) * cos(radians(pitch)) right = vector3.cross(front, Vector3([0.0, 0.0, 1.0])) up = vector3.cross(right, front) return (front, right, up)
def AdjustView(self, eyePosition, targetPosition, upVector): eye = Vector3(eyePosition) target = Vector3(targetPosition) up = Vector3(upVector) zaxis = vector.normalise(eye - target) # The "forward" vector. xaxis = vector.normalise(vector3.cross(up, zaxis)) # The "right" vector. yaxis = vector3.cross(zaxis, xaxis) # The "up" vector. # Create a 4x4 view matrix from the right, up, forward and eye position vectors self.view.r1 = [xaxis[0], yaxis[0], zaxis[0], 0] self.view.r2 = [xaxis[1], yaxis[1], zaxis[1], 0] self.view.r3 = [xaxis[2], yaxis[2], zaxis[2], 0] self.view.r4 =[-vector3.dot( xaxis, eye ), -vector3.dot( yaxis, eye ), -vector3.dot( zaxis, eye ), 1 ] return self.view
def test_operators_vector3(self): v1 = Vector3() v2 = Vector3([1.,2.,3.]) # add self.assertTrue(np.array_equal(v1 + v2, [1.,2.,3.])) # subtract self.assertTrue(np.array_equal(v1 - v2, [-1.,-2.,-3.])) # multiply self.assertTrue(np.array_equal(v1 * v2, [0.,0.,0.])) # divide self.assertTrue(np.array_equal(v1 / v2, [0.,0.,0.])) # or self.assertTrue(np.array_equal(v1 | v2, vector3.dot(v1, v2))) # xor self.assertTrue(np.array_equal(v1 ^ v2, vector3.cross(v1, v2))) # == self.assertTrue(Vector3() == Vector3()) self.assertFalse(Vector3() == Vector3([1.,1.,1.])) # != self.assertTrue(Vector3() != Vector3([1.,1.,1.])) self.assertFalse(Vector3() != Vector3())
def test_bitwise(self): v1 = Vector3([1.,0.,0.]) v2 = Vector3([0.,1.,0.]) # xor (cross) self.assertTrue(np.array_equal(v1 ^ v2, vector3.cross(v1, v2))) # or (dot) self.assertTrue(np.array_equal(v1 | v2, vector3.dot(v1, v2)))
def test_cross_batch(self): result = vector3.cross([[1., 0., 0.], [0., 0., 1.]], [ [0., 1., 0.], [0., 1., 0.], ]) expected = [ [0., 0., 1.], [-1., 0., 0.], ] np.testing.assert_almost_equal(result, expected, decimal=5)
def track_update_camera_vectors(self): pos = Vector3([0.0, 0.0, 0.0]) pos.x = self.distance * cos(radians(self.yaw)) * cos( radians(self.pitch)) pos.y = self.distance * sin(radians(self.pitch)) pos.z = self.distance * sin(radians(self.yaw)) * cos( radians(self.pitch)) self.camera_pos = pos self.camera_right = vector.normalise( vector3.cross(self.world_centre, self.camera_up))
def test_cross_batch(self): result = vector3.cross([ [1.,0.,0.], [0.,0.,1.] ],[ [0.,1.,0.], [0.,1.,0.], ]) expected = [ [0.,0.,1.], [-1.,0.,0.], ] np.testing.assert_almost_equal(result, expected, decimal=5)
def look_at(position: Vector3, target: Vector3, world_up: Vector3) -> Matrix44: z_axis: Vector3 = vector.normalise(position - target) x_axis: Vector3 = vector.normalise( vector3.cross(vector.normalise(world_up), z_axis)) y_axis: Vector3 = vector3.cross(z_axis, x_axis) translation: Matrix44 = Matrix44.identity() translation[3][0] = -position.x translation[3][1] = -position.y translation[3][2] = -position.z rotation: Matrix44 = Matrix44.identity() rotation[0][0] = x_axis[0] rotation[1][0] = x_axis[1] rotation[2][0] = x_axis[2] rotation[0][1] = y_axis[0] rotation[1][1] = y_axis[1] rotation[2][1] = y_axis[2] rotation[0][2] = z_axis[0] rotation[1][2] = z_axis[1] rotation[2][2] = z_axis[2] return rotation * translation
def lookAtMe(self, position, target, world_up): # Calculamos la direccion de la camara en z zaxis = vector.normalise(position - target) # Calculamos la direccion de la camara en x xaxis = vector.normalise( vector3.cross(vector.normalise(world_up), zaxis)) # Calculamos la direccion de la camara en y yaxis = vector3.cross(zaxis, xaxis) # Creamos la matriz de transaltion y de rotation translation = Matrix44.identity() rotation = Matrix44.identity() ''' matrix identidad = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] ''' # Dentro de la matriz ingresamos cada uno de los valores translation[3][0] = -position.x translation[3][1] = -position.y translation[3][2] = -position.z # Añadimos a la matriz los valores rotation[0][0] = xaxis[0] rotation[1][0] = xaxis[1] rotation[2][0] = xaxis[2] rotation[0][1] = yaxis[0] rotation[1][1] = yaxis[1] rotation[2][1] = yaxis[2] rotation[0][2] = zaxis[0] rotation[1][2] = zaxis[1] rotation[2][2] = zaxis[2] return translation * rotation
def update_camera_vectors(self): if not self.rotate_around_base: front: Vector3 = Vector3([0.0, 0.0, 0.0]) front.x = cos(radians(self.yaw + self.yaw_offset)) * cos( radians(self.pitch)) front.y = sin(radians(self.pitch)) front.z = sin(radians(self.yaw + self.yaw_offset)) * cos( radians(self.pitch)) self.camera_front = vector.normalize(front) self.camera_right = vector.normalize( vector3.cross(self.camera_front, Vector3([0.0, 1.0, 0.0]))) self.camera_up = vector.normalise( vector3.cross(self.camera_right, self.camera_front)) self.camera_pos = self.camera_pos + self.camera_right * self.move_vector.x * self.move_speed self.camera_pos = self.camera_pos + self.camera_up * self.move_vector.y * self.move_speed self.camera_pos = self.camera_pos + self.camera_front * self.move_vector.z * self.move_speed else: self.yaw_offset += self.rotation_speed front: Vector3 = Vector3([0.0, 0.0, 0.0]) front.x = cos(radians(self.yaw + self.yaw_offset)) * cos( radians(self.pitch)) front.y = sin(radians(self.pitch)) front.z = sin(radians(self.yaw + self.yaw_offset)) * cos( radians(self.pitch)) front_offset = vector.normalize(front) - self.camera_front self.camera_pos -= front_offset * vector.length(self.camera_pos - self.base) self.camera_front = vector.normalize(front) self.camera_right = vector.normalize( vector3.cross(self.camera_front, Vector3([0.0, 1.0, 0.0]))) self.camera_up = vector.normalise( vector3.cross(self.camera_right, self.camera_front)) self.camera_pos = self.camera_pos + self.camera_right * self.move_vector.x * self.move_speed self.camera_pos = self.camera_pos + self.camera_up * self.move_vector.y * self.move_speed self.camera_pos = self.camera_pos + self.camera_front * self.move_vector.z * self.move_speed
def look_at(self, position, target, world_up): """creates view matrix based on three arguments: camera position, camera target, and camera up Parameters ---------- position: vector position of the camera target: vector point the camera is looking at up: vector pointing in the up direction Returns ------- view matrix Raises ------ Notes ----- """ #returns view matrix based on position, target, and up vector # 1.Position = known # 2.Calculate cameraDirection zaxis = vector_normalise(position - target) # 3.Get positive right axis vector xaxis = vector_normalise(np.cross(vector_normalise(world_up), zaxis)) # 4.Calculate the camera up vector yaxis = vector3.cross(zaxis, xaxis) # create translation and rotation matrix translation = self.IDENTITY translation[3][0] = -position[0] translation[3][1] = -position[1] translation[3][2] = -position[2] rotation = self.IDENTITY2 rotation[0][0] = xaxis[0] rotation[1][0] = xaxis[1] rotation[2][0] = xaxis[2] rotation[0][1] = yaxis[0] rotation[1][1] = yaxis[1] rotation[2][1] = yaxis[2] rotation[0][2] = zaxis[0] rotation[1][2] = zaxis[1] rotation[2][2] = zaxis[2] return np.matmul(translation, rotation)
def look_at(self, position, target, up=Vector3([0., 1., 0.])): direction = vector.normalise(position - target) direction = np.array(direction, dtype=np.float32) up = np.array(up, dtype=np.float32) camera_right = vector.normalise(vector3.cross(up, direction)) camera_up = vector.normalise(vector3.cross(direction, camera_right)) translation = Matrix44.identity() translation[3][0] = -position[0] translation[3][1] = -position[1] translation[3][2] = -position[2] rotation = Matrix44.identity() rotation[0][0] = camera_right[0] rotation[1][0] = camera_right[1] rotation[2][0] = camera_right[2] rotation[0][1] = camera_up[0] rotation[1][1] = camera_up[1] rotation[2][1] = camera_up[2] rotation[0][2] = direction[0] rotation[1][2] = direction[1] rotation[2][2] = direction[2] return np.array(translation * rotation, dtype=np.float32)
def cross(self, other): return type(self)(vector3.cross(self[:3], other[:3]))
def test_cross_coincident(self): result = vector3.cross([1., 0., 0.], [1., 0., 0.]) np.testing.assert_almost_equal(result, [0., 0., 0.], decimal=5)
def test_cross_single_vector(self): result = vector3.cross([1., 0., 0.], [0., 1., 0.]) np.testing.assert_almost_equal(result, [0., 0., 1.], decimal=5)
def test_cross_single_vector(self): result = vector3.cross([1.,0.,0.], [0.,1.,0.]) np.testing.assert_almost_equal(result, [0.,0.,1.], decimal=5)
def test_cross_coincident(self): result = vector3.cross([1.,0.,0.], [1.,0.,0.]) np.testing.assert_almost_equal(result, [0.,0.,0.], decimal=5)