Adapt the scene to the window size

Hi,
I’m tryng to make bounce a cube inside the window.
At the moment if I resize the window the cube bounce always in the same (scaled) area.

Can anybody help me?


from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# Initial square position and size
x1 = 0.0
y1 = 0.0
rsize = 25.
# Step size in x and y directions
# (number of pixels to move each time)
xstep = 1.0
ystep = 1.0
# Keep track of windows changing width and height
#windowWidth = 150.
#windowHeight = 100.


def RenderScene():
    """ Called to draw scene """
    # Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT)

    # Set current drawing color to red # R G B
    glColor3f(1.0, 0.0, 0.0)

    # Draw a filled rectangle with current color
    glRectf(x1, y1, x1 + rsize, y1 - rsize)  ## questo e il quadrato che rimbalza

    # Flush drawing commands and swap
    glutSwapBuffers()

def ChangeSize(w, h):
    global windowWidth, windowHeight
    windowWidth=150
    windowHeight=100
    """ Called by GLUT library when the window has changed size """
    # Prevent a divide by zero 
    if(h == 0): h = 1

    # Set Viewport to window dimensions 
    glViewport(0, 0, w, h)


    # Reset coordinate system 
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()

    # Establish clipping volume (left, right, bottom, top, near, far) 
    ar = float(w) / float(h)
    
    if (w <= h):
        #glOrtho (-100.0, 100.0, -100 / aspectRatio,100.0 / aspectRatio, 1.0, -1.0)
        glOrtho (-100, 100,          -100/ar, 100 / ar,             1.0, -1.0)
    else:
        #glOrtho (-100.0 * ar,100.0 * ar,     -100.0, 100.0,               1.0, -1.0)
        glOrtho (-100 * ar,100 * ar,     -100, 100,               1.0, -1.0)

    glMatrixMode(GL_MODELVIEW) 
    glLoadIdentity() 


def SetupRC():
    """ Set up the rendering state """
    # Set clear color to blue 
    glClearColor(0.0, 0.0, 1.0, 1.0) 


def TimerFunction(value):
    """ Called by GLUT library when idle (window not being resized or moved) """
    global x1,y1,rsize,xstep,ystep,windowWidth,windowHeight

    # Reverse direction when you reach left or right edge
    if (x1 > (windowWidth-rsize)) | (x1 < -windowWidth):
        xstep = -xstep
        
    # Reverse direction when you reach top or bottom edge
    if (y1 > windowHeight) | (y1 < (-windowHeight + rsize)):
        ystep = -ystep
        
    # Actually move the square
    x1 += xstep
    y1 += ystep
    
    # Check bounds. This is in case the window is made
    # smaller while the rectangle is bouncing and the
    # rectangle suddenly finds itself outside the new
    # clipping volume
    if(x1 > (windowWidth-rsize + xstep)):
        x1 = windowWidth-rsize-1
    elif(x1 < -(windowWidth + xstep)):
        x1 = - windowWidth -1
    if(y1 > (windowHeight + ystep)):
        y1 = windowHeight-1
    elif(y1 < -(windowHeight - rsize + ystep)):
        y1 = -windowHeight + rsize -1
    # Redraw the scene with new coordinates
    
    glutPostRedisplay()
    glutTimerFunc(5,TimerFunction, 1)

# Main program entry point
##Problema: Il viewport ha una dimenzione diversa dalla window
if __name__ == "__main__":
    glutInit()
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
    #glutInitWindowSize(800,600) ## dimensione iniziale della finestra
    glutCreateWindow("Bounce")
    glutDisplayFunc(RenderScene) ## definiamo come appare la scena
    glutReshapeFunc(ChangeSize)
    glutTimerFunc(33, TimerFunction, 1)
    SetupRC()
    glutMainLoop()


tnks !

windowWidth=150
windowHeight=100

should be

windowWidth=w
windowHeight=h