def create_translations(): InitShader.model_loc = glGetUniformLocation(InitShader.shader, b"model") # translation for the monkey, flattening on the second line because the rotation InitShader.monkey_model = matrix44.create_from_translation(Vector3([-2.0, 0.0, -3.0])) # translation for the cube InitShader.cube_model = matrix44.create_from_translation(Vector3([2.0, 0.0, -3.0])).flatten().astype("float32") InitShader.c_cube_model = numpy.ctypeslib.as_ctypes(InitShader.cube_model)
def __init__(self): mesh = ObjLoader() mesh.load_model("../models/monkey.obj") num_verts = len(mesh.model_vertices) // 3 self.batch = pyglet.graphics.Batch() self.verts = self.batch.add(num_verts, GL_TRIANGLES, None, ('v3f', mesh.model_vertices), ('t2f', mesh.model_textures)) shader = ShaderLoader.compile_shader("shaders/video_13_vert.glsl", "shaders/video_13_frag.glsl") glUseProgram(shader) # vertices glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, self.verts.vertices) glEnableVertexAttribArray(0) # textures glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, self.verts.tex_coords) glEnableVertexAttribArray(1) projection = matrix44.create_perspective_projection_matrix( 45.0, 1280 / 720, 0.1, 100.0).flatten().astype("float32") view = matrix44.create_from_translation(Vector3( [0.0, 0.0, -2.0])).flatten().astype("float32") self.translation = matrix44.create_from_translation( Vector3([0.0, 0.0, -2.0])) c_projection = numpy.ctypeslib.as_ctypes(projection) c_view = numpy.ctypeslib.as_ctypes(view) view_loc = glGetUniformLocation(shader, b"view") proj_loc = glGetUniformLocation(shader, b"projection") self.model_loc = glGetUniformLocation(shader, b"model") glUniformMatrix4fv(view_loc, 1, GL_FALSE, c_view) glUniformMatrix4fv(proj_loc, 1, GL_FALSE, c_projection) # region texture settings and loading texture = GLuint(0) glGenTextures(1, texture) glBindTexture(GL_TEXTURE_2D, texture) # set the texture wrapping glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set the texture filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) # endregion image = pyglet.image.load('../models/monkey.jpg') image_data = image.get_data('RGB', image.pitch) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data)
def on_render(self, gl_area, gl_context): self.default_ID = glGetIntegerv( GL_FRAMEBUFFER_BINDING ) # GLArea does not seem to use FBO 0 as the default. glClearColor(0.2, 0.25, 0.27, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) rot_y = pyrr.Matrix44.from_y_rotation(self.application_clock * 2) # draw to the default frame buffer glBindVertexArray(self.cube_vao) glBindTexture(GL_TEXTURE_2D, self.crate_texture) for i in range(len(self.cube_positions)): model = matrix44.create_from_translation(self.cube_positions[i]) if i == 0: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, rot_y * model) elif i == 1: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, model) else: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, model) glDrawElements(GL_TRIANGLES, len(self.cube_indices), GL_UNSIGNED_INT, None) # draw to the custom frame buffer glBindFramebuffer(GL_FRAMEBUFFER, self.FBO) glClearColor(0.0, 0.0, 0.0, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) for i in range(len(self.cube_positions)): model = matrix44.create_from_translation(self.cube_positions[i]) if i == 0: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, rot_y * model) elif i == 1: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, model) else: glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, model) glDrawElements(GL_TRIANGLES, len(self.cube_indices), GL_UNSIGNED_INT, None) glBindFramebuffer(GL_FRAMEBUFFER, self.default_ID) glBindVertexArray(0) # draw the plane glBindVertexArray(self.plane_vao) glBindTexture(GL_TEXTURE_2D, self.plane_texture) glUniformMatrix4fv(self.model_loc, 1, GL_FALSE, self.plane_position) glDrawElements(GL_TRIANGLES, len(self.plane_indices), GL_UNSIGNED_INT, None) glBindVertexArray(0) self.queue_draw() # Schedules a redraw for Gtk.GLArea
def matrix( self ): """A matrix representing the transform's translation, orientation and scale. The is an @property decorated method which allows retrieval and assignment of the scale value. """ if self._matrix == None: # matrix transformations must be done in order # scaling # rotation # translation # apply our scale self._matrix = matrix44.create_from_scale( self.scale ) # apply our quaternion self._matrix = matrix44.multiply( self._matrix, matrix44.create_from_quaternion( self.orientation ) ) # apply our translation # we MUST do this after the orientation self._matrix = matrix44.multiply( self._matrix, matrix44.create_from_translation( self.translation ) ) return self._matrix
def create_perspective_projection(): view = matrix44.create_from_translation(Vector3([0.0, 0.0, -2.0])) projection = matrix44.create_perspective_projection_matrix(45.0, 1280.0 / 720.0, 0.1, 100.0) vp = matrix44.multiply(view, projection).flatten().astype("float32") c_vp = numpy.ctypeslib.as_ctypes(vp) vp_loc = glGetUniformLocation(InitShader.shader, b"vp") glUniformMatrix4fv(vp_loc, 1, GL_FALSE, c_vp)
def __init__(self): mesh = ObjLoader() mesh.load_model("../models/cube.obj") num_verts = len(mesh.model_vertices) // 3 group = pyglet.graphics.Group() group.set_state = self.state self.verts = main_batch.add(num_verts, GL_TRIANGLES, group, ('v3f', mesh.model_vertices), ('t2f', mesh.model_textures)) self.model = matrix44.create_from_translation(Vector3( [2.0, 0.0, -4.0])).flatten().astype("float32") self.c_model = numpy.ctypeslib.as_ctypes(self.model) # region texture settings self.texture = GLuint(0) glGenTextures(1, self.texture) glBindTexture(GL_TEXTURE_2D, self.texture) # set the texture wrapping glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) # set the texture filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) image = pyglet.image.load('../models/cube.jpg') image_data = image.get_data('RGB', image.pitch) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, image_data)
def InitGL(self): # triangle = np.array([-0.5, -0.5, 0.0, 1.0, 0.0, 0.0, # 0.5, -0.5, 0.0, 0.0, 1.0, 0.0, # 0.0, 0.5, 0.0, 0.0, 0.0, 1.0], dtype=np.float32) self.mesh = Cube() shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(self.vertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(self.fragment_shader, GL_FRAGMENT_SHADER)) glClearColor(0.0, 0.0, 0.0, 0.0) view = matrix44.create_from_translation(Vector3([0.0, 0.0, -2.0])) projection = matrix44.create_perspective_projection_matrix(45.0, self.aspect_ratio, 0.1, 100.0) vp = matrix44.multiply(view, projection) glUseProgram(shader) glEnable(GL_DEPTH_TEST) vp_loc = glGetUniformLocation(shader, "vp") glUniformMatrix4fv(vp_loc, 1, GL_FALSE, vp) self.model_location = glGetUniformLocation(shader, "model") self.Refresh()
def test_procedural_examples(self): from pyrr import quaternion, matrix44, vector3 import numpy as np point = vector3.create(1.,2.,3.) orientation = quaternion.create() translation = vector3.create() scale = vector3.create(1,1,1) # translate along X by 1 translation += [1.0, 0.0, 0.0] # rotate about Y by pi/2 rotation = quaternion.create_from_y_rotation(np.pi / 2.0) orientation = quaternion.cross(rotation, orientation) # create a matrix # start our matrix off using the scale matrix = matrix44.create_from_scale(scale) # apply our orientation orientation = matrix44.create_from_quaternion(orientation) matrix = matrix44.multiply(matrix, orientation) # apply our translation translation_matrix = matrix44.create_from_translation(translation) matrix = matrix44.multiply(matrix, translation_matrix) # transform our point by the matrix point = matrix44.apply_to_vector(matrix, point)
def matrix(self): """A matrix representing the transform's translation, orientation and scale. The is an @property decorated method which allows retrieval and assignment of the scale value. """ if self._matrix == None: # matrix transformations must be done in order # scaling # rotation # translation # apply our scale self._matrix = matrix44.create_from_scale(self.scale) # apply our quaternion self._matrix = matrix44.multiply( self._matrix, matrix44.create_from_quaternion(self.orientation)) # apply our translation # we MUST do this after the orientation self._matrix = matrix44.multiply( self._matrix, matrix44.create_from_translation(self.translation)) return self._matrix
def test_decompose(self): # define expectations expected_scale = vector3.create(*[1, 1, 2], dtype='f4') expected_rotation = quaternion.create_from_y_rotation(np.pi, dtype='f4') expected_translation = vector3.create(*[10, 0, -5], dtype='f4') expected_model = np.array([ [-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -2, 0], [10, 0, -5, 1], ], dtype='f4') # compose matrix using Pyrr s = matrix44.create_from_scale(expected_scale, dtype='f4') r = matrix44.create_from_quaternion(expected_rotation, dtype='f4') t = matrix44.create_from_translation(expected_translation, dtype='f4') model = s.dot(r).dot(t) np.testing.assert_almost_equal(model, expected_model) self.assertTrue(model.dtype == expected_model.dtype) # decompose matrix scale, rotation, translation = matrix44.decompose(model) np.testing.assert_almost_equal(scale, expected_scale) self.assertTrue(scale.dtype == expected_scale.dtype) np.testing.assert_almost_equal(rotation, expected_rotation) self.assertTrue(rotation.dtype == expected_rotation.dtype) np.testing.assert_almost_equal(translation, expected_translation) self.assertTrue(translation.dtype == expected_translation.dtype)
def render(self, time, frame_time): pyglet.clock.tick() time = self.timer.get_time() # Prepare camera matrix translation = matrix44.create_from_translation(( self.track_cam_x.time_value(time), self.track_cam_y.time_value(time), self.track_cam_z.time_value(time), ), dtype='f4') rotation = matrix44.create_from_eulers(( math.radians(self.track_cam_rot_x.time_value(time)), math.radians(self.track_cam_rot_tilt.time_value(time)), math.radians(self.track_cam_rot_z.time_value(time)), ), dtype='f4') projection = self.camera.projection.matrix modelview = matrix44.multiply(matrix44.multiply(translation, rotation), self.camera.matrix) # Render active effects self.offscreen.use() self.offscreen.clear() for effect in self.router.gen_active_effects(time): effect.render(time=time, projection=projection, modelview=modelview) # # Postprocessing self.wnd.use() self.offscreen.color_attachments[0].use() self.screen_quad_prog['fade'].value = self.track_fade.time_value(time) self.screen_quad.render(self.screen_quad_prog)
def __init__(self, data): 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 is None: self.matrix = matrix44.create_identity() if self.translation is not None: self.matrix = matrix44.create_from_translation(self.translation) if self.rotation is not None: quat = quaternion.create(self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3]) mat = matrix44.create_from_quaternion(quat) self.matrix = matrix44.multiply(mat, self.matrix) if self.scale is not None: self.matrix = matrix44.multiply( matrix44.create_from_scale(self.scale), self.matrix)
def randomize_geometry(self): indices = list(range(len(self.geometry["objs"]))) random.shuffle(indices) taken_positions = [] # Place objects randomly in the scene while avoiding intersections for l in range(len(self.geometry["objs"])): index = indices[l] visibility = random.random() > 0.5 position = np.array([0, 0]) if visibility: occupied = False k = 0 while k < 5: occupied = False position[0] = random.random() * 4.5 - 2.25 position[1] = random.random() * 4.5 - 2.25 # See if there are any intersections for taken in taken_positions: length = np.linalg.norm(position - taken) if length < 1.55: occupied = True break k += 1 # Found an empty position if not occupied: break if not occupied: taken_positions.append(position*1) else: visibility = False if visibility: translation = m44.create_from_translation(np.array([position[0], -0.5, position[1]])) rotation = m44.create_from_y_rotation(2 * math.pi * random.random()) transform = m44.multiply(rotation, translation) self.geometry["objs"][index].visible = True else: transform = m44.create_from_translation(np.array([0, -5, 0])) self.geometry["objs"][index].visible = False self.geometry["objs"][index].transform = transform
def GetWorldMatrix(self): if self.worldMatrix is None or self.isDirty: self.isDirty = False translation = matrix44.create_from_translation(vector3.create(self.position[0], self.position[1], 0.0)) rotation = matrix44.create_from_z_rotation(self.rotation) self.worldMatrix = matrix44.multiply(rotation, translation) return self.worldMatrix
def update_view_matrix(self): self.viewMatrix = matrix44.multiply( matrix44.create_from_x_rotation(math.radians(self.pitch)), matrix44.create_from_y_rotation(math.radians(self.yaw))) self.viewMatrix = matrix44.multiply( self.viewMatrix, matrix44.create_from_translation( Vector3([0, 0, -self.distanceFromPlayer.actual])))
def __init__(self, **kwargs): super().__init__(**kwargs) self.prog = self.load_program('scene_default/bbox.glsl') self.bbox = geometry.bbox() self.prog['color'].value = (1, 1, 1) self.prog['bb_min'].value = (-2, -2, -2) self.prog['bb_max'].value = (2, 2, 2) self.prog['m_model'].write(matrix44.create_from_translation([0.0, 0.0, -8.0], dtype='f4'))
def randomize_lighting(self): for i in range(len(self.lights)): if random.random() < 0.5: self.lights[i].visible = False else: self.lights[i].visible = True position = np.array([random.random() * 4.5 - 2.25, random.random() * 0.85 + 1., random.random() * 4.5 - 2.25]) self.lights[i].transform = m44.create_from_translation(position) self.materials["light" + str(i)].emission = np.array([random.random() * 15, random.random() * 15, random.random() * 15])
def render(self, time: float, frametime: float): self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST) m_rot = Matrix44.from_eulers(Vector3((time, time, time))) m_trans = matrix44.create_from_translation(Vector3((0.0, 0.0, -3.0))) m_mv = matrix44.multiply(m_rot, m_trans) self.prog['m_model'].write(m_mv.astype('f4').tobytes()) self.prog['m_camera'].write(self.camera.matrix.astype('f4').tobytes()) self.cube.render(self.prog)
def get_projection(width, height): world_width = lattice_x world_height = world_width / width * height projection = matrix44.create_orthogonal_projection(-world_width/2, world_width/2, -world_height/2, world_height/2, -1, 1) translation = matrix44.create_from_translation([-lattice_x/2, -lattice_y/2, 0]) point_size = width / world_width return numpy.matmul(translation, projection), point_size
def randomize_lighting(self): # Set light colors a = np.array([0.5, 0.5, 0.5]) b = np.array([0.5, 0.5, 0.5]) c = np.array([1, 1, 1]) d = np.array([0.5, 0.35, 0.2]) color = self.iq_color(random.random(), a, b, c, d) emission = 15 + 8.5 * color self.materials[self.lights[1].material].emission = emission color = self.iq_color(random.random(), a, b, c, d) emission = 10 + 6 * color self.materials[self.lights[0].material].emission = emission # Move light source pos = np.array([1.6 + (random.random() - 0.5), 4.5 * (random.random() - 0.5)]) self.lights[0].transform = m44.create_from_translation(np.array([pos[0], 1.8375, pos[1]])) self.geometry["lamp"][0].transform = m44.create_from_translation(np.array([pos[0], 1.835, pos[1]]))
def batch_render(self, list_translasi): # me-render banyak rock dengan tekstur sama glBindVertexArray(self.vao_rock) glBindTexture(GL_TEXTURE_2D, self.texture) model_loc = ObjectShader.model_loc for translasi in list_translasi: rock_model = matrix44.create_from_translation(Vector3(translasi)).flatten().astype("float32") #this is the model transformation matrix c_rock_model = numpy.ctypeslib.as_ctypes(rock_model) glUniformMatrix4fv(model_loc, 1, GL_FALSE, c_rock_model) glDrawArrays(GL_TRIANGLES, 0, Rock.num_verts) glBindVertexArray(0)
def draw(self): self.shader.use() glBindVertexArray(self.VAO) glBindBuffer(GL_ARRAY_BUFFER, self.VBO) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.EBO) model = matrix44.create_from_translation(Vector3(self.pos)) glBindTexture(GL_TEXTURE_2D, self.texture) self.shader.set_mat4('model', model) glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT, None)
def draw(self): self.shader.use() glBindVertexArray(self.VAO) glEnable(GL_PRIMITIVE_RESTART) glPrimitiveRestartIndex(GL_PRIMITIVE_RESTART_FIXED_INDEX) glBindBuffer(GL_ARRAY_BUFFER, self.VBO) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.EBO) model = matrix44.create_from_translation(Vector3(self.pos)) self.shader.set_mat4('model', model) glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT, None)
def render(self, translasi): # to draw the rock, we need to rebind the rock's vao model_loc = ObjectShader.model_loc rock_model = matrix44.create_from_translation(translasi).flatten().astype("float32") #this is the model transformation matrix c_rock_model = numpy.ctypeslib.as_ctypes(rock_model) glBindVertexArray(self.vao_rock) glBindTexture(GL_TEXTURE_2D, self.texture) glUniformMatrix4fv(model_loc, 1, GL_FALSE, c_rock_model) glDrawArrays(GL_POLYGON, 0, Rock.num_verts) glDrawArrays(GL_TRIANGLES, 0, Rock.num_verts) glBindVertexArray(0)
def test_create_from_translation( self ): translation = numpy.array( [ 2.0, 3.0, 4.0 ] ) mat = matrix44.create_from_translation( translation ) result = mat[ 3, 0:3 ] expected = translation # translation goes down the last column in normal matrix self.assertTrue( numpy.array_equal( result, expected ), "Matrix44 translation not set properly" )
def on_draw(): global time time += 0.01 game_window.clear() ctx.screen.use() trans = matrix44.create_from_translation( (math.cos(time), math.sin(time / 5) * 5 - 6, 0)) rot = matrix44.create_from_y_rotation(math.pi / 2) mat = matrix44.multiply(trans, rot) ctx.enable(moderngl.DEPTH_TEST | moderngl.CULL_FACE) scene.draw( projection_matrix=projection, camera_matrix=mat, ) fbo.use() fbo.clear() gl.glUseProgram(0) gl.glBindVertexArray(0) main_batch.draw() counter.draw() trans = matrix44.create_from_translation([0, 0, -1]) rot = matrix44.create_from_axis_rotation( [math.sin(time) / 4, math.cos(time) / 4, math.sin(time) / 20], math.sin(time) / 5) mat = matrix44.multiply(trans, rot) ctx.screen.use() fbo.color_attachments[0].use(location=0) texture_program['scale'].value = (800 / window_x, 600 / window_y) texture_program['m_proj'].write(projection) texture_program['m_view'].write(mat.astype('f4').tobytes()) quad.render(texture_program)
def render(self, time, frametime): # time = self.wnd.frames / 30 self.ctx.enable_only(moderngl.DEPTH_TEST | moderngl.CULL_FACE) self.render_pygame(time) rotate = matrix44.create_from_eulers((time, time * 1.2, time * 1.3), dtype='f4') translate = matrix44.create_from_translation((0, 0, -3.5), dtype='f4') camera = matrix44.multiply(rotate, translate) self.texture_prog['m_camera'].write(camera) self.pg_texture.use() self.cube.render(self.texture_prog)
def render(self, camera, time, frame_time): """Custom render method to gain exact control over the scene""" self.process_events(time, frame_time) cam = camera.matrix translate = matrix44.create_from_translation((0, -2, -10), dtype='f4') cam = matrix44.multiply(translate, cam) # Draw static geometry with default scene shader self.highway.draw(projection_matrix=camera.projection.matrix, camera_matrix=cam) # Inner rings self.inner_ring_prog['m_cam'].write(cam) self.inner_ring_prog['rotation'] = self.inner_rings_rotation self.inner_ring_prog['ring_spacing'] = self.inner_ring_spacing self.inner_ring_vao.render(self.inner_ring_prog, instances=20) # Outer rings self.outer_ring_prog['m_cam'].write(cam) self.outer_ring_prog['rotation'] = -self.inner_rings_rotation self.outer_ring_vao.render(self.outer_ring_prog, instances=11) # Ring neons self.ring_neon_prog['m_cam'].write(cam) self.ring_neon_prog['rotation'] = -self.inner_rings_rotation self.ring_neon_prog['color'] = self.light_ring_color self.ring_neon_1.render(self.ring_neon_prog, instances=11) self.ring_neon_2.render(self.ring_neon_prog, instances=11) self.ring_neon_3.render(self.ring_neon_prog, instances=11) self.ring_neon_4.render(self.ring_neon_prog, instances=11) # Light - static self.light_static_prog['m_cam'].write(cam) self.light_static_prog['color'] = self.laser_left_color self.light_left_static_vao.render(self.light_static_prog) self.light_static_prog['color'] = self.laser_right_color self.light_right_static_vao.render(self.light_static_prog) self.light_static_prog['color'] = self.light_center_color self.light_center_static_vao.render(self.light_static_prog) self.light_static_prog['color'] = self.light_back_color self.light_back_static_vao.render(self.light_static_prog) # Light - Moving lasers self.laser_prog['m_cam'].write(cam) self.laser_prog['color'] = self.laser_left_color self.laser_prog['rotation'] = self.left_laser_rot self.laser_prog['time'] = time self.laser_left_1.render(self.laser_prog, instances=4) self.laser_prog['color'] = self.laser_right_color self.laser_prog['rotation'] = self.right_laser_rot self.laser_right_1.render(self.laser_prog, instances=4)
def lookAtMatrix( camera, target, up ): forward = vector.normalise(target - camera) side = vector.normalise( vector.cross( forward, up ) ) #shifts 'up' to the camera's up up = vector.cross( side, forward ) matrix2 = array( [[ side[0], up[0], -forward[0], 0.0 ], [ side[1], up[1], -forward[1], 0.0 ], [ side[2], up[2], -forward[2], 0.0 ], [ 0.0, 0.0, 0.0, 1.0 ]], dtype = float32) return array(mat4.multiply( mat4.create_from_translation( -camera ), matrix2 ), dtype=float32)
def create_transformation(self, rotation=None, translation=None): """Convenient transformation method doing rotations and translation""" mat = None if rotation is not None: mat = Matrix44.from_eulers(Vector3(rotation)) if translation is not None: trans = matrix44.create_from_translation(Vector3(translation)) if mat is None: mat = trans else: mat = matrix44.multiply(mat, trans) return mat
def get_projection(width, height): world_height = geometry.size_y world_width = world_height / height * width projection = matrix44.create_orthogonal_projection(-world_width / 2, world_width / 2, -world_height / 2, world_height / 2, -1, 1) translation = matrix44.create_from_translation( [-geometry.size_x / 2, -geometry.size_y / 2, 0]) point_size = width / world_width return numpy.matmul(translation, projection), point_size
def batch_render(self, list_translasi, vertical=True): # me-render banyak fence dengan tekstur sama glBindVertexArray(self.vao_fence) glBindTexture(GL_TEXTURE_2D, self.texture) model_loc = ObjectShader.model_loc if not vertical: rotate = matrix44.create_from_y_rotation(numpy.pi/2) # 90 degree rotation for translasi in list_translasi: fence_model = matrix44.create_from_translation(Vector3(translasi)) #this is the model transformation matrix if not vertical: fence_model = matrix44.multiply(rotate, fence_model) c_fence_model = numpy.ctypeslib.as_ctypes(fence_model.flatten().astype('float32')) glUniformMatrix4fv(model_loc, 1, GL_FALSE, c_fence_model) glDrawArrays(GL_TRIANGLES, 0, Fence.num_verts) glBindVertexArray(0)
def render(self, time: float, frametime: float): self.ctx.enable_only(moderngl.CULL_FACE | moderngl.DEPTH_TEST) m_rot = Matrix44.from_eulers(Vector3((time, time, time))) m_trans = matrix44.create_from_translation(Vector3((0.0, 0.0, -3.0))) m_mv = matrix44.multiply(m_rot, m_trans) self.prog['m_proj'].write(self.camera.projection.tobytes()) self.prog['m_model'].write(m_mv.astype('f4').tobytes()) self.prog['m_camera'].write(self.camera.matrix.astype('f4').tobytes()) self.prog['layer'].value = math.fmod(time, 10) self.prog['texture0'].value = 0 self.texture.use(location=0) self.cube.render(self.prog)
def render(self, translasi): # if self.loadingthread.isAlive(): # return # to draw the mushroom, we need to rebind the mushroom's vao model_loc = ObjectShader.model_loc mushroom_model = matrix44.create_from_translation( translasi).flatten().astype( "float32") #this is the model transformation matrix c_mushroom_model = numpy.ctypeslib.as_ctypes(mushroom_model) glBindVertexArray(self.vao_mushroom) glBindTexture(GL_TEXTURE_2D, self.texture) glUniformMatrix4fv(model_loc, 1, GL_FALSE, c_mushroom_model) glDrawArrays(GL_TRIANGLES, 0, Mushroom.num_verts) glBindVertexArray(0)
def regenViewMatrix(self): forward = array([ math.cos( self.verticalAngle ) * math.sin( self.horizontalAngle ), math.sin( self.verticalAngle ), math.cos( self.verticalAngle ) * math.cos( self.horizontalAngle ) ]) up = array([0.,-1.,0.,]) side = -vector.normalise( vector.cross( forward, up ) ) #shifts 'up' to the camera's up up = vector.cross( side, forward ) matrix2 = array( [[ side[0], up[0], forward[0], 0.0 ], [ side[1], up[1], forward[1], 0.0 ], [ side[2], up[2], forward[2], 0.0 ], [ 0.0, 0.0, 0.0, 1.0 ]], dtype = float32) self.position += forward * self.xMovement self.position += side * self.yMovement self.xMovement = self.yMovement = 0. self.viewMatrix = array(mat4.multiply( mat4.create_from_translation( self.position ), matrix2 ), dtype=float32)
def render(self): t = clock() self.time_parameter.set_value(t) data = ( ((-0.5, -0.5, 0), (0.0, 0.0)), ((0.5, -0.5, 0), (1.0, 0.0)), ((0, 0.5, 0), (0.5, 1.0)), ) proj = matrix44.create_perspective_projection_matrix( 90, 4.0 / 3.0, 0.01, 100.0) mv = matrix44.multiply( matrix44.create_from_translation((sin(t * 10), 0.0, -3.0 * abs(sin(t * 30)))), matrix44.create_from_z_rotation(t * 50), ) mvp = matrix44.multiply(mv, proj) self.model_view_proj.set_value(mvp) for pass_ in self.technique.passes: pass_.begin() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glBegin(gl.GL_TRIANGLES) for position, texcoord in data: gl.glMultiTexCoord2fv(gl.GL_TEXTURE0, texcoord) gl.glVertex3fv(position) gl.glEnd() pass_.end()
def _CreateMatrix(self): self.worldMatrix = matrix44.multiply( matrix44.create_from_translation(self.position), matrix44.create_from_quaternion(self.rotation) )
def test_create_from_translation( self ): result = matrix44.create_from_translation([2.,3.,4.]) expected = np.eye(4) expected[3,:3] = [2.,3.,4.] np.testing.assert_almost_equal(result, expected, decimal=5)
def test_apply_to_vector_with_translation(self): mat = matrix44.create_from_translation([2.,3.,4.]) result = matrix44.apply_to_vector(mat, [1.,1.,1.]) np.testing.assert_almost_equal(result, [3.,4.,5.], decimal=5)