#!/usr/bin/newlisp # # Simple OpenGL demo - tested with Slackware 10 and WinXP # # Read the history of the Utah Teapot here: http://sjbaker.org/teapot/ # # ------------- # Windows notes # ------------- # 1) The "opengl32.dll" library should be on your system already. # 2) Download the following additional libraries: # -GLUT library 3.7 from http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip ("glut32.dll") # or from http://www.xmission.com/~nate/glut.html ("glut32.dll") # -SDL library 1.2.x from http://www.libsdl.org/release/SDL-1.2.8-win32.zip ("SDL.dll") # # Put the downloaded DLL's in the same directory as your newLisp program or in your Windows system directory. # # ------------- # Linux notes # ------------- # All libraries should be on your system already. # # Enjoy! # Peter van Eerten, nov 9 2004 # #------------------------------------------------ # Load libraries (load "SDL.lsp" "GLUT.lsp" "GL.lsp") # Define this program to be the MAIN context (context 'MAIN) # Define lighting and coloring of objects (constant 'lightpos (pack "ffff" 2.0 2.0 -8.0 0.0)) (constant 'lightdif (pack "ffff" 1.0 1.0 1.0 1.0)) (constant 'lightamb (pack "fff" 0.15 0.15 0.15)) (constant 'lightspe (pack "ffff" 1.0 1.0 1.0 1.0)) (constant 'teapot_color (pack "ffff" 0.1 0.7 0.1 0.5)) (constant 'bitmap_font_color (pack "ffff" 1.0 0.0 0.0 0.0)) (constant 'stroke_font_color (pack "ffff" 0.0 0.0 1.0 0.0)) # Initialize OpenGL (define (GL_init) # SDL start video (if (< (SDL:SDL_Init SDL:SDL_INIT_VIDEO) 0) (begin (println "Could not initialize SDL!") (exit))) # Enable double buffering (SDL:SDL_GL_SetAttribute SDL:SDL_GL_DOUBLEBUFFER 1) # Set videomode (SDL:SDL_SetVideoMode 640 480 16 SDL:SDL_OPENGL) # Set title (SDL:SDL_WM_SetCaption "newLisp OpenGL example" "") # Set some variables (set 'x " ") ; x-coordinate from SDL (set 'y " ") ; y-coordinate from SDL (set 'rotx 0) ; rotation around x-axis (set 'roty 330) ; rotation around y-axis (set 'size 0.4) ; size of teapot # Reserve some memory for the SDL_Event struct (set 'event " ")) # Draw some text on the screen (define (bitmap_text txt) (for (l 0 (- (length txt) 1)) (GLUT:glutBitmapCharacter GLUT:GLUT_BITMAP_HELVETICA_18 (char (nth l txt)) ))) # Draw STROKE text on the screen (define (stroke_text txt) (for (l 0 (- (length txt) 1)) (GLUT:glutStrokeCharacter GLUT:GLUT_STROKE_ROMAN (char (nth l txt)) ))) # Draw teapot in angle (define (draw_all) # Define white as clearing color (GL:glClearColor (flt 1.0) (flt 1.0) (flt 1.0) (flt 0.0)) # Clear screen (GL:glClear (| GL:GL_COLOR_BUFFER_BIT GL:GL_DEPTH_BUFFER_BIT)) # Enable shading, depth and lighting (GL:glShadeModel GL:GL_SMOOTH) (GL:glEnable GL:GL_DEPTH_TEST) (GL:glEnable GL:GL_LIGHTING) (GL:glEnable GL:GL_LIGHT0) # Setup lighting (GL:glLightfv GL:GL_LIGHT0 GL:GL_POSITION lightpos) (GL:glLightfv GL:GL_LIGHT0 GL:GL_DIFFUSE lightdif) (GL:glLightfv GL:GL_LIGHT0 GL:GL_AMBIENT lightamb) (GL:glLightfv GL:GL_LIGHT0 GL:GL_SPECULAR lightspe) # Setup reflected color of object (GL:glMaterialfv GL:GL_FRONT GL:GL_AMBIENT_AND_DIFFUSE teapot_color) # Make sure we see the model (GL:glMatrixMode GL:GL_MODELVIEW) # Save current matrix (GL:glPushMatrix) # Rotate (GL:glRotatef (flt rotx) (flt 0.0) (flt 1.0) (flt 0.0)) (GL:glRotatef (flt roty) (flt 1.0) (flt 0.0) (flt 0.0)) # Dump rotated image (GLUT:glutSolidTeapot size) # Undo the last rotation (GL:glLoadIdentity) # Setup reflected color of font (GL:glMaterialfv GL:GL_FRONT GL:GL_AMBIENT_AND_DIFFUSE bitmap_font_color) # Determine position of bitmapped text (GL:glRasterPos2f (flt 0.0) (flt -0.8)) # Draw some bitmapped text (bitmap_text "The nicer GLUT bitmapped font.") # Setup reflected color of font (GL:glMaterialfv GL:GL_FRONT GL:GL_AMBIENT_AND_DIFFUSE stroke_font_color) # Determine position of STROKED text -> drawed so translate (GL:glTranslatef (flt -0.9) (flt 0.8) (flt 0.0)) # Setup scaling -> stroked characters are large, make smaller (GL:glScalef (flt 0.0006) (flt 0.0007) (flt 0.0)) # Draw some stroked text (stroke_text "The ugly GLUT Stroke font.") # Now put back the matrix (GL:glPopMatrix) # Dump buffer on screen (SDL:SDL_GL_SwapBuffers)) #------------------------------------------------------ # Program starts here (GL_init) # This is the mainloop (while (!= (& (get-int event) 15) SDL:SDL_QUIT) (begin # Draw rotated teapot + text (draw_all) # Wait for an event (SDL:SDL_WaitEvent event) # Check on left mousebutton (if (!= (& (SDL:SDL_GetRelativeMouseState x y) SDL:SDL_BUTTON_LEFT) 0) (begin (set 'rotx (- rotx (get-int x))) (set 'roty (- roty (get-int y))))) # Check on mousewheel (if (= (& (get-int event) 0x7FFFF) 0x50005) (set 'size (sub size 0.01))) (if (= (& (get-int event) 0x7FFFF) 0x40005) (set 'size (add size 0.01))) # Make sure rotation is a valid value (if (> rotx 359) (set 'rotx 0)) (if (< rotx 0) (set 'rotx 360)) (if (> roty 359) (set 'roty 0)) (if (< roty 0) (set 'roty 360)) )) # Leave SDL library (SDL:SDL_Quit) # Exit newLisp (exit)