Пример #1
0
    def __init__(self, size: Tuple[int, int], component: int, data):
        self.width, self.height = size
        sized_format = (gl.GL_R8, gl.GL_RG8, gl.GL_RGB8,
                        gl.GL_RGBA8)[component - 1]
        self.format = (gl.GL_R, gl.GL_RG, gl.GL_RGB, gl.GL_RGBA)[component - 1]
        gl.glActiveTexture(gl.GL_TEXTURE0 +
                           0)  # If we need other texture unit...
        self.texture_id = texture_id = gl.GLuint()
        gl.glGenTextures(1, byref(self.texture_id))

        if self.texture_id.value == 0:
            raise ShaderException("Cannot create Texture.")

        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texture_id)
        gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)
        try:
            gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, sized_format, self.width,
                            self.height, 0, self.format, gl.GL_UNSIGNED_BYTE,
                            data)
        except gl.GLException:
            raise gl.GLException(
                f"Unable to create texture. {gl.GL_MAX_TEXTURE_SIZE} {size}")

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                           gl.GL_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                           gl.GL_LINEAR)
        weakref.finalize(self, Texture.release, texture_id)
Пример #2
0
 def my_errcheck(result, func, arguments):
     from pyglet import gl
     error = gl.glGetError()
     if error and error != 1286:
         # HACK: The 1286(INVALID_FRAMEBUFFER_OPERATION) error again!
         # This time I DIDN'T EVEN USE FBO! ATI!!
         msg = ctypes.cast(gl.gluErrorString(error), ctypes.c_char_p).value
         raise gl.GLException((error, msg))
     return result
Пример #3
0
    def _texture_2d(self, data):
        """Create a 2D texture"""
        # Make sure we unpack the pixel data with correct alignment
        # or we'll end up with corrupted textures
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, self._alignment)
        gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, self._alignment)

        # Create depth 2d texture
        if self._depth:
            gl.glTexImage2D(
                self._target,
                0,  # level
                gl.GL_DEPTH_COMPONENT24,
                self._width,
                self._height,
                0,
                gl.GL_DEPTH_COMPONENT,
                gl.GL_FLOAT,
                data,
            )
            self.compare_func = "<="
        # Create normal 2d texture
        else:
            try:
                format_info = pixel_formats[self._dtype]
            except KeyError:
                raise ValueError(
                    f"dype '{self._dtype}' not support. Supported types are : {tuple(pixel_formats.keys())}"
                )

            try:
                (
                    _format,
                    _internal_format,
                    self._type,
                    self._component_size,
                ) = format_info
                self._format = _format[self._components]
                self._internal_format = _internal_format[self._components]
                gl.glTexImage2D(
                    self._target,  # target
                    0,  # level
                    self._internal_format,  # internal_format
                    self._width,  # width
                    self._height,  # height
                    0,  # border
                    self._format,  # format
                    self._type,  # type
                    data,  # data
                )
            except gl.GLException as ex:
                raise gl.GLException(
                    (
                        f"Unable to create texture: {ex} : dtype={self._dtype} size={self.size} components={self._components} "
                        "MAX_TEXTURE_SIZE = {self.ctx.limits.MAX_TEXTURE_SIZE}"
                    )
                )
Пример #4
0
    def __init__(self,
                 ctx: 'Context',
                 size: Tuple[int, int],
                 *,
                 components: int = 4,
                 dtype: str = 'f1',
                 data: Any = None,
                 filter: Tuple[gl.GLuint, gl.GLuint] = None,
                 wrap_x: gl.GLuint = None,
                 wrap_y: gl.GLuint = None):
        """
        A texture can be created with or without initial data.
        NOTE: Currently does not support multisample textures even
        thought ``samples`` is exposed.

        :param Context ctx: The context the object belongs to
        :param Tuple[int, int] size: The size of the texture
        :param int components: The number of components (1: R, 2: RG, 3: RGB, 4: RGBA)
        :param str dtype: The data type of each component: f1, f2, f4 / i1, i2, i4 / u1, u2, u4
        :param Any data: The byte data of the texture. bytes or anything supporting the buffer protocol.
        :param Tuple[gl.GLuint, gl.GLuint] filter: The minification/magnification filter of the texture
        :param gl.GLuint wrap_s
        :param data: The texture data (optional)
        """
        self._ctx = ctx
        self._width, self._height = size
        self._dtype = dtype
        self._components = components
        self._target = gl.GL_TEXTURE_2D
        self._samples = 0
        # Default filters for float and integer textures
        if 'f' in self.dtype:
            self._filter = gl.GL_LINEAR, gl.GL_LINEAR
        else:
            self._filter = gl.GL_NEAREST, gl.GL_NEAREST
        self._wrap_x = gl.GL_REPEAT
        self._wrap_y = gl.GL_REPEAT

        if components not in [1, 2, 3, 4]:
            raise ValueError("Components must be 1, 2, 3 or 4")

        try:
            format_info = self._formats[self._dtype]
        except KeyError:
            raise ValueError(
                f"dype '{dtype}' not support. Supported types are : {tuple(self._formats.keys())}"
            )

        gl.glActiveTexture(
            gl.GL_TEXTURE0)  # Create textures in the default channel (0)

        self._glo = glo = gl.GLuint()
        gl.glGenTextures(1, byref(self._glo))

        if self._glo.value == 0:
            raise RuntimeError(
                "Cannot create Texture. OpenGL failed to generate a texture id"
            )

        gl.glBindTexture(self._target, self._glo)
        gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

        if data is not None:
            byte_length, data = data_to_ctypes(data)

        try:
            _format, _internal_format, self._type, self._component_size = format_info
            self._format = _format[components]
            self._internal_format = _internal_format[components]
            gl.glTexImage2D(
                self._target,  # target
                0,  # level
                self._internal_format,  # internal_format
                self._width,  # width
                self._height,  # height
                0,  # border
                self._format,  # format
                self._type,  # type
                data  # data
            )
        except gl.GLException as ex:
            raise gl.GLException((
                f"Unable to create texture: {ex} : dtype={dtype} size={size} components={components} "
                "MAX_TEXTURE_SIZE = {self.ctx.limits.MAX_TEXTURE_SIZE}"))

        self.filter = filter or self._filter
        self.wrap_x = wrap_x or self._wrap_x
        self.wrap_y = wrap_y or self._wrap_y

        self.ctx.stats.incr('texture')
        weakref.finalize(self, Texture.release, self._ctx, glo)