Skip to content
preview.py 2.2 KiB
Newer Older
Dalai Felinto's avatar
Dalai Felinto committed
"""
Viewport Preview Drawing
************************

Routines to draw in the viewport the result
that is projected in the HMD
"""

from .opengl_helper import (
        draw_rectangle,
        view_reset,
        view_setup,
        )

from bgl import *

TODO = True

class Preview:
    __slots__ = {
            "_texture",
            "_width",
            "_height",
            }

    def init(self, width, height, texture):
        """
        Initialize preview window

        :param width: Horizontal dimension of preview window
        :type width: int
        :param height: Vertical dimension of preview window
        :type height: int
        :param texture: 2D Texture binding ID (bind to the Framebuffer Object)
        :type texture: bgl.GLint
        """
        self._texture = texture
        self.update(width, height)

    def quit(self):
        """
        Destroy preview window
        """
Dalai Felinto's avatar
Dalai Felinto committed

    def update(self, width, height):
        """
        Resize preview window

        :param width: Horizontal dimension of preview window
        :type width: int
        :param height: Vertical dimension of preview window
        :type height: int
        """
        self._width = width
        self._height = height

    def loop(self):
        """
        Draw in the preview window
        """
        texture = self._texture
        width = self._width
        height = self._height
        offset = (600, 200)

        act_tex = Buffer(GL_INT, 1)
        glGetIntegerv(GL_ACTIVE_TEXTURE, act_tex)

        viewport = Buffer(GL_INT, 4)
        glGetIntegerv(GL_VIEWPORT, viewport)

Dalai Felinto's avatar
Dalai Felinto committed
        glViewport(offset[0], offset[1], width, height)
Dalai Felinto's avatar
Dalai Felinto committed
        glScissor(offset[0], offset[1], width, height)

        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LESS)

        view_setup()

        glEnable(GL_TEXTURE_2D)
        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_2D, texture)

        draw_rectangle()

        glBindTexture(GL_TEXTURE_2D, act_tex[0])

        glDisable(GL_TEXTURE_2D)
        glDisable(GL_DEPTH_TEST)

        view_reset(viewport)

        glViewport(viewport[0], viewport[1], viewport[2], viewport[3])
        glScissor(viewport[0], viewport[1], viewport[2], viewport[3])