def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if aspect >= 1: projection = glm.ortho(-aspect, aspect, -1, 1, -1, 1) else: projection = glm.ortho(-1, 1, -1/aspect, 1/aspect, -1, 1) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glDrawArrays(GL_TRIANGLES, 0, 3) glutSwapBuffers() glutPostRedisplay()
def render(self, width, height): self.width, self.height = width, height gl.glUseProgram(self.shaderProgram) proj = glm.ortho(0, self.width, self.height, 0, -1, 1) gl.glUniformMatrix4fv(self.uniform("projection"), 1, gl.GL_FALSE, glm.value_ptr(proj)) yield self
def render2buffer(self, ct, dt, iframe, width, height): if self.iframe == iframe: return ctx = get_context() fb0 = ctx.fbo if (self.width, self.height) != (width, height): self.release() if self.tx0 is None: self.width = width self.height = height self.tx0 = ctx.texture((int(self.width), int(self.height)), self.components, dtype='f4') self.tx1 = ctx.texture((int(self.width), int(self.height)), self.components, dtype='f4') self.fbo is not None and self.fbo.release() self.fbo = ctx.framebuffer(color_attachments=[self.tx1]) self.fbo.use() self.fbo.clear() self.shader._members['jpl_projection'].write(glm.ortho( 0, self.width, 0, self.height, -1, 1 )) self.render(ct, dt) self.tx0, self.tx1 = self.tx1, self.tx0 fb0.use()
def render(self) -> None: """Render ViewCube to canvas.""" if not self.init(): return glViewport(*self._get_viewport()) proj = glm.ortho(-2.3, 2.3, -2.3, 2.3, -2.3, 2.3) mat = glm.lookAt( vec3(0.0, -1.0, 0.0), # position vec3(0.0, 0.0, 0.0), # target vec3(0.0, 0.0, 1.0)) # up modelview = mat * glm.mat4_cast(self.parent.rot_quat) glUseProgram(self.parent.shaders['default']) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(proj)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(modelview)) glBindVertexArray(self._vao) glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, ctypes.c_void_p(0)) self._render_highlighted() glBindVertexArray(0) glUseProgram(0)
def get_projection(self): x = self.center[0] y = self.center[1] h = self.height ar = self.aspect_ratio ortho = glm.ortho(x - h * ar, x + h * ar, y - h, y + h, -100.0, 100.0) return ortho
def set_state_shadowmap(self, shader): view = glm.lookAt(self.position, self.position - self.front, self.up) if self.type == 'directional': projection = glm.ortho( -self.swidth / 2, self.swidth / 2, -self.swidth / 2, self.swidth / 2, self.snear, self.sfar ) elif self.type == 'point': projection = glm.perspective( math.pi / 2, 1., self.snear, self.sfar ) else: projection = glm.perspective( 2 * self.outer_cone, 1., self.snear, self.sfar ) prefix = self.get_uniform_name('') shader[prefix + 'shadowmap_projection'] = flatten(projection * view)
def __init__( self, code, width=800, height=450, x=0, y=0, angle=0.0, anchor_x='left', anchor_y='bottom', color='white', name=None, ): """""" super().__init__( name, rotation=aa2q(glm.radians(angle)), position=glm.vec3(x, y, 0), ) self.t0 = None self.ct = 0 self.dt = 0 self.iframe = 0 w0, h0 = get_window_size() self.shader = load_shadertoy_program(code) self.shader._members['jpl_projection'].write(glm.ortho( 0, w0, 0, h0, -1, 1 )) self.geometry = mglw.geometry.quad_2d( size=(1.0, 1.0), pos=(0.5, 0.5) ) self.channel0 = None self.channel1 = None self.channel2 = None self.channel3 = None self.channeltime = [0., 0., 0., 0.] self.width = width self.height = height self.components = 4 self.color4 = glm.vec4(1., 1., 1., 1.) self.set_anchor(anchor_x, anchor_y) self.color = color self.tx0 = None self.tx1 = None self.fbo = None
def review_point(self): if self.hero_finder is None: return view_left = self.hero_finder.current_x() - (self.view_zoomfactor) view_right = self.hero_finder.current_x() + self.view_zoomfactor view_bottom = self.hero_finder.current_y() - (self.view_zoomfactor) view_up = self.hero_finder.current_y() + self.view_zoomfactor self.projection = glm.ortho(view_left, view_right, view_bottom, view_up)
def __init__(self, ortho=True): self.ortho = ortho self.camera_loc = glm.vec3(0.0, 0.0, 1.0) self.camera_lookat = glm.vec3(0.0, 0.0, 0.0) self.camera_up = glm.vec3(0.0, 1.0, 0.0) self.view_mat = glm.lookAt(self.camera_loc, self.camera_lookat, self.camera_up) self.proj_mat = glm.ortho(-1.0, 1.0, -1.0, 1.0, 0.1, 100.0) gl.glEnable(gl.GL_DEPTH_TEST)
def draw_gui(self, gui, text): self.bind(self.gui_shader) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.active_shader.set_float('opacity', self.opacity) for e in gui: if self.settings['draw/buttons'] or BUTTON not in e: self.push_matrix() if POS in e: self.translate(*e[POS]) if has_components(e, [TEX]): self.set_texture(e[TEX]) if SIZE in e: self.scale(*e[SIZE]) elif FBO_TEX in e: self.set_texture(self.fbo.texture) if SIZE in e: self.scale(*(e[SIZE])) self.update_matrix() self.mesh.draw() self.pop_matrix() self.bind(self.text_shader) proj_mat = glm.ortho(0, self.aspect, 0, 1, 0, 10) self.active_shader.set_mat4('proj', proj_mat) for e in text: tex, size = self.engine.graphics.get_rendered_text(e[TEXT]) self.set_texture(tex) self.push_matrix() if COLOR in e: self.set_color(*e[COLOR]) if POS in e: self.translate(e[POS].x * self.aspect, e[POS].y, e[POS].z) if SIZE in e: self.scale(*(e[SIZE] * size)) self.update_matrix() self.mesh.draw() self.pop_matrix()
def calculate_projection(self): if self.ortho: if min(self.app.size) < 1: return glm.mat4(1) # ratio = self.app.size[0] / self.app.size[1] use_ratio = self._use_ratio() if use_ratio: ratio = self.app.aspect_ratio / 2 return glm.ortho(-ratio, ratio, *self.ortho_bounds()[2:]) else: return glm.ortho(*self.ortho_bounds()) else: # ratio = self.app.size[0] / self.app.size[1] return glm.perspectiveFov( math.tau * self._fov(), self.app.aspect_ratio, # float(self.app.size[0]), 1, # float(self.app.size[1]), 0.01, 1000.0, )
def display(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) projection = glm.ortho(-aspect, aspect, -1, 1, -1, 1) if aspect < 1: projection = glm.ortho(-1, 1, 1 / -aspect, 1 / aspect, -1, 1) elapsed_ms = glutGet(GLUT_ELAPSED_TIME) angle = elapsed_ms * math.pi * 2 / 5000.0 model_view = glm.translate( glm.mat4(1), glm.vec3(math.cos(angle) * 0.5, math.sin(angle) * 0.5, 0)) model_view = glm.scale(model_view, glm.vec3(0.2, 0.2, 1)) glUniformMatrix4fv(0, 1, GL_FALSE, glm.value_ptr(projection)) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(model_view)) glDrawArrays(GL_TRIANGLE_FAN, 0, 4) glutSwapBuffers() glutPostRedisplay()
def __init__(self, left, right, bottom, top): self.position = glm.vec3(0.0, 0.0, 0.0) self.rotation = float(0) self.scale = float(0.0) self.projectionMat = glm.ortho(left, right, bottom, top, -1.0, 1.0) self.viewMat = glm.mat4(1) self.VP = self.projectionMat * self.viewMat self.screenWidth = right self.screenHeight = bottom
def resetProjection(self): # calculate projection gloo.set_viewport(0, 0, *self.physical_size) aspect = self.size[0] / float(self.size[1]) self._projection = perspective(self.fov, aspect, self.nearDistance, self.farDistance) self.program['projection'] = self._projection # calculate projection for the orientation indicator in the lower left orthoScale = 1/500 orthoProjection = glm.ortho( -self.size[0]*orthoScale , self.size[0]*orthoScale, -self.size[1]*orthoScale, self.size[1]*orthoScale) self._oriProjection = glm.translate(orthoProjection,glm.vec3(-self.size[0]*orthoScale+0.28,-self.size[1]*orthoScale+0.28,0))
def _calc_projection(self): asp = self.width / self.height if self._projection == "o": proj = glm.ortho(-3, 3, -3, 3, -30, 30) elif self._projection == "p": proj = glm.perspectiveFov(self.fov, self.width, self.height, 0.01, 100.) proj = glm.translate(proj, glm.vec3(0, 0, -4)) self.projection_matrix = proj
def paintGL(self): currentTime = self.__timer.elapsed() / 1000.0 self.__deltaTime = currentTime - self.__lastTime self.__lastTime = currentTime # change light position over time self.lightPos[2] = math.cos(currentTime) * 2.0 # 1. Render depth of scene to texture (from light's perspective) # Get light projection/view matrix. near_plane = 1.0 far_plane = 7.5 lightProjection = glm.ortho(-10.0, 10.0, -10.0, 10.0, near_plane, far_plane) #lightProjection = glm.perspective(45.0, float(self.width())/self.height(), near_plane, far_plane) lightView = glm.lookAt(self.lightPos, np.zeros(3, np.float32), np.ones(3, np.float32)) lightSpaceMatrix = lightProjection * lightView # now render scene from light's point of view glUseProgram(self.__simpleDepthShader) glUniformMatrix4fv(glGetUniformLocation(self.__simpleDepthShader, 'lightSpaceMatrix'), 1, GL_FALSE, lightSpaceMatrix) glViewport(0, 0, self.shadowWidth, self.shadowHeight) glBindFramebuffer(GL_FRAMEBUFFER, self.depthMapFBO) glClear(GL_DEPTH_BUFFER_BIT) self.renderScene(self.__simpleDepthShader) glBindFramebuffer(GL_FRAMEBUFFER, 0) # 2. render scene as normal glViewport(0, 0, self.width(), self.height()) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glUseProgram(self.__shader) projection = glm.perspective(self.camera.zoom, float(self.width())/self.height(), 0.1, 100.0) view = self.camera.viewMatrix glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'projection'), 1, GL_FALSE, projection) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'view'), 1, GL_FALSE, view) # set light uniforms glUniform3fv(glGetUniformLocation(self.__shader, 'lightPos'), 1, self.lightPos) glUniform3fv(glGetUniformLocation(self.__shader, 'viewPos'), 1, self.camera.position) glUniformMatrix4fv(glGetUniformLocation(self.__shader, 'lightSpaceMatrix'), 1, GL_FALSE, lightSpaceMatrix) # Enable/Disable shadows by pressing 'SPACE' glUniform1i(glGetUniformLocation(self.__shader, 'shadows'), self.shadows) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.woodTexture) glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, self.depthMap) self.renderScene(self.__shader) # 3. DEBUG: visualize depth map by rendering it to plane glUseProgram(self.__debugDepthQuad) glUniform1f(glGetUniformLocation(self.__debugDepthQuad, 'near_plane'), near_plane) glUniform1f(glGetUniformLocation(self.__debugDepthQuad, 'far_plane'), far_plane) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.depthMap) #self.renderQuad() # uncomment this line to see depth map glUseProgram(0)
def resize(width, height): gl.glViewport(0, 0, width, height) screen_size.x = width screen_size.y = height screen_ratio = screen_size.x / screen_size.y global perspective_projection perspective_projection = glm.perspective(glm.radians(45.0), screen_size.x / screen_size.y, 0.1, 100.0) global orthogonal_projection orthogonal_projection = glm.ortho(-10.0 * screen_ratio, 10.0 * screen_ratio, -10.0, 10.0)
def projection_matrix(self) -> mat4: """Returns a mat4 representing the current projection matrix.""" canvas_size = self.get_canvas_size() if self.orthographic: return glm.ortho(-canvas_size.width / 2.0 / self._zoom, canvas_size.width / 2.0 / self._zoom, -canvas_size.height / 2.0 / self._zoom, canvas_size.height / 2.0 / self._zoom, -5.0 * self._dist, 5.0 * self._dist) else: aspect_ratio = canvas_size.width / canvas_size.height return glm.perspective(math.atan(math.tan(math.radians(45.0))), aspect_ratio, 0.1, 2000.0)
def _DrawHUD(self, time): glDisable(GL_DEPTH_TEST) glBindProgramPipeline(self.__hud_pipeline[0]) self.__quad.Bind() hud_projection = glm.ortho(0, self.__vp_size[0], self.__vp_size[1], 0, -1, 1) glProgramUniformMatrix4fv(self.__hud_prog.Object(), 0, 1, GL_FALSE, glm.value_ptr(hud_projection)) for button in self.__buttons: self.drawButton(button.position, button.size, button.color)
def _calc_matrix(self): sc = 11 asp = self.width / self.height # projection if self.projection in "it": ysc = sc / asp proj = glm.ortho(-sc, sc, -ysc, ysc, self._near, self._far) elif self.projection == "o": ysc = sc / asp * .75 proj = glm.ortho(-sc, sc, -ysc, ysc, self._near, self._far) elif self.projection == "p": proj = glm.perspectiveFov(1., self.width, self.height, self._near, self._far) proj = glm.translate(proj, glm.vec3(0, 0, -5)) else: # "e" proj = glm.perspectiveFov(2., self.width, self.height, self._near, self._far) self._mat_project = proj # transformation trans = glm.rotate(glm.mat4(1), self._srotation[0], glm.vec3(1, 0, 0)) trans = glm.rotate(trans, self._srotation[1], glm.vec3(0, 1, 0)) trans = glm.rotate(trans, self._srotation[2], glm.vec3(0, 0, 1)) trans = glm.translate( trans, glm.vec3(0, 0, [-3, -2, -4, -1, 0]["oipet".index(self.projection)])) trans = glm.scale(trans, glm.vec3(max(0.01, 1. + self.zoom / 10.))) if self.user_transformation is not None: trans = trans * self.user_transformation self._mat_transform = trans
def set_perspective(self): view_origin = glm.vec3(0, 0, -1) self.view_matrix = glm.lookAt(view_origin, glm.vec3(0, 0, 0), glm.vec3(0, 1, 0)) if self.transformation_mode % 3 is ORTHO_TRANSFORMATION: self.perspective_matrix = glm.ortho(-1, 1, -1, 1, 0.5, 100) elif self.transformation_mode % 3 is FRUSTUM_TRANSFORMATION: self.perspective_matrix = glm.frustum(-1, 1, -1, 1, 0.5, 100.0) elif self.transformation_mode % 3 is PERSPECTIVE_TRANSFORMATION: self.perspective_matrix = glm.perspective(glm.radians(90), 1, 0.5, 100.0) self.transformation_mode += 1
def update_gl(): light_position = glm.vec3(*Lightning._light_pos()) light_projection = glm.ortho(-1.0, 1.0, -1.0, 1.0, 0.5, 5.0) light_view = glm.lookAt(glm.vec3(*Lightning._light_pos()), glm.vec3(0.0, 0.0, 0.0), glm.vec3(0, 1, 0)) Program.use() Program.forward_vec3("light_position", light_position) Program.forward_mat4("light_projection", light_projection) Program.forward_mat4("light_view", light_view) ShadowProgram.use() ShadowProgram.forward_vec3("light_position", light_position) ShadowProgram.forward_mat4("light_projection", light_projection) ShadowProgram.forward_mat4("light_view", light_view)
def paintGL(self): print("A") GL.glClearColor(0, 0, 0, 0) GL.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT) a = BlockModel.load_from_file(workspace, all_models[self.i]) self.i += 1 try: self.renderer.setup_data_for_block_model(a) self.renderer.draw_loaded_model( glm.lookAt(glm.vec3(15, 5, 5), glm.vec3(5, 5, 5), glm.vec3(0, 1, 0)), "gui", glm.ortho(-10, 10, -10, 10, 0.1, 50)) except: return
def set_view_matrices(self): if self.field_of_view == self.FIELD_OF_VIEW_MIN: # Orthogonal view if self.constant_z_near > 0: self.z_near = self.constant_z_near else: self.z_near = self.distance - 3.0 * self.bounding_box.GetMaxExtent( ) if self.constant_z_far > 0: self.z_far = self.constant_z_far else: self.z_far = self.distance + 3.0 * self.bounding_box.GetMaxExtent( ) self.projection_matrix = np.array(glm.ortho( -self.aspect * self.view_ratio, self.aspect * self.view_ratio, -self.view_ratio, self.view_ratio, self.z_near, self.z_far), dtype=np.float32) else: # Perspective View if self.constant_z_near > 0: self.z_near = self.constant_z_near else: self.z_near = max( 0.01 * self.bounding_box.GetMaxExtent(), self.distance - 3.0 * self.bounding_box.GetMaxExtent()) if self.constant_z_far > 0: self.z_far = self.constant_z_far else: self.z_far = max( 0.01 * self.bounding_box.GetMaxExtent(), self.distance + 3.0 * self.bounding_box.GetMaxExtent()) self.projection_matrix = np.array(glm.perspective( glm.radians(self.field_of_view), self.aspect, self.z_near, self.z_far), dtype=np.float32) # scale = np.matrix(((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, self.scaleZ, 0), (0, 0, 0, 1)), np.float32) # rotZ = np.array(((c, s, 0, 0), (-s, c, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)), np.float32) # rotY = np.matrix(((0, 0, 1, 0), (0, 1, 0, 0), (-1, 0, 0, 0), (0, 0, 0, 1)), np.float32) # trans = np.matrix(((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, (self.near - self.far) / 2, 1)), np.float32) self.eye = glm.vec3(self.eye[0], self.eye[1], self.eye[2]) self.look_at = glm.vec3(self.lookat[0], self.lookat[1], self.lookat[2]) self.view_matrix = np.array(glm.lookAt(self.eye, self.look_at, self.up), dtype=np.float32) self.model_matrix = np.array(glm.identity(glm.mat4), dtype=np.float32) # self.model_matrix = np.array(glm.rotate(glm.identity(glm.mat4), glm.radians(-55.0), glm.vec3(1.0, 0.0, 0.0)), dtype=np.float32) mv_matrix = np.matmul(self.model_matrix, self.view_matrix) self.MVP_matrix = np.matmul(mv_matrix, self.projection_matrix)
def _calc_projection_matrix(self): sc = 11 asp = self.width / self.height # projection if self.projection == self.P_ORTHO: ysc = sc/asp * .75 proj = glm.ortho(-sc,sc, -ysc,ysc, self._near, self._far) elif self.projection == self.P_PERSPECTIVE: proj = glm.perspectiveFov(1., self.width, self.height, self._near, self._far) else: raise ValueError(f"unknown projection type '{self.projection}'") self._mat_project = proj
def loop(self): self.setup() while scge.window_opened() and not scge.key('escape'): scge.poll() if self.obj: scge.clear() debug.begin() debug.matrix(glm.ortho(-2, 2, -2, 2, -2, 2)) for v in self.obj.vertices: debug.point(v.x, v.y) debug.end() scge.swap()
def get_mvp(scale): # camera configuration camPos = glm.vec3(0.0, 0.0, -5e7) camTarget = glm.vec3(0.0, 0.0, 0.0) camDir = glm.normalize(camPos - camTarget) camUp = glm.vec3(0.0, 1.0, 0.0) camRight = glm.normalize(glm.cross(camUp, camDir)) camUp = glm.cross(camDir, camRight) # model view projecction matrix view = glm.lookAt(camPos, camTarget, camUp) projection = glm.ortho(-1.0, 1.0, -1.0, 1.0, 0.1, 1e8) model = glm.scale(glm.mat4(1.0), glm.vec3(scale)) mvp = projection * view * model return mvp
def build_shadow_mvp(self, local_player): lookX, lookY, lookZ = normalize(self.directional_light_uniform.value) render_distance = local_player.render_distance #left, right, bottom, top, near, far proj = glm.ortho(-(render_distance+2)*16, (render_distance+2)*16, -(render_distance+2)*16, (render_distance+2)*16, -10, (render_distance*2+2)*16)#last one is render distance pos = (-lookX*(render_distance+1)*16+local_player.pos[0], 64, local_player.pos[2]) view = glm.lookAt(glm.vec3(pos[0], pos[1], pos[2]), glm.vec3(pos[0]+lookX, pos[1]+lookY, pos[2]+lookZ), glm.vec3(0,1,0)) return proj * view
def on_draw(self): glDisable(GL_CULL_FACE) glEnable(GL_DEPTH_TEST) self.clear() if self._projection in "io": proj = glm.ortho(-.5, .5, .5, -.5, -2, 2) else: proj = glm.perspective(30., self.width / self.height, 0.01, 10.) proj = glm.translate(proj, (0, 0, -1)) proj = glm.rotate(proj, self.rotate_x, (1, 0, 0)) proj = glm.rotate(proj, self.rotate_y, (0, 1, 0)) proj = glm.rotate(proj, self.rotate_z, (0, 0, 1)) #print(proj) if not self.texture.is_created(): self.texture.create() self.texture.bind() #self.texture.upload_image("./assets/STEEL.BMP") #self.texture.upload_image("./assets/bluenoise.png") self.texture.upload_image("./assets/blueplate.png") #self.texture.upload([random.randrange(256) for x in range(16*16*3)], 16, input_type=GL_BYTE) if self.fbo: if self.fbo.is_created(): if self.fbo.width != self.width or self.fbo.height != self.height: self.fbo.release() self.fbo = Framebuffer2D(self.width, self.height) if not self.fbo.is_created(): self.fbo.create() self.fbo.bind() self.fbo.clear() self.drawable.shader.set_uniform("u_projection", proj) self.drawable.shader.set_uniform("u_time", time.time() - self.start_time) self.texture.bind() self.drawable.draw() if self.fbo: self.fbo.unbind() self.fbo.color_texture(0).bind() #self.fbo.depth_texture().bind() self.quad.draw(self.width, self.height)
def render_background_image(bg_model, camera_view=None, fx=1920): # glDisable(GL_DEPTH_TEST) bg_shader_program.use() m = glm.mat4(1.0) rescale = 45 / camera.zoom m = glm.scale(m, glm.vec3(rescale)) fov = 2 * glm.atan(1080 / 2 / fx) / 3.14159 * 180 fov *= camera.zoom / 45 projection = camera.get_projection(fov, SCR_WIDTH, SCR_HEIGHT) bg_ortho = glm.ortho(-SCR_WIDTH * 0.5, SCR_WIDTH * 0.5, -SCR_HEIGHT * 0.5, SCR_HEIGHT * 0.5, 0.3, 5000) bg_shader_program.set_matrix("model", glm.value_ptr(m)) # view = camera.get_view_matrix() bg_shader_program.set_matrix("view", glm.value_ptr(bg_view)) bg_shader_program.set_matrix("projection", glm.value_ptr(bg_ortho)) bg_shader_program.un_use() bg_model.draw(bg_shader_program, draw_type=GL_TRIANGLES) # glEnable(GL_DEPTH_TEST) hand_shader_program.use() hand_shader_program.set_matrix("projection", glm.value_ptr(projection)) hand_shader_program.set_matrix("view", glm.value_ptr(camera_view)) # hand_shader_program.set_matrix("view", glm.value_ptr(view)) m = glm.mat4(1.0) m = glm.translate(m, hand_position) m = glm.rotate(m, glm.radians(hand_rotation.x), glm.vec3(1, 0, 0)) m = glm.rotate(m, glm.radians(hand_rotation.y), glm.vec3(0, 1, 0)) m = glm.rotate(m, glm.radians(hand_rotation.z), glm.vec3(0, 0, 1)) 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() glEnable(GL_CULL_FACE) hand_model.draw(hand_shader_program, draw_type=GL_TRIANGLES) glDisable(GL_CULL_FACE)
def on_display(self): glClearColor(0.2, 0.3, 0.3, 1) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glViewport( 0, 0, self.width, self.height ) glEnable( GL_DEPTH_TEST ) glEnable( GL_BLEND ) glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) glUseProgram(self.shaderProgram) proj = glm.ortho(0, self.width, self.height, 0, -1, 1) glUniformMatrix4fv(1, 1, GL_FALSE, glm.value_ptr(proj) ) glUniform3f(2, 0.5, 0.8, 0.2) self.render_text("This is sample text", (50, 50), 1.2, (1, 0)) glUniform3f(2, 0.3, 0.7, 0.9) self.render_text("using freetype-py", (50, 200), 0.9, (1, -0.25)) glutSwapBuffers()
def testOrtho2D(self) : orthNGL= pyngl.ortho(-1.0, 1.0, -1.0,1.0) orthGLM= glm.ortho( -1.0, 1.0, -1.0,1.0) self.assertTrue(self.glmToNGL(orthGLM)==orthNGL)
def _setup(wd): global _wd, _program, _font_program, _vao, _font_vbo, _font_vao, _vbo, _initstate, _matrix, _white, _img, _color _wd = wd _font_vshader = scge.shader('vertex', '''#version 330 core in vec2 coords; in vec2 texcoords; uniform mat4 matrix; out vec2 texcoord; void main() { texcoord = texcoords; gl_Position = matrix * vec4(coords, 0.0, 1.0); } ''') _font_fshader = scge.shader('fragment', '''#version 330 core #extension GL_ARB_texture_rectangle : require in vec2 texcoord; uniform sampler2DRect tex; uniform vec4 color; out vec4 frag; void main() { frag = color; frag.a *= texture2DRect(tex, texcoord).r; if(frag.a == 0.) discard; } ''') _font_program = scge.program() _font_program.attach(_font_vshader) _font_program.attach(_font_fshader) _font_program.attribute(0, 'coords') _font_program.attribute(1, 'texcoords') _font_program.attribute(2, 'ink') _font_program.link() _vshader = scge.shader('vertex', '''#version 330 core in vec2 coords; in vec4 colors; in vec2 texcoords; uniform mat4 matrix; out vec4 color; out vec2 texcoord; void main() { color = colors; texcoord = texcoords; gl_Position = matrix * vec4(coords, 0.0, 1.0); } ''') _fshader = scge.shader('fragment', '''#version 330 core in vec4 color; in vec2 texcoord; uniform sampler2D tex; out vec4 frag; void main() { frag = texture2D(tex, texcoord) * color; if(frag.a == 0.) discard; } ''') _program = scge.program() _program.attach(_vshader) _program.attach(_fshader) _program.attribute(0, 'coords') _program.attribute(1, 'colors') _program.attribute(2, 'texcoords') _program.link() _vbo = scge.vbo(s_f8_4, 'stream draw') _vao = scge.vao() _wd.use(_vao) _vao.enable(0) _vao.enable(1) _vao.enable(2) _vao.attribute(0, _vbo, 'float', 2, 0) _vao.attribute(1, _vbo, 'float', 4, s_f2_4) _vao.attribute(2, _vbo, 'float', 2, s_f6_4) _font_vbo = scge.vbo(s_f6_4, 'stream draw') _font_vao = scge.vao() _wd.use(_font_vao) _font_vao.enable(0) _font_vao.enable(1) _font_vao.attribute(0, _font_vbo, 'float', 2, 0, s_f * 4) _font_vao.attribute(1, _font_vbo, 'float', 4, s_f * 2, s_f * 4) p = scge.pixelcache(glm.ivec2(1, 1)) p.pixel(glm.ivec2(0, 0), glm.vec4(1)) _white = scge.image(p) _program.uniform('tex', _white) _img = _white _matrix = glm.ortho(0, wd.size().x, 0, wd.size().y, -1, 1) _usingDefaultProgram = True color(glm.vec4(1)) _initstate = 1