; this demo shows how to use lighting primitives ; here we also add color (load "jlib/JLIB.scm") (import "java.awt.event.KeyEvent") (define z (GLWrapper.)) (define tf (textfield "" 20)) (define errors (textarea 5 50)) (define t (maketagger)) (define settings (table 8 5 (label "pos: x y z") (t "px" (textfield "3" 10)) (t "py" (textfield "6" 10)) (t "pz" (textfield "5" 10)) (t "pa" (textfield "1" 10)) (label "amb: x y z") (t "ax" (textfield "1.0" 10)) (t "ay" (textfield "0.2" 10)) (t "az" (textfield "0.3" 10)) (t "aa" (textfield "1.0" 10)) (label "dif: x y z") (t "dx" (textfield "0.2" 10)) (t "dy" (textfield "1.0" 10)) (t "dz" (textfield "0.3" 10)) (t "da" (textfield "1.0" 10)) (label "spec: x y z") (t "sx" (textfield "0.3" 10)) (t "sy" (textfield "0.2" 10)) (t "sz" (textfield "1.0" 10)) (t "sa" (textfield "1.0" 10)) (label "mamb: x y z") (t "max" (textfield "1.0" 10)) (t "may" (textfield "1.0" 10)) (t "maz" (textfield "1.0" 10)) (t "maa" (textfield "1.0" 10)) (label "mdif: x y z") (t "mdx" (textfield "1.0" 10)) (t "mdy" (textfield "1.0" 10)) (t "mdz" (textfield "1.0" 10)) (t "mda" (textfield "1.0" 10)) (label "mspec: x y z") (t "msx" (textfield "1.0" 10)) (t "msy" (textfield "1.0" 10)) (t "msz" (textfield "1.0" 10)) (t "msa" (textfield "1.0" 10)) (label "mshininess: s") (t "msh" (textfield "1.0" 10)) (label "") (label "") (label "") )) (define gobutton (button "submit" (action(lambda(e) (set! lighting-changed #t))))) (define lighting-changed #t) (define w (window "test" (border (center z) (north (col settings gobutton)) (south (col tf errors))))) (define x0 0f) (define y0 0f) (define z0 0f) (define (readrow x y z a) (map (lambda(x) (.floatValue (readexpr x))) (map t (list x y z a)))) (.addKeyListener tf (jsint.Listener. (lambda(e) (let((key (.getKeyCode e))) (cond ((equal? key KeyEvent.VK_U$) (set! y0 (.floatValue (+ 0.1 y0)))) ((equal? key KeyEvent.VK_D$) (set! y0 (.floatValue (+ -0.1 y0)))) ((equal? key KeyEvent.VK_L$) (set! x0 (.floatValue (+ -0.1 x0)))) ((equal? key KeyEvent.VK_R$) (set! x0 (.floatValue (+ 0.1 x0)))) ((equal? key KeyEvent.VK_O$) (set! z0 (.floatValue (+ 0.1 z0)))) ((equal? key KeyEvent.VK_I$) (set! z0 (.floatValue (+ -0.1 z0)))) ((equal? key KeyEvent.VK_Q$) (java.lang.System.exit 0)) (else #t)))))) ; here we define an initialization procedure (more on this later...) (.init_proc$ z ; this gets called when the canvas is initialized (lambda (this . R) (tryCatch (let () (display (list 'init R))(newline) ) (lambda(e) (.append errors {Error in Init:[e]}) )) )) ;; end of init ... ; here we define the display procedure that redraws the screen every time... (.display_proc$ z (lambda (this) (tryCatch (let () (define gl (.gl$ this)) (define glj (.glj$# this)) (define glu (.glu$# this)) (define glut (gl4java.utils.glut.GLUTFuncLightImpl. gl glu)) (if lighting-changed ;; this happens when the submit button is pushed (begin (define mylightpos (list->array float.class (readrow "px" "py" "pz" "pa"))) (define mylightambient (list->array float.class (readrow "ax" "ay" "az" "aa"))) (define mylightdiffuse (list->array float.class (readrow "dx" "dy" "dz" "da"))) (define mylightspecular (list->array float.class (readrow "sx" "sy" "sz" "sa"))) (define mymaterialambient (list->array float.class (readrow "max" "may" "maz" "maa"))) (define mymaterialdiffuse (list->array float.class (readrow "mdx" "mdy" "mdz" "mda"))) (define mymaterialspecular (list->array float.class (readrow "msx" "msy" "msz" "msa"))) ; (.glLightModelfv gl gl4java.GLEnum.GL_LIGHT_MODEL_AMBIENT$ (list->array float.class (list 0.1f 0.2f 0.3f 1.0f))) (.glLightfv gl gl4java.GLEnum.GL_LIGHT0$ gl4java.GLEnum.GL_POSITION$ mylightpos) (.glLightfv gl gl4java.GLEnum.GL_LIGHT0$ gl4java.GLEnum.GL_AMBIENT$ mylightambient) (.glLightfv gl gl4java.GLEnum.GL_LIGHT0$ gl4java.GLEnum.GL_DIFFUSE$ mylightdiffuse) (.glLightfv gl gl4java.GLEnum.GL_LIGHT0$ gl4java.GLEnum.GL_SPECULAR$ mylightspecular) (.glMaterialfv gl gl4java.GLEnum.GL_FRONT$ gl4java.GLEnum.GL_AMBIENT$ mymaterialambient) (.glMaterialfv gl gl4java.GLEnum.GL_FRONT$ gl4java.GLEnum.GL_DIFFUSE$ mymaterialdiffuse) (.glMaterialfv gl gl4java.GLEnum.GL_FRONT$ gl4java.GLEnum.GL_SPECULAR$ mymaterialspecular) (.glMaterialf gl gl4java.GLEnum.GL_FRONT$ gl4java.GLEnum.GL_SHININESS$ (.floatValue (readexpr (t "msh")))) (.glEnable gl gl4java.GLEnum.GL_LIGHTING$) (.glEnable gl gl4java.GLEnum.GL_LIGHT0$) (.append errors {lighting changed\n[(.toString (readrow "px" "py" "pz" "pa"))]\n}) (set! lighting-changed #f) )) (writestring tf {position: (x,y,z)==[(.toString (list x0 y0 z0))]}) (.glClear gl (.intValue (| gl4java.GLEnum.GL_COLOR_BUFFER_BIT$ gl4java.GLEnum.GL_DEPTH_BUFFER_BIT$))) ;update view (.glMatrixMode gl gl4java.GLEnum.GL_MODELVIEW$) (.glLoadIdentity gl) (.gluLookAt glu (.floatValue x0) (.floatValue y0) (.floatValue z0) ; position of eye 0f 0f 0f ; where eye is looking 0f 1f 0f) ; vector pointing up ; draw a teapot ; (.glLoadIdentity gl) (.glutSolidTeapot glut 1.0) ; swap buffers and go (.gljSwap glj) (.gljCheckGL glj) (.gljFree glj) ) (lambda(e) (.append errors {Error: [e]})) ) )) (.pack w) (.show w) (.init z) (.start z) (.resize w 400 300) ;