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) # draw objects 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) # cubes glBindVertexArray(self.cubeVAO) glBindTexture(GL_TEXTURE_2D, self.cubeTexture) model = glm.translate(np.identity(4, np.float32), -1.0, 0.0, -1.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) model = glm.translate(np.identity(4, np.float32), 2.0, 0.0, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) # floor glBindVertexArray(self.planeVAO) glBindTexture(GL_TEXTURE_2D, self.floorTexture) model = np.identity(4, np.float32) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0) glUseProgram(0)
def scale_object(self, direction): scaling_factor = 1.09 translation_factor_x = self.object.object_center_x translation_factor_y = self.object.object_center_y translation_factor_z = self.object.object_center_z self.matrix = glm.translate( self.matrix, glm.vec3([ translation_factor_x, translation_factor_y, translation_factor_z ])) if direction is 'LOWER_X': scaler = [1.0 / scaling_factor, 1.0, 1.0] elif direction is 'INCREASE_X': scaler = [scaling_factor, 1.0, 1.0] if direction is 'LOWER_Y': scaler = [1.0, 1.0 / scaling_factor, 1.0] elif direction is 'INCREASE_Y': scaler = [1.0, scaling_factor, 1.0] if direction is 'LOWER_Z': scaler = [1.0, 1.0, 1 / scaling_factor] elif direction is 'INCREASE_Z': scaler = [1.0, 1.0, scaling_factor] self.matrix = glm.scale(self.matrix, glm.vec3(scaler)) self.matrix = glm.translate( self.matrix, glm.vec3([(-1) * translation_factor_x, (-1) * translation_factor_y, (-1) * translation_factor_z]))
def render_text(self, text, pos, scale=1, dir=(1,0), halign='left', valign='bottom'): offset = glm.vec3() if halign in ('center', 'right'): width = self.width_of(text, scale) offset.x -= width if halign == 'center': offset.x /= 2 if valign in ('center', 'top'): offset.y += self.top_bearing if valign == 'center': offset.y /= 2 gl.glActiveTexture(gl.GL_TEXTURE0) gl.glBindVertexArray(self.vao) angle_rad = math.atan2(dir[1], dir[0]) rotateM = glm.rotate(glm.mat4(1), angle_rad, glm.vec3(0, 0, 1)) transOriginM = glm.translate(glm.mat4(1), glm.vec3(*pos, 0) + offset) gl.glUniform3f(self.uniform("textColor"), .0, .0, .0) char_x = 0 for c in text: c = ord(c) ch = self.characters[c] w, h = ch[1][0] * scale, ch[1][1] * scale xrel, yrel = char_x + ch[2][0] * scale, (ch[1][1] - ch[2][1]) * scale char_x += (ch[3] >> 6) * scale scaleM = glm.scale(glm.mat4(1), glm.vec3(w, h, 1)) transRelM = glm.translate(glm.mat4(1), glm.vec3(xrel, yrel, 0)) modelM = transOriginM * rotateM * transRelM * scaleM gl.glUniformMatrix4fv(self.uniform("model"), 1, gl.GL_FALSE, glm.value_ptr(modelM)) gl.glBindTexture(gl.GL_TEXTURE_2D, ch[0]) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6) gl.glBindVertexArray(0) gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
def render(self, projection): proj = projection.matrix if not self.waypoints_mesh.is_empty(): print("waypoint mesh", len(self.waypoints_mesh.lines_array())) self.waypoints_mesh.update_drawable(self.waypoints_drawable) self.waypoints_mesh.clear() if not self.waypoints_drawable.is_empty(): mat = glm.translate(proj, (.5, .5, .2)) self.waypoints_drawable.shader.set_uniform("u_projection", mat) self.waypoints_drawable.draw() if self.path_changed: if self.agents._paths: mesh = LineMesh() for path in self.agents._paths.values(): prev_node = None for node in path.path: if prev_node is not None: mesh.add_line(self.waypoints.id_to_pos[prev_node], self.waypoints.id_to_pos[node]) prev_node = node mesh.update_drawable(self.path_drawable) else: self.path_drawable.clear() if not self.path_drawable.is_empty(): mat = glm.translate(proj, (.49, .49, .21)) self.path_drawable.shader.set_uniform("u_projection", mat) self.path_drawable.draw()
def key_callback(window, key, scancode, action, mods): if key == glfw.KEY_UP and action == glfw.PRESS: myPlayer.tran = glm.translate( myPlayer.tran, glm.fvec3(myPlayer.x, myPlayer.y + 0.1, 0.0)) translate_loc = glGetUniformLocation(myShader.ID, 'translate') glUniformMatrix4fv(translate_loc, 1, GL_FALSE, glm.value_ptr(myPlayer.tran)) print("UP KEY PRESSED") if key == glfw.KEY_DOWN and action == glfw.PRESS: myPlayer.tran = glm.translate( myPlayer.tran, glm.fvec3(myPlayer.x, myPlayer.y - 0.1, 0.0)) translate_loc = glGetUniformLocation(myShader.ID, 'translate') glUniformMatrix4fv(translate_loc, 1, GL_FALSE, glm.value_ptr(myPlayer.tran)) print("DOWN KEY PRESSED") if key == glfw.KEY_RIGHT and action == glfw.PRESS: myPlayer.tran = glm.translate( myPlayer.tran, glm.fvec3(myPlayer.x + 0.1, myPlayer.y, 0.0)) translate_loc = glGetUniformLocation(myShader.ID, 'translate') glUniformMatrix4fv(translate_loc, 1, GL_FALSE, glm.value_ptr(myPlayer.tran)) print("RIGHT KEY PRESSED") if key == glfw.KEY_LEFT and action == glfw.PRESS: myPlayer.tran = glm.translate( myPlayer.tran, glm.fvec3(myPlayer.x - 0.1, myPlayer.y, 0.0)) translate_loc = glGetUniformLocation(myShader.ID, 'translate') glUniformMatrix4fv(translate_loc, 1, GL_FALSE, glm.value_ptr(myPlayer.tran)) print("LEFT KEY PRESSED")
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 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 render_text(self, text, pos, scale, dir): glActiveTexture(GL_TEXTURE0) glBindVertexArray(self.vao) angle_rad = math.atan2(dir[1], dir[0]) rotateM = glm.rotate(glm.mat4(1), angle_rad, glm.vec3(0, 0, 1)) transOriginM = glm.translate(glm.mat4(1), glm.vec3(*pos, 0)) char_x = 0 for c in text: c = ord(c) ch = self.characters[c] w, h = ch[1][0] * scale, ch[1][1] * scale xrel, yrel = char_x + ch[2][0] * scale, (ch[1][1] - ch[2][1]) * scale char_x += (ch[3] >> 6) * scale scaleM = glm.scale(glm.mat4(1), glm.vec3(w, h, 1)) transRelM = glm.translate(glm.mat4(1), glm.vec3(xrel, yrel, 0)) modelM = transOriginM * rotateM * transRelM * scaleM glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(modelM)) glBindTexture(GL_TEXTURE_2D, ch[0]) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0) glBindTexture(GL_TEXTURE_2D, 0)
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 rotate(self, deltaDegree): # WIP if deltaDegree == 0: return dx = self.x * 2 / display[0] dy = self.y * 2 / display[1] transform = glm.mat4(1) transform = glm.translate(transform, glm.vec3(dx, dy, 0)) transform = glm.rotate(transform, glm.radians(deltaDegree), glm.vec3(0.0, 0.0, 1.0)) transform = glm.translate(transform, glm.vec3(-dx, -dy, 0)) xy1 = glm.vec4(self.pos_data[0], self.pos_data[1], 0, 1) xy2 = glm.vec4(self.pos_data[3], self.pos_data[4], 0, 1) xy3 = glm.vec4(self.pos_data[6], self.pos_data[7], 0, 1) xy4 = glm.vec4(self.pos_data[9], self.pos_data[10], 0, 1) res_xy1 = transform * xy1 res_xy2 = transform * xy2 res_xy3 = transform * xy3 res_xy4 = transform * xy4 glBindBuffer(GL_ARRAY_BUFFER, self.vbo[0]) self.pos_data[0] = res_xy1[0] self.pos_data[1] = res_xy1[1] self.pos_data[3] = res_xy2[0] self.pos_data[4] = res_xy2[1] self.pos_data[6] = res_xy3[0] self.pos_data[7] = res_xy3[1] self.pos_data[9] = res_xy4[0] self.pos_data[10] = res_xy4[1] glBufferData(GL_ARRAY_BUFFER, self.pos_data.nbytes, self.pos_data, GL_DYNAMIC_DRAW) glBindBuffer(GL_ARRAY_BUFFER, 0)
def DrawSprite(self, texture, position, size, rotate, color): self.shader.UseProgram() model = glm.fmat4(1.0) model = glm.translate(model, glm.vec3(position, 0.0)) # TRS model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0)) model = glm.rotate(model, rotate, glm.vec3(0.0, 0.0, 1.0)) model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y, 0.0)) model = glm.scale(model, glm.vec3(size, 1.0)) glUniformMatrix4fv(glGetUniformLocation(self.shader.ID, "model"), 1, GL_FALSE, glm.value_ptr(model)) glUniform3f(glGetUniformLocation(self.shader.ID, "spriteColor"), color.x, color.y, color.z) glActiveTexture(GL_TEXTURE0) texture.BindTexture() glBindVertexArray(self.VAO) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0)
def pan(self, old_pos, new_pos): inv_view = glm.inverse(self.view) view_pos = glm.vec3(inv_view[3]) los = -glm.vec3(inv_view[2]) up = glm.vec3(inv_view[1]) wnd_from = glm.vec3(old_pos[0], old_pos[1], 1) wnd_to = glm.vec3(new_pos[0], new_pos[1], 1) vp_rect = glm.vec4(0, 0, self.width, self.height) world_from = glm.unProject(wnd_from, self.view, self.proj, vp_rect) world_to = glm.unProject(wnd_to, self.view, self.proj, vp_rect) dir_from = glm.normalize(world_from - view_pos) dir_to = glm.normalize(world_to - view_pos) rot_axis = glm.normalize(glm.cross(dir_from, dir_to)) rot_angle = math.acos(glm.dot(dir_from, dir_to)) rotate = glm.rotate(glm.mat4(1), rot_angle, rot_axis) model_rot = glm.translate(glm.mat4(1), view_pos) * rotate * glm.translate( glm.mat4(1), -view_pos) self.view = self.view * model_rot
def render(self): trans_mat = glm.mat4(1.0, dtype=c_float) trans_mat = glm.translate(trans_mat, self.position) loc_trans_mat = glm.mat4(1.0, dtype=c_float) loc_trans_mat = glm.translate(loc_trans_mat, self.local_position) rot_mat = glm.mat4_cast(self.rotation) loc_rot_mat = glm.mat4_cast(self.local_rotation) # push transformations to stack sm.CURRENT_PROGRAM.push() # sm.CURRENT_PROGRAM.model_mat4(glm.mat4(1.0, dtype=c_float).value) sm.CURRENT_PROGRAM.model_mat4(trans_mat.value) sm.CURRENT_PROGRAM.model_mat4(rot_mat.value) sm.CURRENT_PROGRAM.model_mat4(loc_trans_mat.value) sm.CURRENT_PROGRAM.model_mat4(loc_rot_mat.value) # disable depth test if self.disable_depth is True gl.glEnable(gl.GL_DEPTH_TEST) # is_depth_enabled = gl.glIsEnabled(gl.GL_DEPTH_TEST) # if self.disable_depth and is_depth_enabled: # gl.glDisable(gl.GL_DEPTH_TEST) # draw the mesh self._mesh.render() # re-enable depth test if depth test was enabled prior to disabling # if self.disable_depth and is_depth_enabled: # gl.glEnable(gl.GL_DEPTH_TEST) # render children for i in range(len(self._children)): # pushing here separates the transformations of the children from the parent # sm.CURRENT_PROGRAM.push() self._children[i].render() # sm.CURRENT_PROGRAM.pop() # pop transformations from stack sm.CURRENT_PROGRAM.pop()
def render(self, rs: GameRenderSettings, pass_num: int): # print(self.drawable._attributes["a_position"]) proj = rs.projection.projection_matrix_4() trans = rs.projection.transformation_matrix_4() trans *= self.object.transformation_matrix() if 0: self.mesh.render( projection=proj, transformation=trans, ) else: trans = self.object.transformation_matrix() trans = glm.scale(glm.mat4(1), glm.vec3(.2, .2, .2)) self.mesh.render_multi( projection=proj, transformations=[ trans, glm.translate(trans, glm.vec3(-1, -1, 0)), #glm.mat4(1), glm.translate(trans, glm.vec3(1, 1, 0)), glm.translate(trans, glm.vec3(.1, 1, 0)), glm.translate(trans, glm.vec3(.2, 1, 0)), glm.translate(trans, glm.vec3(.3, 1, 0)), #glm.translate(glm.mat4(1), glm.vec3(2, 0, 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 WindowMat(self): vp_rect = self.VpRect() inv_wnd = glm.translate(glm.mat4(1), glm.vec3(-1, -1, -1)) inv_wnd = glm.scale(inv_wnd, glm.vec3(2 / vp_rect[2], 2 / vp_rect[3], 2)) inv_wnd = glm.translate(inv_wnd, glm.vec3(vp_rect[0], vp_rect[1], 0)) return glm.inverse(inv_wnd), inv_wnd
def rotate_object(self, direction): transformation_angle = 3.0 translation_factor_x = self.object.object_center_x translation_factor_y = self.object.object_center_y translation_factor_z = self.object.object_center_z self.matrix = glm.translate(self.matrix, glm.vec3([translation_factor_x, translation_factor_y, translation_factor_z])) if direction is 'LOWER_X': rotator = [1.0, 0.0, 0.0] elif direction is 'INCREASE_X': rotator = [-1.0, 0.0, 0.0] if direction is 'LOWER_Y': rotator = [0.0, -1.0, 0.0] elif direction is 'INCREASE_Y': rotator = [0.0, 1.0, 0.0] if direction is 'LOWER_Z': rotator = [0.0, 0.0, -1.0] elif direction is 'INCREASE_Z': rotator = [0.0, 0.0, 1.0] self.matrix = glm.rotate(self.matrix, glm.radians(transformation_angle), glm.vec3(rotator)) self.matrix = glm.translate(self.matrix, glm.vec3([(-1) * translation_factor_x, (-1) * translation_factor_y, (-1) * translation_factor_z]))
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 DrawNoTex(self, position, size, rotate, color, Grid, Selected): self.shader.UseProgram() model = glm.fmat4(1.0) model = glm.translate(model, glm.vec3(position, 0.0)) model = glm.translate(model, glm.vec3(0.5 * size.x, 0.5 * size.y, 0.0)) model = glm.rotate(model, glm.radians(rotate), glm.vec3(1.0, 0.0, 0.0)) model = glm.translate(model, glm.vec3(-0.5 * size.x, -0.5 * size.y, 0.0)) model = glm.scale(model, glm.vec3(size, 1.0)) glUniformMatrix4fv(glGetUniformLocation(self.shader.ID, "model"), 1, GL_FALSE, glm.value_ptr(model)) glUniform3f(glGetUniformLocation(self.shader.ID, "spriteColor"), color.x, color.y, color.z) glUniform2f(glGetUniformLocation(self.shader.ID, "FullGrid"), Grid.x, Grid.y) glUniform2f(glGetUniformLocation(self.shader.ID, "CurrCoord"), Selected.x, Selected.y) # glActiveTexture(GL_TEXTURE0) glBindVertexArray(self.VAO) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0)
def test_scene(): from .lights import PointLight, SpotLight, DirectionalLight scene = Scene() for j in range(2): for x, roughness in zip(np.linspace(-3, 3, 5), np.linspace(0, 1, 5)): sphere = Mesh(transform=glm.translate(glm.mat4(1), (x, 0.5, j * 3 - 1.5)), geometry=Geometry(*imdraw.geo.sphere()), material=Material(albedo=glm.vec3(0.5), emission=(0, 0, 0), roughness=roughness, metallic=float(j))) scene.add_child(sphere) dirlight = DirectionalLight(direction=glm.vec3(1, -6, -2), color=glm.vec3(1.0), intensity=1.0, position=-glm.vec3(1, -6, -2), radius=5, near=1, far=30) spotlight = SpotLight(position=glm.vec3(-1, 0.5, -3), direction=glm.vec3(1, -0.5, 3), color=glm.vec3(0.04, 0.6, 1.0), intensity=150.0, fov=60, near=1, far=15) pointlight = PointLight(position=glm.vec3(2.5, 1.3, 2.5), color=glm.vec3(1, 0.7, 0.1), intensity=17.5, near=0.1, far=10) scene.add_child(dirlight) scene.add_child(spotlight) scene.add_child(pointlight) # cube = Mesh(transform=glm.translate(glm.mat4(1), (1.0, 0.5, 0.0)) * glm.rotate(glm.mat4(1), glm.radians(30), (0,1,0)), # geometry=Geometry(*imdraw.geo.cube()), # material=Material(albedo=(1, 0, 0), # emission=(0,0,0), # roughness=0.7, # metallic=0.0)) # scene.add_child(cube) plane = Mesh(transform=glm.translate(glm.mat4(1), (0, 0.0, 0.0)), geometry=Geometry(*imdraw.geo.plane()), material=Material(albedo=(0.5, 0.5, 0.5), emission=(0, 0, 0), roughness=0.8, metallic=0.0)) scene.add_child(plane) return scene
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 paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # Bind to framebuffer and draw to color texture # as we normally would glBindFramebuffer(GL_FRAMEBUFFER, self.framebuffer) # Render # Clear the colorbuffer glClearColor(0.1, 0.1, 0.1, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_DEPTH_TEST) glUseProgram(self.__shaderProgram) 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.__shaderProgram, 'model') viewLoc = glGetUniformLocation(self.__shaderProgram, 'view') projLoc = glGetUniformLocation(self.__shaderProgram, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) # Floor glBindVertexArray(self.floorVAO) glBindTexture(GL_TEXTURE_2D, self.floorTexture) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, np.identity(4, np.float32)) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0) # Cubes glBindVertexArray(self.cubeVAO) glBindTexture(GL_TEXTURE_2D, self.cubeTexture) model = glm.translate(np.identity(4, np.float32), -1, 0, -1) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) model = glm.translate(np.identity(4, np.float32), 2, 0, 0) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) # Bind to default framebuffer again and draw the # quad plane with attched screen texture glBindFramebuffer(GL_FRAMEBUFFER, 0) # Clear all relevant buffers glClearColor(1.0, 1.0, 1.0, 1.0) glClear(GL_COLOR_BUFFER_BIT) glDisable(GL_DEPTH_TEST) # Draw Screen glUseProgram(self.__screenShaderProgram) glBindVertexArray(self.quadVAO) glBindTexture(GL_TEXTURE_2D, self.textureColorBuffer) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0)
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 _calculate_node_bounding_box( version: int, node_count: int, node: Node, matrix: glm.mat4 = glm.mat4(1.0)) -> None: parent = node.parent rsm_node = node.impl bbox = BoundingBox() if parent is not None: matrix = glm.translate(matrix, glm.vec3(rsm_node.info.position)) if rsm_node.rot_key_count == 0: if rsm_node.info.rotation_angle > 0.01: matrix = glm.rotate( matrix, glm.radians(rsm_node.info.rotation_angle * 180.0 / math.pi), glm.vec3(rsm_node.info.rotation_axis)) else: quaternion = glm.quat(*rsm_node.rot_key_frames[0].quaternion) matrix *= glm.mat4_cast(glm.normalize(quaternion)) matrix = glm.scale(matrix, glm.vec3(rsm_node.info.scale)) local_matrix = copy.copy(matrix) offset_matrix = mat3tomat4(rsm_node.info.offset_matrix) if node_count > 1: local_matrix = glm.translate(local_matrix, glm.vec3(rsm_node.info.offset_vector)) local_matrix *= offset_matrix for i in range(rsm_node.mesh_vertex_count): x = rsm_node.mesh_vertices[i].position[0] y = rsm_node.mesh_vertices[i].position[1] z = rsm_node.mesh_vertices[i].position[2] v = glm.vec3() v[0] = local_matrix[0][0] * x + local_matrix[1][0] * y + local_matrix[ 2][0] * z + local_matrix[3][0] v[1] = local_matrix[0][1] * x + local_matrix[1][1] * y + local_matrix[ 2][1] * z + local_matrix[3][1] v[2] = local_matrix[0][2] * x + local_matrix[1][2] * y + local_matrix[ 2][2] * z + local_matrix[3][2] for j in range(3): bbox.min[j] = min(v[j], bbox.min[j]) bbox.max[j] = max(v[j], bbox.max[j]) for i in range(3): bbox.offset[i] = (bbox.max[i] + bbox.min[i]) / 2.0 bbox.range[i] = (bbox.max[i] - bbox.min[i]) / 2.0 bbox.center[i] = bbox.min[i] + bbox.range[i] node.bbox = bbox for child in node.children: _calculate_node_bounding_box(version, node_count, child, matrix)
def classic_copy_board(M_matrix, M_location): for i in range(10): M_matrix = glm.translate(M_matrix, glm.vec3(1.0, -10.0, 0.0)) glUniformMatrix4fv(M_location, 1, GL_FALSE, glm.value_ptr(M_matrix)) for j in range(10): M_matrix = glm.translate(M_matrix, glm.vec3(0.0, 1.0, 0.0)) glUniformMatrix4fv(M_location, 1, GL_FALSE, glm.value_ptr(M_matrix)) glDrawArrays(GL_TRIANGLES, 0, 36)
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 _create_transform_for(cls, rot, scale, trans): mat = glm.mat4(1) mat = glm.translate( mat, glm.vec3(trans) + glm.vec3(8, 8, 8) * glm.vec3(scale)) mat = glm.rotate(mat, -math.radians(rot[0]), glm.vec3(0, 0, 1)) mat = glm.rotate(mat, -math.radians(rot[1]), glm.vec3(0, 1, 0)) mat = glm.rotate(mat, -math.radians(rot[2]), glm.vec3(1, 0, 0)) mat = glm.translate(mat, glm.vec3(-8, -8, -8) * glm.vec3(scale)) mat = glm.scale(mat, glm.vec3(scale)) return mat
def getLocalXYZFromAngles(self, angles, len1, len2, joint2Offs): #state_start = time.time() resultTm = glm.mat4x4() resultTm = glm.rotate(resultTm, angles[0], glm.vec3([1.0, 0.0, 0.0])) resultTm = glm.translate(resultTm, joint2Offs) resultTm = glm.rotate(resultTm, angles[1], glm.vec3([0.0, 1.0, 0.0])) resultTm = glm.translate(resultTm, len1) resultTm = glm.rotate(resultTm, angles[2], glm.vec3([0.0, 1.0, 0.0])) resultTm = glm.translate(resultTm, len2) point = glm.vec3(glm.column(resultTm, 3)) #print("tm",(time.time()-state_start)*1000.0) return point '''
def set_rotation(self, origin, axis, angle, rescale): """ Sets rotation matrix on this cube. :param origin: rotation origin :param axis: rotation axis :param angle: rotation angle :param rescale: fixme: currently ignored :return: """ self.matrix = glm.translate(glm.mat4(1), glm.vec3(*origin)) self.matrix = glm.rotate(self.matrix, math.radians(angle), Cube.AXES[axis]) self.matrix = glm.translate(self.matrix, -glm.vec3(*origin))
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) self.view = glm.mat4(1.0) self.view = glm.translate(self.view, glm.vec3(0.0, 0.0, -3.0)) # to rotate camera along 45 degree or unit vector's direction # self.view = glm.rotate(self.view, (time() - self.startTime)* glm.radians(50), glm.vec3(1, 1, 1)) 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.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 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.__shaderProgram) 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.__shaderProgram, 'model') viewLoc = glGetUniformLocation(self.__shaderProgram, 'view') projLoc = glGetUniformLocation(self.__shaderProgram, 'projection') glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) # Cubes glBindVertexArray(self.cubeVAO) glBindTexture(GL_TEXTURE_2D, self.cubeTexture) model = glm.translate(np.identity(4, np.float32), -1, 0, -1) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) model = glm.translate(np.identity(4, np.float32), 2, 0, 0) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) # Floor glBindVertexArray(self.planeVAO) glBindTexture(GL_TEXTURE_2D, self.floorTexture) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, np.identity(4, np.float32)) glDrawArrays(GL_TRIANGLES, 0, 6) # Vergetation glBindVertexArray(self.transparentVAO) glBindTexture(GL_TEXTURE_2D, self.transparentTexture) for i in self.vegetation: model = glm.translate(np.identity(4, np.float32), i[0], i[1], i[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(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 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 # Render # Clear the colorbuffer glClearColor(0.05, 0.05, 0.05, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__shaderProgram) 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, 100.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) model = np.identity(4, np.float32) model = glm.scale(model, 0.2, 0.2, 0.2) model = glm.translate(model, 0.0, -1.75, 0.0) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) self.model.draw(self.__shaderProgram)
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 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 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) objectColorLoc = glGetUniformLocation(self.__lightingShader, 'objectColor') lightColorLoc = glGetUniformLocation(self.__lightingShader, 'lightColor') lightPosLoc = glGetUniformLocation(self.__lightingShader, 'lightPos') viewPosLoc = glGetUniformLocation(self.__lightingShader, 'viewPos') glUniform3f(objectColorLoc, 1.0, 0.5, 0.31) glUniform3f(lightColorLoc, 1.0, 0.5, 1.0) glUniform3f(lightPosLoc, self.lightPos[0], self.lightPos[1], self.lightPos[2]) glUniform3f(viewPosLoc, self.camera.position[0], self.camera.position[1], self.camera.position[2]) 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) # 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) # 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') # set matrices glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view) glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection) model = np.identity(4, np.float32) model = glm.scale(model, 0.2, 0.2, 0.2) model = glm.translate(model, self.lightPos[0], self.lightPos[1], self.lightPos[2]) glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model) # Draw the light object (using light's vertex attributes) glBindVertexArray(self.__lightVAO) glDrawArrays(GL_TRIANGLES, 0, 36) 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 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) view = self.camera.viewMatrix glBindBuffer(GL_UNIFORM_BUFFER, self.uboMatrices) glBufferSubData(GL_UNIFORM_BUFFER, self.identityMat4.nbytes, self.identityMat4.nbytes, view) glBindBuffer(GL_UNIFORM_BUFFER, 0) # Draw 4 cubes # Red glBindVertexArray(self.cubeVAO) glUseProgram(self.__shaderRed) model = glm.translate(np.identity(4, np.float32), -0.75, 0.75, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shaderRed, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) # Green glUseProgram(self.__shaderGreen) model = glm.translate(np.identity(4, np.float32), 0.75, 0.75, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shaderGreen, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) # Blue glUseProgram(self.__shaderBlue) model = glm.translate(np.identity(4, np.float32), -0.75, -0.75, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shaderBlue, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) # Yellow glUseProgram(self.__shaderYellow) model = glm.translate(np.identity(4, np.float32), 0.75, -0.75, 0.0) glUniformMatrix4fv(glGetUniformLocation(self.__shaderYellow, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) 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 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 # 1. Render scene into floating point framebuffer glBindFramebuffer(GL_FRAMEBUFFER, self.hdrFBO) 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) # render tunnel model = glm.scale(np.identity(4, np.float32), 5.0, 5.0, 55.0) model = glm.translate(model, 0.0, 0.0, 25.0) glUniformMatrix4fv(glGetUniformLocation(self.__shader, "model"), 1, GL_FALSE, model) glUniform1i(glGetUniformLocation(self.__shader, "inverse_normals"), GL_TRUE) self.renderCube() 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.__hdrShader) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.colorBuffer) glUniform1i(glGetUniformLocation(self.__hdrShader, "hdr"), self.hdr) glUniform1f(glGetUniformLocation(self.__hdrShader, "exposure"), self.exposure) self.renderQuad() glUseProgram(0) print "exposure: {}".format(self.exposure)
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)
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
w, h = glfwGetFramebufferSize(window) 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.
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)
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) # 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)) transformLoc = glGetUniformLocation(shader2, "model") glUniformMatrix4fv(transformLoc, 1, GL_TRUE, np.array(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)
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime glPolygonMode(GL_FRONT_AND_BACK, GL_LINE if self.wireframe else GL_FILL) # 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, 100.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) for pos in self.objectPosition: model = glm.scale(np.identity(4, np.float32), 0.25, 0.25, 0.25) model = glm.translate(model, pos[0], pos[1], pos[2]) glUniformMatrix4fv(glGetUniformLocation(self.__geometyPassShader, 'model'), 1, GL_FALSE, model) self.cyborg.draw(self.__geometyPassShader) glBindFramebuffer(GL_FRAMEBUFFER, 0) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) # 2. 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.gPosition) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.gNormal) glActiveTexture(GL_TEXTURE2) glBindTexture(GL_TEXTURE_2D, self.gAlbedoSpec) # also send light relevent uniforms for i in range(len(self.lightPos)): glUniform3fv(glGetUniformLocation(self.__lightingPassShader, 'lights[{}].Position'.format(i)), 1, self.lightPos[i]) glUniform3fv(glGetUniformLocation(self.__lightingPassShader, 'lights[{}].Color'.format(i)), 1, self.lightColors[i]) # 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.7 quadratic = 1.8 glUniform1f(glGetUniformLocation(self.__lightingPassShader, 'lights[{}].Linear'.format(i)), linear) glUniform1f(glGetUniformLocation(self.__lightingPassShader, 'lights[{}].Quadratic'.format(i)), quadratic) # Then calculate radius of light volume/sphere lightThreshold = 5.0 # 5 # 256 maxBrightness = max(max(self.lightColors[i][0], self.lightColors[i][1]), self.lightColors[i][2]) radius = (-linear + math.sqrt(linear * linear - 4 * quadratic * (_constant - (256.0 / lightThreshold) * maxBrightness))) / (2 * quadratic) glUniform1f(glGetUniformLocation(self.__lightingPassShader, 'lights[{}].Radius'.format(i)), radius) glUniform3fv(glGetUniformLocation(self.__lightingPassShader, 'viewPos'), 1, self.camera.position) glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'draw_mode'), self.draw_mode) self.renderQuad() # 2.5. Copy content of geometry's depth buffer to default framebuffer's depth buffer glBindFramebuffer(GL_READ_FRAMEBUFFER, self.gbuffer) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) # write to default framebuffer glBlitFramebuffer(0, 0, self.width(), self.height(), 0, 0, self.width(), self.height(), GL_DEPTH_BUFFER_BIT, GL_NEAREST) glBindFramebuffer(GL_FRAMEBUFFER, 0) # 3. Render lights on top of scene, by blitting glUseProgram(self.__lightBoxShader) glUniformMatrix4fv(glGetUniformLocation(self.__lightBoxShader, 'projection'), 1, GL_FALSE, projection) glUniformMatrix4fv(glGetUniformLocation(self.__lightBoxShader, 'view'), 1, GL_FALSE, view) for i in range(len(self.lightPos)): model = glm.scale(np.identity(4, np.float32), 0.25, 0.25, 0.25) model = glm.translate(model, self.lightPos[i][0], self.lightPos[i][1], self.lightPos[i][2]) glUniformMatrix4fv(glGetUniformLocation(self.__lightBoxShader, 'model'), 1, GL_FALSE, model) glUniform3fv(glGetUniformLocation(self.__lightBoxShader, 'lightColor'), 1, self.lightColors[i]) self.renderCube() glUseProgram(0)
def initGL(self): """opengl initialization""" # load shaders vertexShader = self.shaderFromFile(GL_VERTEX_SHADER, self.__vertexShader) fragmentShader = self.shaderFromFile(GL_FRAGMENT_SHADER, self.__fragmentShader) self.__shaderProgram = shaders.compileProgram(vertexShader, fragmentShader) if not self.__shaderProgram: self.close() self.__ubIndex = glGetUniformBlockIndex(self.__shaderProgram, 'Matrices') # assign the block binding glUniformBlockBinding(self.__shaderProgram, self.__ubIndex, self.__matricesBinding) # create uniform buffer self.__ubo = glGenBuffers(1) glBindBuffer(GL_UNIFORM_BUFFER, self.__ubo) glBufferData(GL_UNIFORM_BUFFER, 9 * 4 * 4 * 4, None, GL_DYNAMIC_DRAW) # fill the Model matrix array modelMatrices = [] modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3( 2.0, 2.0, 2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3( 2.0, 2.0, -2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3( 2.0, -2.0, 2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3( 2.0, -2.0, -2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3(-2.0, 2.0, 2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3(-2.0, 2.0, -2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3(-2.0, -2.0, 2.0))) modelMatrices.append(glm.translate(glm.mat4(1.0), glm.vec3(-2.0, -2.0, -2.0))) modelMatrices = np.array(modelMatrices, dtype=np.float32) glBufferSubData(GL_UNIFORM_BUFFER, 4 * 4 * 4, modelMatrices.nbytes, modelMatrices) # generate and bind the vao self.__vao = glGenVertexArrays(1) glBindVertexArray(self.__vao) # generate and bind the buffer object vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) # data for a fullscreen quad vertexData = np.array([ # x y z U V # face 0: 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, # vertex 0 -1.0, 1.0, 1.0, 1.0, 0.0, 0.0, # vertex 1 1.0,-1.0, 1.0, 1.0, 0.0, 0.0, # vertex 2 -1.0,-1.0, 1.0, 1.0, 0.0, 0.0, # vertex 3 # face 1: 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, # vertex 0 1.0,-1.0, 1.0, 0.0, 1.0, 0.0, # vertex 1 1.0, 1.0,-1.0, 0.0, 1.0, 0.0, # vertex 2 1.0,-1.0,-1.0, 0.0, 1.0, 0.0, # vertex 3 # face 2: 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, # vertex 0 1.0, 1.0,-1.0, 0.0, 0.0, 1.0, # vertex 1 -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, # vertex 2 -1.0, 1.0,-1.0, 0.0, 0.0, 1.0, # vertex 3 # face 3: 1.0, 1.0,-1.0, 1.0, 1.0, 0.0, # vertex 0 1.0,-1.0,-1.0, 1.0, 1.0, 0.0, # vertex 1 -1.0, 1.0,-1.0, 1.0, 1.0, 0.0, # vertex 2 -1.0,-1.0,-1.0, 1.0, 1.0, 0.0, # vertex 3 # face 4: -1.0, 1.0, 1.0, 0.0, 1.0, 1.0, # vertex 0 -1.0, 1.0,-1.0, 0.0, 1.0, 1.0, # vertex 1 -1.0,-1.0, 1.0, 0.0, 1.0, 1.0, # vertex 2 -1.0,-1.0,-1.0, 0.0, 1.0, 1.0, # vertex 3 # face 5: 1.0,-1.0, 1.0, 1.0, 0.0, 1.0, # vertex 0 -1.0,-1.0, 1.0, 1.0, 0.0, 1.0, # vertex 1 1.0,-1.0,-1.0, 1.0, 0.0, 1.0, # vertex 2 -1.0,-1.0,-1.0, 1.0, 0.0, 1.0, # vertex 3 ], dtype=np.float32) # fill with data glBufferData(GL_ARRAY_BUFFER, vertexData.nbytes, vertexData, GL_STATIC_DRAW) # set up generic attrib pointers glEnableVertexAttribArray(0) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * 4, None) glEnableVertexAttribArray(1) glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * 4, ctypes.c_void_p(3 * 4)) ibo = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo) indexData = np.array([ # face 0: 0, 1, 2, # first triangle 2, 1, 3, # second triangle # face 1: 4, 5, 6, # first triangle 6, 5, 7, # second triangle # face 2: 8, 9, 10, # first triangle 10, 9, 11, # second triangle # face 3: 12, 13, 14, # first triangle 14, 13, 15, # second triangle # face 4: 16, 17, 18, # first triangle 18, 17, 19, # second triangle # face 5: 20, 21, 22, # first triangle 22, 21, 23, # second triangle ], dtype=np.uint) # fill with data glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData.nbytes, indexData, GL_STATIC_DRAW) glBindVertexArray(0) # we are drawing 3d objects so we want depth testing glEnable(GL_DEPTH_TEST)
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 | GL_STENCIL_BUFFER_BIT) glUseProgram(self.__singleColorProgram) 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.__shaderProgram, 'model') glUniformMatrix4fv(glGetUniformLocation(self.__singleColorProgram, 'view'), 1, GL_FALSE, view) glUniformMatrix4fv(glGetUniformLocation(self.__singleColorProgram, 'projection'), 1, GL_FALSE, projection) glUseProgram(self.__shaderProgram) glUniformMatrix4fv(glGetUniformLocation(self.__shaderProgram, 'view'), 1, GL_FALSE, view) glUniformMatrix4fv(glGetUniformLocation(self.__shaderProgram, 'projection'), 1, GL_FALSE, projection) # Draw floor as normal, we only care about the containers. The floor should NOT fill the stencil buffer so we set its mask to 0x00 glStencilMask(0x00) # Floor glBindVertexArray(self.planeVAO) glBindTexture(GL_TEXTURE_2D, self.floorTexture) glUniformMatrix4fv(glGetUniformLocation(self.__shaderProgram, 'model'), 1, GL_FALSE, np.identity(4, np.float32)) glDrawArrays(GL_TRIANGLES, 0, 6) glBindVertexArray(0) # 1st. Render pass, draw objects as normal, filling the stencil buffer glStencilFunc(GL_ALWAYS, 1, 0xFF) glStencilMask(0xFF) # Cubes glBindVertexArray(self.cubeVAO) glBindTexture(GL_TEXTURE_2D, self.cubeTexture) model = glm.translate(np.identity(4, np.float32), -1, 0, -1) glUniformMatrix4fv(glGetUniformLocation(self.__shaderProgram, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) model = glm.translate(np.identity(4, np.float32), 2, 0, 0) glUniformMatrix4fv(glGetUniformLocation(self.__shaderProgram, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) # 2nd. Render pass, now draw slightly scaled versions of the objects, this time disabling stencil writing. # Because stencil buffer is now filled with several 1s. The parts of the buffer that are 1 are now not drawn, thus only drawing # the objects' size differences, making it look like borders. glStencilFunc(GL_NOTEQUAL, 1, 0xFF) glStencilMask(0x00) glDisable(GL_DEPTH_TEST) glUseProgram(self.__singleColorProgram) scale = 1.1 # Cubes glBindVertexArray(self.cubeVAO) glBindTexture(GL_TEXTURE_2D, self.cubeTexture) model = glm.scale(np.identity(4, np.float32), scale, scale, scale) model = glm.translate(model, -1, 0, -1) glUniformMatrix4fv(glGetUniformLocation(self.__singleColorProgram, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) model = glm.scale(np.identity(4, np.float32), scale, scale, scale) model = glm.translate(model, 2, 0, 0) glUniformMatrix4fv(glGetUniformLocation(self.__singleColorProgram, 'model'), 1, GL_FALSE, model) glDrawArrays(GL_TRIANGLES, 0, 36) glBindVertexArray(0) glStencilMask(0xFF) glEnable(GL_DEPTH_TEST)