def createTransformationMatrix(position, rotX=0, rotY=0, rotZ=0, scale=1): if rotX == 0 and rotY == 0 and rotZ == 0: return Matrix44.from_translation(position) * Matrix44.from_scale( [scale, scale, scale]) return Matrix44.from_translation( position) * pyrr.matrix44.create_from_axis_rotation( (rotX, rotY, rotZ), rotY) * Matrix44.from_scale( [scale, scale, scale])
def scale(self, val): if len(val) == 2: self._scale = val + [1.0] else: self._scale = val self._sm = Matrix44.from_scale(self._scale) self.dirty = True
def __init__(self, data): self.name = data.get('name') self.children = data.get('children') self.matrix = data.get('matrix') self.mesh = data.get('mesh') self.camera = data.get('camera') self.translation = data.get('translation') self.rotation = data.get('rotation') self.scale = data.get('scale') if self.matrix: self.matrix = Matrix44(self.matrix) else: self.matrix = Matrix44.identity() if self.translation is not None: self.matrix = self.matrix * Matrix44.from_translation(self.translation) if self.rotation is not None: quat = quaternion.create( x=self.rotation[0], y=self.rotation[1], z=self.rotation[2], w=self.rotation[3], ) self.matrix = self.matrix * Matrix44.from_quaternion(quat).transpose() if self.scale is not None: self.matrix = self.matrix * Matrix44.from_scale(self.scale)
def select_object(self, camera, x, y): red = green = blue = 8 def make_mask(bits): return 0xFFFFFFFF >> (32 - bits) red_mask = make_mask(red) << (green + blue) green_mask = make_mask(green) << blue blue_mask = make_mask(blue) red_shift = green + blue green_shift = blue for i, obj in enumerate(self.objects): r = ((i & red_mask) >> red_shift) / 255.0 g = ((i & green_mask) >> green_shift) / 255.0 b = (i & blue_mask) / 255.0 camera.push_matrix() camera.matrix *= Matrix44.from_translation(obj.position) * Matrix44.from_scale(obj.scale) obj.render_for_selection_hit(camera, r, g, b) camera.pop_matrix() data = struct.unpack('b'*3, glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE)) index = (data[0] << red_shift) | (data[1] << green_shift) | data[2] if self.objects and 0 <= index < len(self.objects): return self.objects[index] return None
def render(self, time, frame_time): self.ctx.clear(0.2, 0.2, 0.2) self.ctx.enable(moderngl.DEPTH_TEST) self.fps = 1 / frame_time self.control() proj = Matrix44.perspective_projection(45.0, self.aspect_ratio, 0.1, 1000.0) lookat = Matrix44.look_at( (self.movX, self.movY, self.movZ), (200.0, 200.0, 0.0), (0.0, 0.0, 1.0), ) self.light.value = (100, 0, 200) self.texture.use(0) self.mvp_map.write((proj * lookat).astype('f4')) self.vao_map.render(moderngl.TRIANGLE_FAN) model_rot = Matrix44.from_z_rotation( 3.14 / 4) * Matrix44.from_x_rotation(-3.14 / 2) for x in range(int(self.positions.size / 3)): size = 1 + self.production[x] * (2.5 - 1) model_size = Matrix44.from_scale(np.array([size, size, size])) self.gradient.value = self.production[x] model = Matrix44.from_translation(np.array( self.positions[x])) * model_rot * model_size self.mvp.write((proj * lookat * model).astype('f4')) self.vao.render() self.render_ui()
def test_oo_examples(self): from pyrr import Quaternion, Matrix44, Vector3 import numpy as np point = Vector3([1.,2.,3.]) orientation = Quaternion() translation = Vector3() scale = Vector3([1.,1.,1.]) # translate along X by 1 translation += [1.0, 0.0, 0.0] # rotate about Y by pi/2 rotation = Quaternion.from_y_rotation(np.pi / 2.0) orientation = rotation * orientation # create a matrix # start our matrix off using the scale matrix = Matrix44.from_scale(scale) # apply our orientation # we can multiply matricies and quaternions directly! matrix = matrix * orientation # apply our translation translation = Matrix44.from_translation(translation) matrix = matrix * translation # transform our point by the matrix # vectors are transformable by matrices and quaternions directly point = matrix * point
def render(self, time, frame_time): if SimpleRenderer.callback and frame_time > 0: SimpleRenderer.callback(frame_time) # print(time, frame_time) self.camera.update() self.ctx.clear(0.1, 0.1, 0.1) self.ctx.enable(moderngl.DEPTH_TEST) # print('---------') # print(self.camera.elevation, self.camera.azimuth) # print(self.camera.mat_lookat*Vector4([0,0,0,1])) # grid self.mvp.write((self.camera.mat_projection * self.camera.mat_lookat.inverse).astype('f4')) self.grid_vao.render(moderngl.LINES) # objects for obj in SimpleRenderer.objects: # model matrix scale = Matrix44.from_scale([obj.radius, obj.radius, obj.radius]) rotation = Matrix44.from_z_rotation( -np.arctan2(obj.vel.y, obj.vel.x)) translation = Matrix44.from_translation([obj.pos.x, obj.pos.y, 0]) model = translation * rotation * scale # render object self.mvp.write( (self.camera.mat_projection * self.camera.mat_lookat.inverse * model).astype('f4')) self.cyl_vao.render(moderngl.TRIANGLES)
def render(self): if self.current_pointcloud < len(self.pointclouds): self.handle_mouse() self.handle_keys() self.ctx.viewport = self.wnd.viewport self.ctx.clear(0.0, 0.0, 0.0) self.ctx.enable(mgl.BLEND) vertices = np.load(file=self.pointclouds[self.current_pointcloud]) self.vbo.write(vertices.astype('f4').tobytes()) model = Matrix44.from_scale((self.zoom, self.zoom, self.zoom)) model *= Matrix44.from_x_rotation(-self.theta[1]) model *= Matrix44.from_y_rotation(-self.theta[0]) view = Matrix44.look_at((0.0, 0.0, 1.0), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0)) projection = Matrix44.perspective_projection( 45.0, self.wnd.ratio, 0.1, 100.0) self.mvp.write((projection * view * model).astype('f4').tobytes()) self.vao.render(mode=mgl.POINTS) self.sleep_to_target_fps(60) self.current_pointcloud += 1 else: if self.out_dir is None: self.current_pointcloud = 0 else: QtCore.QCoreApplication.instance().quit()
def test_oo_examples(self): from pyrr import Quaternion, Matrix44, Vector3 import numpy as np point = Vector3([1., 2., 3.]) orientation = Quaternion() translation = Vector3() scale = Vector3([1., 1., 1.]) # translate along X by 1 translation += [1.0, 0.0, 0.0] # rotate about Y by pi/2 rotation = Quaternion.from_y_rotation(np.pi / 2.0) orientation = rotation * orientation # create a matrix # start our matrix off using the scale matrix = Matrix44.from_scale(scale) # apply our orientation # we can multiply matricies and quaternions directly! matrix = matrix * orientation # apply our translation translation = Matrix44.from_translation(translation) matrix = matrix * translation # transform our point by the matrix # vectors are transformable by matrices and quaternions directly point = matrix * point
def on_render(self, gl_area, gl_context): glClearColor(0.0, 0.0, 0.0, 0.0) # Set the background colour for the window -> Black glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) # Clear the window background colour to black by resetting the COLOR_BUFFER and clear the DEPTH_BUFFER eye = (0.0, 5, 18.0) # Eye coordinates (location of the camera) target = (0.0, 7.0, 0.0 ) # Target coordinates (where the camera is looking) up = (0.0, 1.0, 0.0) # A vector representing the 'up' direction. view_matrix = Matrix44.look_at(eye, target, up) # Calculate the view matrix glUniformMatrix4fv(self.__location_viewMatrix, 1, GL_FALSE, view_matrix) model_matrix = Matrix44.from_translation( [0.0, 0.0, 0.0]) * pyrr.matrix44.create_from_axis_rotation( (0.0, 1.0, 0.0), self.application_clock) * Matrix44.from_scale( [1.0, 1.0, 1.0]) glUniformMatrix4fv( self.__location_modelMatrix, 1, GL_FALSE, model_matrix ) # Update the value of the ModelViewPerspective matrix attribute variable in the vertex buffer glBindVertexArray(self.chibi[0]) glEnableVertexAttribArray(0) glEnableVertexAttribArray(1) glBindTexture(GL_TEXTURE_2D, self.chibi[2]) glDrawArrays(GL_TRIANGLES, 0, self.chibi[1]) self.queue_draw() # Schedules a redraw for Gtk.GLArea
def fit_inside(self, box: Box, anchor: Vector3): scale = np.amin(box.size() / self.bounding_box().size()) extra = box.size() - (self.bounding_box().size() * scale) matrix = Matrix44.from_translation(-self.bounding_box().min) matrix = Matrix44.from_scale([scale, scale, scale]) * matrix matrix = Matrix44.from_translation(box.min + (extra * anchor)) * matrix self.transform(matrix)
def __init__(self, data): self.name = data.get("name") self.children = data.get("children") self.matrix = data.get("matrix") self.mesh = data.get("mesh") self.camera = data.get("camera") self.translation = data.get("translation") self.rotation = data.get("rotation") self.scale = data.get("scale") if self.matrix: self.matrix = Matrix44(self.matrix) else: self.matrix = Matrix44.identity() if self.translation is not None: self.matrix = self.matrix * Matrix44.from_translation( self.translation) if self.rotation is not None: quat = quaternion.create( x=self.rotation[0], y=self.rotation[1], z=self.rotation[2], w=self.rotation[3], ) self.matrix = self.matrix * Matrix44.from_quaternion( quat).transpose() if self.scale is not None: self.matrix = self.matrix * Matrix44.from_scale(self.scale)
def load_mesh(self, mesh): self.release_mesh() self.vbo = self.ctx.buffer(mesh['gldat'].astype('f4').tobytes()) self.ibo = self.ctx.buffer(mesh['glindex'].astype('i4').tobytes()) vao_content = [(self.vbo, '3f 3f', 'in_vert', 'in_norm')] self.vao = self.ctx.vertex_array(self.prog, vao_content, self.ibo) self.model = Matrix44.from_scale((0.1, 0.1, 0.1))
def buildTransMatrix(pos=[0, 0, 0], rot=[0, 0, 0], scale=[1, 1, 1]): trans = Matrix44.from_translation(pos) rotX = Matrix44.from_x_rotation(np.radians(rot[0])) rotY = Matrix44.from_y_rotation(np.radians(rot[1])) rotZ = Matrix44.from_z_rotation(np.radians(rot[2])) scale = Matrix44.from_scale(scale) tMatrix = trans * scale * rotX * rotY * rotZ return tMatrix
def render(self, t): gl.glEnable(gl.GL_DEPTH_TEST) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) M = np.eye(4, dtype=np.float32) M = M * Matrix44.from_scale((.5, .5, .5)) M = M * Matrix44.from_translation((0, 0, -2)) M = M * Matrix44.from_x_rotation(0) M = M * Matrix44.from_scale((.4, .4, .4)) M = M * Matrix44.from_translation((0, 0, -50)) projection = pyrr.matrix44.create_perspective_projection( 3, 1, 0.001, 10000) self.tree.setProjection(projection) self.tree.setModelView(M) self.tree.render() gl.glDisable(gl.GL_DEPTH_TEST)
def applyTransforms(vector, projectionMat): r = time.clock() + vector[0] * 10 # rotation offset based on vector's 1st component rotX = Matrix44.from_x_rotation(r) rotY = Matrix44.from_y_rotation(r) rotZ = Matrix44.from_z_rotation(r) trans = Matrix44.from_translation(vector) * Matrix44.from_translation([1,1,-5]) # move view back by -5 scale = Matrix44.from_scale([.5,.5,.5]) # uniformly scale by 0.5 tMatrix = projectionMat * trans * scale * rotX * rotY * rotZ prog['transform'].write(tMatrix.astype('f4').tobytes())
def project_and_render(view, zoom, resolution): fovy, aspect, near, far = 60.0, resolution[0] / resolution[1], 0.001, 1000 projection = Matrix44.perspective_projection(fovy, aspect, near, far) view = Matrix44(view) scale = Matrix44.from_scale([zoom, zoom, zoom]) MVP = projection * view * scale MVP = cuda.to_device(MVP) apply_projection[bpg, maxThreadsPerBlock](MVP, embedding, result) return render(result, resolution)
def scatterCubes(vector, projectionMat): view = window.getViewMatrix() r = vector[0] * 10.0 # cube rotation offset based on vector's 1st component rotX = Matrix44.from_x_rotation(r*time.clock()/10.0) # rotate cubes over time rotY = Matrix44.from_y_rotation(r) rotZ = Matrix44.from_z_rotation(r) trans = Matrix44.from_translation(vector) * Matrix44.from_translation([1.0,1.0,-5.0]) scale = Matrix44.from_scale([.5,.5,.5]) # uniformly scale by 0.5 tMatrix = projectionMat * view * trans * scale * rotX * rotY * rotZ prog['transform'].write(tMatrix.astype('f4').tobytes())
def draw(self, prog, data, camera): frustrum = camera.frustrum() for date in self.__date_id: x = self.date_pos(date) if frustrum[0] <= x <= frustrum[0] + frustrum[2]: prog['color'].value = (0.0, 0.0, 1.0, 0.2) prog['m_model'].write( Matrix44.from_translation( (x, 0, 0), dtype='f4') * Matrix44.from_scale( (self.day_width, 20, 1), dtype='f4')) self.model.render(prog)
def resize (self: 'GLQuadRenderer', width: int, height: int) -> None: super ().resize (width, height) if not self.__loaded: return y = self.__image_height / self.__image_width * \ self.gl_widget.width / self.gl_widget.height x = self.__image_width / self.__image_height * \ self.gl_widget.height / self.gl_widget.width if self.gl_widget.height * self.__image_width / self.__image_height > self.gl_widget.width: self.world = Matrix44.from_scale ([1, y, 1]) * \ Matrix44.from_translation ([0, (1 - y) / 2, 0]) else: self.world = Matrix44.from_scale ([x, 1, 1]) * \ Matrix44.from_translation ([(1 - x) / 2, 0, 0]) GL.glUniformMatrix4fv (self.__uniform_world, 1, False, self.world)
def render(self, camera, objects=None): if not objects: objects = self.objects for obj in objects: camera.push_matrix() camera.matrix *= Matrix44.from_translation(obj.position) * Matrix44.from_scale(obj.scale) obj.render(camera) if self.render_bounding_boxes: obj.render_bounding_box(self.bounding_box_color, camera) camera.pop_matrix()
def resize(self: 'GLQuadRenderer', width: int, height: int) -> None: super().resize(width, height) if not self.__loaded: return y = self.__image_height / self.__image_width * \ self.gl_widget.width / self.gl_widget.height x = self.__image_width / self.__image_height * \ self.gl_widget.height / self.gl_widget.width if self.gl_widget.height * self.__image_width / self.__image_height > self.gl_widget.width: self.world = Matrix44.from_scale ([1, y, 1]) * \ Matrix44.from_translation ([0, (1 - y) / 2, 0]) else: self.world = Matrix44.from_scale ([x, 1, 1]) * \ Matrix44.from_translation ([(1 - x) / 2, 0, 0]) GL.glUniformMatrix4fv(self.__uniform_world, 1, False, self.world)
def render(): global bytearray, player global args videoAspect = screenAspect = args.columns / args.rows frame, val = player.get_frame() if frame: img, t = frame data = img.to_bytearray() size = img.get_size() videoAspect = size[0] / size[1] bytearray.setYUV420(data, size[0], size[1]) time.sleep(val) M = np.eye(4, dtype=np.float32) if not args.stretch: if args.fit: if screenAspect > videoAspect: # Pillar box M = M * Matrix44.from_scale( (1, screenAspect / videoAspect, 1, 1)) else: # Letter box M = M * Matrix44.from_scale((videoAspect / screenAspect, 1, 1)) else: if screenAspect > videoAspect: # Pillar box M = M * Matrix44.from_scale((videoAspect / screenAspect, 1, 1)) else: # Letter box M = M * Matrix44.from_scale((1, screenAspect / videoAspect, 1)) bytearray.setProjection(M) bytearray.render()
def paintGL(self): render_start = time.perf_counter() w, h = self.width() * self.devicePixelRatio(), self.height( ) * self.devicePixelRatio() self.ctx.enable(ModernGL.DEPTH_TEST) self.ctx.viewport = (0, 0, w, h) self.ctx.clear(0.9, 0.9, 0.9) # render Entry/Exit-Point Texture into FBO if self.color_texture is None or self.depth_texture is None: # needs to be reset on window resize self.color_texture = self.ctx.texture((w, h), 4, None) self.depth_texture = self.ctx.depth_texture((w, h), None) self.fbo = self.ctx.framebuffer(self.color_texture, self.depth_texture) # set up and draw to the offscreen buffer for the exit points self.fbo.clear() self.fbo.use() self.setup_camera(self.prog_eep) model_mat = Matrix44.from_scale(Vector3([1, 1, 1])) self.prog_eep.uniforms['ModelMat'].write( model_mat.astype('float32').tobytes()) self.draw_box_eep() if hasattr(ModernGL, "default_framebuffer"): ModernGL.default_framebuffer.use() else: # stop using the fbo (@Todo: ModernGL needs a way to handle this) glBindFramebuffer(GL_FRAMEBUFFER, 0) # set up to bind the color texture to the third texture location self.color_texture.use(2) # clear the buffer and set up the uniforms self.ctx.clear(0.9, 0.9, 0.9) self.unf_screensize.value = (w, h) self.unf_stepsize.value = self.sampling_rate / max( self.volume_size) # step size is relative to the number of voxels self.unf_volumetex.value = 0 # the volume texture is bound to the first location self.unf_transferfunc.value = 1 # the transfer function texture is bound to the second location self.unf_exitpoints.value = 2 # the exit points texture is bound to the third location self.setup_camera(self.prog_rc) # do a front face pass to perform the actual raycasting self.draw_box_rc() self.ctx.finish() self.update()
def draw(self, prog, data, camera, timeline): frustrum = camera.frustrum() for _, row in data.iterrows(): date = row['Date'] x = timeline.date_pos(date) if frustrum[0] <= x <= frustrum[0] + frustrum[2]: prog['color'].value = (1.0, 1.0, 0.0, 1.0) prog['m_model'].write( Matrix44.from_translation( (x, camera.frustrum()[1], 0), dtype='f4') * Matrix44.from_scale( (timeline.day_width, 1, 1), dtype='f4')) self.model.render(prog)
def init(wid, hig): """Initialises the display.""" global modelmtx, chaem GL.glClearColor(0.0, 0.2, 0.15, 0.0) GL.glClearDepth(1.0) GL.glDepthFunc(GL.GL_LESS) GL.glEnable(GL.GL_DEPTH_TEST) GL.glShadeModel(GL.GL_SMOOTH) shades() rematr(wid, hig) modelmtx = matrix.from_scale([2, 2, 2]) * matrix.from_translation([0, 0, -.5]) chaem = Raimv((), deltam) chaem.rotspe = 2*pi GL.glUniformMatrix4fv(GL.glGetUniformLocation(shaderp, 'viewmatrix'), 1, False, chaem.lookat()) GL.glUniformMatrix4fv(GL.glGetUniformLocation(shaderp, 'modelmatrix'), 1, False, modelmtx)
def update(dt): proj = Matrix44.perspective_projection(50, width / height, 0.1, 1000.0) rotX = Matrix44.from_x_rotation(0) rotY = Matrix44.from_y_rotation(time.clock() * 1.5) rotZ = Matrix44.from_z_rotation( 180 * np.pi / 180) # rotate 180 degrees. Convert degrees to radians trans = Matrix44.from_translation( [np.sin(time.clock()) / 4, np.sin(time.clock()) / 4, -1.3]) # bounce diagonally from corner to corner, move back by -1.3 scale = Matrix44.from_scale([.5, .5, .5]) # uniformly scale by 0.5 tMatrix = proj * trans * scale * rotX * rotY * rotZ prog['transform'].write(tMatrix.astype('f4').tobytes()) ctx.clear(.1, .1, .1) vao.render()
def getModelMatrix(self): scale = mat4.from_scale([0.2, 0.2, 0.2]) roty = mat4.from_y_rotation(-self.hAngle) vdiff = self.vAngle - self.oldvAngle rotx = mat4.from_x_rotation(self.getxRot(vdiff)) zdiff = self.hAngle - self.oldhAngle rotz = mat4.from_z_rotation(-self.getzRot(zdiff)) trans = mat4.from_translation(self.position, dtype='f') self.oldhAngle = self.hAngle self.oldvAngle = self.vAngle return scale * rotz * rotx * roty * trans
def init(): """Initialises the display.""" global projectmatrix, modelmatrix, chaem GL.glClearColor(0.0, 0.2, 0.15, 0.0) GL.glClearDepth(1.0) GL.glDepthFunc(GL.GL_LESS) GL.glEnable(GL.GL_DEPTH_TEST) GL.glShadeModel(GL.GL_SMOOTH) shades() chaem = Fremv((), deltam) GL.glUniform3f(GL.glGetUniformLocation(shaderp, 'lightColour'), *[1.0, 1.0, 1.0]) modelmatrix = matrix.from_scale([2, 2, 2]) * matrix.from_translation([0, 0, -1]) wid, hig = window.get_size() hig = hig or 1 projectmatrix = matrix.perspective_projection(whel, wid/hig, 0.01, 100) rematr()
def draw(): """Put the main drawing code in here.""" global luxp chaem.mochae(timedelta) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) GL.glEnable(GL.GL_TEXTURE_2D) GL.glUseProgram(shaderp) luxp = [sin(lasttime) * 5, cos(lasttime) * 5, 0.0] GL.glUniform3f(GL.glGetUniformLocation(shaderp, 'lightPos'), *luxp) GL.glUniform3f(GL.glGetUniformLocation(shaderp, 'viewpos'), *chaem.pos) rematr() architincture.draw() mmoodd = modelmatrix * matrix.from_scale([0.2, 0.2, 0.2]) * matrix.from_translation(luxp) * \ matrix.from_eulers([lasttime, lasttime, lasttime]) lux.draw(rematr, mmoodd)
def draw(self, c: OrthographicCamera): if self.dragging: sp = c.project(self.start[0], self.start[1]) drag_start = c.ndc_from_screen_coordinates(sp) sp = c.project(self.end[0], self.end[1]) drag_end = c.ndc_from_screen_coordinates(sp) self.program['color'].value = (0.0, 1.0, 0.0, 0.3) if self.start[1] > self.end[1]: self.program['color'].value = (1.0, 0.0, 0.0, 0.3) self.program['transform'].write( Matrix44.from_translation( (drag_start[0], drag_start[1], 0), dtype='f4') * Matrix44.from_scale((drag_end[0] - drag_start[0], drag_end[1] - drag_start[1], 1), dtype='f4')) self.model.render(self.program)
def __get_transformation_matrix(self): ''' Creates and returns the complete transformation matrix column-major format! I.e.: ( ROTATION x_translation) ( MATRIX y_translation) ( HERE z_translation) (0 0 0 1 ) ''' # ensure column-major format -> numpy thinks matrices have their # origin in the top left corner ... return ((Matrix44.from_scale(self.__scale) * self.__rotation_quaternion).T * Matrix44.from_translation(self.__translation))
def render(self, time, frametime): # Render background self.ctx.wireframe = False if not self.with_blending: self.ctx.enable_only(moderngl.NOTHING) self.quad_fs.render(self.prog_background) # Handle blend mode toggle if self.with_blending: self.ctx.enable_only(moderngl.BLEND) self.ctx.blend_func = moderngl.ONE, moderngl.ONE else: self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE) # Render tetrahedral mesh translate = Matrix44.from_translation((0.0, 2.5, -15.0), dtype='f4') rotate = Matrix44.from_eulers((np.radians(180), 0, 0), dtype='f4') scale = Matrix44.from_scale((400, 400, 400), dtype='f4') mat = self.camera.matrix * translate * rotate * scale # All render calls inside this context are timed with self.query: self.alive_texture.use(location=0) self.prog_gen_tetra['alive_texture'].value = 0 self.prog_gen_tetra['threshold'].value = self.threshold self.prog_gen_tetra['color'].value = self.mesh_color self.prog_gen_tetra['m_cam'].write(mat) self.prog_gen_tetra['m_proj'].write(self.camera.projection.matrix) self.geometry.render(self.prog_gen_tetra, mode=moderngl.LINES_ADJACENCY) # Render lines self.ctx.wireframe = True self.alive_texture.use(location=0) self.prog_gen_tetra_lines['alive_texture'].value = 0 self.prog_gen_tetra_lines['threshold'].value = self.threshold self.prog_gen_tetra_lines['color'].value = self.line_color self.prog_gen_tetra_lines['m_cam'].write(mat) self.prog_gen_tetra_lines['m_proj'].write( self.camera.projection.matrix) self.geometry.render(self.prog_gen_tetra_lines, mode=moderngl.LINES_ADJACENCY) self.total_elapsed = self.query.elapsed
def __updateModelViewMatrix(self, position, rotation, scale, viewMatrix, vboData): translation = Matrix44.from_translation(position) translation.m11 = viewMatrix.m11 translation.m12 = viewMatrix.m21 translation.m13 = viewMatrix.m31 translation.m21 = viewMatrix.m12 translation.m22 = viewMatrix.m22 translation.m23 = viewMatrix.m32 translation.m31 = viewMatrix.m13 translation.m32 = viewMatrix.m23 translation.m33 = viewMatrix.m33 rotation = matrix44.create_from_axis_rotation((0.0, 0.0, 1.0), radians(rotation)) scale = Matrix44.from_scale([scale, scale, scale]) modelMatrix = translation * rotation * scale modelViewMatrix = viewMatrix * modelMatrix self.__storeMatrixData(modelViewMatrix, vboData)
def update_transform_matrix(self): scale_matrix = Matrix44.from_scale(self.scale) translation_matrix = Matrix44.from_translation(self.location) # TODO: For some reason the qx, qy, qz part of the quaternion must be flipped # Need to understand why and fix it # The change need to be made together with the coordinate conversion in NDDS # rotation_matrix = Matrix44.from_quaternion(self.quaternion) qx, qy, qz, qw = self.quaternion test_quaternion = Quaternion([-qx, -qy, -qz, qw]) rotation_matrix = Matrix44.from_quaternion(test_quaternion) # print('update_transform_matrix: rotation_matrix = {}'.format(rotation_matrix)) relative_matrix = (translation_matrix * scale_matrix * rotation_matrix) # self.transform_matrix = relative_matrix * self.initial_matrix self.transform_matrix = relative_matrix
def Modify(object_id, pos, rot, scale): translation = Vector3() translation += pos scale = Vector3(scale) matrix = Matrix44.identity() matrix = matrix * Matrix44.from_translation(translation) matrix = matrix * Matrix44.from_scale(scale) i = 0 for point in objects[object_id].vertices: v = Vector3(point) v = matrix * v point = [v.x, v.y, v.z] objects[object_id].vertices[i] = point i += 1