def standard_perspective(self):
        if self.perspective == PERSPECTIVE_STANDARD:
            return
        # aspect_ratio = cls.display_width / cls.display_height
        self.gl_height = 2.0
        self.gl_width = 2.0 * self.display_aspect
        self.gl_left = -1.0 * self.display_aspect
        self.fov_y = 45 / self.zoom
        self.fov_x = self.fov_y * self.display_aspect / self.zoom

        # left = cls.offset_x - 1.0 * cls.display_aspect / cls.zoom
        # right = cls.offset_x + 1.0 * cls.display_aspect / cls.zoom
        # top = cls.offset_y + 1.0 / cls.zoom
        # bottom = cls.offset_y - 1.0 / cls.zoom

        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()

        # gluPerspective(cls.fov_y, cls.display_aspect, 0.1, 10.1)
        # glTranslatef(-cls.offset_x, -cls.offset_y, 0.0)

        gl.gluPerspective(45.0, 16.0 / 9.0, 0.1, 100.0)

        self.projection = gl.glGetFloatv(gl.GL_PROJECTION_MATRIX)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()
        # glScissor(0, display_yoffset, cls.display_width, cls.display_height)
        # glEnable(GL_SCISSOR_TEST)
        self.perspective = PERSPECTIVE_STANDARD
 def ortho_perspective(self):
     if self.perspective == PERSPECTIVE_ORTHO:
         return
     self.display_aspect = 16 / 9.0
     self.ortho_pscalex = (
         2.0
         / self.display_width
         * (self.display_width / self.display_height)
     )
     self.ortho_pscaley = 2.0 / self.display_height
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glLoadIdentity()
     left = self.offset_x - 1.0 * self.display_aspect / self.zoom
     right = self.offset_x + 1.0 * self.display_aspect / self.zoom
     top = self.offset_y + 1.0 / self.zoom
     bottom = self.offset_y - 1.0 / self.zoom
     gl.glOrtho(left, right, bottom, top, -1.0, 1.0)
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glLoadIdentity()
     self.gl_left = -1.0 * self.display_aspect
     self.gl_right = 1.0 * self.display_aspect
     self.gl_height = 2.0  # * cls.display_aspect
     self.gl_width = 2.0 * self.display_aspect
     self.perspective = PERSPECTIVE_ORTHO
     return self.ortho_pscalex, self.ortho_pscaley
Beispiel #3
0
    def standard_perspective(self):
        if self.perspective == PERSPECTIVE_STANDARD:
            return
        # aspect_ratio = cls.display_width / cls.display_height
        self.gl_height = 2.0
        self.gl_width = 2.0 * self.display_aspect
        self.gl_left = -1.0 * self.display_aspect
        self.fov_y = 45 / self.zoom
        self.fov_x = self.fov_y * self.display_aspect / self.zoom

        # left = cls.offset_x - 1.0 * cls.display_aspect / cls.zoom
        # right = cls.offset_x + 1.0 * cls.display_aspect / cls.zoom
        # top = cls.offset_y + 1.0 / cls.zoom
        # bottom = cls.offset_y - 1.0 / cls.zoom

        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glLoadIdentity()

        # gluPerspective(cls.fov_y, cls.display_aspect, 0.1, 10.1)
        # glTranslatef(-cls.offset_x, -cls.offset_y, 0.0)

        gl.gluPerspective(45.0, 16.0 / 9.0, 0.1, 100.0)

        self.projection = gl.glGetFloatv(gl.GL_PROJECTION_MATRIX)
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glLoadIdentity()
        # glScissor(0, display_yoffset, cls.display_width, cls.display_height)
        # glEnable(GL_SCISSOR_TEST)
        self.perspective = PERSPECTIVE_STANDARD
Beispiel #4
0
 def ortho_perspective(self):
     if self.perspective == PERSPECTIVE_ORTHO:
         return
     self.display_aspect = 16 / 9.0
     self.ortho_pscalex = (
         2.0
         / self.display_width
         * (self.display_width / self.display_height)
     )
     self.ortho_pscaley = 2.0 / self.display_height
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glLoadIdentity()
     left = self.offset_x - 1.0 * self.display_aspect / self.zoom
     right = self.offset_x + 1.0 * self.display_aspect / self.zoom
     top = self.offset_y + 1.0 / self.zoom
     bottom = self.offset_y - 1.0 / self.zoom
     gl.glOrtho(left, right, bottom, top, -1.0, 1.0)
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glLoadIdentity()
     self.gl_left = -1.0 * self.display_aspect
     self.gl_right = 1.0 * self.display_aspect
     self.gl_height = 2.0  # * cls.display_aspect
     self.gl_width = 2.0 * self.display_aspect
     self.perspective = PERSPECTIVE_ORTHO
     return self.ortho_pscalex, self.ortho_pscaley
 def hd_perspective(self):
     if self.perspective == PERSPECTIVE_HD:
         return
     self.display_aspect = 16 / 9.0
     self.ortho_pscalex = 1920 / self.display_width
     # cls.ortho_pscalex = 1920 / cls.display_width * \
     #         (cls.display_width / cls.display_height)
     self.ortho_pscaley = 1080 / self.display_height
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glLoadIdentity()
     left = 0.0
     right = 1920.0
     top = 1080.0
     bottom = 0.0
     gl.glOrtho(left, right, bottom, top, -1.0, 1.0)
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glLoadIdentity()
     self.gl_left = left
     self.gl_right = right
     self.gl_height = 1080.0
     self.gl_width = 1920.0
     self.perspective = PERSPECTIVE_HD
     return self.ortho_pscalex, self.ortho_pscaley
Beispiel #6
0
 def hd_perspective(self):
     if self.perspective == PERSPECTIVE_HD:
         return
     self.display_aspect = 16 / 9.0
     self.ortho_pscalex = 1920 / self.display_width
     # cls.ortho_pscalex = 1920 / cls.display_width * \
     #         (cls.display_width / cls.display_height)
     self.ortho_pscaley = 1080 / self.display_height
     gl.glMatrixMode(gl.GL_PROJECTION)
     gl.glLoadIdentity()
     left = 0.0
     right = 1920.0
     top = 1080.0
     bottom = 0.0
     gl.glOrtho(left, right, bottom, top, -1.0, 1.0)
     gl.glMatrixMode(gl.GL_MODELVIEW)
     gl.glLoadIdentity()
     self.gl_left = left
     self.gl_right = right
     self.gl_height = 1080.0
     self.gl_width = 1920.0
     self.perspective = PERSPECTIVE_HD
     return self.ortho_pscalex, self.ortho_pscaley
Beispiel #7
0
    def render(self, text, x, y, r=1.0, g=1.0, b=1.0, alpha=1.0):
        global text_cache
        if not text:
            return 0

        # find cached text entry, if any

        for i, item in enumerate(text_cache):
            if item["text"] == text and item["font"] == self:
                text_cache.pop(i)
                break
        else:
            item = None

        if item:
            fs_emu_blending(True)
            fs_emu_texturing(True)

            w = item["w"]
            h = item["h"]
            gl.glBindTexture(gl.GL_TEXTURE_2D, item["texture"])
            gl.glBegin(gl.GL_QUADS)
            gl.glColor4f(r * alpha, g * alpha, b * alpha, alpha)
            gl.glTexCoord2d(0.0, 0.0)
            gl.glVertex2d(x, y)
            gl.glTexCoord2d(1.0, 0.0)
            gl.glVertex2d(x + w, y)
            gl.glTexCoord2d(1.0, 1.0)
            gl.glVertex2d(x + w, y + h)
            gl.glTexCoord2d(0.0, 1.0)
            gl.glVertex2d(x, y + h)
            gl.glEnd()

            # re-insert item at front
            text_cache.insert(0, item)
            return w, h

        # calculate size of text

        required_width, required_height = self.measure(text)

        # setup fbo

        render_texture = gl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, render_texture)
        gl.glTexImage2D(
            gl.GL_TEXTURE_2D,
            0,
            gl.GL_RGBA,
            required_width,
            required_height,
            0,
            gl.GL_RGBA,
            gl.GL_UNSIGNED_INT,
            None,
        )
        gl.glTexParameteri(
            gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE
        )
        gl.glTexParameteri(
            gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE
        )
        gl.glTexParameteri(
            gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR
        )

        # FIXME: Mipmapping?
        mip_mapping = 0
        if mip_mapping:
            gl.glTexParameteri(
                gl.GL_TEXTURE_2D,
                gl.GL_TEXTURE_MIN_FILTER,
                gl.GL_LINEAR_MIPMAP_LINEAR,
            )
            gl.glTexParameteri(
                gl.GL_TEXTURE_2D, gl.GL_GENERATE_MIPMAP, gl.GL_TRUE
            )
            gl.glGenerateMipmapEXT(gl.GL_TEXTURE_2D)
        else:
            gl.glTexParameteri(
                gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR
            )

        gl.glBindTexture(gl.GL_TEXTURE_2D, 0)

        # Set up some renderbuffer state

        frame_buffer = gl.GLuint()
        gl.glGenFramebuffersEXT(1, frame_buffer)
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, frame_buffer)
        gl.glFramebufferTexture2DEXT(
            gl.GL_FRAMEBUFFER_EXT,
            gl.GL_COLOR_ATTACHMENT0_EXT,
            gl.GL_TEXTURE_2D,
            render_texture,
            0,
        )

        status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT)
        if status != gl.GL_FRAMEBUFFER_COMPLETE_EXT:
            print("glCheckFramebufferStatusEXT error", status)

        gl.glPushMatrix()
        gl.glLoadIdentity()
        gl.glPushAttrib(int(gl.GL_VIEWPORT_BIT) | int(gl.GL_ENABLE_BIT))
        gl.glViewport(0, 0, required_width, required_height)
        gl.glMatrixMode(gl.GL_PROJECTION)
        gl.glPushMatrix()
        gl.glLoadIdentity()
        gl.gluOrtho2D(0, required_width, 0, required_height)

        gl.glClearColor(0.0, 0.0, 0.0, 0.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        gl.glEnable(gl.GL_BLEND)
        gl.glEnable(gl.GL_TEXTURE_2D)

        self.texture.bind()

        tw = self.texture.w
        th = self.texture.h

        gl.glBegin(gl.GL_QUADS)
        gl.glColor4f(1.0, 1.0, 1.0, 1.0)
        x2 = 0
        h = self.h
        for ch in text:
            c = ord(ch)
            w = self.w[c]
            s1 = self.x[c] / tw
            s2 = (self.x[c] + w) / tw
            t1 = (self.y[c]) / th
            t2 = (self.y[c] + h) / th
            gl.glTexCoord2d(s1, t2)
            gl.glVertex2d(x2, 0)
            gl.glTexCoord2d(s2, t2)
            gl.glVertex2d(x2 + w, 0)
            gl.glTexCoord2d(s2, t1)
            gl.glVertex2d(x2 + w, h)
            gl.glTexCoord2d(s1, t1)
            gl.glVertex2d(x2, h)
            x2 += w
        gl.glEnd()

        gl.glPopMatrix()
        gl.glMatrixMode(gl.GL_MODELVIEW)
        gl.glPopAttrib()
        gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
        gl.glPopMatrix()

        gl.glDeleteFramebuffersEXT(1, frame_buffer)

        if mip_mapping:
            gl.glBindTexture(gl.GL_TEXTURE_2D, render_texture)
            gl.glGenerateMipmapEXT(gl.GL_TEXTURE_2D)
            gl.glBindTexture(gl.GL_TEXTURE_2D, 0)

        new_item = {
            "font": self,
            "text": text,
            "w": required_width,
            "h": required_height,
            "texture": render_texture,
        }
        text_cache.insert(0, new_item)

        item = text_cache.pop()
        if item["texture"]:
            gl.glDeleteTextures([item["texture"]])

        # now the text is in the cache, so call function again
        return self.render(text, x, y, r, g, b, alpha)