Exemple #1
0
    def __init__(self, src, actor=None):
        Component.__init__(self, actor)

        # TODO Include a mesh name (e.g. 'Dragon') as ID as well as src (e.g. '../res/models/Dragon.obj')
        self.src = src
        self.filepath = Context.getInstance().getResourcePath('models', src)
        
        # OpenGL version-dependent code (NOTE assumes major version = 3)
        self.vao = None
        if Context.getInstance().GL_version_minor > 0:  # 3.1 (or greater?)
          self.vao = glGenVertexArrays(1)
        else:  # 3.0 (or less?)
          self.vao = GLuint(0)
          glGenVertexArrays(1, self.vao)
        glBindVertexArray(self.vao)

        self.loadModel(self.filepath)

        self.vbo = VBO(self.meshData, GL_STATIC_DRAW)
        self.vbo.bind()

        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*4, self.vbo+0)

        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*4, self.vbo+12)
        
        self.ebo = glGenBuffers(1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(self.elements)*4, self.elements, GL_STATIC_DRAW)
Exemple #2
0
    def __init__(self, src, actor=None):
        Component.__init__(self, actor)

        # TODO Include a mesh name (e.g. 'Dragon') as ID as well as src (e.g. '../res/models/Dragon.obj')
        self.src = src
        self.filepath = Context.getInstance().getResourcePath('models', src)

        # OpenGL version-dependent code (NOTE assumes major version = 3)
        self.vao = None
        if Context.getInstance().GL_version_minor > 0:  # 3.1 (or greater?)
            self.vao = glGenVertexArrays(1)
        else:  # 3.0 (or less?)
            self.vao = GLuint(0)
            glGenVertexArrays(1, self.vao)
        glBindVertexArray(self.vao)

        self.loadModel(self.filepath)

        self.vbo = VBO(self.meshData, GL_STATIC_DRAW)
        self.vbo.bind()

        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * 4, self.vbo + 0)

        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * 4, self.vbo + 12)

        self.ebo = glGenBuffers(1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.ebo)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                     len(self.elements) * 4, self.elements, GL_STATIC_DRAW)
Exemple #3
0
    def make_solid_obj(self, fn, spacing):

        mesh_object = MeshObject(fn, spacing)
        glBindVertexArray(mesh_object.vao)

        glEnableVertexAttribArray(self.shader.get_attrib("position"))
        glVertexAttribPointer(self.shader.get_attrib("position"),
                              3,
                              GL_FLOAT,
                              False,
                              self.stride,
                              mesh_object.vtVBO)

        glEnableVertexAttribArray(self.shader.get_attrib("normal"))
        glVertexAttribPointer(
            self.shader.get_attrib("normal"),
            3, GL_FLOAT, False, self.stride, mesh_object.vtVBO+12
            )

        glEnableVertexAttribArray(self.shader.get_attrib("color"))
        glVertexAttribPointer(
            self.shader.get_attrib("color"),
            3, GL_FLOAT, False, self.stride, mesh_object.vtVBO+24
            )

        glBindVertexArray(0)

        self.solid_objects.append(mesh_object)
Exemple #4
0
    def make_volume_obj(self, stack, spacing):

        volume_object = VolumeObject(stack, spacing)

        glEnableVertexAttribArray(self.b_shader.get_attrib("position"))
        glVertexAttribPointer(self.b_shader.get_attrib("position"),
                              3,
                              GL_FLOAT,
                              False,
                              self.volume_stride,
                              volume_object.vtVBO)

        glEnableVertexAttribArray(self.b_shader.get_attrib("texcoord"))
        glVertexAttribPointer(
            self.b_shader.get_attrib("texcoord"),
            3, GL_FLOAT, False, self.volume_stride, volume_object.vtVBO+12
            )

        glBindVertexArray(0)
        glDisableVertexAttribArray(self.b_shader.get_attrib("position"))
        glDisableVertexAttribArray(self.b_shader.get_attrib("texcoord"))

        glBindBuffer(GL_ARRAY_BUFFER, 0)

        self.volume_objects.append(volume_object)
Exemple #5
0
    def __init__(self, stack, spacing):
        self.active = True
        self.stack_object = StackObject(stack, spacing)
        shape = self.stack_object.shape

        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)

        tl = np.array((shape[2]*spacing[0],   # x
                       shape[1]*spacing[1],   # y
                       shape[0]*spacing[2]))  # z

        # Vertex buffer: corners of cube.
        # x, y, z, texture_x, texture_y, texture_z
        vb = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],  # Corner 0.
              [tl[0], 0.0, 0.0, 1.0, 0.0, 0.0],
              [0.0, tl[1], 0.0, 0.0, 1.0, 0.0],
              [tl[0], tl[1], 0.0, 1.0, 1.0, 0.0],
              [0.0, 0.0, tl[2], 0.0, 0.0, 1.0],
              [tl[0], 0.0, tl[2], 1.0, 0.0, 1.0],
              [0.0, tl[1], tl[2], 0.0, 1.0, 1.0],
              [tl[0], tl[1], tl[2], 1.0, 1.0, 1.0]]  # Corner 7.

        vb = np.array(vb, dtype=np.float32)
        vb = vb.flatten()

        # Triangles of cube.
        idx_out = np.array([[0, 2, 1], [2, 3, 1],  # Triangle 0, triangle 1.
                            [1, 4, 0], [1, 5, 4],
                            [3, 5, 1], [3, 7, 5],
                            [2, 7, 3], [2, 6, 7],
                            [0, 6, 2], [0, 4, 6],
                            [5, 6, 4], [5, 7, 6]],  # Triangle 10, triangle 11.
                           dtype=np.uint32)

        self.vtVBO = VBO(vb)
        self.vtVBO.bind()

        sc = 1.0/la.norm(tl)
        c = 0.5*tl

        self.transform = np.array(((sc, 0.0, 0.0, -sc*c[0]),
                                   (0.0, sc, 0.0, -sc*c[1]),
                                   (0.0, 0.0, sc, -sc*c[2]),
                                   (0.0, 0.0, 0.0, 1.0)))

        self.tex_transform = np.array(((1.0/tl[0], 0.0, 0.0, 0.0),
                                       (0.0, 1.0/tl[1], 0.0, 0.0),
                                       (0.0, 0.0, 1.0/tl[2], 0.0),
                                       (0.0, 0.0, 0.0, 1.0)))

        glBindVertexArray(0)

        self.elVBO = VBO(idx_out, target=GL_ELEMENT_ARRAY_BUFFER)
        self.elCount = len(idx_out.flatten())
Exemple #6
0
    def __init__(self, fn, spacing):
        self.active = True
        m = GLMesh()
        self.mesh = m
        sc = m.load_ply(fn)
        v_out, n_out, col_out, idx_out = m.generate_arrays()

        vb = np.concatenate((v_out, n_out, col_out), axis=1)
        self.elVBO = VBO(idx_out, target=GL_ELEMENT_ARRAY_BUFFER)
        self.elCount = len(idx_out.flatten())

        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)
        self.vtVBO = VBO(vb)
        self.vtVBO.bind()

        glBindVertexArray(0)

        c = np.array((0, 0, 0))
        self.transform = np.array(((sc, 0.0, 0.0, -sc*c[0]),
                                   (0.0, sc, 0.0, -sc*c[1]),
                                   (0.0, 0.0, sc, -sc*c[2]),
                                   (0.0, 0.0, 0.0, 1.0)))
Exemple #7
0
    def _render_solid_obj(self, solid_object, width, height, VMatrix, PMatrix):

        glEnable(GL_DEPTH_TEST)

        glUseProgram(self.shader.program)
        glBindVertexArray(solid_object.vao)

        solid_object.elVBO.bind()

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

        glDisable(GL_CULL_FACE)
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
#       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        glDrawElements(GL_TRIANGLES, solid_object.elCount,
                       GL_UNSIGNED_INT, solid_object.elVBO)

        solid_object.elVBO.unbind()
        glBindVertexArray(0)
        glUseProgram(0)
Exemple #8
0
    def make_volume_obj(self, fn, spacing):

        volume_object = VolumeObject(fn, spacing)
        volume_object.threshold = 15

        glBindVertexArray(volume_object.vao)

        glEnableVertexAttribArray(self.b_shader.get_attrib("position"))
        glVertexAttribPointer(self.b_shader.get_attrib("position"),
                              3,
                              GL_FLOAT,
                              False,
                              self.volume_stride,
                              volume_object.vtVBO)

        glEnableVertexAttribArray(self.b_shader.get_attrib("texcoord"))
        glVertexAttribPointer(
            self.b_shader.get_attrib("texcoord"),
            3, GL_FLOAT, False, self.volume_stride, volume_object.vtVBO+12
            )

        glBindVertexArray(0)

        self.volume_objects.append(volume_object)
Exemple #9
0
def init():
    initialize_program()
    initialize_vertex_buffer()
    glBindVertexArray( glGenVertexArray() )
Exemple #10
0
 def render(self):
     glBindVertexArray(self.vao)
     glDrawElements(GL_TRIANGLES, self.elements.size, GL_UNSIGNED_INT, None)
Exemple #11
0
def init():
    initialize_program()
    initialize_vertex_buffer()
    glBindVertexArray(glGenVertexArray())
Exemple #12
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)
Exemple #13
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)
Exemple #14
0
 def render(self):
     glBindVertexArray(self.vao)
     glDrawElements(GL_TRIANGLES, self.elements.size, GL_UNSIGNED_INT, None)