def __init__(self, task): assert isinstance(task, RenderTask) self.task = task self.random = Random() self.raytracer = RayTracer(task.getScene()) self.progress = 0.0
def __init__(self, task): if not isinstance(task, RenderTask): raise TypeError( "Incorrect task type: {}. Should be RenderTask".format( type(task))) self.task = task self.random = Random() self.raytracer = RayTracer(task.getScene()) self.progress = 0.0
class ThreadRenderWorkerPool: def __init__( self, baseExpectedSpeed = 1600.0 ): self.rnd = Random() self.baseSpeed = baseExpectedSpeed self.workers = [] def createNextWorker( self, taskable_renderer ): speed = ( 0.5 + self.rnd.real64() ) * self.baseSpeed task = taskable_renderer.getNextTask( speed ) if task: worker = ThreadedRenderWorker( RenderWorker( task ) ) self.workers.append( worker ) worker.start() return worker return None def activeCount( self ): return active_count() - 1 def joinAll( self ): for w in self.workers: w.join()
def render_orig(image, image_file_pathname, camera, scene, iterations): random = Random() try: for frame_no in range(1, iterations + 1): stdout.write('\riteration: %u' % frame_no) stdout.flush() camera.get_frame(scene, random, image) if ((frame_no & (frame_no - 1)) == 0) or frame_no == iterations: image_file = open(image_file_pathname, 'wb') image.get_formatted(image_file, frame_no) image_file.close() print '\nfinished' except KeyboardInterrupt: print '\ninterrupted'
def render_taskable(image, image_file_pathname, camera, scene, num_samples): random = Random() aspect = float(image.height) / float(image.width) samplesPerUpdate = 200 try: totalPasses = float(image.height * image.width) curPass = 0 passUpdateDelta = samplesPerUpdate // num_samples if num_samples < samplesPerUpdate else 1 for y in range(image.height): for x in range(image.width): #separated tasks which should be added to the final image when they are ready (even better simple pixel values can be accumulated simply via additions and num iterations just #has to be passed to tone mapper) r = camera.pixel_accumulated_radiance(scene, random, image.width, image.height, x, y, aspect, num_samples) #accumulation of stored values (can be easily moved to a separate loop over x and y (and the results from radiance calculations) image.add_to_pixel(x, y, r) curPass += 1 if curPass % passUpdateDelta == 0: stdout.write( '\r ') stdout.write('\rProgress: {} %'.format( float(curPass) * 100.0 / totalPasses)) stdout.flush() # image_file = open(image_file_pathname, 'wb') # image.get_formatted(image_file, num_samples) # image_file.close() print '\nfinished' except KeyboardInterrupt: print '\ninterrupted'
def render_rect( rect, img_width, img_height, camera, scene, num_samples ): assert isinstance( rect, Rect ) aspect = float(img_height) / float(img_width) random = Random() samplesPerUpdate = 2000 out = [ 0.0 ] * rect.width * rect.height * 3 curPass = 0 try: totalPasses = float( rect.width * rect.height ) passUpdateDelta = samplesPerUpdate // num_samples if num_samples < samplesPerUpdate else 1 for y in range(rect.y, rect.y + rect.height): for x in range(rect.x, rect.x + rect.width): radiance = camera.pixel_accumulated_radiance(scene, random, img_width, img_height, x, y, aspect, num_samples) absX = x - rect.x absY = y - rect.y if absX >= 0 and absX < rect.width and absY >= 0 and absY < rect.height: index = (absX + ((rect.height - 1 - absY) * rect.width)) * 3 for a in radiance: out[index] += a index += 1 curPass += 1 if curPass: #stdout.write('\r ') stdout.write('\rProgress: {} % \n'.format( float( curPass ) * 100.0 / totalPasses ) ) stdout.flush() print '\nfinished' except KeyboardInterrupt: print '\ninterrupted' print out return get_formatted( out, num_samples )
def __init__( self, baseExpectedSpeed = 1600.0 ): self.rnd = Random() self.baseSpeed = baseExpectedSpeed self.workers = []
class RenderWorker: @classmethod def createWorker(cls, renderTask): if not renderTask.isValid(): return None return RenderWorker(renderTask) def __init__(self, task): if not isinstance(task, RenderTask): raise TypeError( "Incorrect task type: {}. Should be RenderTask".format( type(task))) self.task = task self.random = Random() self.raytracer = RayTracer(task.getScene()) self.progress = 0.0 def get_progress(self): return self.progress def sample_radiance(self, x, y, w, h, aspect, camera, scene, num_samples): acc_radiance = [0.0, 0.0, 0.0] for i in range(num_samples): x_coefficient = ((x + self.random.real64()) * 2.0 / w) - 1.0 y_coefficient = ((y + self.random.real64()) * 2.0 / h) - 1.0 offset = camera.right * x_coefficient + camera.up * ( y_coefficient * aspect) sample_direction = ( camera.view_direction + (offset * tan(camera.view_angle * 0.5))).unitize() radiance = self.raytracer.get_radiance(camera.view_position, sample_direction, self.random) acc_radiance[0] += radiance[0] acc_radiance[1] += radiance[1] acc_radiance[2] += radiance[2] return Vector3f(acc_radiance[0], acc_radiance[1], acc_radiance[2]) def getXY(self, idx, w): return idx % w, idx // w def renderingFinished(self, pixels): result = RenderTaskResult.createRenderTaskResult( self.task.getDesc(), pixels) if result: if self.task.callback: self.task.callback(result) else: print "Failed to acquire result" return result def render(self): desc = self.task.getDesc() x, y, w, h = desc.getX(), desc.getY(), desc.getW(), desc.getH() num_pixels, num_samples = desc.getNumPixels(), desc.getNumSamples() aspect = float(h) / float(w) offset = y * w + x id = desc.getID() pixels = [0.0] * 3 * num_pixels cam = self.task.getCamera() scn = self.task.getScene() for k in range(num_pixels): x, y = self.getXY(k + offset, w) radiance = self.sample_radiance(x, y, w, h, aspect, cam, scn, num_samples) pixels[3 * k + 0] = radiance[0] pixels[3 * k + 1] = radiance[1] pixels[3 * k + 2] = radiance[2] progress = float(k + 1) / float(num_pixels) return self.renderingFinished(pixels)