def render(time): glClearBufferfv(GL_COLOR, 0, [0.0, 0.0, 0.0, 1.0]) glClearBufferfi(GL_DEPTH_STENCIL, 0, 1.0, 0) M_matrix = glm.mat4(1.0) V_matrix = glm.lookAt(glm.vec3(0.0, 0.0, 12.0), glm.vec3(3.0, 3.0, 3.0), glm.vec3(0.0, 1.0, 0.0)) glUseProgram(rendering_program) M_location = glGetUniformLocation(rendering_program, "M_matrix") V_location = glGetUniformLocation(rendering_program, "V_matrix") P_location = glGetUniformLocation(rendering_program, "P_matrix") glUniformMatrix4fv(V_location, 1, GL_FALSE, glm.value_ptr(V_matrix)) glUniformMatrix4fv(P_location, 1, GL_FALSE, glm.value_ptr(P_matrix)) for i in range(10): for i in range(10): M_matrix = glm.translate(M_matrix, glm.vec3(1.0, 0.0, 0.0)) M_matrix = glm.rotate(M_matrix, time, glm.vec3(1.0, 1.0, 0.0)) glUniformMatrix4fv(M_location, 1, GL_FALSE, glm.value_ptr(M_matrix)) glDrawArrays(GL_TRIANGLES, 0, 36) M_matrix = glm.rotate(M_matrix, time, glm.vec3(-1.0, -1.0, 0.0)) M_matrix = glm.translate(M_matrix, glm.vec3(-10.0, 1.0, 0.0))
def drawObject(self): # Update camera model, view, proj = glm.mat4(1), glm.mat4(1), glm.perspective(45, self.width() / self.height(), 0.01, 100) center, up, eye = glm.vec3(0, -0.075, 0), glm.vec3(0, -1, 0), glm.vec3(0, 0, -0.4 * (self.zoomLevel / 10)) view = glm.lookAt(eye, center, up) model = glm.rotate(model, self.xRot / 160.0, glm.vec3(1, 0, 0)) model = glm.rotate(model, self.yRot / 160.0, glm.vec3(0, 1, 0)) model = glm.rotate(model, self.zRot / 160.0, glm.vec3(0, 0, 1)) mvp = proj * view * model GL.glUniformMatrix4fv(self.UNIFORM_LOCATIONS['mvp'], 1, False, glm.value_ptr(mvp)) # Update data self.pos_vbo.set_array(self.pos) self.col_vbo.set_array(self.col) # Point size GL.glPointSize(2) # Position self.pos_vbo.bind() GL.glEnableVertexAttribArray(0) GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None) # Color self.col_vbo.bind() GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer(1, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None) # Draw GL.glDrawArrays(GL.GL_POINTS, 0, self.pos.shape[0])
def render_scene(time, cam1, view, projection): # Light source: lamp light_color = glm.vec3(1, 1, 1) light_position = glm.vec3(np.cos(time), 1.5, np.sin(time)) # Ground glBindVertexArray(vao1) glUseProgram(shader_solid) model = glm.identity(4) model = glm.rotate(np.pi*0.5, glm.vec3(1, 0, 0)) * model glUniform3fv(glGetUniformLocation(shader_solid, "solid_color"), 1, glm.vec3(0, 0, 1)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices1), GL_UNSIGNED_INT, None) glBindVertexArray(0) # Big Cube glBindVertexArray(vao2) glUseProgram(shader_basic_lighting) model = glm.identity(4) model = glm.translate(glm.vec3(.0, 0.5, .0)) * model # model = model * glm.rotate(1.0 * np.pi * time, glm.vec3(1, 1, 0.2)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_color"), 1, light_color) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_pos"), 1, light_position) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "object_color"), 1, glm.vec3(1, .5, .31)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "view_pos"), 1, cam1.position) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "model"), 1, GL_TRUE, np.asarray(model)) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) # Small cube glBindVertexArray(vao2) model = glm.identity(4) model = model * glm.translate(glm.vec3(.75, .95, 0)) model = model * glm.rotate(3.0 * np.pi * time, glm.vec3(1, -.2, 0.8)) model = model * glm.scale(glm.vec3(.1, .1, 0.2)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_color"), 1, light_color) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_pos"), 1, light_position) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "object_color"), 1, glm.vec3(1, 0, 0)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "view_pos"), 1, cam1.position) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "model"), 1, GL_TRUE, np.array(model)) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) # Lamp glUseProgram(shader_solid) glBindVertexArray(vao_lamp) model = glm.translate(light_position) model = model * glm.scale(glm.vec3(.2, .2, .2)) glUniform3fv(glGetUniformLocation(shader_solid, "solid_color"), 1, glm.vec3(1, 1, 1)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices3), GL_UNSIGNED_INT, None) glBindVertexArray(0)
def on_draw(self): self.gui_newFrame() self.ctx.clear() # self.ctx.clear(0.0/255, 20.0/255, 60.0/255) self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST) mat_rotx = glm.rotate(glm.mat4(1.0), -self.camera_roty, glm.vec3(1.0, 0.0, 0.0)) mat_roty = glm.rotate(glm.mat4(1.0), self.camera_rotx, glm.vec3(0.0, 1.0, 0.0)) mat_rotz = glm.rotate(glm.mat4(1.0), 0.0, glm.vec3(0.0, 0.0, 1.0)) translate = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, self.camera_z)) modelview = np.array(translate * mat_rotx * mat_roty * mat_rotz).flatten() self.program_boids['modelview'] = tuple(modelview) self.program_border['modelview'] = tuple(modelview) self.program_lines['modelview'] = tuple(modelview) self.compass.render(mode=moderngl.LINES) self.vao_1.render(instances=self.boid_count) if (self.map_type == MAP_CUBE or self.map_type == MAP_CUBE_T): self.borders.render(mode=moderngl.LINES) self.gui_draw()
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 render_scene(): global model, view global x_theta, y_theta, z_theta, r_time r_time += 0.02 if x_rotate: x_theta = 0.02 elif not x_rotate: x_theta = 0 if y_rotate: y_theta = 0.02 elif not y_rotate: y_theta = 0 if z_rotate: z_theta = 0.02 elif not z_rotate: z_theta = 0 model = glm.rotate(model, x_theta, np.array([1, 0, 0], 'f4')) model = glm.rotate(model, y_theta, np.array([0, 1, 0], 'f4')) model = glm.rotate(model, z_theta, np.array([0, 0, 1], 'f4')) prog['model'].write(model) cam_x = np.sin(r_time) * swivel_radius cam_z = np.cos(r_time) * swivel_radius view = glm.lookAt(np.array((cam_x, 0, cam_z), 'f4'), camtarget, camup) prog['view'].write(view) vao.render(moderngl.TRIANGLES)
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 update_matrices(self): # create projection matrix self.p_matrix = glm.perspective( glm.radians(options.FOV + 20 * (self.speed - WALKING_SPEED) / (SPRINTING_SPEED - WALKING_SPEED)), float(self.view_width) / self.view_height, 0.1, 500) # create modelview matrix self.mv_matrix = glm.mat4(1.0) self.mv_matrix = glm.rotate(self.mv_matrix, self.rotation[1], -glm.vec3(1.0, 0.0, 0.0)) self.mv_matrix = glm.rotate(self.mv_matrix, self.rotation[0] + math.tau / 4, glm.vec3(0.0, 1.0, 0.0)) self.position = glm.vec3(*self.position) self.mv_matrix = glm.translate( self.mv_matrix, -glm.vec3(*self.interpolated_position) - glm.vec3(0, self.eyelevel, 0)) # modelviewprojection matrix self.shader.uniform_matrix(self.mvp_matrix_location, self.p_matrix * self.mv_matrix) self.update_frustum(self.p_matrix * self.mv_matrix)
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 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 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 mainloop(): vp_valid, vp_size, view, projection = glut_navigation.update() angle1 = elapsed_ms() * math.pi * 2 / 5000.0 angle2 = elapsed_ms() * math.pi * 2 / 7333.0 model_matrices = [] for i in range(no_of_meshes): if i == 0: model = glm.mat4(1) model = glm.translate(model, glm.vec3(0, 0, -1)) else: angleY = angle1 + math.pi*2 * (i-1) / (no_of_meshes-1) model = glm.mat4(1) model = glm.rotate(model, angleY, glm.vec3(0, 0, 0.5)) model = glm.translate(model, glm.vec3(diameter/2, 0, 0.5)) model = glm.rotate(model, angle2, glm.vec3(0, 1, 0)) model_matrices.append(model) light_pos = glm.vec3(0, 0, 3) light_dir = glm.vec3(0, 0, -1) light_cone_angle_degree = 60 light_cone_cos = math.cos(math.radians(light_cone_angle_degree) / 2) shadow_proj = glm.perspective(glm.radians(light_cone_angle_degree + 1), 1, 0.1, 100.0) shadow_view = glm.lookAt(light_pos, light_pos+light_dir, glm.vec3(0,1,0)) glBindBuffer(GL_SHADER_STORAGE_BUFFER, model_ssbo) for i, model in enumerate(model_matrices): glBufferSubData(GL_SHADER_STORAGE_BUFFER, i*4*16, glm.sizeof(glm.mat4), glm.value_ptr(model)) glBindFramebuffer(GL_FRAMEBUFFER, shadow_fbo) glViewport(0, 0, *shadow_buffer_size) glClear(GL_DEPTH_BUFFER_BIT) glUseProgram(shadow_program) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(shadow_proj)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(shadow_view)) glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(1.0, 1.0) model_multimesh.draw() glDisable(GL_POLYGON_OFFSET_FILL) glBindFramebuffer(GL_FRAMEBUFFER, 0) glViewport(0, 0, *vp_size) glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(phong_program) 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(shadow_proj)) glUniformMatrix4fv(3, 1, GL_FALSE, glm.value_ptr(shadow_view)) glUniform4fv(4, 1, glm.value_ptr(glm.vec4(light_pos, 1))) glUniform4fv(5, 1, glm.value_ptr(glm.vec4(light_dir, light_cone_cos))) glBindTextureUnit(1, shadow_depth_to) model_multimesh.draw() glutSwapBuffers() glutPostRedisplay()
def renderGL(self): """opengl render method""" # get the time in seconds t = glfwGetTime() # clear first glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # use the shader program glUseProgram(self.__shaderProgram) # calculate ViewProjection matrix projection = glm.perspective(90.0, 4.0 / 3.0, .1, 100.0) projection = np.array(projection, dtype=np.float32) # translate the world/view position view = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, -50.0)) # make the camera rotate around the origin view = glm.rotate(view, 30.0 * math.sin(t * 0.1), glm.vec3(1.0, 0.0, 0.0)) view = glm.rotate(view, -22.5 * t, glm.vec3(0.0, 1.0, 0.0)) view = np.array(view, dtype=np.float32) # set the uniform glUniformMatrix4fv(self.__viewLocation, 1, GL_FALSE, view) glUniformMatrix4fv(self.__projLocation, 1, GL_FALSE, projection) # bind the vao glBindVertexArray(self.__vao) # draw glDrawArrays(GL_POINTS, 0, self.__particles) glBindVertexArray(0) glUseProgram(0)
def cpu_tick(self, elapsed_time): SPD = 5.0 is_MVP_dirty = False for key in self.pressing_keys: if key == glfw.KEY_LEFT or key == glfw.KEY_A: self.view = glm.rotate(self.view, +SPD * elapsed_time, glm.vec3(0.0, 1.0, 0.0)) is_MVP_dirty = True elif key == glfw.KEY_RIGHT or key == glfw.KEY_D: self.view = glm.rotate(self.view, -SPD * elapsed_time, glm.vec3(0.0, 1.0, 0.0)) is_MVP_dirty = True elif key == glfw.KEY_UP or key == glfw.KEY_W: self.view = glm.rotate(self.view, +SPD * elapsed_time, glm.vec3(0.0, 0.0, 1.0)) is_MVP_dirty = True elif key == glfw.KEY_DOWN or key == glfw.KEY_S: self.view = glm.rotate(self.view, -SPD * elapsed_time, glm.vec3(0.0, 0.0, 1.0)) is_MVP_dirty = True if is_MVP_dirty: is_MVP_dirty = False self.MVP = self.projection * self.view * self.model
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 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 rotate_yaw_pitch_row(eulers): # type: (Vector3) -> Matrix4 m = Matrix4.identity() # NOTE in zxy order m = glm.rotate(m, eulers.z, Vector3(0, 0, 1)) m = glm.rotate(m, eulers.x, Vector3(1, 0, 0)) m = glm.rotate(m, eulers.y, Vector3(0, 1, 0)) 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 CreateTransform3D(pos: glm.vec3, size: glm.vec3, rot: glm.vec3) -> glm.mat4: transform = glm.translate(glm.mat4(1.0), pos) transform = glm.scale(transform, size) transform = glm.rotate(transform, rot.x, glm.vec3(1.0, 0.0, 0.0)) transform = glm.rotate(transform, rot.y, glm.vec3(0.0, 1.0, 0.0)) return glm.rotate(transform, rot.z, glm.vec3(0.0, 0.0, 1.0))
def orbit(m: glm.mat4, dx: float, dy: float): horizontal_axis = glm.vec3(m[0][0], m[1][0], m[2][0]) vertical_axis = glm.vec3(0, 1, 0) m *= glm.rotate(np.eye(4, dtype=np.float32), dy * 0.006, horizontal_axis) m *= glm.rotate(np.eye(4, dtype=np.float32), dx * 0.006, vertical_axis) return m
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 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 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 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 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 refresh_MVP(self): self.P = glm.perspective(np.radians(self.view_angle), self.width/self.height, 1, 3) self.MV = glm.translate(glm.mat4(), glm.vec3(self.position_x, self.position_y, -2)) self.MV = glm.rotate(self.MV, np.radians(self.angle_x), glm.vec3(1,0,0)) self.MV = glm.rotate(self.MV, np.radians(self.angle_y), glm.vec3(0,1,0)) self.MVP = np.array(self.P * self.MV) self.MV = np.array(self.MV)
def build_transformation_matrix(position, rotation, scale): mat = glm.mat4x4(1.0) mat = glm.translate(mat, position) mat = glm.rotate(mat, rotation.z, glm.vec3(1, 0, 0)) mat = glm.rotate(mat, rotation.y, glm.vec3(0, 1, 0)) mat = glm.rotate(mat, rotation.x, glm.vec3(0, 0, 1)) mat = glm.scale(mat, scale) return mat
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 draw_objects(shader): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Create perspective matrix pers = glm.perspective(0.5,1.0,0.1,50.0) # Create rotation matrix rot_x = glm.rotate(glm.mat4(1.0),0.5 * glfw.get_time(),glm.vec3(1.0,0.0,0.0)) rot_y = glm.rotate(glm.mat4(1.0),0.5 * glfw.get_time(),glm.vec3(0.0,1.0,0.0)) # Create normal transformation matrix normal_matrix = glm.mat4(1.0) normalMatrixLoc = glGetUniformLocation(shader, "normal_matrix") glUniformMatrix4fv(normalMatrixLoc, 1, GL_FALSE, glm.value_ptr(normal_matrix*rot_x*rot_y)) # Create tranformation matrix for sphere trans = glm.translate(glm.mat4(1.0), glm.vec3(-0.8,0.8,-5.5)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*rot_x*rot_y)) # Draw Sphere sphere_quadric = gluNewQuadric() gluQuadricNormals(sphere_quadric, GLU_SMOOTH) gluSphere(sphere_quadric, 0.35, 20, 20) # Create tranformation matrix for teapot trans = glm.translate(glm.mat4(1.0), glm.vec3(0.0,0.0,-20.5)) scale = glm.scale(glm.mat4(1.0), glm.vec3(0.1, 0.1, 0.1)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*scale*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*scale*rot_x*rot_y)) draw_faces(teapot_faces) # Create tranformation matrix for cylinder trans = glm.translate(glm.mat4(1.0), glm.vec3(0.8,-0.8,-5.5)) modelViewLoc = glGetUniformLocation(shader, "model_view_matrix") glUniformMatrix4fv(modelViewLoc, 1, GL_FALSE, glm.value_ptr(trans*rot_x*rot_y)) modelViewProjLoc = glGetUniformLocation(shader, "model_view_projection_matrix") glUniformMatrix4fv(modelViewProjLoc, 1, GL_FALSE, glm.value_ptr(pers*trans*rot_x*rot_y)) # Draw cylinder cylinder_quad = gluNewQuadric() gluQuadricNormals(cylinder_quad, GLU_SMOOTH) gluCylinder(cylinder_quad, 0.25, 0.25, 0.5, 10, 10)
def _generate_nodeview_matrix1(rsm_version: int, node: AbstractNode, is_only_node: bool, model_bbox: BoundingBox) -> glm.mat4: rsm_node = node.impl # Model view # Translation nodeview_matrix = glm.mat4() if node.parent is None: # Z axis is in the opposite direction in glTF nodeview_matrix = glm.rotate(nodeview_matrix, math.pi, glm.vec3(1.0, 0.0, 0.0)) if is_only_node: nodeview_matrix = glm.translate( nodeview_matrix, glm.vec3(-model_bbox.center[0] + model_bbox.range[0], -model_bbox.max[1] + model_bbox.range[1], -model_bbox.min[2])) else: nodeview_matrix = glm.translate( nodeview_matrix, glm.vec3(-model_bbox.center[0], -model_bbox.max[1], -model_bbox.center[2])) else: nodeview_matrix = glm.rotate(nodeview_matrix, -math.pi / 2, glm.vec3(1.0, 0.0, 0.0)) nodeview_matrix = glm.translate(nodeview_matrix, glm.vec3(rsm_node.info.position)) # Figure out the initial rotation if len(rsm_node.rot_key_frames) == 0: # Static model if rsm_node.info.rotation_angle > 0.01: nodeview_matrix = glm.rotate( nodeview_matrix, glm.radians(rsm_node.info.rotation_angle * 180.0 / math.pi), glm.vec3(rsm_node.info.rotation_axis)) else: # Animated model key_frame = rsm_node.rot_key_frames[0] quaternion = glm.quat(key_frame.quaternion[3], key_frame.quaternion[0], key_frame.quaternion[1], key_frame.quaternion[2]) nodeview_matrix *= glm.mat4_cast(quaternion) # Scaling nodeview_matrix = glm.scale(nodeview_matrix, glm.vec3(rsm_node.info.scale)) # Node view if is_only_node: nodeview_matrix = glm.translate(nodeview_matrix, -model_bbox.range) elif node.parent is not None: nodeview_matrix = glm.translate(nodeview_matrix, glm.vec3(rsm_node.info.offset_vector)) nodeview_matrix *= mat3tomat4(rsm_node.info.offset_matrix) return nodeview_matrix
def renderGL(self): """opengl render method""" # get the time in seconds t = glfwGetTime() # clear first glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # use the shader program glUseProgram(self.__shaderProgram) # calculate ViewProjection matrix projection = glm.perspective(90.0, 4.0 / 3.0, .1, 100.0) # translate the world/view position view = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, -5.0)) # make the camera rotate around the origin view = glm.rotate(view, 90.0 * t, glm.vec3(1.0, 1.0, 1.0)) viewProjection = np.array(projection * view, dtype=np.float32) # set the uniform glUniformMatrix4fv(self.__vpLocation, 1, GL_FALSE, viewProjection) # bind the vao glBindVertexArray(self.__vao) # draw # the additional parameter indicates how many instances to render glDrawElementsInstanced(GL_TRIANGLES, 6 * 6, GL_UNSIGNED_INT, None, 8) glBindVertexArray(0) glUseProgram(0)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # clear the colorbuffer glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # configure view/projection matrices glUseProgram(self.__shader) view = self.camera.viewMatrix projection = glm.perspective(self.camera.zoom, float(self.width())/self.height(), 0.1, 100.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'view'), 1, GL_FALSE, view) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'projection'), 1, GL_FALSE, projection) # render normal-mapped quad rotVec = glm.normalize(np.array([1.0, 0.0, 1.0], np.float32)) model = glm.rotate(np.identity(4, np.float32), currentTime * -10.0, rotVec[0], rotVec[1], rotVec[2]) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) glUniform3fv(glGetUniformLocation(self.__shader, 'lightPos'), 1, self.lightPos) glUniform3fv(glGetUniformLocation(self.__shader, 'viewPos'), 1, self.camera.position) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.diffuseMap) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.normalMap) self.renderQuad() # render light source (simply re-renders a smaller plane at the light's position for debugging/visualization) # model = glm.scale(np.identity(4, np.float32), 0.1, 0.1, 0.1) # model = glm.translate(model, self.lightPos[0], self.lightPos[1], self.lightPos[2]) # glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) # self.renderQuad() glUseProgram(0)
def paintGL(self): # Render # Clear the colorbuffer glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT) glUseProgram(self.__shaderProgram) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.texture1) glUniform1i(glGetUniformLocation(self.__shaderProgram, 'ourTexture1'), 0) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.texture2) glUniform1i(glGetUniformLocation(self.__shaderProgram, 'ourTexture2'), 1) transform = np.identity(4, np.float32) transform = glm.rotate(transform, self.__timer.elapsed() / 1000.0 * 50.0, 0.0, 0.0, 1.0) transform = glm.translate(transform, 0.5, -0.5, 0.0) transformLoc = glGetUniformLocation(self.__shaderProgram, 'transform') glUniformMatrix4fv(transformLoc, 1, GL_FALSE, transform) glBindVertexArray(self.__vao) glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None) glBindVertexArray(0)
def renderScene(self, shader): # Room cube model = np.identity(4, np.float32) model = glm.scale(model, 10.0, 10.0, 10.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) glDisable(GL_CULL_FACE) # Note that we disable culling here since we render 'inside' the cube instead of the usual 'outside' which throws off the normal culling methods. glUniform1i(glGetUniformLocation(shader, 'reverse_normals'), 1) #A small little hack to invert normals when drawing cube from the inside so lighting still works. self.renderCube() glEnable(GL_CULL_FACE) # Cubes model = np.identity(4, np.float32) model = glm.translate(model, 4.0, -3.5, 0.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.translate(np.identity(4, np.float32), 2.0, 3.0, 1.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.translate(np.identity(4, np.float32), -3.0, -1.0, 0.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.translate(np.identity(4, np.float32), -1.5, 1.0, 1.5) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.rotate(np.identity(4, np.float32), 60.0, 1.0, 0.0, 1.0) model = glm.scale(model, 1.5, 1.5, 1.5) model = glm.translate(model, -1.5, 2.0, -3.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube()
def renderScene(self, shader): # Floor model = np.identity(4, np.float32) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) glBindVertexArray(self.planeVAO) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0) # Cubes model = glm.translate(model, 0.0, 1.5, 0.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.translate(np.identity(4, np.float32), 2.0, 0.0, 1.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.rotate(np.identity(4, np.float32), 60.0, 1.0, 0.0, 1.0) model = glm.scale(model, 0.5, 0.5, 0.5) model = glm.translate(model, -1.0, 0.0, 2.0) glUniformMatrix4fv(glGetUniformLocation(shader, 'model'), 1, GL_FALSE, model) self.renderCube()
def render_scene(time, cam1, view, projection): glClearColor(.8, .12, 0.32, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Create meshes points1, normals1 = generate_height_map(nw=8, nl=8, width=4, length=4, t=1.5*time, zscale=0.6) points3, indices3 = box() vao1, vbo1, ebo1 = create_vertex_array2(coords=points1, colors=normals1) vao_lamp, _, _ = create_vertex_array2(coords=points3, indices=indices3) # Light source: lamp light_color = glm.vec3(1, 1, 1) light_position = glm.vec3(np.cos(time), 1., np.sin(time)) # Height map glBindVertexArray(vao1) glUseProgram(shader_basic_lighting) model = glm.identity(4) # model = glm.translate(glm.vec3(.0, 0.5, .0)) * model model = model * glm.rotate(-np.pi/2, glm.vec3(1, 0, 0)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_color"), 1, light_color) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "light_pos"), 1, light_position) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "object_color"), 1, glm.vec3(1, .5, .31)) glUniform3fv(glGetUniformLocation(shader_basic_lighting, "view_pos"), 1, cam1.position) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_basic_lighting, "model"), 1, GL_TRUE, np.asarray(model)) glDrawArrays(GL_TRIANGLES, 0, len(points1)) glBindVertexArray(0) # Lamp glUseProgram(shader_solid) glBindVertexArray(vao_lamp) model = glm.translate(light_position) model = model * glm.scale(glm.vec3(.2, .2, .2)) glUniform3fv(glGetUniformLocation(shader_solid, "solid_color"), 1, glm.vec3(1, 1, 1)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices3), GL_UNSIGNED_INT, None) glBindVertexArray(0)
def render_scene(time, cam1, view, projection): glClearColor(.8, .12, 0.32, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Create meshes points3, indices3 = box() vao_lamp = VAO(attributes=[points3], indices=indices3) # Light source: lamp light_color = glm.vec3(1, 1, 1) light_position = glm.vec3(3*np.cos(time), 2., 3*np.sin(time)) # Height map # glBindVertexArray(vao1) vao1.bind() glUseProgram(shader_flat_shading) model = glm.identity(4) # model = glm.translate(glm.vec3(.0, 0.5, .0)) * model model = model * glm.rotate(-np.pi/2, glm.vec3(1, 0, 0)) glUniform1f(glGetUniformLocation(shader_flat_shading, "time"), time); glUniform3fv(glGetUniformLocation(shader_flat_shading, "light_color"), 1, light_color) glUniform3fv(glGetUniformLocation(shader_flat_shading, "light_pos"), 1, light_position) glUniform3fv(glGetUniformLocation(shader_flat_shading, "object_color"), 1, glm.vec3(1, .5, .31)) glUniform3fv(glGetUniformLocation(shader_flat_shading, "view_pos"), 1, cam1.position) glUniformMatrix4fv(glGetUniformLocation(shader_flat_shading, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_flat_shading, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_flat_shading, "model"), 1, GL_TRUE, np.asarray(model)) glDrawArrays(GL_TRIANGLES, 0, len(points1)) glBindVertexArray(0) # Lamp glUseProgram(shader_solid) vao_lamp.bind() model = glm.translate(light_position) model = model * glm.scale(glm.vec3(.2, .2, .2)) glUniform3fv(glGetUniformLocation(shader_solid, "solid_color"), 1, glm.vec3(1, 1, 1)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniformMatrix4fv(glGetUniformLocation(shader_solid, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices3), GL_UNSIGNED_INT, None) glBindVertexArray(0)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # Render # Clear the colorbuffer glClearColor(0.2, 0.3, 0.3, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__shaderProgram) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.texture1) glUniform1i(glGetUniformLocation(self.__shaderProgram, 'ourTexture1'), 0) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.texture2) glUniform1i(glGetUniformLocation(self.__shaderProgram, 'ourTexture2'), 1) view = self.camera.viewMatrix view = glm.translate(view, 0.0, 0.0, -3.0) projection = glm.perspective(self.camera.zoom, float(self.width()) / self.height(), 0.1, 1000.0) # get their uniform location modelLoc = glGetUniformLocation(self.__shaderProgram, 'model') viewLoc = glGetUniformLocation(self.__shaderProgram, 'view') projLoc = glGetUniformLocation(self.__shaderProgram, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) glBindVertexArray(self.__vao) for i in self.__cubePositions: # calculate the model matrix for each object and pass it to shader before drawing angle = math.degrees(20.0 * self.__cubePositions.index(i)) model = glm.rotate(np.identity(4, np.float32), angle, 1.0, 0.3, 0.5) model = glm.translate(model, i[0], i[1], i[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0)
def renderGL(self): """opengl render method""" # get the time in seconds t = glfwGetTime() # use the transform shader program glUseProgram(self.__tshaderProgram) # set the uniforms glUniform3fv(self.__centerLocation, 3, self.__center) glUniform1fv(self.__radiusLocation, 3, self.__radius) glUniform3fv(self.__gLocation, 1, self.__g) glUniform1f(self.__dtLocation, self.__dt) glUniform1f(self.__bounceLocation, self.__bounce) glUniform1i(self.__seedLocation, randint(0, 0x7fff)) # bind the current vao glBindVertexArray(self.__vao[(self.__currentBuffer + 1) % self.__bufferCount]) # bind transform feedback target glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, self.__vbo[self.__currentBuffer]) glEnable(GL_RASTERIZER_DISCARD) # perform transform feedback glBeginTransformFeedback(GL_POINTS) glDrawArrays(GL_POINTS, 0, self.__particles) glEndTransformFeedback() glDisable(GL_RASTERIZER_DISCARD) # clear first glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # use the shader program glUseProgram(self.__shaderProgram) # calculate ViewProjection matrix projection = glm.perspective(90.0, 4.0 / 3.0, .1, 100.0) projection = np.array(projection, dtype=np.float32) # translate the world/view position view = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, -30.0)) # make the camera rotate around the origin view = glm.rotate(view, 30.0, glm.vec3(1.0, 0.0, 0.0)) view = glm.rotate(view, -22.5 * t, glm.vec3(0.0, 1.0, 0.0)) view = np.array(view, dtype=np.float32) # set the uniform glUniformMatrix4fv(self.__viewLocation, 1, GL_FALSE, view) glUniformMatrix4fv(self.__projLocation, 1, GL_FALSE, projection) # bind the vao glBindVertexArray(self.__vao[self.__currentBuffer]) # draw glDrawArrays(GL_POINTS, 0, self.__particles) glBindVertexArray(0) glUseProgram(0) # advance buffer index self.__currentBuffer = (self.__currentBuffer + 1) % self.__bufferCount
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # 1. Render scene into floating point framebuffer glBindFramebuffer(GL_FRAMEBUFFER, self.bloomFBO) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) projection = glm.perspective(self.camera.zoom, float(self.width())/self.height(), 0.1, 100.0) view = self.camera.viewMatrix glUseProgram(self.__shader) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'projection'), 1, GL_FALSE, projection) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'view'), 1, GL_FALSE, view) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.woodTexture) # set lighting uniforms for i in range(len(self.lightPos)): glUniform3fv(glGetUniformLocation(self.__shader, 'lights[{}].Position'.format(i)), 1, self.lightPos[i]) glUniform3fv(glGetUniformLocation(self.__shader, 'lights[{}].Color'.format(i)), 1, self.lightColors[i]) glUniform3fv(glGetUniformLocation(self.__shader, 'viewPos'), 1, self.camera.position) # create one large cube that acts as the floor model = glm.scale(np.identity(4, np.float32), 25.0, 1.0, 25.0) model = glm.translate(model, 0.0, -1.0, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() # then create multiple cubes as the scenery glBindTexture(GL_TEXTURE_2D, self.containerTexture) model = glm.translate(np.identity(4, np.float32), 0.0, 1.5, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.translate(np.identity(4, np.float32), 2.0, 0.0, 1.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.rotate(np.identity(4, np.float32), 60.0, 1.0, 0.0, 1.0) model = glm.scale(model, 2.0, 2.0, 2.0) model = glm.translate(model, -1.0, -1.0, 2.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.rotate(np.identity(4, np.float32), 23.0, 1.0, 0.0, 1.0) model = glm.scale(model, 2.5, 2.5, 2.5) model = glm.translate(model, 0.0, 2.7, 4.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() model = glm.rotate(np.identity(4, np.float32), 124.0, 1.0, 0.0, 1.0) model = glm.scale(model, 2.0, 2.0, 2.0) model = glm.translate(model, -2.0, 1.0, -3.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() self.renderCube() model = glm.translate(np.identity(4, np.float32), -3.0, 0.0, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) self.renderCube() # finally show all the light sources as bright cubes glUseProgram(self.__lightShader) glUniformMatrix4fv(glGetUniformLocation(self.__lightShader, 'projection'), 1, GL_FALSE, projection) glUniformMatrix4fv(glGetUniformLocation(self.__lightShader, 'view'), 1, GL_FALSE, view) for i in range(len(self.lightPos)): model = glm.scale(np.identity(4, np.float32), 0.5, 0.5, 0.5) model = glm.translate(model, self.lightPos[i][0], self.lightPos[i][1], self.lightPos[i][2]) glUniformMatrix4fv(glGetUniformLocation(self.__lightShader, 'model'), 1, GL_FALSE, self.lightPos[i]) glUniform3fv(glGetUniformLocation(self.__lightShader, 'lightColor'), 1, self.lightColors[i]) self.renderCube() glBindFramebuffer(GL_FRAMEBUFFER, 0) # 2. Blur bright fragments w/ two-pass Gaussian Blur horizontal = True first_iteration = True amount = 10 glUseProgram(self.__blurShader) for i in range(amount): glBindFramebuffer(GL_FRAMEBUFFER, self.pingpongFBO[int(horizontal)]) glUniform1i(glGetUniformLocation(self.__blurShader, 'horizontal'), horizontal) glBindTexture(GL_TEXTURE_2D, self.colorBuffers[1] if first_iteration else self.pingpongColorbuffers[int(not horizontal)]) self.renderQuad() horizontal = not horizontal if first_iteration: first_iteration = False glBindFramebuffer(GL_FRAMEBUFFER, 0) # 2. Now render floating point color buffer to 2D quad and tonemap HDR colors to default framebuffer's (clamped) color range glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__bloomFinalShader) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.colorBuffers[0]) # glBindTexture(GL_TEXTURE_2D, self.pingpongColorbuffers[int(not horizontal)]) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.pingpongColorbuffers[int(not horizontal)]) glUniform1i(glGetUniformLocation(self.__bloomFinalShader, 'bloom'), self.bloom) glUniform1f(glGetUniformLocation(self.__bloomFinalShader, 'exposure'), self.exposure) self.renderQuad() glUseProgram(0) print 'exposure: {}'.format(self.exposure)
glViewport(0, 0, w, h) # glDepthRange(-10, 10) glEnable(GL_DEPTH_TEST) # draw only if the shape is closer to the viewer glDepthFunc(GL_LESS) # smaller means closer # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Wireframe while not glfwWindowShouldClose(window): time = glfwGetTime() glClearColor(0.6, 0.6, 0.8, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # glClear(GL_COLOR_BUFFER_BIT) glUseProgram(shader1) view = glm.identity(4) view = glm.translate(glm.vec3(0, 0, -2)) view = view * glm.rotate(time, glm.vec3(0, 1, 0)) # glUniformMatrix4fv(glGetUniformLocation(shader1, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader1, "projection"), 1, GL_FALSE, np.asarray(projection)) glBindVertexArray(vao2) model = glm.identity(4) # model = glm.translate(glm.vec3(.0, 0, .0)) # model = model * glm.rotate(1.0 * np.pi * time, glm.vec3(1, 1, 0.2)) glUniformMatrix4fv(glGetUniformLocation(shader1, "model"), 1, GL_TRUE, np.asarray(model)) # (uniform location, num matrices, transpose?, matrix) # In numpy matrices (2D arrays) are expressed row by row whereas # in OpenGL they are column by column. Hence true is given # as the 3rd argument to transpose the matrix. # print(model, view, projection)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # Render # Clear the colorbuffer glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__lightingShader) lightPosLoc = glGetUniformLocation(self.__lightingShader, 'light.position') lightSpotdirLoc = glGetUniformLocation(self.__lightingShader, 'light.direction') lightSpotCutoffLoc = glGetUniformLocation(self.__lightingShader, 'light.cutOff') lightSpotOuterCutoffLoc = glGetUniformLocation(self.__lightingShader, 'light.outerCutOff') viewPosLoc = glGetUniformLocation(self.__lightingShader, 'viewPos') glUniform3f(lightPosLoc, self.camera.position[0], self.camera.position[1], self.camera.position[2]) glUniform3f(lightSpotdirLoc, self.camera.front[0], self.camera.front[1], self.camera.front[2]) glUniform1f(lightSpotCutoffLoc, math.cos(math.radians(12.5))) glUniform1f(lightSpotOuterCutoffLoc, math.cos(math.radians(17.5))) glUniform3f(viewPosLoc, self.camera.position[0], self.camera.position[1], self.camera.position[2]) # set lights properties glUniform3f(glGetUniformLocation(self.__lightingShader, 'light.ambient'), 0.1, 0.1, 0.1) glUniform3f(glGetUniformLocation(self.__lightingShader, 'light.diffuse'), 0.8, 0.8, 0.8) glUniform3f(glGetUniformLocation(self.__lightingShader, 'light.specular'), 1.0, 1.0, 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, 'light.constant'), 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, 'light.linear'), 0.09) glUniform1f(glGetUniformLocation(self.__lightingShader, 'light.quadratic'), 0.032) # set material properties glUniform1f(glGetUniformLocation(self.__lightingShader, 'material.shininess'), 32.0) view = self.camera.viewMatrix projection = glm.perspective(self.camera.zoom, float(self.width()) / self.height(), 0.1, 100.0) # get their uniform location modelLoc = glGetUniformLocation(self.__lightingShader, 'model') viewLoc = glGetUniformLocation(self.__lightingShader, 'view') projLoc = glGetUniformLocation(self.__lightingShader, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) # bind diffuse map glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.diffuseMap) # bind specular map glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.specularMap) ## Draw the container (using container's vertex attributes) #glBindVertexArray(self.__containerVAO) #model = np.identity(4, np.float32) #glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) #glDrawArrays(GL_TRIANGLES, 0, 36) #glBindVertexArray(0) glBindVertexArray(self.__containerVAO) for i in self.__cubePositions: # calculate the model matrix for each object and pass it to shader before drawing angle = math.degrees(20.0 * self.__cubePositions.index(i)) model = glm.rotate(np.identity(4, np.float32), angle, 1.0, 0.3, 0.5) model = glm.translate(model, i[0], i[1], i[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0)
# projection = glm.create_perspective_projection_matrix(45., 600. / 600., .1, 100.0, dtype=np.float32) # View via user-controlled camera object view = cam1.get_view_matrix() projection = glm.create_perspective_projection_matrix(cam1.fov, 600. / 600., .1, 100.0, dtype=np.float32) # projection = glm.create_orthogonal_projection_matrix(-3, 3, -3, 3, 0.1, 100, dtype=np.float32) # Render objects # Ground glUseProgram(shader1) glUniformMatrix4fv(glGetUniformLocation(shader1, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader1, "projection"), 1, GL_FALSE, np.asarray(projection)) glUniform3fv(glGetUniformLocation(shader1, "solid_color"), 1, glm.vec3(0, 0, 1)) glBindVertexArray(vao1) model = glm.identity(4) model = glm.rotate(np.pi*0.5, glm.vec3(1, 0, 0)) * model glUniformMatrix4fv(glGetUniformLocation(shader1, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices2), GL_UNSIGNED_INT, None) glBindVertexArray(0) # Big Cube glUseProgram(shader2) glBindVertexArray(vao2) glUniformMatrix4fv(glGetUniformLocation(shader2, "view"), 1, GL_TRUE, np.asarray(view)) glUniformMatrix4fv(glGetUniformLocation(shader2, "projection"), 1, GL_FALSE, np.asarray(projection)) model = glm.identity(4) model = glm.translate(glm.vec3(.0, 0.5, .0)) * model # model = model * glm.rotate(1.0 * np.pi * time, glm.vec3(1, 1, 0.2)) glUniformMatrix4fv(glGetUniformLocation(shader2, "model"), 1, GL_TRUE, np.asarray(model)) glDrawElements(GL_TRIANGLES, len(indices2), GL_UNSIGNED_INT, None) glBindVertexArray(0)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # Render # Clear the colorbuffer glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__lightingShader) viewPosLoc = glGetUniformLocation(self.__lightingShader, 'viewPos') glUniform3f(viewPosLoc, self.camera.position[0], self.camera.position[1], self.camera.position[2]) # set material properties glUniform1f(glGetUniformLocation(self.__lightingShader, 'material.shininess'), 32.0) # Directional light glUniform3f(glGetUniformLocation(self.__lightingShader, "dirLight.direction"), -0.2, -1.0, -0.3) glUniform3f(glGetUniformLocation(self.__lightingShader, "dirLight.ambient"), 0.05, 0.05, 0.05) glUniform3f(glGetUniformLocation(self.__lightingShader, "dirLight.diffuse"), 0.4, 0.4, 0.4) glUniform3f(glGetUniformLocation(self.__lightingShader, "dirLight.specular"), 0.5, 0.5, 0.5) # point lights for i in self.__pointLightPositions: index = self.__pointLightPositions.index(i) glUniform3f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].position".format(index)), i[0], i[0], i[0]) glUniform3f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].ambient".format(index)), 0.05, 0.05, 0.05) glUniform3f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].diffuse".format(index)), 0.8, 0.8, 0.8) glUniform3f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].specular".format(index)), 1.0, 1.0, 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].constant".format(index)), 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].linear".format(index)), 0.09) glUniform1f(glGetUniformLocation(self.__lightingShader, "pointLights[{}].quadratic".format(index)), 0.032) # spotlight glUniform3f(glGetUniformLocation(self.__lightingShader, "spotLight.position"), self.camera.position[0], self.camera.position[1], self.camera.position[2]) glUniform3f(glGetUniformLocation(self.__lightingShader, "spotLight.direction"), self.camera.front[0], self.camera.front[1], self.camera.front[2]) glUniform3f(glGetUniformLocation(self.__lightingShader, "spotLight.ambient"), 0.0, 0.0, 0.0) glUniform3f(glGetUniformLocation(self.__lightingShader, "spotLight.diffuse"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(self.__lightingShader, "spotLight.specular"), 1.0, 1.0, 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, "spotLight.constant"), 1.0) glUniform1f(glGetUniformLocation(self.__lightingShader, "spotLight.linear"), 0.09); glUniform1f(glGetUniformLocation(self.__lightingShader, "spotLight.quadratic"), 0.032) glUniform1f(glGetUniformLocation(self.__lightingShader, "spotLight.cutOff"), math.cos(math.radians(12.5))) glUniform1f(glGetUniformLocation(self.__lightingShader, "spotLight.outerCutOff"), math.cos(math.radians(15.0))) view = self.camera.viewMatrix projection = glm.perspective(self.camera.zoom, float(self.width()) / self.height(), 0.1, 100.0) # get their uniform location modelLoc = glGetUniformLocation(self.__lightingShader, 'model') viewLoc = glGetUniformLocation(self.__lightingShader, 'view') projLoc = glGetUniformLocation(self.__lightingShader, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) # bind diffuse map glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.diffuseMap) # bind specular map glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.specularMap) glBindVertexArray(self.__containerVAO) for i in self.__cubePositions: # calculate the model matrix for each object and pass it to shader before drawing angle = math.degrees(20.0 * self.__cubePositions.index(i)) model = glm.rotate(np.identity(4, np.float32), angle, 1.0, 0.3, 0.5) model = glm.translate(model, i[0], i[1], i[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) # Also draw the lamp object, again binding the appropriate shader glUseProgram(self.__lampShader) # Get location objects for the matrices on the lamp shader (these could be different on a different shader) modelLoc = glGetUniformLocation(self.__lampShader, 'model') viewLoc = glGetUniformLocation(self.__lampShader, 'view') projLoc = glGetUniformLocation(self.__lampShader, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) glBindVertexArray(self.__lightVAO) for i in self.__pointLightPositions: model = glm.scale(np.identity(4, np.float32), 0.2, 0.2, 0.2) model = glm.translate(model, i[0], i[1], i[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # 1. Geometry Pass: render scene's geometry/color data into gbuffer glBindFramebuffer(GL_FRAMEBUFFER, self.gbuffer) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) projection = glm.perspective(self.camera.zoom, float(self.width()) / self.height(), 0.1, 50.0) view = self.camera.viewMatrix glUseProgram(self.__geometyPassShader) glUniformMatrix4fv(glGetUniformLocation(self.__geometyPassShader, 'projection'), 1, GL_FALSE, projection) glUniformMatrix4fv(glGetUniformLocation(self.__geometyPassShader, 'view'), 1, GL_FALSE, view) # Floor cube model = glm.scale(np.identity(4, np.float32), 20.0, 1.0, 28.0) model = glm.translate(model, 0.0, -1.0, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__geometyPassShader, 'model'), 1, GL_FALSE, model) self.renderCube() # Nanosuit model on the floor model = glm.scale(np.identity(4, np.float32), 0.5, 0.5, 0.5) model = glm.rotate(model, -90.0, 1.0, 0.0, 0.0) model = glm.translate(model, 0.0, 0.0, 5.0) glUniformMatrix4fv(glGetUniformLocation(self.__geometyPassShader, 'model'), 1, GL_FALSE, model) self.cyborg.draw(self.__geometyPassShader) glBindFramebuffer(GL_FRAMEBUFFER, 0) # 2. Create SSAO texture glBindFramebuffer(GL_FRAMEBUFFER, self.ssaoFBO) glClear(GL_COLOR_BUFFER_BIT) glUseProgram(self.__ssaoShader) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.gPositionDepth) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.gNormal) glActiveTexture(GL_TEXTURE2) glBindTexture(GL_TEXTURE_2D, self.noiseTexture) # send kernel + rotation [glUniform3fv(glGetUniformLocation(self.__ssaoShader, 'samples[{}]'.format(i)), 1, self.ssaoKernel[i]) for i in range(64)] glUniformMatrix4fv(glGetUniformLocation(self.__ssaoShader, 'projection'), 1, GL_FALSE, projection) self.renderQuad() glBindFramebuffer(GL_FRAMEBUFFER, 0) # 3. Blur SSAO texture to remove noise glBindFramebuffer(GL_FRAMEBUFFER, self.ssaoBlurFBO) glClear(GL_COLOR_BUFFER_BIT) glUseProgram(self.__ssaoBlurShader) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.ssaoColorBuffer) self.renderQuad() glBindFramebuffer(GL_FRAMEBUFFER, 0) # 4. Lighting Pass: calculate lighting by iterating over a screen filled quad pixel-by-pixel using the gbuffer's content. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__lightingPassShader) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.gPositionDepth) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.gNormal) glActiveTexture(GL_TEXTURE2) glBindTexture(GL_TEXTURE_2D, self.gAlbedo) glActiveTexture(GL_TEXTURE3) # add extra SSAO texture to lighting pass glBindTexture(GL_TEXTURE_2D, self.ssaoColorBufferBlur) # also send light relevent uniforms lightPosView = (self.camera.viewMatrix * np.array([self.lightPos[0], self.lightPos[1], self.lightPos[2], 1.0], np.float32))[3, :4] glUniform3fv(glGetUniformLocation(self.__lightingPassShader, 'light.Position'), 1, lightPosView) glUniform3fv(glGetUniformLocation(self.__lightingPassShader, 'light.Color'), 1, self.lightColor) # Update attenuation parameters and calculate radius _constant = 1.0 # Note that we don't send this to the shader, we assume it is always 1.0 (in our case) linear = 0.09 quadratic = 0.032 glUniform1f(glGetUniformLocation(self.__lightingPassShader, 'light.Linear'), linear) glUniform1f(glGetUniformLocation(self.__lightingPassShader, 'light.Quadratic'), quadratic) glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'draw_mode'), self.draw_mode) self.renderQuad() glUseProgram(0)