コード例 #1
0
ファイル: renderable_cube.py プロジェクト: fredroy/PyGLy
class LegacyCube(object):

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

        void main(void) 
        {
            // apply projection and model view matrix to vertex
            gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;

            gl_FrontColor = gl_Color;
        }
        """)

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

        void main(void) 
        {
            // set colour of each fragment
            gl_FragColor = gl_Color;
        }
        """)

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

        global vertices

        self.use_shaders = True

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

        # create our vertex buffer
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data=vertices,
        )

        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes['position'] = VertexAttribute.from_dtype(
            self.buffer, vertices.dtype, 'position')

        self.buffer_attributes['colour'] = ColourAttribute.from_dtype(
            self.buffer, vertices.dtype, 'colour')

    def draw(self):
        global vertices

        if self.use_shaders:
            self.shader.bind()

        self.buffer_attributes.push_attributes()

        # set the vertex pointer to the position data
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()

        GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(vertices))

        self.buffer_attributes.pop_attributes()

        if self.use_shaders:
            self.shader.unbind()
コード例 #2
0
class LegacyTriangle( object ):

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

        void main(void) 
        {
            // apply projection and model view matrix to vertex
            gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;

            gl_FrontColor = gl_Color;
        }
        """ )

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

        void main(void) 
        {
            // set colour of each fragment
            gl_FragColor = gl_Color;
        }
        """ )


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

        global vertices

        self.use_shaders = True

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

        # create our vertex buffer
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data = vertices,
            )

        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes[ 'position' ] = VertexAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'position'
            )
        self.buffer_attributes[ 'colour' ] = ColourAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'colour'
            )

    def draw( self ):
        global vertices
        
        if self.use_shaders:
            self.shader.bind()

        self.buffer_attributes.push_attributes()

        # set the vertex pointer to the position data
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()

        GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )

        self.buffer_attributes.pop_attributes()

        if self.use_shaders:
            self.shader.unbind()
コード例 #3
0
class LegacyQuad( object ):

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

        void main(void) 
        {
            // apply projection and model view matrix to vertex
            gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;

            // select the texture coordinate to use
            gl_TexCoord[0]  = gl_MultiTexCoord0;
        }
        """ )

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

        // input
        uniform sampler2D in_diffuse_texture;

        void main(void) 
        {
            // set colour of each fragment
            gl_FragColor = texture2D( in_diffuse_texture, gl_TexCoord[0].st );
        }
        """ )


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

        global vertices

        self.use_shaders = True

        GL.glEnable(GL.GL_TEXTURE_2D)

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

        # create our vertex buffer
        self.buffer = VertexBuffer(
            GL.GL_ARRAY_BUFFER,
            GL.GL_STATIC_DRAW,
            data = vertices,
            )

        self.buffer_attributes = BufferAttributes()
        self.buffer_attributes[ 'position' ] = VertexAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'position'
            )

        self.buffer_attributes[ 'uv' ] = TextureCoordAttribute.from_dtype(
            self.buffer,
            vertices.dtype,
            'texture_coord'
            )

    def draw( self ):
        global vertices

        if self.use_shaders:
            self.shader.bind()
            self.shader.uniforms[ 'in_diffuse_texture' ].value = 0

        self.buffer_attributes.push_attributes()

        # set the vertex pointer to the position data
        self.buffer.bind()
        self.buffer_attributes.set()
        self.buffer.unbind()

        GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )

        self.buffer_attributes.pop_attributes()

        if self.use_shaders:
            self.shader.unbind()