# This code example is created for educational purpose
# by Thorsten Thormaehlen (contact: www.thormae.de).
# It is distributed without any warranty.

# Requires PyOpenGL, which can be install with:
# pip install PyOpenGL PyOpenGL_accelerate

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys

class Renderer:
    def __init__(self):
        pass

    # public member functions
    def init(self):
        pass

    def resize(self, w, h):
        glViewport(0, 0, w, h)

    def display(self):
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)
        glColor3f(1.0, 1.0, 1.0)
        glBegin(GL_TRIANGLES)
        glVertex3f(-0.5, -0.5, 0.0) # vertex on the left
        glVertex3f(0.5, -0.5, 0.0)  # vertex on the right
        glVertex3f(0.0, 0.5, 0.0)   # vertex at the top of the triangle
        glEnd()

    def dispose(self):
        pass

# --- Global Renderer Instance ---
renderer = Renderer()

# --- GLUT Callbacks ---
def glut_resize(w, h):
    renderer.resize(300, 300)

def glut_display():
    renderer.display()
    glutSwapBuffers()
    
def glut_close():
    renderer.dispose()

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
    glutInitWindowPosition(100, 100)
    glutInitWindowSize(320, 320)
    
    glutCreateWindow(b"FirstTriangle")

    # Register Callbacks
    glutDisplayFunc(glut_display)
    glutReshapeFunc(glut_resize)
    glutCloseFunc(glut_close)
    
    # Initialize Renderer
    renderer.init()

    # Enter the main event loop
    glutMainLoop()

if __name__ == "__main__":
    main()