def __init__(self, model, position, orientation, scale): self.model = model self.position = position self.orientation = orientation self.scale = scale self.transform = Matrix44.translation_vec3(position) * Matrix44.scale(scale) * orientation
def __init__(self, model, position, orientation, scale): self.model = model self.position = position self.orientation = orientation self.scale = scale self.transform = Matrix44.translation(position.x, position.y, position.z) * Matrix44.scale(scale) * orientation self.bounds_radius = model.bounds_radius * scale # the bounds radius should also change self.bounds_center = self.transform.transform_vec3(model.bounds_center)
def __init__(self, position, orientation, clipping_planes): self.position = position self.orientation = orientation self.clipping_planes = clipping_planes cameraTranlationMatrixInverse = Matrix44.translation_vec3(position).get_inverse_translation() cameraRotationInverse = orientation.get_inverse_rotation() self.matrix = cameraRotationInverse * cameraTranlationMatrixInverse
def renderScene(camera, instances): """ """ cameraTranlationMatrixInverse = Matrix44.translation_vec3(camera.position).get_inverse_translation() cameraRotationInverse = camera.orientation.get_inverse_rotation() cameraMatrix = cameraRotationInverse * cameraTranlationMatrixInverse for instance in instances: transform = cameraMatrix * instance.transform transformAndClip(camera.clipping_planes, instance, transform)
def renderScene(camera, instances): """ """ cameraTranlationMatrixInverse = Matrix44.translation_vec3(camera.position).get_inverse_translation() cameraRotationInverse = camera.orientation.get_inverse_rotation() cameraMatrix = cameraRotationInverse * cameraTranlationMatrixInverse for instance in instances: transform = cameraMatrix * instance.transform renderModel(instance.model, transform)
def main(): pygame.init() screen = pygame.display.set_mode(res, pygame.OPENGL|pygame.DOUBLEBUF) pygame.display.set_caption("data renderer") print(GL.glGetString(GL.GL_VERSION)) GL.glClearColor(0.5, 0.5, 0.5, 1.0) GL.glEnable(GL.GL_DEPTH_TEST) shader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(vertex_shader, GL.GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(fragment_shader, GL.GL_FRAGMENT_SHADER) ) vertex_array_object = create_vertex_array_object(gVertices, gColors) line_vbos = create_axes_vbo(Vector3.from_floats(-3.0, -3.0, -3.0), Vector3.from_floats(3.0, 3.0, 3.0)) clock = pygame.time.Clock() global view view = Matrix44().translation(0.0, 0.0, -5.0) global proj proj = Matrix44.perspective_projection_fov(math.pi / 2, res[0] / float(res[1]), 0.1, 100.0) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: return clock.tick() simtime = pygame.time.get_ticks() * 0.001 handle_arcball() GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) display(shader, vertex_array_object, simtime, GL.GL_POINTS, int(len(gVertices) / 3)) display(shader, line_vbos[0], simtime, GL.GL_LINES, 2) display(shader, line_vbos[1], simtime, GL.GL_LINES, 2) display(shader, line_vbos[2], simtime, GL.GL_LINES, 2) pygame.display.flip()
def handle_arcball(): global last_click_pos global model_mod global model global view_mod global view # LMB = rotate arcball if (pygame.mouse.get_pressed()[0]): if (last_click_pos[0] == (-1, -1)): last_click_pos[0] = pygame.mouse.get_pos() v_new = screen_to_arcball(pygame.mouse.get_pos()) v_old = screen_to_arcball(last_click_pos[0]) angle = math.acos(min(1.0, v_new.dot(v_old))) axis = (v_new.cross(v_old)).normalize() model_mod = Matrix44.rotation_about_axis(axis, angle) else: if (last_click_pos[0] != (-1, -1)): last_click_pos[0] = (-1, -1) model = model_mod * model model_mod = Matrix44.identity() # MMB = translate arcball if (pygame.mouse.get_pressed()[1]): if (last_click_pos[1] == (-1, -1)): last_click_pos[1] = pygame.mouse.get_pos() dx = (pygame.mouse.get_pos()[0] - last_click_pos[1][0]) / float(res[0]) dy = (pygame.mouse.get_pos()[1] - last_click_pos[1][1]) / float(res[1]) view_mod = Matrix44.translation(-dx, -dy, 0) else: if (last_click_pos[1] != (-1, -1)): last_click_pos[1] = (-1, -1) view = view_mod * view view_mod = Matrix44.identity() # RMB = scale arcball (based on RMB delta y) if (pygame.mouse.get_pressed()[2]): if (last_click_pos[2] == (-1, -1)): last_click_pos[2] = pygame.mouse.get_pos() dy = (pygame.mouse.get_pos()[1] - last_click_pos[2][1]) / float(res[1]) model_mod = Matrix44.scale(max(-dy + 1.0, 0.01)) else: if (last_click_pos[2] != (-1, -1)): last_click_pos[2] = (-1, -1) model = model_mod * model model_mod = Matrix44.identity()
background_color = (255, 255, 255) image = Image.new("RGB", (screen_width, screen_height), background_color) pixels = image.load() cube = Model(vertexes, triangles, Vector3(0, 0, 0), sqrt(3)) sqrt_2 = sqrt(2) clipping_planes = [ Plane(Vector3(0, 0, 1), -1), # Near Plane(Vector3(sqrt_2, 0, sqrt_2), 0), # Left Plane(Vector3(-sqrt_2, 0, sqrt_2), 0), # Right Plane(Vector3(0, -sqrt_2, sqrt_2), 0), # Top Plane(Vector3(0, sqrt_2, sqrt_2), 0) # Bottom ] camera = Camera(Vector3(-3, 1, 2), Matrix44().y_rotation(radians(-30)), clipping_planes) depth_buffer = [[0 for _ in range(screen_height)] for _ in range(screen_width)] instances = [ Instance(cube, Vector3(-1.5, -1, 7), Matrix44(), 0.75), Instance(cube, Vector3(1.25, 2.5, 7.5), Matrix44.y_rotation(radians(195)), 1.0) ] renderScene(camera, instances) image.save("raster12.png")
cube = Model(vertexes, triangles, Vector3(0, 0, 0), sqrt(3)) #----- Light lights = [ Light('AMBIENT', 0.2, None), Light('POINT', 0.6, Vector3(-3, 2, -10)), Light('DIRECTIONAL', 0.2, Vector3(-1, 0, 1)) ] #----- Camera sqrt_2 = sqrt(2) clipping_planes = [ Plane(Vector3(0, 0, 1), -1), # Near Plane(Vector3(sqrt_2, 0, sqrt_2), 0), # Left Plane(Vector3(-sqrt_2, 0, sqrt_2), 0), # Right Plane(Vector3(0, -sqrt_2, sqrt_2), 0), # Top Plane(Vector3(0, sqrt_2, sqrt_2), 0) # Bottom ] camera = Camera(Vector3(-3, 1, 2), Matrix44().y_rotation(radians(-30)), clipping_planes) depth_buffer = [[0 for _ in range(screen_height)] for _ in range(screen_width)] instances = [Instance(cube, Vector3(-1.5, 0, 7), Matrix44(), 0.75), Instance(cube, Vector3(1.25, 2.5, 7.5), Matrix44.y_rotation(radians(195)), 1.0), Instance(cube, Vector3(1.75, 0, 5), Matrix44.y_rotation(radians(-30)), 1.0)] renderScene(camera, instances) image.save("raster18.png")
void main() { float t = fract(time); vec4 c = vcolor; gl_FragColor = vec4(c.xyz, 1.0f); } """ # GLOBALS gVertices = data.getVertices() gColors = data.getColors() # MVP matrices model = Matrix44.identity() view = Matrix44.identity() proj = Matrix44.identity() view_mod = Matrix44.identity() model_mod = Matrix44.identity() # mouse vars last_click_pos = [(-1, -1), (-1, -1), (-1, -1)] # display vars res = (1024, 1024) # Read data into vertices # TODO: Streamline this
Triangle(0, 1, 2, RED), Triangle(0, 2, 3, RED), Triangle(4, 0, 3, GREEN), Triangle(4, 3, 7, GREEN), Triangle(5, 4, 7, BLUE), Triangle(5, 7, 6, BLUE), Triangle(1, 5, 6, YELLOW), Triangle(1, 6, 2, YELLOW), Triangle(4, 5, 1, PURPLE), Triangle(4, 1, 0, PURPLE), Triangle(2, 6, 7, CYAN), Triangle(2, 7, 3, CYAN), ] background_color = (255, 255, 255) image = Image.new("RGB", (screen_width, screen_height), background_color) cube = Model(vertexes, triangles) camera = Camera(Vector3(-3, 1, 2), Matrix44.y_rotation(radians(-30))) instances = [ Instance(cube, Vector3(-1.5, 0, 7), Matrix44(), 0.75), Instance(cube, Vector3(1.25, 2.5, 7.5), Matrix44.y_rotation(radians(195)), 1.0) ] renderScene(camera, instances) image.save("raster07.png")