Ejemplo n.º 1
0
    def draw(self):
        '''Draw the mesh on screen (using display list if compiled)'''
        if self.list:
            glCallList(self.list)
            return

        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT)
        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)
        for group in self.groups:
            if group.material:
                group.material.apply()
            if group.array is None:
                if group.material and group.material.texture:
                    if group.material.texture.rectangle:
                        # texture is a rectangle texture
                        # that's mean we need to adjust the range of texture
                        # coordinate from original 0-1 to 0-width/0-height
                        group.vertices[0::8] = map(
                            lambda x: x * group.material.texture.width,
                            group.vertices[0::8]
                        )
                        group.vertices[1::8] = map(
                            lambda x: x * group.material.texture.height,
                            group.vertices[1::8]
                        )
                group.array = (GLfloat * len(group.vertices))(*group.vertices)
                group.triangles = len(group.vertices) / 8
            glInterleavedArrays(GL_T2F_N3F_V3F, 0, group.array)
            glDrawArrays(GL_TRIANGLES, 0, group.triangles)
            if group.material:
                group.material.unapply()
        glPopAttrib()
        glPopClientAttrib()
Ejemplo n.º 2
0
    def init_opengl(self):
        """ initialize the opengl settings to render the scene """

        self.inverseModelView = numpy.identity(4)

        self.modelView = numpy.identity(4)

        glEnable(GL_CULL_FACE)

        glCullFace(GL_BACK)

        glEnable(GL_DEPTH_TEST)

        glDepthFunc(GL_LESS)

        glEnable(GL_LIGHT0)

        glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0))

        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1))

        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)

        glEnable(GL_COLOR_MATERIAL)

        glClearColor(0.4, 0.4, 0.4, 0.0)
    def init_opengl(self):
        """ 初始化opengl的配置 """
        #模型视图矩阵
        self.inverseModelView = numpy.identity(4)
        #模型视图矩阵的逆矩阵
        self.modelView = numpy.identity(4)

        #开启剔除操作效果
        glEnable(GL_CULL_FACE)
        #取消对多边形背面进行渲染的计算(看不到的部分不渲染)
        glCullFace(GL_BACK)
        #开启深度测试
        glEnable(GL_DEPTH_TEST)
        #测试是否被遮挡,被遮挡的物体不予渲染
        glDepthFunc(GL_LESS)
        #启用0号光源
        glEnable(GL_LIGHT0)
        #设置光源的位置
        glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0))
        #设置光源的照射方向
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1))
        #设置材质颜色
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)
        #设置清屏的颜色
        glClearColor(0.4, 0.4, 0.4, 0.0)
Ejemplo n.º 4
0
    def draw(self,
             camera_matrix: numpy.ndarray,
             camera_position: PointCoordinatesAny = None):
        """
        Draw the selection box
        :param camera_matrix: 4x4 transformation matrix for the camera
        :param camera_position: The position of the camera. Used to flip draw direction if camera inside box.
        :return:
        """
        self._setup()
        if self._rebuild:
            self._create_geometry()
        self._draw_mode = GL_TRIANGLES

        transformation_matrix = numpy.matmul(camera_matrix,
                                             self.transformation_matrix)

        glPushAttrib(GL_DEPTH_BUFFER_BIT
                     | GL_POLYGON_BIT)  # store opengl state

        if camera_position is not None and camera_position in self:
            glCullFace(GL_FRONT)
        else:
            glCullFace(GL_BACK)

        self.draw_start = 0
        self.draw_count = 36
        super()._draw(transformation_matrix)

        # draw the lines around the boxes
        glDisable(GL_DEPTH_TEST)
        self._draw_mode = GL_LINE_STRIP
        super()._draw(transformation_matrix)

        glPopAttrib()  # reset to starting state
Ejemplo n.º 5
0
    def init_opengl(self):

        #model view matrix
        self.inverseModelView = numpy.identity(4)
        #its anti-matrix
        self.modelView = numpy.identity(4)

        #open tichu effect
        glEnable(GL_CULL_FACE)
        #to not rend invisible part
        glCullFace(GL_BACK)
        #open depth test
        glEnable(GL_DEPTH_TEST)
        #objects being covered ot to rend
        glDepthFunc(GL_LESS)
        #open light 0
        glEnable(GL_LIGHT0)
        #to set the position of light
        glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0))
        #to set the direction that light sheds at
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1))
        #to set material color
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)
        #set the color of a clear-screen
        glClearColor(0.4, 0.4, 0.4, 0.0)
Ejemplo n.º 6
0
    def draw(self):
        '''Draw the mesh on screen (using display list if compiled)'''
        if self.list:
            glCallList(self.list)
            return

        glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
        glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT)
        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)
        for group in self.groups:
            if group.material:
                group.material.apply()
            if group.array is None:
                if group.material and group.material.texture:
                    if group.material.texture.rectangle:
                        # texture is a rectangle texture
                        # that's mean we need to adjust the range of texture
                        # coordinate from original 0-1 to 0-width/0-height
                        group.vertices[0::8] = map(
                            lambda x: x * group.material.texture.width,
                            group.vertices[0::8])
                        group.vertices[1::8] = map(
                            lambda x: x * group.material.texture.height,
                            group.vertices[1::8])
                group.array = (GLfloat * len(group.vertices))(*group.vertices)
                group.triangles = len(group.vertices) / 8
            glInterleavedArrays(GL_T2F_N3F_V3F, 0, group.array)
            glDrawArrays(GL_TRIANGLES, 0, group.triangles)
            if group.material:
                group.material.unapply()
        glPopAttrib()
        glPopClientAttrib()
Ejemplo n.º 7
0
Archivo: View.py Proyecto: fossabot/mfm
    def __enable_depth_test(self):
        """Enable depth test and faces culling.

        Needed to not render invisible vertices and triangles.
        """
        glDepthMask(GL_TRUE)
        glDepthFunc(GL_LESS)
        glCullFace(GL_FRONT)

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_STENCIL_TEST)
Ejemplo n.º 8
0
Archivo: View.py Proyecto: char-lie/mfm
    def __enable_depth_test(self):
        """Enable depth test and faces culling.

        Needed to not render invisible vertices and triangles.
        """
        glDepthMask(GL_TRUE)
        glDepthFunc(GL_LESS)
        glCullFace(GL_FRONT)

        glEnable(GL_DEPTH_TEST)
        glEnable(GL_STENCIL_TEST)
Ejemplo n.º 9
0
 def __generate_model(self):
     """Generate rotated model with shadows."""
     glEnable(GL_CULL_FACE)
     glCullFace(GL_FRONT)
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     self.__sh.change_shader(vertex=0, fragment=0)
     self.__prepare_shaders(self.__model_matrix, self.__light_matrix, False)
     self.__sh.bind_buffer()
     self.__sh.use_shaders()
     glDrawElements(GL_TRIANGLES, View.__triangles.size, GL_UNSIGNED_SHORT,
                    View.__triangles)
     self.__sh.clear()
Ejemplo n.º 10
0
 def draw(self, camera_matrix: numpy.ndarray):
     """Draw all of the levels."""
     for level, transform, is_mirrored in zip(self._objects,
                                              self._transformation_matrices,
                                              self._is_mirrored):
         glPushAttrib(GL_POLYGON_BIT)  # store opengl state
         if is_mirrored:
             glCullFace(GL_FRONT)
         else:
             glCullFace(GL_BACK)
         level.draw(numpy.matmul(camera_matrix, transform))
         glPopAttrib()  # reset to starting state
Ejemplo n.º 11
0
 def render(self,mode='OPAQUE'):
     opaque_pieces = self._style.get('opaque-pieces',())
     if opaque_pieces == True or not self._has_transparent:
         opaque_pieces = self.pieces
     glEnable(GL_CULL_FACE)
     if mode == 'OPAQUE':
         if self._has_transparent:
             return  # can't guarantee opaque render
         pieces = opaque_pieces
         glCullFace(GL_BACK)
         self.tnv.prepare()
         for piece in pieces:
             self.tnv.drawpiece(piece)
     elif mode == 'TRANSPARENT':
         glEnable(GL_BLEND)  # just make sure
         pieces = list(set(self.pieces) - set(opaque_pieces))
         glCullFace(GL_FRONT) # draw backfaces first
         self.tnv.prepare()
         for piece in pieces:
             self.tnv.drawpiece(piece)
         glCullFace(GL_BACK) # draw front faces over back faces
         for piece in pieces:
             self.tnv.drawpiece(piece)
     elif mode == 'PICK': #PICK
         glCullFace(GL_BACK)
         pieces = self.pieces
         self.tnv.prepare()
         for piece in pieces:
             picking.label((self.__class__.__name__, self._name, piece))
             self.tnv.drawpiece(piece)
         picking.nolabel()
     else:
         raise ValueError("render mode %s?" % mode)
Ejemplo n.º 12
0
 def initialize_draw(self):
     self.quadric = gluNewQuadric()
     gluQuadricNormals(self.quadric, GLU_SMOOTH)
     self.set_specular(True)
     self.set_bright(False)
     glLight(GL_LIGHT0, GL_SPECULAR, [0.7, 0.7, 0.7, 1.0])
     glLight(GL_LIGHT0, GL_AMBIENT, [0.1, 0.1, 0.1, 1.0])
     glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 3.0, 0.0])
     glEnable(GL_LIGHT0)
     glEnable(GL_LIGHTING)
     glDepthFunc(GL_LESS)
     glEnable(GL_DEPTH_TEST)
     glCullFace(GL_BACK)
     VisBackend.initialize_draw(self)
     self.tool.initialize_gl()
Ejemplo n.º 13
0
 def initialize_draw(self):
     self.quadric = gluNewQuadric()
     gluQuadricNormals(self.quadric, GLU_SMOOTH)
     self.set_specular(True)
     self.set_bright(False)
     glLight(GL_LIGHT0, GL_SPECULAR, [0.7, 0.7, 0.7, 1.0])
     glLight(GL_LIGHT0, GL_AMBIENT, [0.1, 0.1, 0.1, 1.0])
     glLight(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 3.0, 0.0])
     glEnable(GL_LIGHT0)
     glEnable(GL_LIGHTING)
     glDepthFunc(GL_LESS)
     glEnable(GL_DEPTH_TEST)
     glCullFace(GL_BACK)
     VisBackend.initialize_draw(self)
     self.tool.initialize_gl()
Ejemplo n.º 14
0
    def gl_setup(self, width, height):
        """Private."""
        # cut out invisible faces
        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)

        # enable depth buffer
        glEnable(GL_DEPTH_TEST)

        # enable alpha-blending
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        # clear to black
        glClearColor(0.3, 0.3, 0.3, 1)
Ejemplo n.º 15
0
    def init_opengl(self):
        """ initialize the opengl settings to render the scene """
        self.inverseModelView = numpy.identity(4)
        self.modelView = numpy.identity(4)

        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)
        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LESS)

        glEnable(GL_LIGHT0)
        glLightfv(GL_LIGHT0, GL_POSITION, GLfloat_4(0, 0, 1, 0))
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, GLfloat_3(0, 0, -1))

        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
        glEnable(GL_COLOR_MATERIAL)
        glClearColor(1.0, 1.0, 1.0, 0.0)
Ejemplo n.º 16
0
    def __init__(self,
                 display_width: int,
                 display_height: int,
                 camera: Camera,
                 pixel_size: float = 0.5,
                 projection: str = '3d') -> None:
        super().__init__()

        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)

        # I haven't implemented yet
        self.__projection: str = projection
        self.__camera: Camera = camera

        self.__renderers: Dict[str, AbstractRenderer] = {}
        self.__projection_matrix = self.__create_orthographic_projection_matrix(
            display_width, display_height, pixel_size)
Ejemplo n.º 17
0
def render_to_png(filename,
                  callback,
                  obb,
                  model_matrix=(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                                1)):
    from pygame import init, display, quit
    from pygame.constants import OPENGL, DOUBLEBUF
    from OpenGL.GL import glLightfv, glCullFace, glEnable, glShadeModel, glMatrixMode, glLoadIdentity, glClear, \
        glLoadMatrixf, glPolygonMode, glCallList, glReadPixels, GL_LIGHT0, GL_POSITION, GL_AMBIENT, GL_DIFFUSE, \
        GL_BACK, GL_LIGHTING, GL_COLOR_MATERIAL, GL_DEPTH_TEST, GL_SMOOTH, GL_PROJECTION, GL_MODELVIEW, \
        GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_FRONT_AND_BACK, GL_FILL, GL_LINE, GL_BGR, GL_UNSIGNED_BYTE
    from OpenGL.GLU import gluPerspective
    from cv2 import imwrite
    from numpy import frombuffer, uint8
    init()
    viewport = (800, 600)
    display.set_mode(viewport, OPENGL | DOUBLEBUF)
    glLightfv(GL_LIGHT0, GL_POSITION, (0, -1, 0, 0))
    glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1))
    glCullFace(GL_BACK)
    glEnable(GL_LIGHT0)
    glEnable(GL_LIGHTING)
    glEnable(GL_COLOR_MATERIAL)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    width, height = viewport
    gluPerspective(90.0, width / float(height), 0.1, 100.0)
    glEnable(GL_DEPTH_TEST)
    glMatrixMode(GL_MODELVIEW)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    glLoadMatrixf(model_matrix)
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
    glCallList(callback())
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
    glCallList(create_obb_gl_list(obb))
    img_data = glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE)
    img = frombuffer(img_data, dtype=uint8)
    img = img.reshape((height, width, 3))
    imwrite(filename, img)
    quit()
    def draw(
        self, camera_matrix: numpy.ndarray, camera_position: PointCoordinatesAny = None
    ):
        """
        Draw the selection box
        :param camera_matrix: 4x4 transformation matrix for the camera
        :param camera_position: The position of the camera. Used to flip draw direction if camera inside box.
        :return:
        """
        self._setup()
        if self._rebuild:
            self._create_geometry()

        transformation_matrix = numpy.matmul(camera_matrix, self.transformation_matrix)

        if camera_position is not None and camera_position in self:
            glCullFace(GL_FRONT)
        else:
            glCullFace(GL_BACK)

        self._draw_mode = GL_TRIANGLES
        self.draw_start = 0
        self.draw_count = 108
        super()._draw(transformation_matrix)
        glCullFace(GL_BACK)

        # draw the lines around the boxes
        glDisable(GL_DEPTH_TEST)
        self._draw_mode = GL_LINE_STRIP
        self.draw_count = 36
        for start in range(0, 3 * 36, 36):
            # these must be drawn individually otherwise there will be lines connecting them.
            self.draw_start = start
            super()._draw(transformation_matrix)
        glEnable(GL_DEPTH_TEST)
Ejemplo n.º 19
0
    def draw(self,
             camera_matrix: numpy.ndarray,
             camera_position: PointCoordinatesAny = None):
        """
        Draw the selection box
        :param camera_matrix: 4x4 transformation matrix for the camera
        :param camera_position: The position of the camera. Used to flip draw direction if camera inside box.
        :return:
        """
        self._setup()
        if self._rebuild:
            self._create_geometry()

        transformation_matrix = numpy.matmul(camera_matrix,
                                             self.transformation_matrix)

        # draw the lines around the boxes
        self.draw_start = 0
        self.draw_count = 36
        glDisable(GL_DEPTH_TEST)
        self._draw_mode = GL_LINE_STRIP
        super()._draw(transformation_matrix)
        glEnable(GL_DEPTH_TEST)

        if camera_position is not None and camera_position in self:
            glCullFace(GL_FRONT)
        else:
            glCullFace(GL_BACK)

        self._draw_mode = GL_TRIANGLES
        self.draw_start = 36
        # 6 faces, 9 quads/face, 2 triangles/quad, 3 verts/triangle
        self.draw_count = 324
        super()._draw(transformation_matrix)
        glCullFace(GL_BACK)
Ejemplo n.º 20
0
 def draw(self, camera_matrix: numpy.ndarray):
     """Draw all of the levels."""
     for level, transform, is_mirrored in zip(
         self._objects, self._transformation_matrices, self._is_mirrored
     ):
         cull_state = glGetIntegerv(GL_CULL_FACE_MODE)
         if is_mirrored:
             glCullFace(GL_FRONT)
         else:
             glCullFace(GL_BACK)
         level.draw(numpy.matmul(camera_matrix, transform))
         glCullFace(cull_state)
Ejemplo n.º 21
0
    def draw(self,
             camera_matrix: numpy.ndarray,
             camera_position: PointCoordinatesAny = None):
        """
        Draw the selection box
        :param camera_matrix: 4x4 transformation matrix for the camera
        :param camera_position: The position of the camera. Used to flip draw direction if camera inside box.
        :return:
        """
        self._setup()
        if self._needs_rebuild:
            self._create_geometry()
        self._draw_mode = GL_TRIANGLES

        transformation_matrix = numpy.matmul(camera_matrix,
                                             self.transformation_matrix)

        depth_state = glGetBooleanv(GL_DEPTH_TEST)
        cull_state = glGetIntegerv(GL_CULL_FACE_MODE)

        if camera_position is not None and camera_position in self:
            if cull_state == GL_BACK:
                glCullFace(GL_FRONT)
            elif cull_state == GL_FRONT:
                glCullFace(GL_BACK)

        self.draw_start = 0
        self.draw_count = 36
        super()._draw(transformation_matrix)

        # draw the lines around the boxes
        if depth_state:
            glDisable(GL_DEPTH_TEST)
        self._draw_mode = GL_LINE_STRIP
        super()._draw(transformation_matrix)
        if depth_state:
            glEnable(GL_DEPTH_TEST)

        glCullFace(cull_state)
Ejemplo n.º 22
0
    def processFrame(self, timePassedSecs):
        """ draws a scene """

        # update the model view matrix
        self.sceneCamera.updateKeys()
        self.sceneCamera.update()

        # process queued GLObject events
        GLObject.signalsEmit()

        # enable some default scene states
        glEnable(GL_DEPTH_TEST)

        ######## SHADOW MAP RENDERING START ########

        # offset the geometry slightly to prevent z-fighting
        # note that this introduces some light-leakage artifacts
        glEnable(GL_POLYGON_OFFSET_FILL)
        glPolygonOffset(1.1, 4096.0)

        # cull front faces for shadow rendering,
        # this moves z-fighting to backfaces.
        glCullFace(GL_FRONT)
        # enable depth rendering shader.
        # FIXME: support segment geometry shader!
        #          geometry shader could change the shadow shape!
        self.depthShader.enable()

        map(_updateLightShadowMap, self.lights)
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glDisable(GL_POLYGON_OFFSET_FILL)
        ######## SHADOW MAP RENDERING STOP ########

        #### TODO: FOG: integrate FOG ####
        glEnable(GL_FOG)
        glFogi(GL_FOG_MODE, GL_EXP2)
        # approximate the atmosphere's filtering effect as a linear function
        sunDir = array([4.0, 4.0, 4.0, 0.0], float32)  # TODO: FOG: what is the sun dir ?
        skyColor = array([0.8, sunDir[1] * 0.1 + 0.7, sunDir[1] * 0.4 + 0.5, 1.0], float32)
        glClearColor(*skyColor)
        glFogf(GL_FOG_DENSITY, 0.4)
        glFogf(GL_FOG_START, 16.0)
        glFogf(GL_FOG_END, self.farClip)
        glFogfv(GL_FOG_COLOR, skyColor)

        # fill projection matrix
        glMatrixMode(GL_PROJECTION)
        glLoadMatrixf(self.projectionMatrix)
        glMatrixMode(GL_MODELVIEW)

        # draw stuff in 3d projection
        lastCam = None
        glPushAttrib(GL_COLOR_BUFFER_BIT)
        for (fbo, draw, cam) in self.sceneFBOS:
            # render to fbo
            fbo.enable()
            if cam != lastCam:
                cam.enable()
                lastCam = cam
            draw()
        # disable render to texture
        FBO.disable()
        glPopAttrib()
        glViewport(0, 0, self.winSize[0], self.winSize[1])

        #### TODO: FOG: integrate FOG ####
        glDisable(GL_FOG)

        # change to orthogonal projection
        glMatrixMode(GL_PROJECTION)
        glLoadMatrixf(self.orthoMatrix)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        # no depth test needed in orthogonal rendering
        glDisable(GL_DEPTH_TEST)

        # draw orthogonal to the screen
        self.orthogonalPass()
 def draw_box_rc(self):
     # decide which face to render (@Todo: ModernGL needs a way to handle this)
     glEnable(GL_CULL_FACE)
     glCullFace(GL_BACK)
     self.vao_rc.render()
     glDisable(GL_CULL_FACE)
Ejemplo n.º 24
0
    def _render_volume_obj(self, volume_object, width, height, VMatrix, PMatrix):

        glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)
        glViewport(0, 0, width, height)
        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_3D, volume_object.stack_object.stack_texture)

        glClear(GL_COLOR_BUFFER_BIT)  # Clear back buffer.

        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)

        glUseProgram(self.b_shader.program)

        glBindVertexArray(volume_object.vao)
        volume_object.elVBO.bind()

        mv_matrix = np.dot(VMatrix, volume_object.transform)
        glUniformMatrix4fv(self.b_shader.get_uniform("mv_matrix"),
                           1, True, mv_matrix.astype('float32'))
        glUniformMatrix4fv(self.b_shader.get_uniform("p_matrix"),
                           1, True, PMatrix.astype('float32'))

        glDrawElements(GL_TRIANGLES, volume_object.elCount,
                       GL_UNSIGNED_INT, volume_object.elVBO)

        volume_object.elVBO.unbind()
        glBindVertexArray(0)
        glUseProgram(0)

        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glActiveTexture(GL_TEXTURE0 + 1)
        glBindTexture(GL_TEXTURE_2D, self.bfTex)

        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_3D, volume_object.stack_object.stack_texture)

        glUseProgram(self.f_shader.program)

        glUniform1i(self.f_shader.get_uniform("texture3s"), 0)
        glUniform1i(self.f_shader.get_uniform("backfaceTex"), 1)

        tex_inv_matrix = np.dot(PMatrix,
                                np.dot(mv_matrix,
                                       la.inv(volume_object.tex_transform)))
        glUniformMatrix4fv(self.f_shader.get_uniform('tex_inv_matrix'),
                           1,
                           True,
                           tex_inv_matrix.astype('float32'))

        glUniform1f(self.f_shader.get_uniform('isolevel'),
                    volume_object.threshold/255.0)

        glEnable(GL_CULL_FACE)
        glCullFace(GL_FRONT)

        glBindVertexArray(volume_object.vao)
        volume_object.elVBO.bind()

        glUniformMatrix4fv(self.f_shader.get_uniform("mv_matrix"),
                           1, True, mv_matrix.astype('float32'))
        glUniformMatrix4fv(self.f_shader.get_uniform("p_matrix"),
                           1, True, PMatrix.astype('float32'))

        glDrawElements(GL_TRIANGLES, volume_object.elCount,
                       GL_UNSIGNED_INT, volume_object.elVBO)

        glActiveTexture(GL_TEXTURE0+1)
        glBindTexture(GL_TEXTURE_2D, 0)

        glCullFace(GL_BACK)
        volume_object.elVBO.unbind()
        glBindVertexArray(0)
        glUseProgram(0)
Ejemplo n.º 25
0
    def _render_volume_obj(self, volume_object, width, height, VMatrix, PMatrix):

        glBindFramebuffer(GL_FRAMEBUFFER, self.fbo)
        glViewport(0, 0, width, height)
        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_3D, volume_object.stack_object.stack_texture)

        glClear(GL_COLOR_BUFFER_BIT)  # Clear back buffer.

        glEnable(GL_CULL_FACE)
        glCullFace(GL_FRONT)  # NB flipped

#        glValidateProgram(self.b_shader.program)
#        logging.debug("b_valid ", glGetProgramiv(self.b_shader.program,
#                                         GL_VALIDATE_STATUS))
#        logging.debug(glGetProgramInfoLog(self.b_shader.program).decode())

        glUseProgram(self.b_shader.program)

        glBindVertexArray(volume_object.vao)

        volume_object.elVBO.bind()

        mv_matrix = np.dot(VMatrix, volume_object.transform)
        glUniformMatrix4fv(self.b_shader.get_uniform("mv_matrix"),
                           1, True, mv_matrix.astype('float32'))
        glUniformMatrix4fv(self.b_shader.get_uniform("p_matrix"),
                           1, True, PMatrix.astype('float32'))

        glDrawElements(GL_TRIANGLES, volume_object.elCount,
                       GL_UNSIGNED_INT, volume_object.elVBO)

        volume_object.elVBO.unbind()
        glBindVertexArray(0)
        glUseProgram(0)

        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glActiveTexture(GL_TEXTURE0 + 1)
        glBindTexture(GL_TEXTURE_2D, self.bfTex)

        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_3D, volume_object.stack_object.stack_texture)

        glUseProgram(self.f_shader.program)

        glUniform1i(self.f_shader.get_uniform("texture3s"), 0)
        glUniform1i(self.f_shader.get_uniform("backfaceTex"), 1)

        glEnable(GL_CULL_FACE)
        glCullFace(GL_BACK)

        glBindVertexArray(volume_object.vao)
        volume_object.elVBO.bind()

        glUniformMatrix4fv(self.f_shader.get_uniform("mv_matrix"),
                           1, True, mv_matrix.astype('float32'))
        glUniformMatrix4fv(self.f_shader.get_uniform("p_matrix"),
                           1, True, PMatrix.astype('float32'))

        glDrawElements(GL_TRIANGLES, volume_object.elCount,
                       GL_UNSIGNED_INT, volume_object.elVBO)

        glActiveTexture(GL_TEXTURE0+1)
        glBindTexture(GL_TEXTURE_2D, 0)

        glCullFace(GL_BACK)
        volume_object.elVBO.unbind()
        glBindVertexArray(0)
        glUseProgram(0)