def drawFunc(): # Gets called in a loop glClearColor(1.0, 1.0, 1.0, 0.0) # Set clear color to white glClearDepth(1.0) glPointSize(5) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # FOV, AspectRatio, near, far projection = glm.perspective(glm.radians(camera.zoom), SCR_WIDTH * 1.0 / SCR_HEIGHT, 0.1, 200) view = camera.get_view_matrix() robot_program.use() # Load matrices in shader through set_matrix function robot_program.set_matrix("projection", glm.value_ptr(projection)) robot_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.rotate(m, glm.radians(-90), glm.vec3(1, 0, 0)) m = glm.rotate(m, glm.radians(30), glm.vec3(0, 0, 1)) robot_program.set_matrix("model", glm.value_ptr(m)) robot_program.un_use() time0 = glfw.get_time() human_model.animation(robot_program, speed) time1 = glfw.get_time() delta_time = (time1 - time0) if delta_time < 16: time.sleep((16 - delta_time) / 1000)
def updateUniformBuffers(self): # see https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/ #vulk=glm.mat4((1,0,0,0),(0,-1,0,0),(0,0,0.5,0),(0,0,0.5,1)) #self.uboVS['projectionMatrix'] = vulk*glm.perspective(glm.radians(60.0), self.width / self.height, 0.1, 256.0) #self.uboVS['projectionMatrix'] = glm.perspectiveRH_ZO(glm.radians(60.0), self.width / self.height, 0.1, 256.0) self.uboVS['projectionMatrix'] = glm.transpose( glm.perspectiveRH_ZO(glm.radians(60.0), self.width / self.height, 0.1, 256.0)) self.uboVS['viewMatrix'] = glm.transpose( glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, self.zoom))) self.uboVS['modelMatrix'] = glm.mat4(1.0) self.uboVS['modelMatrix'] = glm.rotate(self.uboVS['modelMatrix'], glm.radians(self.rotation.x), glm.vec3(1.0, 0.0, 0.0)) self.uboVS['modelMatrix'] = glm.rotate(self.uboVS['modelMatrix'], glm.radians(self.rotation.y), glm.vec3(0.0, 1.0, 0.0)) self.uboVS['modelMatrix'] = glm.rotate(self.uboVS['modelMatrix'], glm.radians(self.rotation.z), glm.vec3(0.0, 0.0, 1.0)) uboVSSize = sum([glm.sizeof(ubo) for ubo in self.uboVS.values()]) uboVSBuffer = np.concatenate( (np.array(self.uboVS['projectionMatrix']).flatten(order='C'), np.array(self.uboVS['modelMatrix']).flatten(order='C'), np.array(self.uboVS['viewMatrix']).flatten(order='C'))) data = vk.vkMapMemory(self.device, self.uniformBufferVS['memory'], 0, uboVSSize, 0) datawrapper = np.array(data, copy=False) np.copyto(datawrapper, uboVSBuffer.view(dtype=np.uint8), casting='no') vk.vkUnmapMemory(self.device, self.uniformBufferVS['memory'])
def getViewMat(self) -> glm.mat4: mat = glm.mat4(1) mat = glm.rotate(mat, glm.radians(self.__degrees.x), glm.vec3(1, 0, 0)) mat = glm.rotate(mat, glm.radians(self.__degrees.y), glm.vec3(0, 1, 0)) mat = glm.translate(mat, -self.pos.getVec()) return mat
def mouse_event(window, xpos, ypos): global firstMouse, cameraFront, yaw, pitch, lastX, lastY if firstMouse: lastX = xpos lastY = ypos firstMouse = False xoffset = xpos - lastX yoffset = lastY - ypos lastX = xpos lastY = ypos sensitivity = 0.3 xoffset *= sensitivity yoffset *= sensitivity yaw += xoffset pitch += yoffset if pitch >= 90.0: pitch = 90.0 if pitch <= -90.0: pitch = -90.0 front = glm.vec3() front.x = math.cos(glm.radians(yaw)) * math.cos(glm.radians(pitch)) front.y = math.sin(glm.radians(pitch)) front.z = math.sin(glm.radians(yaw)) * math.cos(glm.radians(pitch)) cameraFront = glm.normalize(front)
def __init__(self, screen): self.screen = screen _, _, self.width, self.height = screen.get_rect() #Gl Enable as Depth and Create our Viewport glEnable(GL_DEPTH_TEST) glViewport(0, 0, self.width, self.height) self.modelShow = 0 self.modelList = [] # Perspective Projection Matrix self.projection = glm.perspective(glm.radians(60), self.width / self.height, 0.1, 1000) #Cam pos, cube pos and cam rotation self.camPos = glm.vec3(0, 0, 3) self.cubePos = glm.vec3(0, 0, 0) self.yawCam = 0 self.pitchCam = 0 self.rollCam = 0 # Light self.pointLight = glm.vec4(0, 0, 0, 0) # Perspective Projection Matrix self.projection = glm.perspective(glm.radians(60), self.width / self.height, 0.1, 1000)
def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) elapsed_ms = glutGet(GLUT_ELAPSED_TIME) projection = glm.perspective(glm.radians(60), aspect, 0.1, 20.0) view = glm.lookAt(glm.vec3(-1, -8, 4), glm.vec3(-1, 0, -1), glm.vec3(0, 0, 1)) angle = 0#elapsed_ms * math.pi * 2 / 10000.0 model = glm.rotate(glm.mat4(1), glm.radians(-30), glm.vec3(0, 0, 1)) model = glm.rotate(model, angle, glm.vec3(0, 1, 0)) model = glm.scale(model, glm.vec3(1, 5, 0.2)) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(view)) glUniformMatrix4fv(2, 1, GL_FALSE, glm.value_ptr(model)) glUniform3f(10, 50/255, 40/255, 30/255) glUniform3f(11, 200/255, 150/255, 100/255) glUniform1f(12, 1.0) # frequency glUniform1f(13, 10.0) # noiseScale glUniform1f(14, 0.1) # ringScale glUniform1f(15, 1.0) # contrast glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, None) glutSwapBuffers() glutPostRedisplay()
def update_camera_vectors(self): front = glm.vec3(x=np.cos(glm.radians(self.yaw)) * np.cos(glm.radians(self.pitch)), y=np.sin(glm.radians(self.pitch)), z=np.sin(glm.radians(self.yaw)) * np.cos(glm.radians(self.pitch))) self.front = glm.normalize(front) self.right = glm.normalize(np.cross(self.front, self.world_up)) self.up = glm.normalize(np.cross(self.right, self.front))
def on_mouse_move(self, event): x_pos = event.pos[0] y_pos = event.pos[1] if self.first_mouse: self.first_mouse = False self.last_x = x_pos self.last_y = y_pos x_offset = x_pos - self.last_x y_offset = y_pos - self.last_y self.last_x = x_pos self.last_y = y_pos sensitivity = 0.05 x_offset *= sensitivity y_offset *= sensitivity self.yaw += x_offset self.pitch += y_offset if self.pitch > 89: self.pitch = 89 if self.pitch < -89: self.pitch = -89 direction = glm.vec3(1) direction.x = cos(glm.radians(self.yaw) * cos(glm.radians(self.pitch))) direction.y = sin(-glm.radians(self.pitch)) direction.z = sin(glm.radians(self.yaw)) * cos(glm.radians(self.pitch)) self.cameraFront = glm.normalize(direction) self.update()
def on_draw(self, event): self.model = glm.mat4(1.0) self.model = glm.rotate(self.model, glm.radians(-55.0), glm.vec3(1, 0, 0)) self.view = glm.mat4(1.0) self.view = glm.translate(self.view, glm.vec3(0.0, 0.0, -3.0)) self.projection = glm.mat4(1.0) self.projection = glm.perspective(glm.radians(45.0), builtins.width/builtins.height, 0.1, 100.0) # vispy takes numpy array in m * n matrix form self.model = (np.array(self.model.to_list()).astype(np.float32)) 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.model = self.model.reshape((1, self.model.shape[0] * self.model.shape[1])) 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])) gloo.clear([0.2, 0.3, 0.3, 1.0]) self.program['model'] = self.model self.program['view'] = self.view self.program['projection'] = self.projection self.program.draw('triangles', self.indices) self.update()
def make_env_with_best_settings(envName): env = gym.make(envName) env.env.advancedLevel = True env.env.advancedLevelRandom = False env.env.randomInitDir = True env.env.addObstacles = False env.env.ActionIsAngles = True env.env.ActionIsAnglesType = 2 env.env.ActionsIsAdditive = False env.env.inputsSpace = 0 env.env.actionsSpace = 0 env.env.simRewardOnly = False env.env.maxAnglesPerSec = 90.0 env.env.maxDesiredTargetSpeed = 0.25 env.env.speedInTargetDir = True env.env.targetDesired_episode_from = 0 env.env.targetDesired_episode_to = 10000 env.env.targetDesired_angleFrom = np.pi/2.0 env.env.targetDesired_angleTo = np.pi/2.0 env.env.spawnYawMultiplier = 0.0 env.env.targetDesiredYawMultiplier = 0.0 env.env.analyticReward = False env.env.analyticRewardType = 1 env.env.anglesRewardMultiplier = 0.0 env.env.check90Angles = False env.env.goalRandomTargetDirClamp = glm.radians(40.0) env.env.goalRandomChassisDirClamp = glm.radians(40.0) env.env.progressDirChassisMultiplier = 0.6 env.env.progressDirTargetMultiplier = 0.6 env.env.progressMultiplier = 1.0 env.env.aliveMultiplier = 1.0 env.env.aliveMultiplierClampMax = 0.0 env.env.yawRewardMultiplier = 0.0 env.env.holdingTorqueMultiplier=0.0 env.env.energyCostMultiplier = 0.0 env.env.useLegsHeightReward = True env.env.angleLimitszFromMp2ToP2 = False env.env.ground_pos_random_x = 0.5 env.env.ground_pos_random_y = 0.5 env = MaxAndSkipEnv(env,max_and_skip,False) env = FrameStack(env,frames_stack,True) return env
def updateUniformBuffers(self): self.uboVS['projection'] = glm.transpose( glm.perspectiveRH_ZO(glm.radians(60.0), self.width / self.height, 0.001, 256.0)) view = glm.transpose( glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, self.zoom))) self.uboVS['model'] = view * glm.translate(glm.mat4(1.0), self.cameraPos) self.uboVS['model'] = glm.rotate(self.uboVS['model'], glm.radians(self.rotation.x), glm.vec3(1.0, 0.0, 0.0)) self.uboVS['model'] = glm.rotate(self.uboVS['model'], glm.radians(self.rotation.y), glm.vec3(0.0, 1.0, 0.0)) self.uboVS['model'] = glm.rotate(self.uboVS['model'], glm.radians(self.rotation.z), glm.vec3(0.0, 0.0, 1.0)) self.uboVS['viewPos'] = glm.vec4(0.0, 0.0, -self.zoom, 0.0) uboVSSize = sum([glm.sizeof(ubo) for ubo in self.uboVS.values()]) uboVSBuffer = np.concatenate( (np.array(self.uboVS['projection']).flatten(order='C'), np.array(self.uboVS['model']).flatten(order='C'), np.array(self.uboVS['viewPos']).flatten(order='C'), np.array(self.uboVS['lodBias']).flatten(order='C'))) self.uniformBufferVS.map() self.uniformBufferVS.copyTo(uboVSBuffer, uboVSSize) self.uniformBufferVS.unmap()
def draw(self, view, projection, light, view_position): this_mesh_renderer: Optional[MeshRenderer] = self.get_component( MeshRenderer) this_mesh_filter: Optional[MeshFilter] = self.get_component(MeshFilter) if this_mesh_renderer is not None: this_mesh_renderer.use() m = glm.translate(glm.mat4(1), self.transform.position) for c in self.draw_rotate: if c == 'x': m = glm.rotate(m, glm.radians(self.transform.rotation.x), glm.vec3(1, 0, 0)) elif c == 'y': m = glm.rotate(m, glm.radians(self.transform.rotation.y), glm.vec3(0, 1, 0)) elif c == 'z': m = glm.rotate(m, glm.radians(self.transform.rotation.z), glm.vec3(0, 0, 1)) m = glm.scale(m, self.transform.scale) model, view, projection = self.renderer(model=m, view=view, projection=projection) this_mesh_renderer.set_matrix('model', glm.value_ptr(model)) this_mesh_renderer.set_matrix('view', glm.value_ptr(view)) this_mesh_renderer.set_matrix('projection', glm.value_ptr(projection)) this_mesh_renderer.draw(light_tuple=light, view_position=view_position) # this_mesh_renderer.un_use() if this_mesh_filter is not None: this_mesh_filter.draw()
def mouse_callback2(win, mouse_x, mouse_y): global last_mouse_x, last_mouse_y global yaw, pitch global camera_front global first_mouse if first_mouse: first_mouse = False last_mouse_x = mouse_x last_mouse_y = mouse_y offset_x = mouse_x - last_mouse_x offset_y = last_mouse_y - mouse_y # invert y last_mouse_x = mouse_x last_mouse_y = mouse_y sensitivity = 0.2 offset_x *= sensitivity offset_y *= sensitivity yaw += offset_x pitch += offset_y # print(yaw, pitch) if pitch > 89.0: pitch = 89.0 if pitch < -89.0: pitch = -89.0 front = glm.vec3(x=np.cos(glm.radians(yaw)) * np.cos(glm.radians(pitch)), y=np.sin(glm.radians(pitch)), z=np.sin(glm.radians(yaw)) * np.cos(glm.radians(pitch))) camera_front = glm.normalize(front)
def render(self, shadersDict): for entity in shadersDict: shadersDict[entity][0].use() e = entity model = entity.model glBindVertexArray(model.vao) transform = glm.identity(glm.mat4x4) transform = glm.translate(transform, glm.vec3(e.pos.x, e.pos.y, e.pos.z)) transform = glm.rotate(transform, glm.radians(e.angle_x), glm.vec3(1, 0, 0)) # x-axis transform = glm.rotate(transform, glm.radians(e.angle_y), glm.vec3(0, 1, 0)) # y-axis transform = glm.rotate(transform, glm.radians(e.angle_z), glm.vec3(0, 0, 1)) # z-axis transform = glm.scale(transform, glm.vec3(e.scale.x, e.scale.y, e.scale.z)) shadersDict[entity][0].setMatrix4f("mat_transform", transform) if shadersDict[entity][1]: glDrawElements(GL_TRIANGLES, model.indices_len, GL_UNSIGNED_INT, None) else: glDrawArrays(GL_TRIANGLES, 0, model.vertex_count)
def init_test_object(): global _test_object, _child1, _child2, _child3, _child4 color = np.array( [[1.0, 0.0, 0.0, 1.0], [0.5, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 1.0], [0.0, 0.5, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.5, 1.0]], c_float) long_rect = RectPrismMesh(0.5, 0.1, 0.1, face_colors=color) _child1 = WorldObject(long_rect) _child2 = WorldObject(long_rect) _child3 = WorldObject(long_rect) _child4 = WorldObject(long_rect) _child1.set_local_position((1.25, 0.0, 0.0)) _child2.set_local_position((0.0, 1.25, 0.0)) _child3.set_local_position((-1.25, 0.0, 0.0)) _child4.set_local_position((0.0, -1.25, 0.0)) _child1.set_local_rotation((0.0, 0.0, 0.0)) _child2.set_local_rotation((0.0, 0.0, glm.radians(90.0))) _child3.set_local_rotation((0.0, 0.0, glm.radians(180.0))) _child4.set_local_rotation((0.0, 0.0, glm.radians(270.0))) cube = RectPrismMesh(0.5, 0.5, 0.5, face_colors=color) _test_object = WorldObject(cube) _test_object.set_render_mode(gl.GL_LINE_LOOP) _test_object.add_child(_child1) _test_object.add_child(_child2) _test_object.add_child(_child3) _test_object.add_child(_child4)
def getModelMat(self): mat = glm.mat4(1.0) mat = glm.rotate(mat, glm.radians(self.__z), (0.0, 0.0, 1.0)) mat = glm.rotate(mat, glm.radians(self.__y), (0.0, 1.0, 0.0)) mat = glm.rotate(mat, glm.radians(self.__x), (1.0, 0.0, 0.0)) return mat
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 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 getView(self): m = glm.mat4(1.0) m = glm.translate(m, self.offset) m = glm.rotate(m, glm.radians(self.pitch), (1, 0, 0)) m = glm.rotate(m, glm.radians(self.yaw), (0, 1, 0)) m = glm.translate(m, self.pos) return m
def getView(self): self.viewMatrix = glm.identity(glm.mat4x4) self.viewMatrix = glm.rotate(self.viewMatrix, glm.radians(self.pitch), glm.vec3(1, 0, 0)) # x-axis self.viewMatrix = glm.rotate(self.viewMatrix, glm.radians(self.yaw), glm.vec3(0, 1, 0)) # y-axis self.viewMatrix = glm.rotate(self.viewMatrix, glm.radians(self.roll), glm.vec3(0, 0, 1)) # z-axis self.offsetCamera = glm.vec3(-self.x, -self.y, -self.z) self.viewMatrix = glm.translate(self.viewMatrix, self.offsetCamera) return self.viewMatrix
def __update_camera_vectors(self): front = glm.vec3(0) 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(front) self.right = glm.normalize(glm.cross(self.front, self.world_up))
def getViewMatrix(self): i = glm.mat4(1) camTranslate = glm.translate(i, self.camPosition) camPitch = glm.rotate(i, glm.radians( self.camRotation.x ), glm.vec3(1,0,0)) camYaw = glm.rotate(i, glm.radians( self.camRotation.y ), glm.vec3(0,1,0)) camRoll = glm.rotate(i, glm.radians( self.camRotation.z ), glm.vec3(0,0,1)) camRotate = camPitch * camYaw * camRoll return glm.inverse( camTranslate * camRotate )
def getDirVec(self): vec = glm.vec4(0, 0, -1, 0) vec = glm.rotate(glm.mat4(1), glm.radians(-self.__degrees.x), (1, 0, 0)) * vec vec = glm.rotate(glm.mat4(1), glm.radians(-self.__degrees.y), (0, 1, 0)) * vec return glm.normalize(vec)
def render(): glClearColor(0, 0, 0, 0.0) glClearDepth(1.0) glPointSize(5) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) projection = glm.perspective( glm.radians(camera.zoom), window.window_width * 1.0 / window.window_height, 0.1, 1000) view = camera.get_view_matrix() # # draw grid shader_program.use() shader_program.set_matrix("projection", glm.value_ptr(projection)) shader_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.translate(m, grid_position[2]) m = glm.rotate(m, glm.radians(90), grid_position[1][1]) m = glm.scale(m, glm.vec3(5)) shader_program.set_matrix("model", glm.value_ptr(m)) shader_program.un_use() grid_model.draw(shader_program, draw_type=GL_LINES) # # draw hand hand_shader_program.use() hand_shader_program.set_matrix("projection", glm.value_ptr(projection)) hand_shader_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.translate(m, glm.vec3(0, 6, 0)) m = glm.rotate(m, glm.radians(-90), glm.vec3(1, 0, 0)) # m = glm.rotate(m, glm.radians(model_position[1][1][0]), model_position[1][1][1]) # m = glm.rotate(m, glm.radians(model_position[1][2][0]), model_position[1][2][1]) m = glm.scale(m, glm.vec3(0.02, 0.02, 0.02)) hand_shader_program.set_matrix("model", glm.value_ptr(m)) hand_shader_program.set_uniform_3f("lightColor", light_color) hand_shader_program.set_uniform_3f("lightPos", light_position) hand_shader_program.set_uniform_3f("handColor", hand_color) hand_shader_program.un_use() hand_model.draw(hand_shader_program, draw_type=GL_TRIANGLES) # # draw light cube light_shader_program.use() light_shader_program.set_matrix("projection", glm.value_ptr(projection)) light_shader_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.translate( m, glm.vec3(light_position[0], light_position[1], light_position[2])) m = glm.scale(m, glm.vec3(1, 1, 1)) light_shader_program.set_matrix("model", glm.value_ptr(m)) light_shader_program.set_uniform_3f("lightColor", light_color) light_shader_program.un_use() light_cube.draw(light_shader_program, draw_type=GL_TRIANGLES)
def getMatrix(self): i = glm.mat4(1) translate = glm.translate(i, self.position) pitch = glm.rotate(i, glm.radians(self.rotation.x), glm.vec3(1, 0, 0)) yaw = glm.rotate(i, glm.radians(self.rotation.y), glm.vec3(0, 1, 0)) roll = glm.rotate(i, glm.radians(self.rotation.z), glm.vec3(0, 0, 1)) rotate = pitch * yaw * roll scale = glm.scale(i, self.scale) return translate * rotate * scale
def get_mvp(): fov = glm.radians(45.0) aspect = self.u_width / self.u_height near, far = 0.2, 20.0 p = glm.perspective(fov, aspect, near, far) v = glm.rotate(glm.mat4(1.0), glm.radians(-180.0), glm.vec3(0.0, 1.0, 0.0)) m = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, 2.0)) return m, v, p, p * v * m
def _update_cam_unit_pos(self): self._pitch = max(-89., min(89., self._pitch)) pitch = glm.radians(self._pitch) yaw = glm.radians(self._yaw) self._cam_unit_pos = glm.normalize(glm.vec3(glm.cos(yaw) * glm.cos(pitch), glm.sin(pitch), glm.sin(yaw) * glm.cos(pitch)))
def createTheMatrix(counter): translate = glm.translate(i, glm.vec3(0, 0, 0)) rotate = glm.rotate(i, glm.radians(counter), glm.vec3(0, 1, 0)) scale = glm.scale(i, glm.vec3(1, 1, 1)) model = translate * rotate * scale view = glm.lookAt(glm.vec3(0, 0, 200), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0)) projection = glm.perspective(glm.radians(45), 800/600, 0.1, 1000) return projection * view * model
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 getMatrix(self, camera_pos, projection, camera_rot): i = glm.mat4(1) translate = glm.translate(i, glm.vec3(0, 0, 0)) pitch = glm.rotate(i, glm.radians(self.rotation.x), glm.vec3(1, 0, 0)) yaw = glm.rotate(i, glm.radians(self.rotation.y), glm.vec3(0, 1, 0)) roll = glm.rotate(i, glm.radians(self.rotation.z), glm.vec3(0, 0, 1)) rotate = pitch * yaw * roll scale = glm.scale(i, self.scale) view = glm.lookAt(camera_pos, self.position, glm.vec3(0, 1, 0)) return translate * rotate * scale
def update(self): self.model_matrix = glm.mat4(1.0) self.model_matrix = glm.translate(self.model_matrix, self.pos) self.model_matrix = glm.scale(self.model_matrix, self.scale) self.model_matrix = glm.rotate(self.model_matrix,\ glm.radians(self.angle.x), glm.vec3(1.0, 0.0, 0.0)) self.model_matrix = glm.rotate(self.model_matrix,\ glm.radians(self.angle.y), glm.vec3(0.0, 1.0, 0.0)) self.model_matrix = glm.rotate(self.model_matrix,\ glm.radians(self.angle.z), glm.vec3(0.0, 0.0, 1.0))
def drawFunc(): glClearColor(1.0, 1.0, 1.0, 0.0) glClearDepth(1.0) glPointSize(5) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) current_frame = glutGet(GLUT_ELAPSED_TIME) projection = glm.perspective(glm.radians(camera.zoom), SCR_WIDTH * 1.0 / SCR_HEIGHT, 0.1, 200) view = camera.get_view_matrix() shader_program.use() shader_program.set_matrix("projection", glm.value_ptr(projection)) shader_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.translate(m, grid_position[2]) m = glm.rotate(m, glm.radians(90), grid_position[1][1]) m = glm.scale(m, glm.vec3(5)) shader_program.set_matrix("model", glm.value_ptr(m)) shader_program.un_use() grid_model.draw(shader_program, draw_type=GL_LINES) robot_program.use() robot_program.set_matrix("projection", glm.value_ptr(projection)) robot_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.rotate(m, glm.radians(-90), glm.vec3(1, 0, 0)) robot_program.set_matrix("model", glm.value_ptr(m)) robot_program.un_use() human_model.animation(robot_program) # global last_frame last_frame = glutGet(GLUT_ELAPSED_TIME) global delta_time delta_time = (last_frame - current_frame) camera.process_keyboard(delta_time / 1000) if delta_time < 16: time.sleep((16 - delta_time) / 1000) calculate_FPS() global fps_count if fps_count == 100: fps_count = 0 print('fps: %.2f' % _fps) fps_count += 1 glutSwapBuffers() glutPostRedisplay()