Exemplo n.º 1
0
def render(voxels, angle1=45, angle2=10, save=None):
    sz_x, sz_y, sz_z, channels = voxels.shape
    thresh = 0.5

    _verts, faces = measure.marching_cubes(abs(voxels[:, :, :, 0]), thresh)
    #_verts,faces = mcubes.marching_cubes(voxels[:,:,:,0],thresh)

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)

    if True:
        #print t

        # render a helix (this is similar to the previous example, but
        # this time we'll render to a texture)

        # initialize projection

        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        gluPerspective(90, 1, 0.01, 1000)
        gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0)

        glMatrixMode(GL_MODELVIEW)

        glShadeModel(GL_SMOOTH)

        glPushMatrix()
        glRotatef(angle1, 0.0, 1.0, 0.0)
        glRotatef(angle2, 0.1, 0.0, 0.0)

        #glEnable(GL_CULL_FACE)
        glEnable(GL_DEPTH_TEST)
        #glEnable(GL_LIGHTING)
        #glDisable(GL_CULL_FACE)
        #glDisable(GL_DEPTH_TEST)
        # Black background for the Helix
        glClearColor(0.5, 0.5, 0.5, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Fallback to white

        #lightZeroPosition = [0.,50.,-2.,1.]
        #lightZeroColor = [1.8,1.0,0.8,1.0] #green tinged
        #glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
        #glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
        #glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
        #glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
        #glEnable(GL_LIGHT0)
        # The helix

        #color = [1.0,0.,0.,1.]
        #glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
        glBegin(GL_TRIANGLES)

        color_idx = np.asarray(_verts, dtype=int)

        colors = abs(voxels[color_idx[:, 0], color_idx[:, 1], color_idx[:, 2],
                            1:])
        colors = np.clip(colors, 0, 1)
        colors = hsv_to_rgb(colors)

        verts = _verts - numpy.array((sz_x / 2, sz_y / 2, sz_z / 2))
        #Create an indexed view into the vertex array using the array of three indices for triangles
        tris = verts[faces]
        tricols = colors[faces]

        #print faces.shape
        if save != None:
            saveply.save(save, verts, colors, faces)
        #Calculate the normal for all the triangles, by taking the cross product of the vectors v1-v0, and v2-v0 in each triangle
        n = numpy.cross(tris[::, 1] - tris[::, 0], tris[::, 2] - tris[::, 0])
        # n is now an array of normals per triangle. The length of each normal is dependent the vertices,
        # we need to normalize these, so that our next step weights each normal equally.
        n = normalize_v3(n)

        #verts, faces = measure.marching_cubes(abs(voxels[:,:,:,0]), thresh)
        f_idx = 0
        for tri in tris:
            #glNormal3f(*(n[f_idx]))
            colors = tricols[f_idx]
            f_idx += 1
            for k in xrange(3):
                #k.reverse()
                #print verts[k]
                #color = voxels[ints[0],ints[1],ints[2],1:]
                #color = hsv_to_rgb(*color) #hsv_to_rgb(color)
                #print color
                glColor3f(*colors[k])
                glVertex3f(*tri[k])

        glEnd()
        glPopMatrix()
        # do not render to texture anymore - "switch off" rtt
        out = glReadPixels(0, 0, 512, 512, GL_RGB, GL_FLOAT)
        return out
Exemplo n.º 2
0
def render(voxels,
           bg_color=[0.5, 0.5, 0.5],
           angle1=45,
           angle2=10,
           save=None,
           amb=0.2,
           spec=1.0,
           shiny=100,
           lighting=True,
           diff=0.5):
    global disp_sz, init_done

    if not init_done:
        do_init()
    sz_x, sz_y, sz_z, channels = voxels.shape
    thresh = 0.5
    #print "LIGHTING",lighting
    #raw_input()
    #verts, faces = measure.marching_cubes(abs(voxels[:,:,:,0]), thresh)
    _verts, faces = mcubes.marching_cubes(voxels[:, :, :, 0], thresh)

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)

    if True:
        #print t

        # render a helix (this is similar to the previous example, but
        # this time we'll render to a texture)

        # initialize projection

        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        gluPerspective(90, 1, 0.01, 1000)
        gluLookAt(0, 0, sz_z, 0, 0, 0, 0, 1, 0)

        glMatrixMode(GL_MODELVIEW)

        glShadeModel(GL_SMOOTH)

        if lighting:
            glEnable(GL_COLOR_MATERIAL)
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)
            glEnable(GL_LIGHT1)
            glEnable(GL_LIGHT2)

            light = diff
            glLightfv(GL_LIGHT0, GL_DIFFUSE, [light, light, light, 1.0])
            glLightfv(GL_LIGHT1, GL_DIFFUSE, [light, light, light, 1.0])
            glLightfv(GL_LIGHT2, GL_DIFFUSE, [light, light, light, 1.0])

            glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [amb, amb, amb, 1.0])
            glMaterialfv(GL_FRONT, GL_SPECULAR, [spec, spec, spec])
            glMaterialfv(GL_FRONT, GL_SHININESS, shiny)

            glLightfv(GL_LIGHT0, GL_POSITION, [0.0, 2.0, -1.0, 0.0])
            glLightfv(GL_LIGHT1, GL_POSITION, [10.0, -5.0, 20.0, 0.0])
            glLightfv(GL_LIGHT2, GL_POSITION, [-10.0, 0.0, 10.0, 0.0])

        glPushMatrix()
        glRotatef(angle1, 0.0, 1.0, 0.0)
        glRotatef(angle2, 0.1, 0.0, 0.0)

        glEnable(GL_CULL_FACE)
        glEnable(GL_DEPTH_TEST)

        #glDisable(GL_CULL_FACE)
        #glDisable(GL_DEPTH_TEST)
        # Black background for the Helix

        glClearColor(bg_color[0], bg_color[1], bg_color[2], 1.0)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Fallback to white

        #lightZeroPosition = [0.,50.,-2.,1.]
        #lightZeroColor = [1.8,1.0,0.8,1.0] #green tinged
        #glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
        #glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
        #glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
        #glEnable(GL_LIGHT0)
        # The helix

        #color = [1.0,0.,0.,1.]
        #glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
        #glBegin(GL_TRIANGLES);

        color_idx = np.asarray(_verts, dtype=int)

        colors = abs(voxels[color_idx[:, 0], color_idx[:, 1], color_idx[:, 2],
                            1:])
        colors = np.clip(colors, 0, 1)
        colors = hsv_to_rgb(colors)

        verts = _verts - numpy.array((sz_x / 2, sz_y / 2, sz_z / 2))
        #Create an indexed view into the vertex array using the array of three indices for triangles
        tris = verts[faces]
        tricols = colors[faces]

        #print faces.shape
        if save != None:
            saveply.save(save, verts, colors, faces)
        #Calculate the normal for all the triangles, by taking the cross product of the vectors v1-v0, and v2-v0 in each triangle
        n = numpy.cross(tris[::, 1] - tris[::, 0], tris[::, 2] - tris[::, 0])
        # n is now an array of normals per triangle. The length of each normal is dependent the vertices,
        # we need to normalize these, so that our next step weights each normal equally.
        n = normalize_v3(n)

        vnorms = numpy.zeros(verts.shape, dtype=numpy.float32)

        if True:  #angle1%2==0:
            for idx in xrange(len(tris)):
                face = faces[idx]
                vnorms[face[0]] += n[idx]
                vnorms[face[1]] += n[idx]
                vnorms[face[2]] += n[idx]
        else:
            vnorms[faces[:, 0]] += n
            vnorms[faces[:, 1]] += n
            vnorms[faces[:, 2]] += n

        vnorms = normalize_v3(vnorms)

        verts = numpy.asarray(verts, dtype=numpy.float32)
        colors = numpy.asarray(colors, dtype=numpy.float32)
        vnorms = numpy.asarray(vnorms, dtype=numpy.float32)

        glEnableClientState(GL_VERTEX_ARRAY)
        glEnableClientState(GL_COLOR_ARRAY)
        glEnableClientState(GL_NORMAL_ARRAY)

        glVertexPointer(3, GL_FLOAT, 0, verts)
        glColorPointer(3, GL_FLOAT, 0, colors)
        glNormalPointer(GL_FLOAT, 0, vnorms)

        faces = numpy.asarray(faces, dtype=numpy.uint)
        glDrawElements(GL_TRIANGLES,
                       faces.flatten().shape[0], GL_UNSIGNED_INT,
                       faces.flatten())

        glDisableClientState(GL_VERTEX_ARRAY)
        glDisableClientState(GL_COLOR_ARRAY)
        glDisableClientState(GL_NORMAL_ARRAY)
        glPopMatrix()
        out = glReadPixels(0, 0, disp_sz, disp_sz, GL_RGB, GL_FLOAT)

        return out
Exemplo n.º 3
0
def render(
    voxels,
    bg_color=[0.5, 0.5, 0.5],
    angle1=45,
    angle2=10,
    save=None,
    amb=0.2,
    spec=1.0,
    shiny=100,
    lighting=True,
    diff=0.5,
):
    global disp_sz, init_done

    if not init_done:
        do_init()
    sz_x, sz_y, sz_z, channels = voxels.shape
    thresh = 0.5
    # print "LIGHTING",lighting
    # raw_input()
    # verts, faces = measure.marching_cubes(abs(voxels[:,:,:,0]), thresh)
    _verts, faces = mcubes.marching_cubes(voxels[:, :, :, 0], thresh)

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)

    if True:
        # print t

        # render a helix (this is similar to the previous example, but
        # this time we'll render to a texture)

        # initialize projection

        glClearColor(0.0, 0.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        gluPerspective(90, 1, 0.01, 1000)
        gluLookAt(0, 0, sz_z, 0, 0, 0, 0, 1, 0)

        glMatrixMode(GL_MODELVIEW)

        glShadeModel(GL_SMOOTH)

        if lighting:
            glEnable(GL_COLOR_MATERIAL)
            glEnable(GL_LIGHTING)
            glEnable(GL_LIGHT0)
            glEnable(GL_LIGHT1)
            glEnable(GL_LIGHT2)

            light = diff
            glLightfv(GL_LIGHT0, GL_DIFFUSE, [light, light, light, 1.0])
            glLightfv(GL_LIGHT1, GL_DIFFUSE, [light, light, light, 1.0])
            glLightfv(GL_LIGHT2, GL_DIFFUSE, [light, light, light, 1.0])

            glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)
            glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [amb, amb, amb, 1.0])
            glMaterialfv(GL_FRONT, GL_SPECULAR, [spec, spec, spec])
            glMaterialfv(GL_FRONT, GL_SHININESS, shiny)

            glLightfv(GL_LIGHT0, GL_POSITION, [0.0, 2.0, -1.0, 0.0])
            glLightfv(GL_LIGHT1, GL_POSITION, [10.0, -5.0, 20.0, 0.0])
            glLightfv(GL_LIGHT2, GL_POSITION, [-10.0, 0.0, 10.0, 0.0])

        glPushMatrix()
        glRotatef(angle1, 0.0, 1.0, 0.0)
        glRotatef(angle2, 0.1, 0.0, 0.0)

        glEnable(GL_CULL_FACE)
        glEnable(GL_DEPTH_TEST)

        # glDisable(GL_CULL_FACE)
        # glDisable(GL_DEPTH_TEST)
        # Black background for the Helix

        glClearColor(bg_color[0], bg_color[1], bg_color[2], 1.0)

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Fallback to white

        # lightZeroPosition = [0.,50.,-2.,1.]
        # lightZeroColor = [1.8,1.0,0.8,1.0] #green tinged
        # glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
        # glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
        # glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
        # glEnable(GL_LIGHT0)
        # The helix

        # color = [1.0,0.,0.,1.]
        # glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
        # glBegin(GL_TRIANGLES);

        color_idx = np.asarray(_verts, dtype=int)

        colors = abs(voxels[color_idx[:, 0], color_idx[:, 1], color_idx[:, 2], 1:])
        colors = np.clip(colors, 0, 1)
        colors = hsv_to_rgb(colors)

        verts = _verts - numpy.array((sz_x / 2, sz_y / 2, sz_z / 2))
        # Create an indexed view into the vertex array using the array of three indices for triangles
        tris = verts[faces]
        tricols = colors[faces]

        # print faces.shape
        if save != None:
            saveply.save(save, verts, colors, faces)
        # Calculate the normal for all the triangles, by taking the cross product of the vectors v1-v0, and v2-v0 in each triangle
        n = numpy.cross(tris[::, 1] - tris[::, 0], tris[::, 2] - tris[::, 0])
        # n is now an array of normals per triangle. The length of each normal is dependent the vertices,
        # we need to normalize these, so that our next step weights each normal equally.
        n = normalize_v3(n)

        vnorms = numpy.zeros(verts.shape, dtype=numpy.float32)

        if True:  # angle1%2==0:
            for idx in xrange(len(tris)):
                face = faces[idx]
                vnorms[face[0]] += n[idx]
                vnorms[face[1]] += n[idx]
                vnorms[face[2]] += n[idx]
        else:
            vnorms[faces[:, 0]] += n
            vnorms[faces[:, 1]] += n
            vnorms[faces[:, 2]] += n

        vnorms = normalize_v3(vnorms)

        verts = numpy.asarray(verts, dtype=numpy.float32)
        colors = numpy.asarray(colors, dtype=numpy.float32)
        vnorms = numpy.asarray(vnorms, dtype=numpy.float32)

        glEnableClientState(GL_VERTEX_ARRAY)
        glEnableClientState(GL_COLOR_ARRAY)
        glEnableClientState(GL_NORMAL_ARRAY)

        glVertexPointer(3, GL_FLOAT, 0, verts)
        glColorPointer(3, GL_FLOAT, 0, colors)
        glNormalPointer(GL_FLOAT, 0, vnorms)

        faces = numpy.asarray(faces, dtype=numpy.uint)
        glDrawElements(GL_TRIANGLES, faces.flatten().shape[0], GL_UNSIGNED_INT, faces.flatten())

        glDisableClientState(GL_VERTEX_ARRAY)
        glDisableClientState(GL_COLOR_ARRAY)
        glDisableClientState(GL_NORMAL_ARRAY)
        glPopMatrix()
        out = glReadPixels(0, 0, disp_sz, disp_sz, GL_RGB, GL_FLOAT)

        return out
Exemplo n.º 4
0
def render(voxels,angle1=45,angle2=10,save=None):
 sz_x,sz_y,sz_z,channels = voxels.shape
 thresh = 0.5

 _verts, faces = measure.marching_cubes(abs(voxels[:,:,:,0]), thresh)
 #_verts,faces = mcubes.marching_cubes(voxels[:,:,:,0],thresh)

 glClearColor(0.0, 0.0, 0.0, 1.0)
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
 glMatrixMode(GL_MODELVIEW);

 if True:
    #print t

    # render a helix (this is similar to the previous example, but 
    # this time we'll render to a texture)

    # initialize projection

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity()

    gluPerspective(90,1,0.01,1000)
    gluLookAt(0,0,20, 0,0,0 ,0,1,0)

    glMatrixMode(GL_MODELVIEW)

    glShadeModel(GL_SMOOTH)

    glPushMatrix()
    glRotatef (angle1, 0.0, 1.0, 0.0); 
    glRotatef (angle2, 0.1, 0.0, 0.0); 

    #glEnable(GL_CULL_FACE)
    glEnable(GL_DEPTH_TEST)
    #glEnable(GL_LIGHTING)
    #glDisable(GL_CULL_FACE)
    #glDisable(GL_DEPTH_TEST)
    # Black background for the Helix
    glClearColor(0.5, 0.5, 0.5, 1.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    # Fallback to white


    #lightZeroPosition = [0.,50.,-2.,1.]
    #lightZeroColor = [1.8,1.0,0.8,1.0] #green tinged
    #glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
    #glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
    #glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
    #glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
    #glEnable(GL_LIGHT0)
    # The helix

    #color = [1.0,0.,0.,1.]
    #glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
    glBegin(GL_TRIANGLES);

    color_idx = np.asarray(_verts,dtype=int)


    colors = abs(voxels[color_idx[:,0],color_idx[:,1],color_idx[:,2],1:])
    colors = np.clip(colors,0,1)
    colors = hsv_to_rgb(colors)

    verts = _verts - numpy.array((sz_x/2,sz_y/2,sz_z/2))
    #Create an indexed view into the vertex array using the array of three indices for triangles
    tris = verts[faces]
    tricols = colors[faces]

    #print faces.shape
    if save!=None:
     saveply.save(save,verts,colors,faces)  
    #Calculate the normal for all the triangles, by taking the cross product of the vectors v1-v0, and v2-v0 in each triangle             
    n = numpy.cross( tris[::,1 ] - tris[::,0]  , tris[::,2 ] - tris[::,0] )
    # n is now an array of normals per triangle. The length of each normal is dependent the vertices, 
    # we need to normalize these, so that our next step weights each normal equally.
    n=normalize_v3(n)
     
    #verts, faces = measure.marching_cubes(abs(voxels[:,:,:,0]), thresh)
    f_idx=0
    for tri in tris:
            #glNormal3f(*(n[f_idx]))
            colors=tricols[f_idx]
            f_idx+=1
            for k in xrange(3):
             #k.reverse()
             #print verts[k]
             #color = voxels[ints[0],ints[1],ints[2],1:] 
             #color = hsv_to_rgb(*color) #hsv_to_rgb(color)
             #print color
             glColor3f(*colors[k])
             glVertex3f( *tri[k])
             
    glEnd();
    glPopMatrix()
    # do not render to texture anymore - "switch off" rtt
    out = glReadPixels(0,0,512,512,GL_RGB,GL_FLOAT)
    return out