Example #1
0
    def activate(self):
        """ Activate the object on GPU """

        if self._base is not None:
            self._base.activate()
        else:
            GLObject.activate(self)
Example #2
0
    def __init__(self, program, name, gtype):
        """ Initialize the data into default state """

        # Make sure variable type is allowed (for ES 2.0 shader)
        if gtype not in [
                gl.GL_FLOAT, gl.GL_FLOAT_VEC2, gl.GL_FLOAT_VEC3,
                gl.GL_FLOAT_VEC4, gl.GL_INT, gl.GL_BOOL, gl.GL_FLOAT_MAT2,
                gl.GL_FLOAT_MAT3, gl.GL_FLOAT_MAT4, gl.GL_SAMPLER_2D,
                gl.GL_SAMPLER_CUBE
        ]:
            raise VariableException("Unknown variable type")

        GLObject.__init__(self)

        # Program this variable belongs to
        self._program = program

        # Name of this variable in the program
        self._name = name

        # GL type
        self._gtype = gtype

        # CPU data
        self._data = None

        # Whether this variable is active
        self._active = True

        # Location of the variable in the program slots
        self._location = 0
Example #3
0
    def deactivate(self):
        """ Deactivate the object on GPU """

        if self._base is not None:
            self._base.deactivate()
        else:
            GLObject.deactivate(self)
Example #4
0
    def __init__(self, program, name, gtype):
        """ Initialize the data into default state """

        # Make sure variable type is allowed (for ES 2.0 shader)
        if gtype not in [gl.GL_FLOAT,      gl.GL_FLOAT_VEC2,
                         gl.GL_FLOAT_VEC3, gl.GL_FLOAT_VEC4,
                         gl.GL_INT,        gl.GL_BOOL,
                         gl.GL_FLOAT_MAT2, gl.GL_FLOAT_MAT3,
                         gl.GL_FLOAT_MAT4, gl.GL_SAMPLER_1D,
                         gl.GL_SAMPLER_2D]:
            raise TypeError("Unknown variable type")

        GLObject.__init__(self)

        # Program this variable belongs to
        self._program = program

        # Name of this variable in the program
        self._name = name

        # Build dtype
        size, _, base = gl_typeinfo[gtype]
        self._dtype = (name,base,size)

        # GL type
        self._gtype = gtype

        # CPU data
        self._data = None

        # Whether this variable is active
        self._active = True
Example #5
0
    def __init__(self, program, name, gtype):
        """ Initialize the data into default state """

        # Make sure variable type is allowed (for ES 2.0 shader)
        if gtype not in  [ gl.GL_FLOAT,        gl.GL_FLOAT_VEC2,
                           gl.GL_FLOAT_VEC3,   gl.GL_FLOAT_VEC4,
                           gl.GL_INT,          gl.GL_BOOL,
                           gl.GL_FLOAT_MAT2,   gl.GL_FLOAT_MAT3,
                           gl.GL_FLOAT_MAT4,   gl.GL_SAMPLER_2D,
                           gl.GL_SAMPLER_CUBE]:
            raise VariableException("Unknown variable type")

        GLObject.__init__(self)
        
        # Program this variable belongs to
        self._program = program

        # Name of this variable in the program
        self._name = name

        # GL type
        self._gtype = gtype

        # CPU data
        self._data = None

        # Whether this variable is active
        self._active = True

        # Location of the variable in the program slots
        self._location = 0
Example #6
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)
Example #7
0
    def update(self):
        """ Update the object in GPU """

        if self._base is not None:
            self._base.update()
        else:
            GLObject.update(self)
Example #8
0
    def deactivate(self):
        """ Deactivate the object on GPU """

        if self._base is not None:
            self._base.deactivate()
        else:
            GLObject.deactivate(self)
Example #9
0
    def activate(self):
        """ Activate the object on GPU """

        if self._base is not None:
            self._base.activate()
        else:
            GLObject.activate(self)
Example #10
0
    def update(self):
        """ Update the object in GPU """

        if self._base is not None:
            self._base.update()
        else:
            GLObject.update(self)
Example #11
0
    def __init__(self, code=None):
        """
        Initialize the shader and get code if possible.

        Parameters
        ----------

        code: str
            code can be a filename or the actual code
        """

        GLObject.__init__(self)
        self._code = None
        self._source = None

        if code is not None:
            self._set_code(code)
Example #12
0
    def __init__(self, code=None):
        """
        Initialize the shader and get code if possible.

        Parameters
        ----------

        code: str
            code can be a filename or the actual code
        """

        GLObject.__init__(self)
        self._code = None
        self._source = None

        if code is not None:
            self._set_code(code)
Example #13
0
    def __init__(self,
                 data=None,
                 target=gl.GL_ARRAY_BUFFER,
                 nbytes=0,
                 resizeable=True):
        """ Initialize buffer

        Parameters
        ----------

        target : GLenum
            gl.GL_ARRAY_BUFFER or gl.GL_ELEMENT_ARRAY_BUFFER

        data : ndarray
            Buffer data

        nbytes : int
            Buffer byte size

        resizeable : boolean
            Indicates whether buffer is resizeable
        """

        GLObject.__init__(self)
        self._need_resize = True
        self._resizeable = resizeable
        self._views = []
        self._valid = True

        # Store and check target
        if target not in (gl.GL_ARRAY_BUFFER, gl.GL_ELEMENT_ARRAY_BUFFER):
            raise ValueError("Invalid target for buffer object")
        self._target = target

        # Bytesize of buffer in GPU memory
        self._nbytes = nbytes

        # Buffer usage (GL_STATIC_DRAW, G_STREAM_DRAW or GL_DYNAMIC_DRAW)
        self._usage = gl.GL_DYNAMIC_DRAW

        # Set data
        self._pending_data = []
        if data is not None:
            data = np.array(data, copy=True)
            self._nbytes = data.nbytes
            self.set_data(data, copy=True)
Example #14
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()
Example #15
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()
    def test_init_default(self):
        O = GLObject()

        assert O._handle == -1
        assert O._target == None
        assert O._need_create == True
        assert O._need_update == True
        assert O._need_delete == False
        assert O._id > 0
        assert O._id == GLObject._idcount
Example #17
0
    def __init__(self, data=None, target=gl.GL_ARRAY_BUFFER, nbytes=0, resizeable=True):
        """ Initialize buffer

        Parameters
        ----------

        target : GLenum
            gl.GL_ARRAY_BUFFER or gl.GL_ELEMENT_ARRAY_BUFFER

        data : ndarray
            Buffer data

        nbytes : int
            Buffer byte size

        resizeable : boolean
            Indicates whether buffer is resizeable
        """

        GLObject.__init__(self)
        self._need_resize = True
        self._resizeable = resizeable
        self._views = []
        self._valid = True

        # Store and check target
        if target not in (gl.GL_ARRAY_BUFFER, gl.GL_ELEMENT_ARRAY_BUFFER):
            raise ValueError("Invalid target for buffer object")
        self._target = target

        # Bytesize of buffer in GPU memory
        self._nbytes = nbytes

        # Buffer usage (GL_STATIC_DRAW, G_STREAM_DRAW or GL_DYNAMIC_DRAW)
        self._usage = gl.GL_DYNAMIC_DRAW

        # Set data
        self._pending_data = []
        if data is not None:
            data = np.array(data,copy=True)
            self._nbytes = data.nbytes
            self.set_data(data,copy=True)
Example #18
0
    def __init__(self, target, code=None):
        """
        Initialize the shader and get code if possible.

        Parameters
        ----------

        code: str
            code can be a filename or the actual code
        """

        GLObject.__init__(self)
        if target not in [gl.GL_VERTEX_SHADER, gl.GL_FRAGMENT_SHADER]:
            raise ValueError("Shader target must be vertex or fragment")

        self._target = target
        self._code = None
        self._source = None
        if code is not None:
            self.code = code
Example #19
0
    def __init__(self, data=None):
        """ Initialize the data buffer """

        GLObject.__init__(self)

        # CPU data
        self._data = None

        # Size of buffer in terms of element
        self._size = 0

        # Number of bytes separating two consecutive values
        self._stride = 0

        # Where does the actual data start relative to buffer start
        self._offset = 0

        # Element type
        self._gtype = 0

        # Set data if any
        if data is not None:
            self.set_data(data)
Example #20
0
    def __init__(self, data=None):
        """ Initialize the data buffer """

        GLObject.__init__(self)

        # CPU data
        self._data = None
        
        # Size of buffer in terms of element
        self._size = 0

        # Number of bytes separating two consecutive values
        self._stride = 0

        # Where does the actual data start relative to buffer start
        self._offset = 0

        # Element type
        self._gtype = 0

        # Set data if any
        if data is not None:
            self.set_data(data)
Example #21
0
    def __init__(self, data=None, shape=(), dtype=None, base=None, target=None,
                       offset=None, store=True, copy=False, resizeable=True):
        """
        Initialize the texture

        Parameters
        ----------

        target : GLEnum
            gl.GL_TEXTURE1D, gl.GL_TEXTURE2D

        data : np.ndarray
            Texture data (optional)

        dtype : np.dtype
            Texture data type (optional)

        shape : tuple of integers
            Texture shape

        base : Texture
           Base texture of this texture

        offset : tuple of integers
           Offset of this texture relative to base texture

        store : boolean
           Indicate whether to use an intermediate CPU storage

        copy : boolean
           Indicate whether to use given data as CPU storage

        resizeable : boolean
            Indicates whether texture can be resized
        """

        GLObject.__init__(self)
        self._data = None
        self._base = base
        self._store = store
        self._copy = copy
        self._target = target
        self._offset = offset
        self._pending_data = []
        self._resizeable = resizeable
        self._need_resize = False
        self._need_update = False
        self._valid = True
        self._views = []

        self._interpolation = gl.GL_NEAREST, gl.GL_NEAREST
        self._wrapping = gl.GL_CLAMP_TO_EDGE
        self._need_parameterization = True

        # Do we have data to build texture upon ?
        if data is not None:
            self._need_resize = True
            if dtype is not None:
                data = np.array(data,dtype=dtype,copy=False)
            else:
                data = np.array(data,copy=False)
            self._dtype = data.dtype
            self._shape = data.shape
            if self._store:
                if base is None and not data.flags["C_CONTIGUOUS"]:
                    self._copy = True
                self._data = np.array(data,copy=self._copy)
                self.set_data(self.data,copy=False)
            else:
                self.set_data(data,copy=True)
        elif dtype is not None:
            if shape:
                self._need_resize = True
            self._shape = shape
            self._dtype = dtype
            if self._store:
                self._data = np.empty(self._shape, dtype=self._dtype)
        else:
            raise ValueError("Either data or dtype must be given")

        if offset is None:
            self._offset = (0,) * len(self._shape)
        else:
            self._offset = offset

        self._gtype = Texture._types.get(self.dtype, None)
        if self._gtype is None:
            raise ValueError("Type not allowed for texture")
Example #22
0
 def test_init(self):
     obj = GLObject()
     assert obj._handle == 0
     assert obj.dirty == True
     assert obj.status == False
     assert obj.id == 1
Example #23
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)
Example #24
0
    def __init__(self,
                 data=None,
                 shape=(),
                 dtype=None,
                 base=None,
                 target=None,
                 offset=None,
                 store=True,
                 copy=False,
                 resizeable=True):
        """
        Initialize the texture

        Parameters
        ----------

        target : GLEnum
            gl.GL_TEXTURE1D, gl.GL_TEXTURE2D

        data : np.ndarray
            Texture data (optional)

        dtype : np.dtype
            Texture data type (optional)

        shape : tuple of integers
            Texture shape

        base : Texture
           Base texture of this texture

        offset : tuple of integers
           Offset of this texture relative to base texture

        store : boolean
           Indicate whether to use an intermediate CPU storage

        copy : boolean
           Indicate whether to use given data as CPU storage

        resizeable : boolean
            Indicates whether texture can be resized
        """

        GLObject.__init__(self)
        self._data = None
        self._base = base
        self._store = store
        self._copy = copy
        self._target = target
        self._offset = offset
        self._pending_data = []
        self._resizeable = resizeable
        self._need_resize = False
        self._need_update = False
        self._valid = True
        self._views = []

        self._interpolation = gl.GL_NEAREST, gl.GL_NEAREST
        self._wrapping = gl.GL_CLAMP_TO_EDGE
        self._need_parameterization = True

        # Do we have data to build texture upon ?
        if data is not None:
            self._need_resize = True
            if dtype is not None:
                data = np.array(data, dtype=dtype, copy=False)
            else:
                data = np.array(data, copy=False)
            self._dtype = data.dtype
            self._shape = data.shape
            if self._store:
                if base is None and not data.flags["C_CONTIGUOUS"]:
                    self._copy = True
                self._data = np.array(data, copy=self._copy)
                self.set_data(self.data, copy=False)
            else:
                self.set_data(data, copy=True)
        elif dtype is not None:
            if shape:
                self._need_resize = True
            self._shape = shape
            self._dtype = dtype
            if self._store:
                self._data = np.empty(self._shape, dtype=self._dtype)
        else:
            raise ValueError("Either data or dtype must be given")

        if offset is None:
            self._offset = (0, ) * len(self._shape)
        else:
            self._offset = offset

        self._gtype = Texture._types.get(np.dtype(self.dtype), None)
        if self._gtype is None:
            raise ValueError("Type not allowed for texture")