#lang racket/gui
(require (planet "rgl.rkt" ("stephanh" "RacketGL.plt" 1 2)))
(require ffi/vector)
(require "viewer.rkt")
(define (load-program-source shader port)
(let* ((lines (for/vector ((line (in-lines port))) line))
(sizes (for/list ((line (in-vector lines))) (string-length line)))
(sizes (list->s32vector sizes)))
(glShaderSource shader (vector-length lines) lines sizes)))
(define (load-program port)
(let ((program (glCreateProgram))
(shader (glCreateShader GL_FRAGMENT_SHADER)))
(load-program-source shader port)
(glCompileShader shader)
(glAttachShader program shader)
(glLinkProgram program)
program))
(define program #f)
(define (setup)
(if (gl-version-at-least? '(2 0))
(set! program (call-with-input-file "test.glsl" load-program))
(printf "This OpenGL does not support shaders, you'll get a plain white rectangle.~%")))
(define (draw)
(define vertex-array
(f64vector -0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5))
(define texcoord-array
(f64vector 0 0
0.5 0
0.5 0.5
0 0.5))
(when program
(glUseProgram program))
(let-values (((type cptr) (gl-vector->type/cpointer vertex-array)))
(glVertexPointer 2 type 0 cptr))
(let-values (((type cptr) (gl-vector->type/cpointer texcoord-array)))
(glTexCoordPointer 2 type 0 cptr))
(glEnableClientState GL_VERTEX_ARRAY)
(glEnableClientState GL_TEXTURE_COORD_ARRAY)
(glDrawArrays GL_QUADS 0 4)
(glDisableClientState GL_TEXTURE_COORD_ARRAY)
(glDisableClientState GL_VERTEX_ARRAY)
(when program
(glUseProgram 0)))
(view draw setup)