def testGetCompileStatus(self):
        data = [
            (gl.GL_TRUE, True),
            (gl.GL_FALSE, False),
        ]
        for getReturn, expected in data:
            shader = FragmentShader(['src'])
            shader._get = Mock(return_value=getReturn)

            actual = shader.getCompileStatus()

            self.assertEquals(shader._get.call_args,
                ((gl.GL_COMPILE_STATUS,), {}))
            self.assertEquals(actual, expected)
            self.assertEquals(type(actual), type(expected))
Beispiel #2
0
    def __init__(self, verts=[], frags=[], count=0):
        """Initialize the program and register shaders to be linked.

        Parameters
        ----------

        verts : list of vertex shaders
            Vertex shaders to be used by this program

        frags : list of fragment shaders
            Fragment shaders to be used by this program

        count : int
            Number of vertices this program will use

        Note
        ----
        If several vertex shaders are specified, only one can contain the main
        function.

        If several fragment shaders are specified, only one can contain the main
        function.
        """

        GLObject.__init__(self)

        self._count = count
        self._buffer = None

        # Get all vertex shaders
        self._verts = []
        if type(verts) in [str, VertexShader]:
            verts = [verts]
        for shader in verts:
            if type(shader) is str:
                self._verts.append(VertexShader(shader))
            elif shader not in self._verts:
                self._verts.append(shader)

        # Get all fragment shaders
        self._frags = []
        if type(frags) in [str, FragmentShader]:
            frags = [frags]
        for shader in frags:
            if type(shader) is str:
                self._frags.append(FragmentShader(shader))
            elif shader not in self._frags:
                self._frags.append(shader)

        # Build uniforms and attributes
        self._build_uniforms()
        self._build_attributes()

        # Build associated structured vertex buffer if count is given
        if self._count > 0:
            dtype = []
            for attribute in self._attributes.values():
                dtype.append(attribute.dtype)
            self._buffer = VertexBuffer(np.zeros(self._count, dtype=dtype))
            self.bind(self._buffer)
    def test_setitem(self):
        vert = VertexShader("")
        frag = FragmentShader("")

        program = Program(vert,frag)
        with self.assertRaises(ValueError):
            program["A"] = 1
 def test_detach(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program(vert, frag)
     program.detach(frag)
     assert len(program.shaders) == 1
     assert program.shaders[0].code == "A"
 def test_attach(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program(vert)
     program.attach(frag)
     assert len(program.shaders) == 2
     assert program.shaders[0].code == "A"
     assert program.shaders[1].code == "B"
    def test_failed_build(self):
        vert = VertexShader("A")
        frag = FragmentShader("B")

        program = Program(verts = vert)
        with self.assertRaises(ValueError):
            program.activate()

        program = Program(frags = frag)
        with self.assertRaises(ValueError):
            program.activate()
Beispiel #7
0
    def __init__(self, verts=[], frags=[]):
        """Initialize the program and register shaders to be linked.

        Parameters
        ----------

        verts : list of vertex shaders
        frags : list of fragment shaders

        Note
        ----

        If several vertex shaders are specified, only one can contain the main
        function.

        If several fragment shaders are specified, only one can contain the main
        function.
        """

        GLObject.__init__(self)

        # Get all vertex shaders
        self._verts = []
        if type(verts) in [str, VertexShader]:
            verts = [verts]
        for shader in verts:
            if type(shader) is str:
                self._verts.append(VertexShader(shader))
            elif shader not in self._verts:
                self._verts.append(shader)

        # Get all fragment shaders
        self._frags = []
        if type(frags) in [str, FragmentShader]:
            frags = [frags]
        for shader in frags:
            if type(shader) is str:
                self._frags.append(FragmentShader(shader))
            elif shader not in self._frags:
                self._frags.append(shader)

        # Build uniforms and attributes
        self.build_uniforms()
        self.build_attributes()
Beispiel #8
0
    """

    fragment = """
    #version 120

    uniform vec4 color;
    void main()
    {
        gl_FragColor = color;
    }
    """

    # No GL context required
    # ----------------------
    program = Program(vertex,fragment)
    frag = FragmentShader("uniform int dummy;")
    print("Attaching shader")
    program.attach(frag)
    print("Dettaching shader")
    program.detach(frag)

    print("Uniforms:   %s" % program.all_uniforms)
    print("Attributes: %s" % program.all_attributes)
    print("Shaders: %s" % program.shaders)
    print("Program status: %s" % (
          "dirty (needs new build) " if program.dirty else "clean"))

    print("Setting color")
    program['color'] = 1,1,1,1

    # This should raise a ProgramException
 def test_attributes(self):
     vert = VertexShader("attribute float A;")
     frag = FragmentShader("")
     program = Program(vert,frag)
     assert program.all_attributes == [ ("A", gl.GL_FLOAT) ]
 def test_uniform(self):
     vert = VertexShader("uniform float A;")
     frag = FragmentShader("uniform float A; uniform vec4 B;")
     program = Program(vert,frag)
     assert program.all_uniforms == [ ("A", gl.GL_FLOAT),
                                      ("B", gl.GL_FLOAT_VEC4) ]
 def test_unique_shader(self):
     vert = VertexShader("A")
     frag = FragmentShader("B")
     program = Program([vert,vert],[frag,frag,frag])
     assert len(program.shaders) == 2
 def test_init_from_shader(self):
     program = Program(VertexShader("A"),FragmentShader("B"))
     assert len(program.shaders) == 2
     assert program.shaders[0].code == "A"
     assert program.shaders[1].code == "B"
Beispiel #13
0
from other.old_stuff.mathematics import create_transformation_matrix, create_perspective_matrix, sin, cos, pi
from other.old_stuff.uniforms import TexturedMaterial, PointLight, SpotLight, SunLight, uniform_struct, \
    uniform_struct_array

# All calls to OpenGL functions must happen after we've created the context!
window = Window(width=1024, height=1024)

glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK)
glClearColor(0.1, 0.1, 0.12, 1.0)

object_shader = Program(
    vertex_shader=VertexShader(path='shaders/lightning_shader.vs'),
    fragment_shader=FragmentShader(
        path='shaders/debug_lightning_shader_multiple_lights.fs'),
    uniforms=[
        'transform', 'view', 'projection', 'time',
        *uniform_struct('material', TexturedMaterial),
        *uniform_struct('spotlight', SpotLight),
        *uniform_struct_array('light', 4, PointLight),
        *uniform_struct('sunlight', SunLight)
    ],
    attributes=['position', 'normal', 'texture_coordinate'])
lamp_shader = Program(
    vertex_shader=VertexShader(path='shaders/lamp_shader.vs'),
    fragment_shader=FragmentShader(path='shaders/lamp_shader.fs'),
    uniforms=['transform', 'view', 'projection', 'color'],
    attributes=['position'])
terrain_shader = Program(
    vertex_shader=VertexShader(path='shaders/lamp_shader.vs'),
 def test_init(self):
     shader = FragmentShader()
     assert shader.type == gl.GL_FRAGMENT_SHADER