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 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 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 draw_centered(self, width: float, height: float, tex_width: float, tex_height: float): #print("quaddraw_centered", width, height, tex_width, tex_height) if height < width: proj = glm.scale(glm.mat4(1), glm.vec3(height/width, 1, 1)) else: proj = glm.scale(glm.mat4(1), glm.vec3(1, width/height, 1)) if tex_height < tex_width: proj = glm.scale(proj, glm.vec3(tex_width/tex_height, 1, 1)) else: proj = glm.scale(proj, glm.vec3(1, tex_height/tex_width, 1)) self.drawable.shader.set_uniform("u_projection", proj) self.drawable.shader.set_uniform("u_resolution", (tex_width, tex_height, 1./max(1, tex_width), 1./max(1, tex_height))) self.drawable.draw()
def mouse_scroll_callback(self, window: glfw._GLFWwindow, x_offset: float, y_offset: float): """ @fn mouse_scroll_callback() @brief The callback function for glfw.set_sroll_callback(). @param window The ID of the window which is resized. @param x_offset The offset along x axis. @param y_offset The offset along y axis. """ if y_offset != 0.: if y_offset > 0.: tmat = glm.scale(glm.mat4(1.), glm.vec3(1.25)) else: tmat = glm.scale(glm.mat4(1.), glm.vec3(0.8)) self.camera_property.transform_matrix = self.camera_property.transform_matrix * tmat self.update_camera_matrix()
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 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 model_transform(self, event): model = glm.mat4(1) model = glm.translate(model, self.trans) #model = glm.rotate(model, glm.radians(80.0), glm.vec3(0, 1, 0)) #model = glm.rotate(model, glfw.get_time(), glm.vec3(0, 1, 0)) model = glm.scale(model, self.scale) return model
def get_node_transformation( self, node_id ) -> glm.mat4: # Get Node transformation matrix using Node ID. matrix = glm.mat4(1.0) if self.gltf.nodes[node_id].matrix: # Convert gltf matrix to glm matrix. matrix = glm.mat4(*self.gltf.nodes[node_id].matrix) print("Node has matrix.") elif self.gltf.nodes[node_id].rotation or self.gltf.nodes[ node_id].scale or self.gltf.nodes[node_id].rotation: # Convert transformation vectors to matrix. print("Node has vectors.") translation = glm.vec3() rotation = glm.quat() scale = glm.vec3(1.0, 1.0, 1.0) if self.gltf.nodes[node_id].translation: translation = glm.vec3(*self.gltf.nodes[node_id].translation) if self.gltf.nodes[node_id].rotation: rotation = glm.quat(*self.gltf.nodes[node_id].rotation) if self.gltf.nodes[node_id].scale: scale = glm.vec3(*self.gltf.nodes[node_id].scale) trans_mat = glm.translate(glm.mat4(1.0), translation) rot_mat = glm.mat4_cast(rotation) scale_mat = glm.scale(glm.mat4(1.0), scale) matrix = trans_mat * rot_mat * scale_mat return matrix
def update_gl(): Camera.model = glm.scale( glm.mat4(), glm.vec3(Config.scale, Config.scale, Config.scale)) Camera.position = -glm.normalize( glm.vec3( math.cos(math.radians(Camera._yaw)) * math.cos(math.radians(Camera._pitch)), math.sin(math.radians(Camera._pitch)), math.sin(math.radians(Camera._yaw)) * math.cos(math.radians(Camera._pitch)))) Camera.projection = glm.perspective( math.radians(Camera._field_of_view), Config.width / Config.height, 0.1, 100) Camera.view = glm.lookAt(Camera.position, glm.vec3(), Camera.camera_up) Program.forward_mat4("model", Camera.model) Program.forward_mat4("projection", Camera.projection) Program.forward_mat4("view", Camera.view) Program.forward_vec3("camera_position", Camera.position) ShadowProgram.forward_mat4("model", Camera.model) ShadowProgram.forward_mat4("projection", Camera.projection) ShadowProgram.forward_mat4("view", Camera.view) ShadowProgram.forward_vec3("camera_position", Camera.position)
def drawButton(self, pos, size, color): hud_model = glm.translate(glm.mat4(1), glm.vec3(*pos, 0)) * glm.scale( glm.mat4(1), glm.vec3(*size, 1)) glProgramUniformMatrix4fv(self.__hud_prog.Object(), 1, 1, GL_FALSE, glm.value_ptr(hud_model)) glProgramUniform4fv(self.__hud_prog.Object(), 2, 1, color) self.__quad.Draw()
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 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 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 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 display_quadrado(indiceVAO, translatarX, translatarY, translatarZ, escalarX, escalarY, escalarZ): gl.glBindVertexArray(VAO[indiceVAO]) # Cria matrizes de transformação matriz_transformacao = glm.identity(glm.mat4) # Cria matriz identidade # Quanto eu quero transladar em cada eixo vetor_translacao = glm.vec3(translatarX, translatarY, translatarZ) matriz_transformacao = glm.translate(matriz_transformacao, vetor_translacao) # Aumentar o tamanho do quadrado (escala) #escalar = 3.0 # Aplica um mesmo valor de escala em todos os eixos vetor_escala = glm.vec3(escalarX, escalarY, escalarZ) identidade = glm.mat4(1.0) matriz_escala = glm.scale(identidade, vetor_escala) matriz_transformacao = matriz_transformacao * matriz_escala # Localizacao da variavel Uniform matriz transformacao no vertex shader transformacao_loc = gl.glGetUniformLocation(shaderProgram, "transformacao") # Copia os dados da matriz de transformacao para matriz Uniform dentro do vertex shader gl.glUniformMatrix4fv(transformacao_loc, 1, gl.GL_FALSE, glm.value_ptr(matriz_transformacao)) quant_vertices = 6 gl.glDrawArrays(gl.GL_TRIANGLES, 0, quant_vertices) gl.glBindVertexArray(0) # Desvincula o VAO
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 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 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 transformation_matrix_4(self) -> glm.mat4: m = glm.rotate( glm.mat4(1), -self.rotation_deg / 180 * glm.pi(), glm.vec3(0, 0, 1) ) m = m * glm.scale(glm.mat4(), glm.vec3(2. / self.scale)) m = m * glm.translate(glm.mat4(), glm.vec3(-self.location.x, -self.location.y, 0)) return m
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 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 _generate_nodeview_matrix2( rsm_version: int, node: AbstractNode) -> Tuple[glm.mat4, glm.mat4]: # Transformations which are inherited by children local_transform_matrix = glm.mat4() rsm_node = node.impl # Scaling if len(rsm_node.scale_key_frames) > 0: local_transform_matrix = glm.scale( local_transform_matrix, glm.vec3(rsm_node.scale_key_frames[0].scale)) # Rotation if len(rsm_node.rot_key_frames) > 0: # 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]) local_transform_matrix *= glm.mat4_cast(quaternion) else: # Static model local_transform_matrix = rag_mat4_mul( local_transform_matrix, mat3tomat4(rsm_node.info.offset_matrix)) if node.parent: parent_offset_matrix = mat3tomat4( node.parent.impl.info.offset_matrix) local_transform_matrix = rag_mat4_mul( local_transform_matrix, glm.inverse(parent_offset_matrix)) # Translation if rsm_version >= 0x203 and len(rsm_node.pos_key_frames) > 0: key_frame = rsm_node.pos_key_frames[0] position = glm.vec3(key_frame.position) elif node.parent: position = glm.vec3(rsm_node.info.offset_vector) - \ glm.vec3(node.parent.impl.info.offset_vector) parent_offset_matrix = mat3tomat4(node.parent.impl.info.offset_matrix) position = glm.vec3( glm.inverse(parent_offset_matrix) * glm.vec4(position, 1.0)) else: position = glm.vec3(rsm_node.info.offset_vector) # Transformations which are applied only to this node final_transform_matrix = copy.copy(local_transform_matrix) # Reset translation transformation to `position` final_transform_matrix[3] = glm.vec4(position, 1.0) # Inherit transformations from ancestors parent = node.parent while parent: final_transform_matrix = rag_mat4_mul(final_transform_matrix, parent.local_transform_matrix) parent = parent.parent if node.parent: parent_translation = glm.vec3(node.parent.final_transform_matrix[3]) final_transform_matrix[3] += glm.vec4(parent_translation, 0.0) return (local_transform_matrix, final_transform_matrix)
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 _drawOrientationIndicator(self): gloo.set_state(line_width=5) # we want the rotation of the camera but not the translation view = glm.inverse(glm.mat4_cast(self.cam._currentTransform.orientation)) view = glm.scale( view, glm.vec3(0.25)) self._oriProgram['projection'] = self._oriProjection self._oriProgram['view'] = view self._oriProgram.draw('lines')
def __init__(self, shape, voxel): self.__shape = shape self.__voxel = voxel self.__cube_model = glm.scale(glm.translate(glm.mat4(1), glm.vec3(-1)), glm.vec3(2)) self.__invalid = True self.__current_mark = None self.__current_add = None
def model(angle, r_x, r_y, r_z, t_x, t_y, t_z, s_x, s_y, s_z): angle = math.radians(angle) matrix_transform = glm.mat4(1.0) matrix_transform = glm.rotate(matrix_transform, angle, glm.vec3(r_x, r_y, r_z)) matrix_transform = glm.translate(matrix_transform, glm.vec3(t_x, t_y, t_z)) matrix_transform = glm.scale(matrix_transform, glm.vec3(s_x, s_y, s_z)) matrix_transform = np.array(matrix_transform).T return matrix_transform
def render_side_view(camera_intrinsic, camera_extrinsic, width, height, stereo_index, hand_color=glm.vec4(0.8, 0.8, 0.8, 0)): ## # draw grid ## shader_program.use() shader_program.set_matrix( "projection", build_projection_matrix(camera_intrinsic, width, height)) shader_program.set_matrix( "view", build_model_view_matrix(camera_extrinsic[stereo_index])) m = glm.mat4(1.0) m = glm.scale(m, glm.vec3(70)) shader_program.set_matrix("model", glm.value_ptr(m)) shader_program.un_use() grid_model.draw(shader_program, draw_type=GL_LINES) ## # draw cube ## cube_shader_program.use() cube_shader_program.set_matrix( "projection", build_projection_matrix(camera_intrinsic, width, height)) cube_shader_program.set_matrix( "view", build_model_view_matrix(camera_extrinsic[stereo_index])) m = glm.mat4(1.0) m = glm.translate(m, model_position[2]) m = glm.rotate(m, glm.radians(model_position[1][0][0]), model_position[1][0][1]) 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, model_position[0]) cube_shader_program.set_matrix("model", glm.value_ptr(m)) cube_shader_program.set_uniform_3f("handColor", hand_color) cube_shader_program.set_uniform_3f("lightColor", light_color) cube_shader_program.set_uniform_3f("lightPos", light_position) cube_shader_program.un_use() hand_model.draw(cube_shader_program, draw_type=GL_TRIANGLES)
def draw(self, goal_reached, degree): self.model = glm.mat4(1.) self.model = glm.scale(self.model, glm.vec3(.2, .2, 1)) # look for center if self.player_type == 'enemy': self.model = glm.translate(self.model, glm.vec3(.5, 1., 0)) else: self.model = glm.translate(self.model, glm.vec3(.5, .2, 0)) # center in cell self.model = glm.translate( self.model, glm.vec3(self.__current_x * 10, self.__current_y * 10, .0)) if goal_reached: self.model = glm.scale(self.model, glm.vec3(.5, .5, 1)) self.model = glm.rotate(self.model, glm.radians(degree * 50), glm.vec3(0, 0, 1.)) return self.model
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 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 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 # 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 # 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)
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) # Rendering finished glfwSwapBuffers(window) # for arr in [vao1, ]: # glDeleteVertexArrays(1, arr) # for buf in [vbo1, ebo1, ]: # glDeleteBuffers(1, buf) # for prog in [shader1, ]: # glDeleteProgram(prog) glfwTerminate()
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 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)