pythonpygletorthographic

Why this octagon doesn't fit window?


I was trying to create an octagon:

import pyglet
from pyglet.gl import *

class mywindow(pyglet.window.Window):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.set_minimum_size(300,300)

    def on_draw(self):


        self.clear()    
        glBegin(GL_POLYGON)
        glColor3ub(255,0,0)
        glVertex2f(0,0)
        glVertex2f(1.0,0)
        glVertex2f(1.5,0.5)
        glVertex2f(1.5,1.5)
        glVertex2f(1.0,2.0)
        glVertex2f(0,2.0)
        glVertex2f(-0.5,1.5)
        glVertex2f(-0.5,0.5)
        glEnd()

    def on_resize(self, width, height):
        glViewport(10,10, width, height)
window = mywindow(300,300,"deneme",True)
pyglet.app.run()

everything seems fine. But when I run this code I see this output: . any idea how can I fix it?


Solution

  • Most of the octagon is out of the viewport. By default the bottom left coordinate of the viewport is (-1, -1) and the top right is (1, 1).
    You can set an orthographic projection matrix, to change the projected area (respectively volume), by glOrtho:

    class mywindow(pyglet.window.Window):
        # [...]
    
        def on_resize(self, width, height):
            glViewport(0, 0, width, height)
           
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            glOrtho(-3, 3, -3, 3, -1, 1)
    
            glMatrixMode(GL_MODELVIEW)
            glLoadIdentity()