Making Samples executable. #!/usr/bin/env io and chmod a+x *.io
[io/jrb1.git] / addons / GLFW / samples / lesson08.io
blobf7fd19522c53090a9877670f5db0c71975524a78
1 #!/usr/bin/env io
3 # Port of http://glfw.sourceforge.net/tutorials/lesson08/lesson08.html
5 appendProto(GLFW)
6 appendProto(OpenGL)
8 draw := method(
9 # Get current time
10 t := glfwGetTime()
12 # Get window size
13 size := glfwGetWindowSize
15 # Make sure that height is non-zero to avoid division by zero
16 if(size height < 1, size setHeight(1))
18 # Set viewport
19 glViewport( 0, 0, size width, size height )
21 # Clear color and depth buffers
22 glClearColor( 0, 0, 0, 0 )
23 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
25 # Calculate field of view as a function of time
26 fieldOfView := (0.5 * t) sin * 30 + 50;
28 # Set up projection matrix
29 glMatrixMode( GL_PROJECTION ) # Select projection matrix
30 glLoadIdentity # Start with an identity matrix
31 gluPerspective( # Set perspective view
32 fieldOfView, # Field of view
33 size width / size height, # Window aspect (assumes square pixels)
34 1, # Near Z clipping plane
35 100 # Far Z clippling plane
38 # Calculate camera position
39 camera := vector((0.3 * t) cos * 20, (0.3 * t) sin * 20, t sin + 4)
41 # Set up modelview matrix
42 glMatrixMode( GL_MODELVIEW ) # Select modelview matrix
43 glLoadIdentity # Start with an identity matrix
44 gluLookAt( # Set camera position and orientation
45 camera x, camera y, camera z, # Camera position (x,y,z)
46 0, 0, 0, # View point (x,y,z)
47 0, 1, 0 # Up-vector (x,y,z)
50 # Enable Z buffering
51 glEnable( GL_DEPTH_TEST )
52 glDepthFunc( GL_LEQUAL )
54 # Draw a grid
55 glColor3f( 0.7, 1, 1 )
56 glBegin( GL_LINES )
57 (-10) to(10) foreach(i,
58 glVertex3f( -10, 0, i ) # Line along X axis
59 glVertex3f( 10, 0, i ) # -"-
60 glVertex3f( i, 0, -10 ) # Line along Z axis
61 glVertex3f( i, 0, 10 ) # -"-
63 glEnd
65 # Create a GLU quadrics object
66 quadric := gluNewQuadric
68 # Draw a blue cone in the center
69 glPushMatrix
70 glRotatef( -90, 1, 0, 0 )
71 glColor3f( 0, 0, 1 )
72 gluCylinder( quadric, 1.5, 0, 4, 30, 30 )
73 glPopMatrix
75 # Draw four spheres in the corners o the grid
76 glColor3f( 1, 0.2, 0 )
77 glPushMatrix
78 glTranslatef( -9, 1, -9 )
79 gluSphere( quadric, 1, 30, 30 )
80 glPopMatrix
81 glPushMatrix
82 glTranslatef( 9, 1, -9 )
83 gluSphere( quadric, 1, 30, 30 )
84 glPopMatrix
85 glPushMatrix
86 glTranslatef( -9, 1, 9 )
87 gluSphere( quadric, 1, 30, 30 )
88 glPopMatrix
89 glPushMatrix
90 glTranslatef( 9, 1, 9 )
91 gluSphere( quadric, 1, 30, 30 )
92 glPopMatrix
94 # Destroy the GLU quadrics object
95 gluDeleteQuadric( quadric )
98 glfwInit
100 glfwOpenWindow(
101 640, 480, # Width and height of window
102 8, 8, 8, # Number of red, green, and blue bits for color buffer
103 8, # Number of bits for alpha buffer
104 24, # Number of bits for depth buffer (Z-buffer)
105 0, # Number of bits for stencil buffer
106 GLFW_WINDOW # We want a desktop window (could be GLFW_FULLSCREEN)
109 glfwSetWindowTitle( "My OpenGL program" )
110 glfwEnable( GLFW_STICKY_KEYS )
112 while(glfwGetKey(GLFW_KEY_ESC) == 0 and glfwGetWindowParam(GLFW_OPENED) != 0,
113 draw
114 glfwSwapBuffers
117 glfwTerminate