def update(self, timeout=0.0): glfw.glfwWaitEventsTimeout(timeout) self.update_gui() gl_utils.glFlush() glfw.glfwSwapBuffers(self._window) if self.hdpi_changed(): # calling resize will handle hdpi changes and resize the UI accordingly self.manual_resize()
def render(window): global program, vao, indices, cube_positions GLFW.glfwPollEvents() GL.glViewport(0, 0, window.width, window.height) cameraPos = GLM.vec3(0.0, 0.0, 3.0) cameraTarget = GLM.vec3(0.0, 0.0, 0.0) cameraDirection = GLM.normalize(cameraPos - cameraTarget) up = GLM.vec3(0.0, 1.0, 0.0) cameraRight = GLM.normalize(GLM.cross(up, cameraDirection)) cameraUp = GLM.cross(cameraDirection, cameraRight) view = GLM.lookAt(GLM.vec3(0.0, 0.0, 3.0), GLM.vec3(0.0, 0.0, 0.0), GLM.vec3(0.0, 1.0, 0.0)) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) program.useProgram() view = GLM.mat4(1.0) view = GLM.translate(view, GLM.vec3(0.0, 0.0, -3.0)) projection = GLM.perspective(GLM.radians(45.0), window.width / window.height, 0.1, 100.0) program.setMat4("projection", projection) for w in range(10): radius = 10.0 camX = MATH.sin(GLFW.glfwGetTime()) * radius camZ = MATH.cos(GLFW.glfwGetTime()) * radius view = GLM.lookAt(GLM.vec3(camX, 0.0, camZ), GLM.vec3(0.0, 0.0, 0.0), GLM.vec3(0.0, 1.0, 0.0)) model = GLM.mat4(1.0) model = GLM.translate(model, cube_positions[w]) model = GLM.rotate(model, GLM.radians(20.0 * w), GLM.vec3(1.0, 0.3, 0.5)) program.setMat4("model", model) program.setMat4("view", view) vao.useVAO(program) GL.glDrawArrays(GL.GL_TRIANGLES, 0, 36) vao.unBind() GLFW.glfwSwapBuffers(window.getWindow())
def on_framebuffer_resize(self, window, w, h): """Updates windows/UI sizes and redraws the UI with correct HDPI scaling.""" self.framebuffer_size = w, h if self.is_minimized(): return # NOTE: macOS and some Linux versions with the wayland display server have an # upscaled framebuffer when using a high-DPI monitor. This means that screen # coordinates != pixel coordinates. We need to know if this is the case in order # to transform UI input into the right coordinates, so we check for this case # here. window_size = glfw.glfwGetWindowSize(window) self.has_scaled_framebuffer = window_size != self.framebuffer_size # Always clear buffers on resize to make sure that the black stripes left/right # are black and not polluted from previous frames. Make sure this is applied on # the whole window and not within glViewport! gl_utils.glClear(gl_utils.GL_COLOR_BUFFER_BIT) gl_utils.glClearColor(0, 0, 0, 1) self.hdpi_factor = glfw.glfwGetWindowContentScale(window)[0] self.gui.scale = self.gui_user_scale * self.hdpi_factor with self.use_content_area() as (x, y, content_w, content_h): # update GUI window to full window self.gui.update_window(w, h) # update content container to content square # NOTE: since this is part of the UI, it will be affected by the gui # scaling, but we actually need real coordinates, so we need to convert it # back. self.cont.outline = ui.FitBox( ui.Vec2(x // self.hdpi_factor, y // self.hdpi_factor), ui.Vec2(content_w // self.hdpi_factor, content_h // self.hdpi_factor), ) self.draw_texture() self.gui.collect_menus() self.update_gui() gl_utils.glFlush() glfw.glfwSwapBuffers(self._window)
def swap(self): glfw.glfwSwapBuffers(self._native_window)