]> git.jsancho.org Git - gacela.git/blob - gacela_draw.lisp
93575a36b578178f578e4ab5ab6d425d7f875ed1
[gacela.git] / gacela_draw.lisp
1 ;;; Gacela, a GNU Common Lisp 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 (eval-when (compile load eval)
19            (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20            (in-package 'gacela :nicknames '(gg) :use '(lisp)))
21
22
23 (defmacro with-color (color &body code)
24   (cond (color
25          `(let ((original-color (get-current-color))
26                 result)
27             (apply #'set-current-color ,color)
28             (setq result ,@code)
29             (apply #'set-current-color original-color)
30             result))
31         (t
32          `(progn
33             ,@code))))
34
35 (defmacro progn-textures (&body code)
36   `(let (values)
37      (init-video-mode)
38      (glEnable GL_TEXTURE_2D)
39      (setq values (multiple-value-list (progn ,@code)))
40      (glDisable GL_TEXTURE_2D)
41      (apply #'values values)))
42
43 (defun draw (&rest vertexes)
44   (begin-draw (length vertexes))
45   (draw-vertexes vertexes)
46   (glEnd))
47
48 (defun begin-draw (number-of-points)
49   (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
50         ((= number-of-points 4) (glBegin GL_QUADS))))
51
52 (defun draw-vertexes (vertexes)
53   (cond ((null vertexes) nil)
54         (t (draw-vertex (car vertexes))
55            (draw-vertexes (cdr vertexes)))))
56
57 (defun draw-vertex (vertex &key texture-coord)
58   (cond ((consp (car vertex))
59          (with-color (car vertex)
60                      (apply #'simple-draw-vertex (cadr vertex))))
61         (t (cond (texture-coord (apply #'glTexCoord2f texture-coord)))
62            (apply #'simple-draw-vertex vertex))))
63
64 (defun simple-draw-vertex (x y &optional (z 0))
65   (cond ((3d-mode?) (glVertex3f x y z))
66         (t (glVertex2f x y))))
67
68 (defun load-image-for-texture (filename)
69   (init-video-mode)
70   (let ((image (IMG_Load filename)))
71     (cond ((/= image 0)
72            (let* ((width (surface-w image)) (height (surface-h image))
73                   (power-2 (nearest-power-of-two (min width height)))
74                   zoomed-image)
75              (cond ((and (= width power-2) (= height power-2)) (values image width height))
76                    (t (setq zoomed-image (resize-image image power-2 power-2))
77                       (SDL_FreeSurface image)
78                       (cond ((/= zoomed-image 0) (values zoomed-image width height))))))))))
79
80 (defun resize-image (image width height)
81   (let ((old-width (surface-w image)) (old-height (surface-h image)))
82     (cond ((and (= width old-width) (= height old-height)) image)
83           (t (let ((zoomx (/ width old-width)) (zoomy (/ height old-height)) zoomed-image)
84                (setq zoomed-image (zoomSurface image zoomx zoomy 0))
85                (let ((new-width (surface-w zoomed-image)) (new-height (surface-h zoomed-image)))
86                  (cond ((or (and (= width new-width) (= height new-height))
87                             (and (= old-width new-width) (= old-height new-height))) zoomed-image)
88                        (t (let (rezoomed)
89                             (setq rezoomed (resize-image zoomed-image width height))
90                             (SDL_FreeSurface zoomed-image)
91                             rezoomed)))))))))
92
93 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
94   (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
95     (cond ((get-resource key) key)
96           (t (true-load-texture filename min-filter mag-filter static)))))
97
98 (defun true-load-texture (filename min-filter mag-filter static)
99   (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
100     (progn-textures
101      (multiple-value-bind
102       (image real-w real-h) (load-image-for-texture filename)
103       (cond (image
104              (let ((width (surface-w image)) (height (surface-h image))
105                    (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
106                                 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
107                                 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
108                    (texture (car (glGenTextures 1))))
109
110                (glBindTexture GL_TEXTURE_2D texture)
111                (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
112                (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
113                (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
114                (SDL_FreeSurface image)
115                (set-resource key
116                              `(:id-texture ,texture :width ,real-w :height ,real-h)
117                              (lambda () (true-load-texture filename min-filter mag-filter static))
118                              (lambda () (glDeleteTextures 1 `(,texture)))
119                              :static static)
120                key)))))))
121
122 (defun draw-image-function (filename)
123   (let ((texture (load-texture filename)))
124     (lambda (&optional (f 1))
125       (cond (texture
126              (let ((width (getf (get-resource texture) :width))
127                    (height (getf (get-resource texture) :height)))
128                (draw-rectangle (* f width) (* f height) :texture texture)))))))
129
130 (defun draw-quad (v1 v2 v3 v4 &key texture)
131   (let ((id-texture (getf (get-resource texture) :id-texture)))
132     (cond (id-texture
133            (progn-textures
134             (glBindTexture GL_TEXTURE_2D id-texture)
135             (begin-draw 4)
136             (draw-vertex v1 :texture-coord '(0 0))
137             (draw-vertex v2 :texture-coord '(1 0))
138             (draw-vertex v3 :texture-coord '(1 1))
139             (draw-vertex v4 :texture-coord '(0 1))
140             (glEnd)))
141           ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
142           (t (draw v1 v2 v3 v4)))))
143
144 (defun draw-rectangle (width height &key texture)
145   (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
146     (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
147
148 (defun draw-square (&key (size 1) texture)
149   (draw-rectangle size size :texture texture))
150
151 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
152   (let ((-size (neg size)))
153     (progn-textures
154      (glNormal3f 0 0 1)
155      (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
156      (glNormal3f 0 0 -1)
157      (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
158      (glNormal3f 0 1 0)
159      (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
160      (glNormal3f 0 -1 0)
161      (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
162      (glNormal3f 1 0 0)
163      (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
164      (glNormal3f -1 0 0)
165      (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
166
167 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
168   (init-lighting)
169   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
170   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
171   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
172   (and turn-on (glEnable id))
173   id)
174
175 (defun translate (x y &optional (z 0))
176   (glTranslatef x y z))
177
178 (defun rotate (&rest rot)
179   (cond ((3d-mode?) (apply #'3d-rotate rot))
180         (t (apply #'2d-rotate rot))))
181
182 (defun 3d-rotate (xrot yrot zrot)
183   (glRotatef xrot 1 0 0)
184   (glRotatef yrot 0 1 0)
185   (glRotatef zrot 0 0 1))
186
187 (defun 2d-rotate (rot)
188   (glRotatef rot 0 0 1))
189
190 (defun to-origin ()
191   (glLoadIdentity)
192   (cond ((3d-mode?) (camera-look))))
193
194 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
195   (defun set-camera (&key eye center up)
196     (cond (eye (setq camera-eye eye)))
197     (cond (center (setq camera-center center)))
198     (cond (up (setq camera-up up))))
199
200   (defun camera-look ()
201     (apply #'gluLookAt (concatenate 'list camera-eye camera-center camera-up))))