# -*- coding: cp1252 -*-
# Importazione dei moduli necessari all' esecuzione delle istruzioni del programma:
###################################################################################
import sys
from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
########################
#Dichiarazione delle variabili globali:
#######################################
ESCAPE = '\033'
window = 0 # numero di finestre GLUT.
#####################################
# Definizione delle funzioni:
#############################
# Le callback:
#############
# La funzione generale di inizializzazione di OpenGL setta tutti i parametri iniziali e viene
# chiamata come penultima istruzione nella definizione del main, prima di glut.MainLoop();
def InitGL(Width, Height):
glClearColor(0.0, 0.0, 0.0, 0.0) # imposta il colore di sfondo della finestra;
glClearDepth(1.0) # imposta il valore libero per l'amplificatore di profondità;
glDepthFunc(GL_LESS) # imposta il valore usato per i confronti dell'amplificatore
# di profondità;
glEnable(GL_DEPTH_TEST) # abilita il valore precedentemente specificato;
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION)
glLoadIdentity() # Reset The Projection Matrix
# Calculate The Aspect Ratio Of The Window
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# La funzione chiamata quando la finestra è ridimensionata (which shouldn't happen if you enable
# fullscreen, below)
def ReSizeGLScene(Width, Height):
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small
Height = 1
glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# La funzione principale di disegno:
def DrawGLScene():
# Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity() # Reset The View
# since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers()
# La funzione chiamata quando è premuto un pulsante. Note the use of Python tuples to pass
# in: (key, x, y)
def keyPressed(*args):
if args[0] == ESCAPE: # if escape is pressed, kill everything.
glutDestroyWindow(window)
sys.exit()
################################################################################################
# Il main:
##########
def main():
global window
glutInit(sys.argv) # richiede al sistema operativo le risorse per aprire una finestra su cui
# disegnare;
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) # setta le
# caratteristiche di tale finestra tramite OR di maschere di bit.
# Creazione della finestra:
glutInitWindowSize(640, 480) # fissa le dimensioni a h * w;
glutInitWindowPosition(0, 0) # fissa la posizione del vertice superiore sinistro della
# finestra a (x,y);
window = glutCreateWindow("Titolo della finestra") # crea una finestra con un titolo.
glutDisplayFunc(DrawGLScene) # Specifica la funzione di rendering.
# Uncomment this line to get full screen.
#glutFullScreen()
glutIdleFunc(DrawGLScene) # Registra la funzione di idle, ovvero la funzione che viene
# chiamata quando non ci sono input da parte dell’utente.
# Register the function called when our window is resized.
glutReshapeFunc(ReSizeGLScene)
glutKeyboardFunc(keyPressed) # Registra la funzione che gestisce la tastiera normale.
# Initialize our window.
InitGL(640, 480)
glutMainLoop() # Entra nel loop principale dell'applicazione.
################################################################################################
# Programma:
############
# Print message to console, and kick off the main to get it rolling.
print "Hit ESC key to quit."
main()