Ejemplo n.º 1
0
    def __init__(self, attributes, bone_nodes, bone_offsets, index=None):

        # setup shader attributes for linear blend skinning shader
        self.vertex_array = VertexArray(attributes, index)

        # feel free to move this up in Viewer as shown in previous practicals
        self.skinning_shader = Shader(SKINNING_VERT, SIMPLE_COLOR_FRAG)

        # store skinning data
        self.bone_nodes = bone_nodes
        self.bone_offsets = bone_offsets
Ejemplo n.º 2
0
class TexturedPlane:
    """ Simple first textured object """
    def __init__(self, file):
        # feel free to move this up in the viewer as per other practicals
        self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG)

        # triangle and face buffers
        vertices = 100 * np.array(
            ((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)), np.float32)
        faces = np.array(((0, 1, 2), (0, 2, 3)), np.uint32)
        self.vertex_array = VertexArray([vertices], faces)

        # interactive toggles
        self.wrap = cycle([
            GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER,
            GL.GL_CLAMP_TO_EDGE
        ])
        self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
                             (GL.GL_LINEAR, GL.GL_LINEAR),
                             (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)
        self.file = file

        # setup texture and upload it to GPU
        self.texture = Texture(file, self.wrap_mode, *self.filter_mode)

    def draw(self, projection, view, model, win=None, **_kwargs):

        # some interactive elements
        if glfw.get_key(win, glfw.KEY_F6) == glfw.PRESS:
            self.wrap_mode = next(self.wrap)
            self.texture = Texture(self.file, self.wrap_mode,
                                   *self.filter_mode)

        if glfw.get_key(win, glfw.KEY_F7) == glfw.PRESS:
            self.filter_mode = next(self.filter)
            self.texture = Texture(self.file, self.wrap_mode,
                                   *self.filter_mode)
        GL.glUseProgram(self.shader.glid)

        # projection geometry
        loc = GL.glGetUniformLocation(self.shader.glid, 'modelviewprojection')
        GL.glUniformMatrix4fv(loc, 1, True, projection @ view @ model)

        # texture access setups
        loc = GL.glGetUniformLocation(self.shader.glid, 'diffuseMap')
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid)
        GL.glUniform1i(loc, 0)
        self.vertex_array.draw(GL.GL_TRIANGLES)

        # leave clean state for easier debugging
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glUseProgram(0)
Ejemplo n.º 3
0
    def __init__(self,
                 max_particles=50,
                 width=1,
                 height=15,
                 density=10):  # density is the ratio width/particle_size
        super().__init__()

        self.max_particles = max_particles
        self.width = width
        self.height = height
        self.density = density

        self.color_shader = Shader(UNIFORM_COLOR_VERT, UNIFORM_COLOR_FRAG)

        # creating the small cube for a single particle
        self.particle = [(-0.5, -0.5, -0.5), (0.5, -0.5, -0.5),
                         (0.5, 0.5, -0.5), (-0.5, 0.5, -0.5),
                         (-0.5, -0.5, 0.5), (0.5, -0.5, 0.5), (0.5, 0.5, 0.5),
                         (-0.5, 0.5, 0.5)]
        self.particle_quad = VertexArray(
            [self.particle],
            (0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 0, 4, 3, 3, 4, 7, 1, 2, 5, 2,
             6, 5, 2, 3, 7, 2, 7, 6, 0, 1, 4, 1, 5, 4))

        self.particles = np.empty([max_particles], Particle)
        for i in range(max_particles):
            self.particles[i] = self.new_particle()
            self.add(self.particles[i])

        self.last_time = glfw.get_time()
Ejemplo n.º 4
0
class TexturedMesh:
    def __init__(self, texture, attributes, index=None):
        # feel free to move this up in the viewer as per other practicals
        self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG)

        # triangle and face buffers

        self.vertex_array = VertexArray(attributes, index)

        # interactive toggles
        # self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT,
        #                    GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE])
        # self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
        #                      (GL.GL_LINEAR, GL.GL_LINEAR),
        #                      (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        # self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)

        # setup texture and upload it to GPU
        self.texture = texture

    #def draw(self, projection, view, model, color_shader=None, color=(1, 1, 1, 1), **param):
    def draw(self, projection, view, model, win=None, **_kwargs):

        # some interactive elements
        # if glfw.get_key(win, glfw.KEY_F6) == glfw.PRESS:
        #     self.wrap_mode = next(self.wrap)
        #     self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode)
        #
        # if glfw.get_key(win, glfw.KEY_F7) == glfw.PRESS:
        #     self.filter_mode = next(self.filter)
        #     self.texture = Texture(self.file, self.wrap_mode, *self.filter_mode)
        GL.glUseProgram(self.shader.glid)

        # projection geometry
        loc = GL.glGetUniformLocation(self.shader.glid, 'modelviewprojection')
        GL.glUniformMatrix4fv(loc, 1, True, projection @ view @ model)

        # texture access setups
        loc = GL.glGetUniformLocation(self.shader.glid, 'diffuseMap')
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid)
        GL.glUniform1i(loc, 0)
        self.vertex_array.draw(GL.GL_TRIANGLES)

        # leave clean state for easier debugging
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        GL.glUseProgram(0)
Ejemplo n.º 5
0
class SkinnedMesh:
    """class of skinned mesh nodes in scene graph """
    def __init__(self, attributes, texture, bone_nodes, bone_offsets, index=None):

        # setup shader attributes for linear blend skinning shader
        self.vertex_array = VertexArray(attributes, index)

        # feel free to move this up in Viewer as shown in previous practicals
        self.skinning_shader = Shader(SKINNING_VERT, TEXTURE_FRAG)

        # store skinning data
        self.bone_nodes = bone_nodes
        self.bone_offsets = bone_offsets
        self.texture = texture

    def draw(self, projection, view, _model, **_kwargs):
        """ skinning object draw method """

        shid = self.skinning_shader.glid
        GL.glUseProgram(shid)

        # setup camera geometry parameters
        loc = GL.glGetUniformLocation(shid, 'projection')
        GL.glUniformMatrix4fv(loc, 1, True, projection)
        loc = GL.glGetUniformLocation(shid, 'view')
        GL.glUniformMatrix4fv(loc, 1, True, view)

        # bone world transform matrices need to be passed for skinning
        for bone_id, node in enumerate(self.bone_nodes):
            bone_matrix = node.world_transform @ self.bone_offsets[bone_id]

            bone_loc = GL.glGetUniformLocation(shid, 'boneMatrix[%d]' % bone_id)
            GL.glUniformMatrix4fv(bone_loc, 1, True, bone_matrix)

        # draw mesh vertex array

        loc = GL.glGetUniformLocation(shid, 'diffuseMap')
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture.glid)
        GL.glUniform1i(loc, 0)
        self.vertex_array.draw(GL.GL_TRIANGLES)
        # leave clean state for easier debugging
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)

        # leave with clean OpenGL state, to make it easier to detect problems
        GL.glUseProgram(0)
Ejemplo n.º 6
0
    def __init__(self, texture, attributes, index=None):
        # feel free to move this up in the viewer as per other practicals
        self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG)

        # triangle and face buffers

        self.vertex_array = VertexArray(attributes, index)

        # interactive toggles
        # self.wrap = cycle([GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT,
        #                    GL.GL_CLAMP_TO_BORDER, GL.GL_CLAMP_TO_EDGE])
        # self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
        #                      (GL.GL_LINEAR, GL.GL_LINEAR),
        #                      (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        # self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)

        # setup texture and upload it to GPU
        self.texture = texture
Ejemplo n.º 7
0
    def __init__(self, file):
        # feel free to move this up in the viewer as per other practicals
        self.shader = Shader(TEXTURE_VERT, TEXTURE_FRAG)

        # triangle and face buffers
        vertices = 100 * np.array(
            ((-1, -1, 0), (1, -1, 0), (1, 1, 0), (-1, 1, 0)), np.float32)
        faces = np.array(((0, 1, 2), (0, 2, 3)), np.uint32)
        self.vertex_array = VertexArray([vertices], faces)

        # interactive toggles
        self.wrap = cycle([
            GL.GL_REPEAT, GL.GL_MIRRORED_REPEAT, GL.GL_CLAMP_TO_BORDER,
            GL.GL_CLAMP_TO_EDGE
        ])
        self.filter = cycle([(GL.GL_NEAREST, GL.GL_NEAREST),
                             (GL.GL_LINEAR, GL.GL_LINEAR),
                             (GL.GL_LINEAR, GL.GL_LINEAR_MIPMAP_LINEAR)])
        self.wrap_mode, self.filter_mode = next(self.wrap), next(self.filter)
        self.file = file

        # setup texture and upload it to GPU
        self.texture = Texture(file, self.wrap_mode, *self.filter_mode)