class Display(object): ''' Handles the rendering ''' def __init__(self, size): ''' Constructor ''' self.size = size self.ctx = None def setup_opencl(self): platforms = cl.get_platforms() if not platforms: raise OpenCLError("No OpenCL platforms found.") self.ctx = cl.Context(platforms[0].get_devices(device_type=cl.device_type.GPU)) self.queue = cl.CommandQueue(self.ctx) def setup_kernels(self): self.cl_program = build_program(self.ctx, "kernels") self.raytrace = self.cl_program.raytrace self.init_chunk_array = self.cl_program.init_chunk_array def setup_scene(self): self.scene = Scene(self.ctx, self.queue) self._h_img_canvas = numpy.empty((self.size[0]*4, self.size[1]), dtype=numpy.ubyte) im_format = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.UNORM_INT8) self._d_img_canvas = cl.Image(self.ctx, cl.mem_flags.WRITE_ONLY, im_format, self.size) def setup_pygame(self): pygame.init() self.surface = pygame.display.set_mode(self.size) def init_chunk_array(self): self.init_chunk_array(self.queue, ) def render(self): self.scene.upload_buffers() self.raytrace(self.queue, (16, 4), self.size, self._d_img_canvas, self.scene._cam_buffer) cl.enqueue_copy(self.queue, self._h_img_canvas, self._d_img_canvas) self.surface.blit(pygame.image.fromstring(self._h_img_canvas.tostring(), self.size, 'RGBA'), (0,0)) def render_loop(self): self.setup_opencl() self.setup_kernels() self.setup_scene() self.setup_pygame() self.render() clock = pygame.time.Clock() while True: elapsed = clock.tick(30)/1000.0 for event in pygame.event.get(): if event.type == pygame.QUIT: return self.render()
def setup_scene(self): self.scene = Scene(self.ctx, self.queue) self._h_img_canvas = numpy.empty((self.size[0]*4, self.size[1]), dtype=numpy.ubyte) im_format = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.UNORM_INT8) self._d_img_canvas = cl.Image(self.ctx, cl.mem_flags.WRITE_ONLY, im_format, self.size)
from transformations import quaternion_from_euler, quaternion_about_axis,\ quaternion_multiply, quaternion_inverse, euler_from_quaternion import time import transformations import cmath import sys from world.generate.diamondsquaregenerator import DiamondSquareGenerator from world.generate.perlinnoisegenerator import PerlinNoiseGenerator if __name__ == '__main__': platform = pyopencl.get_platforms() my_gpu_devices = platform[0].get_devices(device_type=pyopencl.device_type.CPU) ctx = pyopencl.Context(devices=my_gpu_devices) print(ctx.devices[0].max_compute_units) queue = pyopencl.CommandQueue(ctx) scene = Scene(ctx, queue) gen = DiamondSquareGenerator({ 'widthlog2': 8, 'roughness': 1, 'factor': 0.6 }) #gen = PerlinNoiseGenerator({ 'widthlog2': 8, 'freq': 32, 'steps': 4, 'amplitude_factor': 1 }) gen.generate(scene._chunk_array, ctx, queue, scene.raytrace_pgrm.write_height_map) sphere = scene.create_object("sphere", position=(2,0,0), radius=2) #cube = scene.create_object("aacube", position=(0,0,0,0), width=6) scene.create_object("sphere", position=(-4,0,0), radius=1) scene.create_object("aacube", position=(-4,0,-5), width=1) scene.create_object("aacube", position=(-2, 0, 0), width=1.5) scene.create_object("aacube", position=(-2, 0, -5), width=1.5) scene.create_object("aacube", position=(-10, 3, -10), width=15) light = scene.create_light("directional", direction=(1,1,1), color=(1,1,1)) #scene.create_light("directional", direction=(1,1,1), color=(1,1,1))