def key_event(window, key, scancode, action, mods): global cameraPos, cameraFront, cameraUp, polygonal_mode cameraSpeed = 0.8 if key == 87 and (action == 1 or action == 2) and ( cameraPos + (cameraSpeed * cameraFront)).y > 2 and ( cameraPos + (cameraSpeed * cameraFront)).y < 245: # tecla W cameraPos += cameraSpeed * cameraFront if key == 83 and (action == 1 or action == 2): # tecla S cameraPos -= cameraSpeed * cameraFront if key == 65 and (action == 1 or action == 2): # tecla A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 68 and (action == 1 or action == 2): # tecla D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 80 and action == 1 and polygonal_mode == True: polygonal_mode = False else: if key == 80 and action == 1 and polygonal_mode == False: polygonal_mode = True
def rotation(self, orig, dest): identityQuat = glm.quat(1.0, 0.0, 0.0, 0.0) epsilon = 0.00001 cosTheta = glm.dot(orig, dest) if cosTheta >= 1.0 - epsilon: #// orig and dest point in the same direction return identityQuat if cosTheta < -1.0 + epsilon: ''' // special case when vectors in opposite directions : // there is no "ideal" rotation axis // So guess one; any will do as long as it's perpendicular to start // This implementation favors a rotation around the Up axis (Y), // since it's often what you want to do. ''' rotationAxis = glm.cross(glm.vec3(0.0, 0.0, 1.0), orig) if glm.length( rotationAxis ) < epsilon: # // bad luck, they were parallel, try again! rotationAxis = glm.cross(glm.vec3(1.0, 0.0, 0.0), orig) rotationAxis = glm.normalize(rotationAxis) return glm.angleAxis(glm.pi(), rotationAxis) #// Implementation from Stan Melax's Game Programming Gems 1 article rotationAxis = glm.cross(orig, dest) s = math.sqrt((1.0 + cosTheta) * 2.0) invs = 1.0 / s return glm.quat(s * 0.5, rotationAxis.x * invs, rotationAxis.y * invs, rotationAxis.z * invs)
def key_event(window, key, scancode, action, mods): global cameraPos, cameraFront, cameraUp, polygonal_mode lim_x = limita_bordas(cameraPos.x, cameraFront.x, [-2.0, 27.0], key) lim_y = limita_bordas(cameraPos.y, cameraFront.y, [0.0, 28.0], key) lim_z = limita_bordas(cameraPos.z, cameraFront.z, [-14.0, 18.0], key) if (lim_x and lim_y and lim_z): cameraSpeed = 0.5 else: cameraSpeed = 0.0 if key == 87 and (action == 1 or action == 2): # tecla W cameraPos += cameraSpeed * cameraFront if key == 83 and (action == 1 or action == 2): # tecla S cameraPos -= cameraSpeed * cameraFront if key == 65 and (action == 1 or action == 2): # tecla A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 68 and (action == 1 or action == 2): # tecla D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 80 and action == 1 and polygonal_mode == True: polygonal_mode = False else: if key == 80 and action == 1 and polygonal_mode == False: polygonal_mode = True
def process(self): # build view matrix position = self.world.component_for_entity(self.world.camera_id, com.Transformation).position orientation = self.world.component_for_entity(self.world.camera_id, com.CameraOrientation) forward = glm.normalize(position - orientation.look_at) right = glm.normalize(glm.cross(orientation.up, forward)) up = glm.normalize(glm.cross(forward, right)) mat = glm.mat4x4(1.0) mat[0][0] = right.x mat[1][0] = right.y mat[2][0] = right.z mat[0][1] = up.x mat[1][1] = up.y mat[2][1] = up.z mat[0][2] = forward.x mat[1][2] = forward.y mat[2][2] = forward.z mat[3][0] = -(glm.dot(right, position)) mat[3][1] = -(glm.dot(up, position)) mat[3][2] = -(glm.dot(forward, position)) self.world.view_matrix = mat # Upload shader data self.world.standard_shader.start() self.world.standard_shader.set_view_matrix(self.world.view_matrix) self.world.standard_shader.load_light_setup(self.world.light_setup)
def plane_intersection(p1: vec3, d1: vec3, p2: vec3, d2: vec3): """ Compute the line of intersection of the two planes. Note: if the two planes are parallel or equal this returns None. :param p1: a point in the first plane :param d1: a vector normal to the first plane :param p2: a point in the second plane :param d2: a normal vector of the second plane :return: None if they are parallel else (p3, d3) where p3 is a point in the line of intersection and d3 is the direction of this line """ d1 = normalize(d1) d2 = normalize(d2) if d1 in (d2, -d2): # planes are parallel return None d3 = cross(d1, d2) # d3 and v1 are an orthonormal base of the first plane v1 = cross(d3, d1) b = -dot(p1, d2) / dot(v1, d2) p3 = p1 + b * v1 return p3, d3
def mousehandler(self, window, x, y): if self.firstmouse: self.lastX = x self.lastY = y self.firstmouse = False xOff = x - self.lastX yOff = self.lastY - y self.lastX = x self.lastY = y xOff *= self.sensitivity yOff *= self.sensitivity if glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS: self.yaw += xOff self.pitch += yOff if self.pitch > 89: self.pitch = 89 if self.pitch < -89: self.pitch = -89 direction = glm.vec3(0.0, 0.0, 0.0) direction.x = cos(radians(self.yaw)) * cos(radians(self.pitch)) direction.y = sin(radians(self.pitch)) direction.z = sin(radians(self.yaw)) * cos(radians(self.pitch)) self.cameraFront = glm.normalize(direction) self.cameraRight = glm.normalize( glm.cross(self.cameraFront, glm.vec3(0.0, 1.0, 0.0))) self.cameraUp = glm.normalize( glm.cross(self.cameraRight, self.cameraFront))
def update(self, deltaTime): self.updated = False if self.typecam == CameraType.firstperson: if self.moving(): camFront = glm.vec3() camFront.x = -math.cos(glm.radians( self.rotation.x)) * math.sin(glm.radians(self.rotation.y)) camFront.y = math.sin(glm.radians(self.rotation.x)) camFront.z = math.cos(glm.radians(self.rotation.x)) * math.cos( glm.radians(self.rotation.y)) camFront = glm.normalize(camFront) moveSpeed = deltaTime * self.movementSpeed if self.keys['up']: self.position += camFront * moveSpeed elif self.keys['down']: self.position -= camFront * moveSpeed elif self.keys['left']: self.position -= glm.normalize( glm.cross(camFront, glm.vec3(0.0, 1.0, 0.0))) * moveSpeed elif self.keys['right']: self.position += glm.normalize( glm.cross(camFront, glm.vec3(0.0, 1.0, 0.0))) * moveSpeed self.updateViewMatrix()
def __init__(self, eye, forward, up, fovy): self.eye = eye self.forward = glm.normalize(forward) self.horizontal = glm.normalize(glm.cross(self.forward, up)) self.up = glm.normalize(glm.cross(self.horizontal, self.forward)) self.fovy2 = fovy / 2.0 self.fovx2 = self.fovy2
def updateCameraVectors(self): front_x = np.cos(glm.radians(self.yaw) * np.cos(glm.radians(self.pitch))) front_y = np.sin(glm.radians(self.pitch)) front_z = np.sin(glm.radians(self.yaw) * np.cos(glm.radians(self.pitch))) self.front = glm.normalize(glm.vec3(front_x, front_y, front_z)) self.right = glm.normalize(glm.cross(self.front, self.world_up)) self.up = glm.normalize(glm.cross(self.right, self.front))
def align_direction(self, old_dir, new_dir): norm = glm.vec3(new_dir) up = glm.vec3(old_dir) c = glm.cross(norm, up) up = glm.cross(c, norm) quat = glm.quatLookAt(up, norm) self._trans = glm.rotate(self._trans, glm.angle(quat), glm.axis(quat))
def UpdateCameraVectors(self): front = glm.vec3(1) front.x = cos(glm.radians(self.Yaw)) * cos(glm.radians(self.Pitch)) front.y = sin(-glm.radians(self.Pitch)) front.z = sin(glm.radians(self.Yaw)) * cos(glm.radians(self.Pitch)) self.Front = glm.normalize(front) self.Right = glm.normalize(glm.cross(self.Front, self.WorldUp)) self.Up = glm.normalize(glm.cross(self.Right, self.Front))
def key_event(window, key, scancode, action, mods): global fovy, aspect, near, far global cameraPos, cameraFront, cameraUp, polygonal_mode cameraSpeed = 5 if key == 87 and (action == 1 or action == 2): # key W cameraPos += cameraSpeed * cameraFront if key == 83 and (action == 1 or action == 2): # key S cameraPos -= cameraSpeed * cameraFront if key == 65 and (action == 1 or action == 2): # key A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 68 and (action == 1 or action == 2): # key D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if cameraPos[1] > 97: cameraPos[1] = 97 elif cameraPos[1] < 1: cameraPos[1] = 1 if cameraPos[0] < -96: cameraPos[0] = -96 elif cameraPos[0] > 96: cameraPos[0] = 96 if cameraPos[2] < -96: cameraPos[2] = -96 elif cameraPos[2] > 96: cameraPos[2] = 96 if key == 80 and action == 1 and polygonal_mode == True: polygonal_mode = False elif key == 80 and action == 1 and polygonal_mode == False: polygonal_mode = True if key == 49: # key 1, increase 'fovy' fovy += 1 elif key == 50: # key 2, decrease 'fovy' fovy -= 1 if key == 51: # key 3, increase 'aspect' aspect += 0.1 elif key == 52: # key 4, decrease 'aspect' aspect -= 0.1 if key == 53: # key 5, increase 'near' near += 0.1 elif key == 54: # key 6, decrease 'near' near -= 0.1 if key == 55: # key 7, increase 'far' far += 500 elif key == 56: # key 8, decrease 'far' far -= 500
def updateCameraVectors(self): self.front.x = glm.cos(glm.radians(self.yaw)) * glm.cos( glm.radians(self.pitch)) self.front.y = glm.sin(glm.radians(self.pitch)) self.front.z = glm.sin(glm.radians(self.yaw)) * glm.cos( glm.radians(self.pitch)) self.front = glm.normalize(self.front) self.right = glm.normalize(glm.cross(self.front, self.worldUp)) self.up = glm.normalize(glm.cross(self.right, self.front))
def processInput(self, window, deltaTime): delta = self.speed * deltaTime if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS: self.pos += delta * self.front if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS: self.pos -= delta * self.front if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS: self.pos -= glm.normalize(glm.cross(self.front, self.up)) * delta if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS: self.pos += glm.normalize(glm.cross(self.front, self.up)) * delta
def getTranslationRotationMatricesForCircles(mesh): rms, tms = [], [] v_n0 = glm.vec3(0.0, 1.0, 0.0) # normal vector of first circle area! for i in range(0, n): # 0,... n - 1 (= last position of the pose) p_i = mesh.positions[ i] # each mesh contains the positions p_i of the pose rm, tm = glm.mat4(), glm.mat4() # initialize unit matrices tm = glm.translate(tm, glm.vec3(p_i[0], p_i[1], p_i[2])) # each p_i: [x, y, z] tms.append(tm) # if first pose position: if i == 0: rms.append(rm) # append a unit matrix = no rotation continue # if last pose position: if i == n - 1: # position vector at: i-1 p_i_prev = glm.vec3(mesh.positions[i - 1][0], mesh.positions[i - 1][1], mesh.positions[i - 1][2]) # normalized gradient vector at p_i: v_ni = glm.normalize(p_i - p_i_prev) if v_ni == v_n0: rms.append(rm) # append a unit matrix = no rotation continue angle = glm.acos(glm.length(v_n0 * v_ni)) # get the rotation angle rot_axis = glm.cross(v_n0, v_ni) # get the rotation axis rm = glm.rotate(rm, angle, rot_axis) rms.append(rm) # append the rotation matrix continue # position vector at: i+1 p_i_next = glm.vec3(mesh.positions[i + 1][0], mesh.positions[i + 1][1], mesh.positions[i + 1][2]) # position vector at: i-1 p_i_prev = glm.vec3(mesh.positions[i - 1][0], mesh.positions[i - 1][1], mesh.positions[i - 1][2]) # normalized gradient vector at p_i: v_ni = glm.normalize(p_i_next - p_i_prev) if v_ni == v_n0: rms.append(rm) # append a unit matrix = no rotation continue angle = glm.acos(glm.length(v_n0 * v_ni)) # get the rotation angle rot_axis = glm.cross(v_n0, v_ni) # get the rotation axis rm = glm.rotate(rm, angle, rot_axis) rms.append(rm) # append the rotation matrix return rms, tms
def rotate(self, x, y): alpha = x * self.ROTATION_RADIAN_PER_PIXEL beta = y * self.ROTATION_RADIAN_PER_PIXEL self.front = glm.normalize(self.front * math.cos(alpha) - self.right * math.sin(alpha)) self.right = glm.normalize(glm.cross(self.up, self.front)) self.front = glm.normalize(self.front * math.cos(beta) + self.up * math.sin(beta)) self.up = glm.normalize(glm.cross(self.front, self.right)) self.set_projection_parameters()
def quaternion_of_rotation(rotation:glm.Mat3) -> glm.Quat: """ Note that R . axis = axis RI = (R - I*999/1000) Then RI . axis = axis/1000 Then det(RI) is going to be 1/1000 * at most 2 * at most 2 And if na is perp to axis, then RI.na = R.na - 999/1000.na, which is perp to axis Then |R.na| < 2|na| If RI' . RI = I, then consider v' = RI' . v for some v=(a*axis + b*na0 + c*na1) (a'*axis + b'*na0 + c'*na1) = RI' . (a*axis + b*na0 + c*na1) Then RI . (a'*axis + b'*na0 + c'*na1) = (a*axis + b*na0 + c*na1) Then a'*RI.axis + b'*RI.na0 + c'*RI.na1 = a*axis + b*na0 + c*na1 Then a'/1000*axis + b'*(R.na0-0.999.na0) + c'*(R.na1-0.999.na1) = a*axis + b*na0 + c*na1 Then a = a'/1000, and -0.999b' + b'cos(angle) + c'sin(angle) = b, etc If we set |v| to be 1, then |v'| must be det(RI') = 1/det(RI) > 100 If angle is not close to zero, then a' / b' >> 1 This can be repeated: v' = normalize(RI' . v) v'' = normalize(RI' . v') v''' = normalize(RI' . v'') etc This gets closer and closer to the axis """ rot_min_id : glm.Mat3 = rotation - (0.99999 * glm.mat3()) # type: ignore rot_min_id_i : glm.Mat3 = glm.inverse(rot_min_id) # type: ignore for j in range(3): v = glm.vec3() v[j] = 1. for i in range(10): last_v = v rid_i_v : glm.Vec3 = rot_min_id_i * v # type: ignore v = glm.normalize(rid_i_v) pass axis = v dist2 = glm.length2(v - last_v) # type: ignore if dist2<0.00001: break pass w = glm.vec3([1.0,0,0]) if axis[0]>0.9 or axis[0]<-0.9: w = glm.vec3([0,1.0,0]) na0 : glm.Vec3 = glm.normalize(glm.cross(w, axis)) na1 : glm.Vec3 = glm.cross(axis, na0) # Rotate w_perp_n around the axis of rotation by angle A na0_r : glm.Vec3 = rotation * na0 # type: ignore na1_r : glm.Vec3 = rotation * na1 # type: ignore # Get angle of rotation cos_angle = glm.dot(na0, na0_r) sin_angle = -glm.dot(na0, na1_r) angle = math.atan2(sin_angle, cos_angle) # Set quaternion return glm.angleAxis(angle, axis)
def key_event(window, key, scancode, action, mods): global cameraPos, cameraFront, cameraUp, polygonal_mode, obj_x, obj_y, obj_z """para limitar a camera, a idea era criar uma função (getHei) que retorne a coordenada Y (altura) do chao e do céu e limitar a movimentação da câmera. Porém desta forma fica muito ineficiente e mesmo assim (até a data de entrega) não consegui retornar o valor desejado """ #print('camera Y:',cameraPos.y, ' Terrain heigth: ',getHei(cameraPos.x,cameraPos.z,chaoVert)) #controles de objeto (para descobrir os valores de translação de cada matriz model) if key == 266 and (action == 1 or action == 2): # tecla pageup obj_z += .5 print('Z: ', obj_z) if key == 267 and (action == 1 or action == 2): # tecla pagedown obj_z -= .5 print('Z: ', obj_z) if key == 268 and (action == 1 or action == 2): # tecla home obj_y += .5 print('Y: ', obj_y) if key == 269 and (action == 1 or action == 2): # tecla end obj_y -= .5 print('Y: ', obj_y) if key == 260 and (action == 1 or action == 2): # tecla insert obj_x += .5 print('X: ', obj_x) if key == 261 and (action == 1 or action == 2): # tecla delete obj_x -= .5 print('X: ', obj_x) cameraSpeed = 3.2 #movimentar a camera um pouco mais rapido if key == 79 and (action == 1 or action == 2): # tecla O sobe rapidao cameraPos += 30 * cameraFront if key == 76 and (action == 1 or action == 2): # tecla l desce rapidao cameraPos -= 30 * cameraFront if key == 87 and (action == 1 or action == 2): # tecla W cameraPos += cameraSpeed * cameraFront if key == 83 and (action == 1 or action == 2): # tecla S cameraPos -= cameraSpeed * cameraFront if key == 65 and (action == 1 or action == 2): # tecla A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 68 and (action == 1 or action == 2): # tecla D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 80 and action == 1 and polygonal_mode == True: polygonal_mode = False else: if key == 80 and action == 1 and polygonal_mode == False: polygonal_mode = True
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 = glm.normalize(front) self.camera_right = glm.normalize( glm.cross(self.camera_front, glm.vec3(0.0, 1.0, 0.0))) self.camera_up = glm.normalize( glm.cross(self.camera_right, self.camera_front))
def key_event(window, key, scancode, action, mods): global cameraPos, cameraFront, cameraUp, turnOn, ka_offset, obj_x, obj_y, obj_z #para ajuste de posições if key == 266 and (action == 1 or action == 2): # tecla pageup obj_y += .5 print('Y: ', obj_y) if key == 267 and (action == 1 or action == 2): # tecla pagedown obj_y -= .5 print('Y: ', obj_y) if key == 263 and (action == 1 or action == 2): # tecla up obj_z += .5 print('Z: ', obj_z) if key == 262 and (action == 1 or action == 2): # tecla down obj_z -= .5 print('Z: ', obj_z) if key == 265 and (action == 1 or action == 2): # tecla ri obj_x += .5 print('X: ', obj_x) if key == 264 and (action == 1 or action == 2): # tecla le obj_x -= .5 print('X: ', obj_x) cameraSpeed = 3.2 if key == 87 and (action == 1 or action == 2): # tecla W cameraPos += cameraSpeed * cameraFront #print(cameraPos) if key == 83 and (action == 1 or action == 2): # tecla S cameraPos -= cameraSpeed * cameraFront #print(cameraPos) if key == 65 and (action == 1 or action == 2): # tecla A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed #print(cameraPos) if key == 68 and (action == 1 or action == 2): # tecla D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed #print(cameraPos) if key == 76 and (action == 1 or action == 2): # tecla l turnOn = not turnOn if key == 85 and (action == 1 or action == 2): # tecla u ka_offset += 0.05 print("ambient (+)") if key == 80 and (action == 1 or action == 2): # tecla p ka_offset -= 0.05 print("ambient (-)") checkPosition(cameraPos)
def set_projection_parameters(self): self.front = glm.normalize(self.front) self.right = glm.normalize(glm.cross(self.up, self.front)) self.up = glm.normalize(glm.cross(self.front, self.right)) self.view_ratio = self.zoom * self.bounding_box.GetMaxExtent() if self.field_of_view == self.FIELD_OF_VIEW_MIN: # Orthogonal Projection self.distance = self.view_ratio / math.tan( self.FIELD_OF_VIEW_STEP * 0.5 / 180.0 * math.pi) else: self.distance = self.view_ratio / math.tan( self.field_of_view * 0.5 / 180.0 * math.pi) self.eye = self.lookat + self.front * self.distance
def __update_camera_vectors(self): # calculate the new Front vector self.look_direction.x = math.cos(glm.radians(self.yaw)) * math.cos( glm.radians(self.pitch)) self.look_direction.y = math.sin(glm.radians(self.pitch)) self.look_direction.z = math.sin(glm.radians(self.yaw)) * math.cos( glm.radians(self.pitch)) self.look_direction = glm.normalize(self.look_direction) self.right_direction = glm.normalize( glm.cross(self.look_direction, self.up_direction)) self.up_direction = glm.normalize( glm.cross(self.right_direction, self.look_direction))
def on_key_press(symbol, modifiers): print("KEYPRESSED: ",symbol, modifiers) cameraSpeed = 0.05 if symbol == key.W: camera.pos += cameraSpeed * camera.front*2 if symbol == key.S: camera.pos -= cameraSpeed * camera.front*2 if symbol == key.A: camera.pos -= glm.normalize(glm.cross(camera.front, camera.up)) * cameraSpeed if symbol == key.D: camera.pos += glm.normalize(glm.cross(camera.front, camera.up)) * cameraSpeed
def on_draw(self, event): #Read about depth testing and changing stated in vispy here http://vispy.org/gloo.html?highlight=set_state gloo.clear(color=[0.2, 0.3, 0.3, 1.0], depth=True) #delta_time self.current_frame = time() self.delta_time = self.current_frame - self.last_frame self.last_frame = self.current_frame if self.bool_a: self.cameraPos -= glm.normalize(glm.cross(self.cameraFront, self.cameraUp)) * self.cameraSpeed * self.delta_time if self.bool_w: self.cameraPos += self.cameraSpeed * self.cameraFront * self.delta_time if self.bool_s: self.cameraPos -= self.cameraSpeed * self.cameraFront * self.delta_time if self.bool_d: self.cameraPos += glm.normalize(glm.cross(self.cameraFront, self.cameraUp)) * self.cameraSpeed * self.delta_time self.view = glm.lookAt(self.cameraPos, self.cameraPos + self.cameraFront, self.cameraUp) self.projection = glm.mat4(1.0) self.projection = glm.perspective(glm.radians(self.fov), builtins.width/builtins.height, 0.1, 100.0) # vispy takes numpy array in m * n matrix form self.view = (np.array(self.view.to_list()).astype(np.float32)) self.projection = (np.array(self.projection.to_list()).astype(np.float32)) # reshaping to (m, n) to (1, m*n) to support data input in vispy self.view = self.view.reshape((1, self.view.shape[0] * self.view.shape[1])) self.projection = self.projection.reshape((1, self.projection.shape[0] * self.projection.shape[1])) self.program['view'] = self.view self.program['projection'] = self.projection i = 0 for cubePosition in self.cubePositions: self.model = glm.mat4(1.0) self.model = glm.translate(self.model, cubePosition) if i % 3 == 0: self.model = glm.rotate(self.model, glm.radians((time() - self.startTime) * glm.radians(2000) * i/2), glm.vec3(1.0, 0.3, 0.5)) self.model = (np.array(self.model.to_list()).astype(np.float32)) self.model = self.model.reshape((1, self.model.shape[0] * self.model.shape[1])) self.program['model'] = self.model self.program.draw('triangles') i += 1 self.update()
def process_input(window): if glfw.get_key(window, glfw.KEY_ESCAPE) == glfw.PRESS: glfw.set_window_should_close(window, True) global camera_pos, camera_front, camera_up camera_speed = 0.05 if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS: camera_pos += camera_speed * camera_front if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS: camera_pos -= camera_speed * camera_front if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS: camera_pos -= glm.normalize(glm.cross(camera_front, camera_up)) * camera_speed if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS: camera_pos += glm.normalize(glm.cross(camera_front, camera_up)) * camera_speed
def __init__(self, lookfrom, lookat, vup, vfov, aspect): self.theta = vfov * M_PI / 180 self.half_height = math.tan(self.theta / 2) self.half_width = aspect * self.half_height self.origin = lookfrom self.w = glm.vec3( (lookfrom - lookat) / (glm.length(lookfrom - lookat))) self.u = glm.vec3( glm.cross(vup, self.w) / glm.length(glm.cross(vup, self.w))) self.v = glm.cross(self.w, self.u) self.lower_left_corner = glm.vec3(-self.half_width, -self.half_height, -1.0) self.lower_left_corner = self.origin - self.half_width * self.u - self.half_height * self.v - self.w self.horizontal = 2 * self.half_width * self.u self.vertical = 2 * self.half_height * self.v
def key_event(window,key,scancode,action,mods): global cameraPos, cameraFront, cameraUp cameraSpeed = 0.01 if key == 87 and (action==1 or action==2): # tecla W cameraPos += cameraSpeed * cameraFront if key == 83 and (action==1 or action==2): # tecla S cameraPos -= cameraSpeed * cameraFront if key == 65 and (action==1 or action==2): # tecla A cameraPos -= glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed if key == 68 and (action==1 or action==2): # tecla D cameraPos += glm.normalize(glm.cross(cameraFront, cameraUp)) * cameraSpeed
def setFromUW(self, u, w): # Set up an ONB from the normalized input vectors 'u' and 'w', that will be assumed to be aligned to # the vectors 'u' (right) and 'w' vectors of the ONB to be created. self.u = u self.w = w self.v = glm.cross(self.w, self.u) self.setBasisMatrix()
def move_right(self, meters): ''' Moves the camera to the right of the look direction by specified meters :param meters: Amount of meters to move :return: ''' self.__camera_coords += glm.normalize(glm.cross(self.__camera_front, self.__camera_up)) * meters
def _compute_rest_rotation(self, direction): def compute_rotation(a, v): c = math.cos(a) s = math.sin(a) axis = glm.normalize(v) tmp = glm.vec3((1.0 - c) * axis) R = glm.mat4(c + ((1) - c) * axis[0] * axis[0], ((1) - c) * axis[0] * axis[1] + s * axis[2], ((1) - c) * axis[0] * axis[2] - s * axis[1], (0), ((1) - c) * axis[1] * axis[0] - s * axis[2], c + ((1) - c) * axis[1] * axis[1], ((1) - c) * axis[1] * axis[2] + s * axis[0], (0), ((1) - c) * axis[2] * axis[0] + s * axis[1], ((1) - c) * axis[2] * axis[1] - s * axis[0], c + ((1) - c) * axis[2] * axis[2], (0), 0, 0, 0, 1) return R initial_dir_unnorm = glm.vec3(direction) initial_dir = glm.normalize(initial_dir_unnorm) ortho = glm.cross(self._reference_up, initial_dir) angle = math.acos(glm.dot(self._reference_up, initial_dir)) if math.isnan(angle): return glm.mat4(1.0) return compute_rotation(angle, ortho) #glm.rotate(glm.mat4(), angle, ortho)