Last active
December 28, 2015 00:59
-
-
Save kristianlm/5c5a7e07065ce9483b3f to your computer and use it in GitHub Desktop.
CHICKEN glls example on android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use sdl2 miscmacros (prefix opengl-glew gl:) glls-render gl-utils gl-math) | |
;; unhappy REPL on Android? (import (prefix opengl-glew gl:)) | |
(define window (create-window! "sokoworld" 0 0 640 480 '(resizable opengl))) | |
(gl-attribute-set! 'context-major-version 3) | |
(gl-attribute-set! 'context-minor-version 0) | |
(gl-create-context! window) | |
(gl:init) | |
(define rect (make-mesh vertices: '(attributes: ((position #:float 2) | |
(color #:unsigned-byte 3 | |
normalized: #t)) | |
initial-elements: ((position . (-1 -1 | |
1 -1 | |
1 1 | |
-1 1)) | |
(color . (1 0 0 | |
0 1 0 | |
0 0 1 | |
1 0 1)))) | |
indices: '(type: #:ushort | |
initial-elements: (0 1 2 | |
0 2 3)))) | |
(define projection-matrix (mat4-identity #t)) | |
(define view-matrix (mat4-identity #t)) | |
;; reset it all: | |
;; (mat4-identity projection-matrix) (mat4-identity view-matrix) | |
(define (update-view-matrix!) | |
(receive (w h) (window-size window) | |
(define aspect (/ w h)) | |
(define s 1.5) (define -s -1.5) | |
(ortho-viewport (* -s aspect) (* s aspect) -s s ;; lrbt | |
-1 1 ;; near far | |
-1 1 -1 1 ;; lrbt viewport | |
projection-matrix))) | |
;;; Pipeline definition | |
(begin | |
(define-pipeline simple-shader | |
((#:vertex version: 300 | |
input: ((position #:vec2) (color #:vec3)) | |
uniform: ((projection #:mat4) (view #:mat4)) | |
output: ((c #:vec3))) | |
(define (main) #:void | |
;;(set! model (vec4 1)) | |
(set! gl:position (* projection view (vec4 position 0.0 1.0))) | |
(set! c color))) | |
((#:fragment version: 300 | |
input: ((c (highp #:vec3))) | |
output: ((frag-color (highp #:vec4)))) | |
(define (main) #:void | |
(set! frag-color (vec4 0 (.. c x) 1 1))))) | |
(compile-pipeline simple-shader) | |
(set! renderable | |
(make-simple-shader-renderable mesh: rect | |
projection: projection-matrix | |
view: view-matrix))) | |
(define renderable #f) | |
(define (render) | |
(unless (mesh-vao rect) | |
;;(compile-pipeline simple-shader) ;; <-- needed for OpenGL ES? | |
(mesh-make-vao! rect (pipeline-mesh-attributes simple-shader) #:stream) | |
(set! renderable (make-simple-shader-renderable mesh: rect | |
view: view-matrix | |
projection: projection-matrix))) | |
(rotate-z 0.01 view-matrix) | |
(render-simple-shader renderable)) | |
(define (handle event) | |
(case (event-type event) | |
((window) | |
(receive (w h) (window-size window) | |
(gl:viewport 0 0 w h) | |
(update-view-matrix!))) | |
(else (print "unhandled " event)))) | |
(define (core-iteration) | |
(while* (poll-event!) (handle it)) | |
(gl-swap-window! window) | |
(gl:clear (bitwise-ior gl:+color-buffer-bit+)) | |
(render)) | |
(define frame 0) | |
;; (thread-state game-thread) | |
(begin (handle-exceptions e void (thread-terminate! game-thread)) | |
(define game-thread | |
(thread-start! | |
(lambda () | |
(let loop ((n 0) | |
(t (current-milliseconds))) | |
;;(thread-sleep! 0.2) | |
(thread-yield!) | |
(core-iteration) | |
(cond ((> (- (current-milliseconds) t) 2000) | |
(set! fps (inexact->exact (round (/ n (/ (- (current-milliseconds) t) 1000))))) | |
(set! t (current-milliseconds)) | |
(set! n 0))) | |
(set! frame (add1 frame)) | |
(loop (add1 n) t))))) | |
(thread-quantum-set! game-thread 500000)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use sdl2 miscmacros (prefix opengl-glew gl:) glls-render gl-utils gl-math) | |
;; unhappy REPL on Android? (import (prefix opengl-glew gl:)) | |
(define window (create-window! "sokoworld" 0 0 640 480 '(resizable opengl))) | |
(gl-attribute-set! 'context-major-version 3) | |
(gl-attribute-set! 'context-minor-version 0) | |
(gl-create-context! window) | |
(gl:init) | |
(define rect (make-mesh vertices: '(attributes: ((position #:float 2) | |
(color #:unsigned-byte 3 | |
normalized: #t)) | |
initial-elements: ((position . (-1 -1 | |
1 -1 | |
1 1 | |
-1 1)) | |
(color . (1 0 0 | |
0 1 0 | |
0 0 1 | |
1 0 1)))) | |
indices: '(type: #:ushort | |
initial-elements: (0 1 2 | |
0 2 3)))) | |
;;; Matrices | |
(define projection-matrix | |
(perspective 640 480 0.1 100 70)) | |
(define mvp (ortho-viewport -1 1 -1 1 | |
0.1 10 | |
0 600 0 400 | |
#t)) | |
(define view-matrix | |
(look-at (make-point 1 0 3) | |
(make-point 0 0 0) | |
(make-point 0 1 0))) | |
(define model-matrix (mat4-identity)) | |
(define mvp (m* projection-matrix | |
(m* view-matrix model-matrix) | |
#t ; Matrix should be in a non-GC'd area | |
)) | |
;;; Pipeline definition | |
(begin | |
(define-pipeline simple-shader | |
((#:vertex version: 300 | |
input: ((position #:vec2) (color #:vec3)) | |
uniform: ((mvp #:mat4)) | |
output: ((c #:vec3))) | |
(define (main) #:void | |
(set! gl:position (* mvp (vec4 position 0.0 1.0))) | |
(set! c color))) | |
((#:fragment version: 300 | |
input: ((c (highp #:vec3))) | |
output: ((frag-color (highp #:vec4)))) | |
(define (main) #:void | |
(set! frag-color (vec4 c 1))))) | |
(compile-pipeline simple-shader)) | |
(define renderable #f) | |
(define (render) | |
(unless (mesh-vao rect) | |
;;(compile-pipeline simple-shader) ;; <-- needed for OpenGL ES? | |
(mesh-make-vao! rect (pipeline-mesh-attributes simple-shader) #:stream) | |
(set! renderable (make-simple-shader-renderable mesh: rect mvp: mvp))) | |
(rotate-z 0.001 mvp) | |
(render-simple-shader renderable)) | |
(define (handle event) | |
(case (event-type event) | |
((window) | |
(receive (w h) (window-size window) | |
(gl:viewport 0 0 w h))) | |
(else (print "unhandled " event)))) | |
(define (core-iteration) | |
(while* (poll-event!) (handle it)) | |
(gl-swap-window! window) | |
(gl:clear-color 1 0 1 1) | |
(gl:clear (bitwise-ior gl:+color-buffer-bit+)) | |
(render)) | |
(define frame 0) | |
;; (thread-state game-thread) | |
(begin (handle-exceptions e void (thread-terminate! game-thread)) | |
(define game-thread | |
(thread-start! | |
(lambda () | |
(let loop ((n 0) | |
(t (current-milliseconds))) | |
;;(thread-sleep! 0.05) | |
(thread-yield!) | |
(core-iteration) | |
(cond ((> (- (current-milliseconds) t) 2000) | |
(set! fps (inexact->exact (round (/ n (/ (- (current-milliseconds) t) 1000))))) | |
(set! t (current-milliseconds)) | |
(set! n 0))) | |
(set! frame (add1 frame)) | |
(loop (add1 n) t))))) | |
(thread-quantum-set! game-thread 500000)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got this working on my Nexus 4 and Nexus 9. It's a little rough around the edges, but a lot of stuff is working!