Exemple #1
0
    def __init__(self):
        super(CoreCube, self).__init__()

        global vertices

        # create our shader
        self.shader = ShaderProgram(VertexShader(self.vertex_shader), FragmentShader(self.fragment_shader))

        # create a vertex buffer
        # we pass in a list of regions we want to define
        # we only have 1 region here
        # for each region, we pass in how many rows, and the dtype
        self.buffer = VertexBuffer(GL.GL_ARRAY_BUFFER, GL.GL_STATIC_DRAW, data=vertices)

        # pass the shader and region to our VAO
        # and bind each of the attributes to a VAO index
        # the shader name is the variable name used in the shader
        # the buffer name is the name of the property in our vertex dtype
        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes["position"] = GenericAttribute.from_dtype(
            self.buffer, vertices.dtype, "position", location=self.shader.attributes["in_position"]
        )
        self.buffer_attributes["colour"] = GenericAttribute.from_dtype(
            self.buffer, vertices.dtype, "colour", location=self.shader.attributes["in_colour"]
        )

        # create our vertex array
        self.vao = VertexArray()

        self.vao.bind()
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()
        self.vao.unbind()
    def __init__( self ):
        super( CoreQuad, self ).__init__()

        global vertices

        # create our shader
        self.shader = ShaderProgram(
            VertexShader( self.vertex_shader ),
            FragmentShader( self.fragment_shader )
            )

        # create a vertex buffer
        # we pass in a list of regions we want to define
        # we only have 1 region here
        # for each region, we pass in how many rows, and the dtype
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data = vertices,
            )

        # pass the shader and region to our VAO
        # and bind each of the attributes to a VAO index
        # the shader name is the variable name used in the shader
        # the buffer name is the name of the property in our vertex dtype
        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes[ 'position' ] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'position',
            location = self.shader.attributes[ 'in_position' ]
            )
        self.buffer_attributes[ 'uv' ] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'texture_coord',
            location = self.shader.attributes[ 'in_uv' ]
            )

        # create our vertex array
        self.vao = VertexArray()

        self.vao.bind()
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()
        self.vao.unbind()
class CoreTriangle( object ):

    vertex_shader = textwrap.dedent( """
        #version 150

        // input
        in vec3 in_position;
        in vec3 in_colour;
        uniform mat4 model_view;
        uniform mat4 projection;

        // shared
        out vec3 ex_colour;

        void main(void) 
        {
            // apply projection and model view matrix to vertex
            gl_Position = projection * model_view * vec4( in_position, 1.0 );

            ex_colour = in_colour;
        }
        """ )

    fragment_shader = textwrap.dedent( """
        #version 150

        // shared
        in vec3 ex_colour;

        // output
        out vec4 fragColor;

        void main(void) 
        {
            // set colour of each fragment
            fragColor = vec4( ex_colour, 1.0 );
        }
        """ )


    def __init__( self ):
        super( CoreTriangle, self ).__init__()

        global vertices

        # create our shader
        self.shader = ShaderProgram(
            VertexShader( self.vertex_shader ),
            FragmentShader( self.fragment_shader ),
            )

        # create a vertex buffer
        # we pass in a list of regions we want to define
        # we only have 1 region here
        # for each region, we pass in how many rows, and the dtype
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data = vertices,
            )

        # pass the shader and region to our VAO
        # and bind each of the attributes to a VAO index
        # the shader name is the variable name used in the shader
        # the buffer name is the name of the property in our vertex dtype
        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes[ 'position' ] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'position',
            location = self.shader.attributes[ 'in_position' ]
            )
        self.buffer_attributes[ 'colour' ] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'colour',
            location = self.shader.attributes[ 'in_colour' ]
            )

        # create our vertex array
        self.vao = VertexArray()

        self.vao.bind()
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()
        self.vao.unbind()

    def draw( self, projection, model_view ):
        global vertices

        self.shader.bind()
        self.shader.uniforms[ 'projection' ].value = projection
        self.shader.uniforms[ 'model_view' ].value = model_view

        self.vao.bind()
        GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )
        self.vao.unbind()

        self.shader.unbind()
Exemple #4
0
class CoreCube(object):

    vertex_shader = textwrap.dedent("""
        #version 150

        // input
        in vec3 in_position;
        in vec3 in_colour;
        uniform mat4 model_view;
        uniform mat4 projection;

        // shared
        out vec3 ex_colour;

        void main(void) 
        {
            // apply projection and model view matrix to vertex
            gl_Position = projection * model_view * vec4( in_position, 1.0 );

            ex_colour = in_colour;
        }
        """)

    fragment_shader = textwrap.dedent("""
        #version 150

        // shared
        in vec3 ex_colour;

        // output
        out vec4 fragColor;

        void main(void) 
        {
            // set colour of each fragment
            fragColor = vec4( ex_colour, 1.0 );
        }
        """)

    def __init__(self):
        super(CoreCube, self).__init__()

        global vertices

        # create our shader
        self.shader = ShaderProgram(VertexShader(self.vertex_shader),
                                    FragmentShader(self.fragment_shader))

        # create a vertex buffer
        # we pass in a list of regions we want to define
        # we only have 1 region here
        # for each region, we pass in how many rows, and the dtype
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data=vertices,
        )

        # pass the shader and region to our VAO
        # and bind each of the attributes to a VAO index
        # the shader name is the variable name used in the shader
        # the buffer name is the name of the property in our vertex dtype
        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes['position'] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'position',
            location=self.shader.attributes['in_position'])
        self.buffer_attributes['colour'] = GenericAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'colour',
            location=self.shader.attributes['in_colour'])

        # create our vertex array
        self.vao = VertexArray()

        self.vao.bind()
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()
        self.vao.unbind()

    def draw(self, projection, model_view):
        global vertices

        self.shader.bind()
        self.shader.uniforms['projection'].value = projection
        self.shader.uniforms['model_view'].value = model_view

        self.vao.bind()
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(vertices))
        self.vao.unbind()

        self.shader.unbind()