Exemplo n.º 1
0
class TexturedTriangleShader(gl.AbstractShaderProgram):
    POSITION = gl.Attribute(
        gl.Attribute.Kind.GENERIC, 0,
        gl.Attribute.Components.TWO,
        gl.Attribute.DataType.FLOAT)
    TEXTURE_COORDINATES = gl.Attribute(
        gl.Attribute.Kind.GENERIC, 1,
        gl.Attribute.Components.TWO,
        gl.Attribute.DataType.FLOAT)

    _texture_unit = 0

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

        vert = gl.Shader(gl.Version.GL330, gl.Shader.Type.VERTEX)
        vert.add_source("""
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 textureCoordinates;

out vec2 interpolatedTextureCoordinates;

void main() {
    interpolatedTextureCoordinates = textureCoordinates;

    gl_Position = position;
}
""".lstrip())
        vert.compile()
        self.attach_shader(vert)

        frag = gl.Shader(gl.Version.GL330, gl.Shader.Type.FRAGMENT)
        frag.add_source("""
uniform vec3 color = vec3(1.0, 1.0, 1.0);
uniform sampler2D textureData;

in vec2 interpolatedTextureCoordinates;

out vec4 fragmentColor;

void main() {
    fragmentColor.rgb = color*texture(textureData, interpolatedTextureCoordinates).rgb;
    fragmentColor.a = 1.0;
}
""".lstrip())
        frag.compile()
        self.attach_shader(frag)

        self.link()

        self._color_uniform = self.uniform_location('color')
        self.set_uniform(self.uniform_location('textureData'), self._texture_unit)

    def color(self, color: Color3):
        self.set_uniform(self._color_uniform, color)
    color = property(None, color)

    def bind_texture(self, texture: gl.Texture2D):
        texture.bind(self._texture_unit)
Exemplo n.º 2
0
 def test_init(self):
     a = gl.Attribute(gl.Attribute.Kind.GENERIC, 2,
                      gl.Attribute.Components.TWO,
                      gl.Attribute.DataType.FLOAT)
     self.assertEqual(a.kind, gl.Attribute.Kind.GENERIC)
     self.assertEqual(a.location, 2)
     self.assertEqual(a.components, gl.Attribute.Components.TWO)
     self.assertEqual(a.data_type, gl.Attribute.DataType.FLOAT)
Exemplo n.º 3
0
    def test_add_buffer(self):
        buffer = gl.Buffer()
        buffer_refcount = sys.getrefcount(buffer)

        # Adding a buffer to the mesh should increase its ref count
        mesh = gl.Mesh()
        mesh.add_vertex_buffer(buffer, 0, 8, gl.Attribute(gl.Attribute.Kind.GENERIC, 2, gl.Attribute.Components.TWO, gl.Attribute.DataType.FLOAT))
        self.assertEqual(len(mesh.buffers), 1)
        self.assertIs(mesh.buffers[0], buffer)
        self.assertEqual(sys.getrefcount(buffer), buffer_refcount + 1)

        # Deleting the mesh should decrease it again
        del mesh
        self.assertEqual(sys.getrefcount(buffer), buffer_refcount)