mplisp

miniPicoLisp with FFI and modules for Buddy BDD library, OpenGL, Gtk and GMP
git clone https://logand.com/git/mplisp.git/
Log | Files | Refs

stereo-view.l (3785B)


      1 # 03mar08jk
      2 # 21oct07abu
      3 
      4 # To get a stereoscopic view, you must either cross your eyes so
      5 # the left and right scenes blend into one. This may take a little
      6 # training. Alternatively, you can use some optical stereo viewer,
      7 # but then you'll have to negate EyeAngle to swap the left and right
      8 # scenes on the screen.
      9 
     10 (load "@simul/gl/lib.l")
     11 
     12 (setq *WinWidth 1024 *WinHeight 720)
     13 (setq *AngleX 0.0 *AngleY 0.0)
     14 (setq *LastX 0 *LastY 0)
     15 (setq *EyeAngle 2.0)		# positive for x-eye, negative for viewer
     16 (setq *CameraDist -11.0)
     17 (setq *ObjectRotation 0.0)
     18 
     19 (de setViewPerspective (Width Height)
     20 	#(println "setViewPerspective" Width Height)
     21 	(gl:MatrixMode GL_PROJECTION)
     22 	(gl:LoadIdentity)
     23 	(glu:Perspective 45.0 (*/ Width 1.0 Height) 0.1 100.0)
     24 	(gl:MatrixMode GL_MODELVIEW) )
     25 
     26 (de initGL (Width Height)
     27 	# Set the OpenGL attributes to use with gl:Clear ...
     28 	(gl:ClearColor 0.6 0.8 0.9 0)	# the background color
     29 	(gl:ClearDepth 1.0)
     30 	
     31 	# Set up the depth buffer ...
     32 	(gl:DepthFunc GL_LESS)
     33 	(gl:Enable GL_DEPTH_TEST)
     34 	
     35 	# Set up antialiasing ...
     36 	
     37 	# Enable blending ...
     38 	(gl:Enable GL_BLEND)
     39 	(gl:BlendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA)
     40 	(gl:Enable GL_LINE_SMOOTH)
     41 	
     42 	# Enable materials and the default OpenGL light ...
     43 	(gl:Enable GL_COLOR_MATERIAL)
     44 	
     45 	(gl:Enable GL_LIGHTING)
     46 	(gl:Enable GL_LIGHT0)
     47 	
     48 	(setViewPerspective Width Height) )
     49 
     50 (de drawView (X Y W H ViewAngle)
     51 	#(println "drawView" X Y W H ViewAngle)
     52 	(gl:Viewport X Y W H)
     53 	(setViewPerspective W H)
     54 	
     55 	(gl:LoadIdentity)
     56 	(gl:Translatef 0.0 0.0 *CameraDist)
     57 	(gl:Rotatef (+ *AngleY ViewAngle) 0 1 0)
     58 	(gl:Rotatef *AngleX 1 0 0)
     59 	
     60 	# Brown teapot in front
     61 	(gl:PushMatrix)
     62 	(gl:Translatef 0.0 -0.6 1.6)
     63 	(gl:Rotatef (- *ObjectRotation) 0 1 0)
     64 	(gl:Color3f 0.8 0.2 0.0)
     65 	(glut:SolidTeapot 1.5)
     66 	(gl:PopMatrix)
     67 	
     68 	# Green teapot behind
     69 	(gl:PushMatrix)
     70 	(gl:Translatef -0.8 0.8 -1.6)
     71 	(gl:Rotatef *ObjectRotation 1 0 0)
     72 	(gl:Color3f 0.2 0.6 0.0)
     73 	(glut:SolidTeapot 1.5)
     74 	(gl:PopMatrix)
     75 )
     76 
     77 (de myMouse (Btn State X Y)
     78 	#(println "myMouse" Btn State X Y)
     79 	(setq *LastX X *LastY Y) )
     80 
     81 (de myMotion (X Y)
     82 	#(println "myMotion" X Y)
     83 	(inc '*AngleX (* (- Y *LastY) 1.0))
     84 	(inc '*AngleY (* (- X *LastX) 1.0))
     85 	(setq *LastX X *LastY Y)
     86    (glut:PostRedisplay) )
     87 
     88 (de myReshape (Width Height)
     89 	#(println "myReshape" Width Height)
     90 	(setq *WinWidth Width *WinHeight Height)
     91 	# Reset the current viewport and perspective transformation
     92 	(gl:Viewport 0 0 Width Height)
     93 	(setViewPerspective Width Height) )
     94 
     95 (de mySpecial (Key X Y)
     96 	#(println "mySpecial" Key X Y)
     97 	(cond
     98 		((= Key GLUT_KEY_UP) (inc '*CameraDist -1.0))
     99 		((= Key GLUT_KEY_DOWN) (inc '*CameraDist 1.0))
    100 		((= Key GLUT_KEY_LEFT) (inc '*EyeAngle -2.0) (println "*EyeAngle" *EyeAngle))
    101 		((= Key GLUT_KEY_RIGHT) (inc '*EyeAngle 2.0) (println "*EyeAngle" *EyeAngle)) ) )
    102 
    103 (de myTimer (Val)
    104 	#(println "myTimer")
    105 	(inc '*ObjectRotation 1.0)
    106 	(glut:PostRedisplay)
    107 	(timerFunc 500 myTimer 0) )
    108 
    109 (glut:Init)
    110 # Set display mode: RGBA color, Double buffer, Alpha support, Depth buffer
    111 (glut:InitDisplayMode (| GLUT_RGBA GLUT_DOUBLE GLUT_ALPHA GLUT_DEPTH))
    112 (glut:InitWindowSize *WinWidth *WinHeight)
    113 (glut:InitWindowPosition 10 50)
    114 (glut:CreateWindow "Stereo View")
    115 
    116 (initGL *WinWidth *WinHeight)
    117 
    118 
    119 (displayFunc ()
    120 	#(println "myDisplay")
    121 	(gl:Clear (| GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
    122 	(drawView 0 0 (/ *WinWidth 2) *WinHeight (- *EyeAngle))
    123 	(drawView (/ *WinWidth 2) 0 (/ *WinWidth 2) *WinHeight *EyeAngle)
    124 	(gl:Flush)
    125 	# Since this is double buffered, swap the buffers to display what just got drawn.
    126 	(glut:SwapBuffers) )
    127 
    128 (mouseFunc (Btn State X Y)
    129 	(myMouse Btn State X Y) )
    130 
    131 (motionFunc (X Y)
    132 	(myMotion X Y) )
    133 
    134 (reshapeFunc (Width Height)
    135 	(myReshape Width Height) )
    136 
    137 (specialFunc (Key X Y)
    138 	(mySpecial Key X Y) )
    139 
    140 (timerFunc 500 myTimer 0)
    141 
    142 (glut:MainLoop)