Initial commit of newLISP.
[newlisp.git] / examples / opengl-demo.lsp
blob054552cc505bef1a46c04132947725e70b21a567
1 #!/usr/bin/newlisp
3 # OpenGL and GLUT demo - opengl-demo.lsp
4 # currently tested working only on Win32
5 # Linux is untested and Mac OS X not working yet
7 # WIn32
8 # opengl32.dll - should be already on you WindowsXP installation or at www.opengl.org
9 # glut32.dll - available here: http://www.opengl.org/resources/libraries/glut/
11 # Linux/UNIX - not tested
12 # libGLU.so - should be on your Linux/UNIX installation or at www.opengl.org
13 # glut-3.7.so - already on your system or at:
14 # http://www.opengl.org/resources/libraries/glut/
16 # Mac OS X - cannot work on PPC, works well on Intel based Mac OS X
17 # libGL.dylib - installed in /System/Library/Frameworks/OpenGL.Framework
18 # GLUT - installed in /System/Library/Frameworks/GLUT.Framework
20 # for a complete function reference for OpenGL look here:
21 # http://rush3d.com/reference/opengl-bluebook-1.0/index.html
22 # linked from:
23 # http://www.opengl.org/documentation/
25 # for descriptions and refence to GLU see here:
26 # http://www.opengl.org/resources/libraries/glut/
28 # Calling patterns for OpenGL functions:
29 # Functions are described in the manual with the following types:
31 # All the following integer types are passed as is or with (int ...):
32 # GLenum GLboolean GLbitfield GLbyte GLshort GLint GLsizei GLubyte GLushort GLuint
33 # and types ending in: 3b, si, 3s
35 # The following double float types are passed as is or with (float ...):
36 # GLdouble GLclampd
37 # and types ending in: 3d
39 # The following small float types are must with (flt ...) conversion:
40 # GLfloat GLclampf
41 # and all types ending 3f:
45 (if
46 (= ostype "OSX") ;; drawings will be visible only on x86 based OS X (not tested)
47 (begin
48 (set 'GL_LIB "/System/Library/Frameworks/OpenGL.Framework/libraries/libGL.dylib")
49 (set 'GLUT_LIB "/System/Library/Frameworks/GLUT.Framework/GLUT")
51 (= ostype "Win32") ;; everything works
52 (begin
53 (set 'GL_LIB "opengl32.dll")
54 (set 'GLUT_LIB "glut32.dll"))
55 (= ostype "Linux") ;; not tested
56 (begin
57 (set 'GL_LIB "libGL.so")
58 (set 'GLUT_LIB "libglut.so"))
61 (import GL_LIB "glClear")
62 (import GL_LIB "glClearColor")
63 (import GL_LIB "glEnable")
64 (import GL_LIB "glColor3d")
65 (import GL_LIB "glBegin")
66 (import GL_LIB "glEnd")
67 (import GL_LIB "glVertex3d")
68 (import GL_LIB "glFlush")
69 (import GL_LIB "glFinish")
70 (import GL_LIB "glRotated")
72 (import GLUT_LIB "glutInit")
73 (import GLUT_LIB "glutDisplayFunc")
74 (import GLUT_LIB "glutInitWindowSize")
75 (import GLUT_LIB "glutInitDisplayMode")
76 (import GLUT_LIB "glutInitWindowPosition")
77 (import GLUT_LIB "glutDisplayFunc")
78 (import GLUT_LIB "glutKeyboardFunc")
79 (import GLUT_LIB "glutMouseFunc")
80 (import GLUT_LIB "glutMotionFunc")
81 (import GLUT_LIB "glutIdleFunc")
82 (import GLUT_LIB "glutCreateWindow")
83 (import GLUT_LIB "glutCreateSubWindow")
84 (import GLUT_LIB "glutMainLoop")
85 (import GLUT_LIB "glutSwapBuffers")
86 (import GLUT_LIB "glutPostRedisplay")
88 (import GLUT_LIB "glutSolidCube")
89 (import GLUT_LIB "glutSolidTeapot")
90 (import GLUT_LIB "glutWireCube")
91 (import GLUT_LIB "glutWireTeapot")
93 (constant 'GL_COLOR_BUFFER_BIT 0x00004000)
94 (constant 'GL_TRIANGLES 0x0004)
96 (constant 'GLUT_DOUBLE 0x0002)
97 (constant 'GLUT_SINGLE 0x0000)
98 (constant 'GLUT_RGB 0x0000)
99 (constant 'GLUT_RGBA 0x0000)
101 (set 'argc 0)
102 (set 'argv1 "")
103 (set 'argv (pack "lu" argv1))
104 (set 'rotx 0.0)
105 (set 'roty 0.0)
106 (set 'PI (mul (acos 0) 2))
108 (define (draw)
109 (glClear GL_COLOR_BUFFER_BIT )
110 (glRotated rotx 0.0 1.0 0.0)
111 (glRotated roty 1.0 0.0 0.0)
112 (glutWireTeapot 0.5)
113 (glutSwapBuffers)
116 (define (drawObject)
117 (glClear GL_COLOR_BUFFER_BIT )
118 (glColor3d 1.0 0.85 0.35)
119 (glBegin GL_TRIANGLES)
120 (glVertex3d 0.0 0.6 0.0)
121 (glVertex3d -0.2 -0.3 0.0)
122 (glVertex3d 0.2 -0.3 0.0)
123 (glEnd)
126 (define (rotation)
127 (set 'rotx (mod (sub rotx .01) PI))
128 (set 'roty (mod (sub roty .012) PI))
129 (sleep 30)
130 (glutPostRedisplay)
133 (define (keyboard key x y)
134 (if (= (& key 0xFF) 27) (exit)) ; 0xFF mask necessary in Win32
135 (println "key:" (& key 0xFF) " x:" x " y:" y))
137 (define (mouse button state x y)
138 (if (= state 0)
139 (glutIdleFunc 0) ; auto-motion off on button press
140 (glutIdleFunc (callback 4 'rotation))) ; auto-motinn on
141 (println "button: " button " state:" state " x:" x " y:" y))
143 (define (motion x y)
144 (set 'rotx (mul (div 200 x) PI))
145 (set 'roty (mul (div 150 y) PI))
146 (glutPostRedisplay)
147 (println "x:" x " y:" y))
149 (glutInit (address argc) (address argv))
150 (glutInitDisplayMode (| GLUT_RGB GLUT_DOUBLE ))
151 (glutInitWindowSize 800 600)
152 (glutInitWindowPosition 80 80)
153 (set 'id (glutCreateWindow "OpenGL and newLISP - drag mouse - ESC to exit"))
155 (glClearColor (flt 0.5) (flt 0.3) (flt 0.3) (flt 0.0))
156 (glColor3d 1.0 0.85 0.35)
158 (glutDisplayFunc (callback 0 'draw))
159 ;(glutDisplayFunc (callback 0 'drawObject))
160 (glutKeyboardFunc (callback 1 'keyboard))
161 (glutMouseFunc (callback 2 'mouse))
162 (glutMotionFunc (callback 3 'motion))
163 (glutIdleFunc (callback 4 'rotation))
164 (glutMainLoop)
166 ;; eof