]> git.jsancho.org Git - gacela.git/blob - src/gacela_draw.scm
5fbc92e6f65e2c7f6e2da579861f97da276b56fa
[gacela.git] / src / gacela_draw.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (define-macro (with-color color . code)
19   (cond (color
20          `(let ((original-color (get-current-color))
21                 (result #f))
22             (apply set-current-color ,color)
23             (set! result (begin ,@code))
24             (apply set-current-color original-color)
25             result))
26         (else `(begin ,@code))))
27
28 (define-macro (progn-textures . code)
29   `(let ((result #f))
30      (init-video-mode)
31      (glEnable GL_TEXTURE_2D)
32      (set! result (begin ,@code))
33      (glDisable GL_TEXTURE_2D)
34      result))
35
36 (define (draw . vertexes)
37   (begin-draw (length vertexes))
38   (draw-vertexes vertexes)
39   (glEnd))
40
41 (define (begin-draw number-of-points)
42   (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
43         ((= number-of-points 4) (glBegin GL_QUADS))))
44
45 (define (draw-vertexes vertexes)
46   (cond ((not (null? vertexes))
47          (draw-vertex (car vertexes))
48          (draw-vertexes (cdr vertexes)))))
49
50 (define* (draw-vertex vertex #:key texture-coord)
51   (cond ((list? (car vertex))
52          (with-color (car vertex)
53                      (apply simple-draw-vertex (cadr vertex))))
54         (else
55          (cond (texture-coord (apply glTexCoord2f texture-coord)))
56          (apply simple-draw-vertex vertex))))
57
58 (define* (simple-draw-vertex x y #:optional (z 0))
59   (cond ((3d-mode?) (glVertex3f x y z))
60         (else (glVertex2f x y))))
61
62 (define (load-image-for-texture filename)
63   (init-sdl)
64   (let ((image (IMG_Load filename)))
65     (cond (image
66            (SDL_SetAlpha image 0 0)
67            (let* ((width (surface-w image)) (height (surface-h image))
68                   (power-2 (nearest-power-of-two (min width height)))
69                   (resized-image #f))
70              (cond ((and (= width power-2) (= height power-2)) (values image width height))
71                    (else (set! resized-image (resize-surface image power-2 power-2))
72                          (if resized-image (values resized-image width height))))))
73           (else
74            (values #f 0 0)))))
75
76 (define (resize-surface surface width height)
77   (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
78     (cond ((and (= width old-width) (= height old-height)) surface)
79           (else (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
80                (zoomSurface surface zoomx zoomy 0))))))
81
82 (define* (load-texture filename #:key (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
83   (progn-textures
84    (receive
85     (image real-w real-h) (load-image-for-texture filename)
86     (cond (image
87            (let ((width (surface-w image)) (height (surface-h image))
88                  (byteorder (if (= SDL_BYTEORDER SDL_LIL_ENDIAN)
89                                 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)
90                                 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)))
91                  (texture (car (glGenTextures 1))))
92
93              (glBindTexture GL_TEXTURE_2D texture)
94              (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
95              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
96              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
97              (set-texture-size! texture real-w real-h)
98              texture))))))
99
100 (define* (draw-image filename #:optional (zoom 1))
101   (let ((texture (load-texture filename)))
102     (cond (texture (draw-texture texture zoom)))))
103
104 (define* (draw-texture texture #:optional (zoom 1))
105   (cond (texture
106          (let ((width (texture-w texture))
107                (height (texture-h texture)))
108            (draw-rectangle (* zoom width) (* zoom height) #:texture texture)))))
109
110 (define* (draw-quad v1 v2 v3 v4 #:key texture color)
111   (cond (texture
112          (progn-textures
113           (glBindTexture GL_TEXTURE_2D texture)
114           (begin-draw 4)
115           (draw-vertex v1 #:texture-coord '(0 0))
116           (draw-vertex v2 #:texture-coord '(1 0))
117           (draw-vertex v3 #:texture-coord '(1 1))
118           (draw-vertex v4 #:texture-coord '(0 1))
119           (glEnd)))
120         (color
121          (with-color color (draw v1 v2 v3 v4)))
122         (else
123          (draw v1 v2 v3 v4))))
124
125 (define* (draw-rectangle width height #:key texture color)
126   (let ((w (/ width 2)) (h (/ height 2)))
127     (draw-quad (list (- w) h 0)
128                (list w h 0)
129                (list w (- h) 0)
130                (list (- w) (- h) 0)
131                #:texture texture
132                #:color color)))
133
134 (define* (draw-square #:key (size 1) texture color)
135   (draw-rectangle size size #:texture texture #:color color))
136
137 (define* (draw-cube #:key (size 1)
138                    texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6
139                    color color-1 color-2 color-3 color-4 color-5 color-6)
140   (let ((-size (- size)))
141     (progn-textures
142      (glNormal3f 0 0 1)
143      (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) #:texture (or texture-1 texture) #:color (or color-1 color))
144      (glNormal3f 0 0 -1)
145      (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) #:texture (or texture-2 texture) #:color (or color-2 color))
146      (glNormal3f 0 1 0)
147      (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) #:texture (or texture-3 texture) #:color (or color-3 color))
148      (glNormal3f 0 -1 0)
149      (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture) #:color (or color-4 color))
150      (glNormal3f 1 0 0)
151      (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture) #:color (or color-5 color))
152      (glNormal3f -1 0 0)
153      (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture) #:color (or color-6 color)))))
154
155 (define* (add-light #:key light position ambient (id GL_LIGHT1) (turn-on t))
156   (init-lighting)
157   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
158   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
159   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
160   (and turn-on (glEnable id))
161   id)
162
163 (define* (translate x y #:optional (z 0))
164   (glTranslatef x y z))
165
166 (define* (rotate #:rest rot)
167   (cond ((3d-mode?) (apply 3d-rotate rot))
168         (else (apply 2d-rotate rot))))
169
170 (define (3d-rotate xrot yrot zrot)
171   (glRotatef xrot 1 0 0)
172   (glRotatef yrot 0 1 0)
173   (glRotatef zrot 0 0 1))
174
175 (define (2d-rotate rot)
176   (glRotatef rot 0 0 1))
177
178 (define (to-origin)
179   (glLoadIdentity)
180   (cond ((3d-mode?) (camera-look))))
181
182 (define set-camera #f)
183 (define camera-look #f)
184
185 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
186   (set! set-camera
187         (lambda* (#:key eye center up)
188           (cond (eye (set! camera-eye eye)))
189           (cond (center (set! camera-center center)))
190           (cond (up (set! camera-up up)))))
191
192   (set! camera-look
193         (lambda ()
194           (apply gluLookAt (append camera-eye camera-center camera-up)))))