#!/usr/bin/newlisp # # Simple OpenGL demo - tested with Slackware 10 and WinXP # # ---> Requires newLisp 8.3.0 or higher <--- # # 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 22 2004 # #------------------------------------------------ # Determine which library to use first. (if (= (last (sys-info)) 6) (begin # Define libs for Windows (constant 'SDL "SDL.dll") (constant 'GL "opengl32.dll") (constant 'GLUT "glut32.dll") # Fonts are handled differently in Win32 (constant 'GLUT_BITMAP_HELVETICA_18 8) (constant 'GLUT_STROKE_ROMAN 0) ) (begin # Define libs for Linux (constant 'SDL "libSDL-1.2.so.0") (constant 'GL "libGL.so") (constant 'GLUT "libglut.so") # Get the memory address of the font (import GLUT "glutBitmapHelvetica18") (constant 'GLUT_BITMAP_HELVETICA_18 (address glutBitmapHelvetica18)) (import GLUT "glutStrokeRoman") (constant 'GLUT_STROKE_ROMAN (address glutStrokeRoman)) ) ) # Define SDL constants (constant 'SDL_INIT_VIDEO 0x00000020) (constant 'SDL_OPENGL 0x00000002) (constant 'SDL_FULLSCREEN 0x80000000) (constant 'SDL_GL_DOUBLEBUFFER 5) (constant 'SDL_MOUSEBUTTONDOWN 5) (constant 'SDL_QUIT 12) (constant 'SDL_BUTTON_LEFT 1) # Define GL constants (constant 'GL_COLOR_BUFFER_BIT 0x4000) (constant 'GL_DEPTH_BUFFER_BIT 0x0100) (constant 'GL_SMOOTH 0x1D01) (constant 'GL_DEPTH_TEST 0x0B71) (constant 'GL_LIGHTING 0x0b50) (constant 'GL_LIGHT0 0x4000) (constant 'GL_POSITION 0x1203) (constant 'GL_DIFFUSE 0x1201) (constant 'GL_AMBIENT 0x1200) (constant 'GL_SPECULAR 0x1202) (constant 'GL_FRONT 0x0404) (constant 'GL_AMBIENT_AND_DIFFUSE 0x1602) (constant 'GL_MODELVIEW 0x1700) # Import SDL functions (import SDL "SDL_Init") (import SDL "SDL_SetVideoMode") (import SDL "SDL_GL_SwapBuffers") (import SDL "SDL_WM_SetCaption") (import SDL "SDL_Quit") (import SDL "SDL_GL_SetAttribute") (import SDL "SDL_GetRelativeMouseState") (import SDL "SDL_WaitEvent") # Import GL functions (import GL "glClearColor") (import GL "glClear") (import GL "glShadeModel") (import GL "glEnable") (import GL "glLightfv") (import GL "glMaterialfv") (import GL "glMatrixMode") (import GL "glPushMatrix") (import GL "glRotatef") (import GL "glPopMatrix") (import GL "glRasterPos2f") (import GL "glLoadIdentity") (import GL "glScalef") (import GL "glTranslatef") # Import GLUT functions (import GLUT "glutSolidTeapot") (import GLUT "glutBitmapCharacter") (import GLUT "glutStrokeCharacter") # 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_Init SDL_INIT_VIDEO) 0) (begin (println "Could not initialize SDL!") (exit))) # Enable double buffering (SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER 1) # Set videomode (SDL_SetVideoMode 640 480 16 SDL_OPENGL) # Set title (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)) (glutBitmapCharacter GLUT_BITMAP_HELVETICA_18 (char (nth l txt)) ))) # Draw STROKE text on the screen (define (stroke_text txt) (for (l 0 (- (length txt) 1)) (glutStrokeCharacter GLUT_STROKE_ROMAN (char (nth l txt)) ))) # Draw teapot in angle (define (draw_all) # Define white as clearing color (glClearColor (flt 1.0) (flt 1.0) (flt 1.0) (flt 0.0)) # Clear screen (glClear (| GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)) # Enable shading, depth and lighting (glShadeModel GL_SMOOTH) (glEnable GL_DEPTH_TEST) (glEnable GL_LIGHTING) (glEnable GL_LIGHT0) # Setup lighting (glLightfv GL_LIGHT0 GL_POSITION lightpos) (glLightfv GL_LIGHT0 GL_DIFFUSE lightdif) (glLightfv GL_LIGHT0 GL_AMBIENT lightamb) (glLightfv GL_LIGHT0 GL_SPECULAR lightspe) # Setup reflected color of object (glMaterialfv GL_FRONT GL_AMBIENT_AND_DIFFUSE teapot_color) # Make sure we see the model (glMatrixMode GL_MODELVIEW) # Save current matrix (glPushMatrix) # Rotate (glRotatef (flt rotx) (flt 0.0) (flt 1.0) (flt 0.0)) (glRotatef (flt roty) (flt 1.0) (flt 0.0) (flt 0.0)) # Dump rotated image (glutSolidTeapot size) # Undo the last rotation (glLoadIdentity) # Setup reflected color of font (glMaterialfv GL_FRONT GL_AMBIENT_AND_DIFFUSE bitmap_font_color) # Determine position of bitmapped text (glRasterPos2f (flt 0.0) (flt -0.8)) # Draw some bitmapped text (bitmap_text "The nicer GLUT bitmapped font.") # Setup reflected color of font (glMaterialfv GL_FRONT GL_AMBIENT_AND_DIFFUSE stroke_font_color) # Determine position of STROKED text -> drawed so translate (glTranslatef (flt -0.9) (flt 0.8) (flt 0.0)) # Setup scaling -> stroked characters are large, make smaller (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 (glPopMatrix) # Dump buffer on screen (SDL_GL_SwapBuffers)) #------------------------------------------------------ # Program starts here (GL_init) # This is the mainloop (while (!= (& (get-int event) 15) SDL_QUIT) (begin # Draw rotated teapot + text (draw_all) # Wait for an event (SDL_WaitEvent event) # Check on left mousebutton (if (!= (& (SDL_GetRelativeMouseState x y) 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_Quit) # Exit newLisp (exit)