Exemplo n.º 1
0
class App(ZglAppWX):
    volumeRender = Instance(VolumeRenderer)
    multiSample  = Bool(True)
    turn         = Range(0.0, 360.0, 0.0, mode='slider')

    @on_trait_change('multiSample')
    def selectVoxelizer(self):
        if not self.multiSample:
            self.voxelizer    = self.simpleVox
            self.voxelTexFunc = lambda : self.simpleVox.dumpToTex()
        else:
            self.voxelizer    = self.multiVox
            self.voxelTexFunc = lambda : self.multiVox.densityToTex()

    def __init__(self):
        ZglAppWX.__init__(self, viewControl = FlyCamera())

        size = 512
        self.simpleVox = Voxelizer(size)
        self.multiVox = Voxelizer(size*2)
        self.selectVoxelizer()
        
        (v, f) = load_obj("data/bunny/bunny.obj") #"data/bunny/bunny.obj"
        v = fit_box(v)[:,[0, 2, 1]]

        self.vertBuf = BufferObject(v)
        self.idxBuf = BufferObject(f)
        self.idxNum = len(f) * 3

        self.volumeRender = VolumeRenderer()
        
    def drawMesh(self):
        with self.vertBuf.array:
            glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, None)
        with ctx(self.idxBuf.elementArray, vattr(0)):
            glDrawElements(GL_TRIANGLES, self.idxNum, GL_UNSIGNED_INT, None)
    def drawFrame(self):
        d = 1.0 / self.voxelizer.size
        drawBox((0, 0, 0), (1, 1, 1))
        drawBox((d, d, 0), (1-d, 1-d, 1))
        drawBox((d, 0, d), (1-d, 1, 1-d))
        drawBox((0, d, d), (1, 1-d, 1-d))

    def display(self):
        clearGLBuffers()
        
        with ctx(glprofile('voxelize'), self.voxelizer):
            clearGLBuffers()
            #self.drawFrame()
            glTranslate(0.5, 0.5, 0)
            glRotate(self.turn, 0, 0, 1)            
            glTranslate(-0.5, -0.5, 0)
            self.drawMesh()
        self.volumeRender.volumeTex = self.voxelTexFunc()

        with ctx(glprofile('volumeRender'), self.viewControl.with_vp):
            self.volumeRender.render()
Exemplo n.º 2
0
    class App(ZglAppWX):
        volumeRender = Instance(VolumeRenderer)

        def __init__(self):
            ZglAppWX.__init__(self, viewControl = FlyCamera())

            gridSize = 256
            
            a = zeros([gridSize]*3, float32)
            #a[:128] = 1.0
            col = linspace(0.0, 1.0, gridSize).astype(float32)
            a[:] = col[...,newaxis, newaxis]
            
            sinks = (random.rand(10000, 3)*(gridSize, gridSize/2, gridSize/2)).astype(int32)
            for x, y, z in sinks:
                a[z, y, x] = Diffusion.SINK
            a[:, gridSize/2 + 1, :gridSize/2] = Diffusion.OBSTACLE
            
            self.diffusion = Diffusion(gridSize)
            self.diffusion.src.set(a)
                        
            volumeTex = Texture3D( size = [gridSize]*3, format = GL_LUMINANCE_FLOAT32_ATI )
            self.volumeRender = VolumeRenderer(volumeTex)
            a = self.diffusion.src.get()
            with self.volumeRender.volumeTex:
                glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, a.shape[0], a.shape[1], a.shape[2], GL_LUMINANCE, GL_FLOAT, a)
            #self.step()

        def step(self):
            for i in xrange(20):
                self.diffusion.step()
                print '.',
            print
            a = self.diffusion.src.get()
            with self.volumeRender.volumeTex:
                glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, a.shape[0], a.shape[1], a.shape[2], GL_LUMINANCE, GL_FLOAT, a)

        def key_SPACE(self):
            self.step()

        def display(self):
            clearGLBuffers()
            
            with ctx(self.viewControl.with_vp):
                self.volumeRender.render()
Exemplo n.º 3
0
class App(ZglAppWX):
    volumeRender = Instance(VolumeRenderer)    
    rd = Instance(ReactDiff)    
    iterPerFrame = Int(20)
    
    def __init__(self):
        ZglAppWX.__init__(self, viewControl = FlyCamera())
        
        self.rd = ReactDiff(64)
        a = self.rd.d_dst.get()
        self.volumeRender = VolumeRenderer(Texture3D(img = a[...,1]))

    def display(self):
        clearGLBuffers()
        
        for i in xrange(self.iterPerFrame):
            self.rd.iterate()
        a = self.rd.d_dst.get()
        a = ascontiguousarray(a[...,1])
        with self.volumeRender.volumeTex:
            glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, a.shape[0], a.shape[1], a.shape[2], GL_LUMINANCE, GL_FLOAT, a)
            
        with ctx(self.viewControl.with_vp):
            self.volumeRender.render()
Exemplo n.º 4
0
    class App(ZglAppWX):
        coral          = Instance(Coral)
        batchIters     = Int(10)
        iterCount      = Int(0)
        saveGrowIters  = Bool(False)
        
        volumeRender   = Instance(VolumeRenderer)
        showMesh       = Bool(True)
        showVolume     = Bool(False)

        orbitCamera    = Bool(False)
        orbitPitch     = Range(-90.0, 90.0, -45.0)
        orbitDist      = Range(0.0, 5.0, 1.0)
        orbitCenterZ   = Range(0.0, 1.0, 0.5)
        orbitSpeed     = Range(0.0, 90.0, 45.0)
        
        traits_view = View(Item(name='iterCount', style='readonly'),
                           Item(name='batchIters'),
                           Item(name='coral'),
                           Item(name='volumeRender'),
                           Item(name='saveGrowIters'),
                           Item(name='showMesh'),
                           Item(name='showVolume'),
                           
                           Group(
                           Item(name='orbitCamera'),
                           Item(name='orbitPitch'),
                           Item(name='orbitDist'),
                           Item(name='orbitCenterZ'),
                           Item(name='orbitSpeed'),
                           label = 'Orbit Camera',
                           show_border = True),

                           resizable = True,
                           buttons = ["OK"],
                           title='Coral')
                           

        def __init__(self):
            ZglAppWX.__init__(self, viewControl = FlyCamera())
            import pycuda.gl.autoinit
            
            self.coral = Coral()

            self.viewControl.speed = 0.2
            self.viewControl.zNear = 0.01
            self.viewControl.zFar  = 10.0

            self.growLeft = 0

            verts, idxs, quads = create_box()
            verts *= self.coral.gridSize
            def drawBox():
                glColor(0.5, 0.5, 0.5)
                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
                drawArrays(GL_QUADS, verts = verts, indices = quads)
                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
            self.drawBox = drawBox
            
            self.volumeRender = VolumeRenderer()

        def resize(self, x, y):
            self.depthTex = Texture2D(size=(x, y), format=GL_DEPTH_COMPONENT24, srcFormat=GL_DEPTH_COMPONENT)

        def key_SPACE(self):
            if self.growLeft == 0:
                self.growLeft = self.batchIters
            else:
                self.growLeft = 0
        def key_1(self):
            save_obj("t.obj", self.coral.positions, self.coral.faces)
        def key_2(self):
            saveProfileLogs('grow_log')
        def key_3(self):
            self.showVolume = not self.showVolume
            if self.showVolume:
                print self.showVolume
                a = self.coral.diffusion.src.get()
                tex = Texture3D(img=a, format = GL_LUMINANCE8)
                self.volumeRender.volumeTex = tex

        def save_coral(self):
            fname = "coral_%03d" % (self.iterCount,)
            print "saving '%s' ..." % (fname,),

            #filter unreferenced
            mark = zeros(len(self.coral.positions), int32)
            mark[self.coral.faces] = 1
            ofs = cumsum(mark)-1
            positions = self.coral.positions[mark==1].copy() / self.coral.gridSize
            normals   = self.coral.normals[mark==1].copy()
            absorb    = self.coral.absorb[mark==1].copy()
            faces = ofs[self.coral.faces]

            savez(fname, 
              positions = positions, 
              faces     = faces,
              normals   = normals,
              absorb    = absorb)
            print 'ok'
            
        def display(self):
            clearGLBuffers()
            if self.growLeft > 0:
                self.coral.grow()
                self.iterCount += 1
                self.growLeft -= 1
                if self.saveGrowIters:
                    self.save_coral()
            
            if self.orbitCamera:
               c = V(0.5, 0.5, self.orbitCenterZ)
               crs = self.time * self.orbitSpeed
               pitch = self.orbitPitch

               self.viewControl.course = crs
               self.viewControl.pitch = pitch

               crs = radians(crs)
               pitch = radians(pitch)
               cp = cos(pitch)
               d = V(cos(crs)*cp, sin(crs)*cp, sin(pitch))
               self.viewControl.eye = c-d*self.orbitDist

            if self.showMesh:
                with ctx(self.viewControl.with_vp, glstate(GL_DEPTH_TEST, GL_DEPTH_CLAMP_NV)):
                    scale = 1.0 / self.coral.gridSize
                    glScale(scale, scale, scale)
                    glColor3f(0.5, 0.5, 0.5)
                    with glstate(GL_POLYGON_OFFSET_FILL):
                        glPolygonOffset(1.0, 1.0)
                        drawArrays(GL_TRIANGLES, verts = self.coral.positions, indices = self.coral.faces)

                    glColor3f(1, 1, 1)
                    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
                    drawArrays(GL_TRIANGLES, verts = self.coral.positions, indices = self.coral.faces)
                    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
                    self.drawBox()

            if self.showVolume:
                with ctx(self.depthTex):
                    w, h = self.viewSize
                    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 0, 0, w, h, 0)
                with ctx(self.viewControl.with_vp, glstate(GL_BLEND)):
                    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                    glBlendEquation(GL_FUNC_ADD);
                    self.volumeRender.render(self.depthTex)