]> git.jsancho.org Git - gacela.git/blob - gacela_draw.lisp
(no commit message)
[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                   resized-image)
75              (cond ((and (= width power-2) (= height power-2)) (values image width height))
76                    (t (setq resized-image (resize-surface image power-2 power-2))
77                       (SDL_FreeSurface image)
78                       (cond ((/= resized-image 0) (values resized-image width height))))))))))
79
80 (defun resize-surface (surface width height)
81   (let ((old-width (surface-w surface)) (old-height (surface-h surface)))
82     (cond ((and (= width old-width) (= height old-height)) surface)
83           (t (let ((zoomx (/ (+ width 0.5) old-width)) (zoomy (/ (+ height 0.5) old-height)))
84                (zoomSurface surface zoomx zoomy 0))))))
85
86 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
87   (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
88     (cond ((get-resource key) key)
89           (t (true-load-texture filename min-filter mag-filter static)))))
90
91 (defun true-load-texture (filename min-filter mag-filter static)
92   (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
93     (progn-textures
94      (multiple-value-bind
95       (image real-w real-h) (load-image-for-texture filename)
96       (cond (image
97              (let ((width (surface-w image)) (height (surface-h image))
98                    (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
99                                 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)
100                                 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)))
101                    (texture (car (glGenTextures 1))))
102
103                (glBindTexture GL_TEXTURE_2D texture)
104                (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
105                (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
106                (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
107                (SDL_FreeSurface image)
108                (set-resource key
109                              `(:id-texture ,texture :width ,real-w :height ,real-h)
110                              (lambda () (true-load-texture filename min-filter mag-filter static))
111                              (lambda () (glDeleteTextures 1 `(,texture)))
112                              :static static)
113                key)))))))
114
115 (defun draw-image (filename &ptional (zoom 1))
116   (let ((texture (load-texture filename)))
117     (cond (texture (draw-texture texture zoom)))))
118
119 (defun draw-texture (texture &optional (zoom 1))
120   (cond (texture
121          (let ((width (getf (get-resource texture) :width))
122                (height (getf (get-resource texture) :height)))
123            (draw-rectangle (* zoom width) (* zoom height) :texture texture)))))
124
125 (defun draw-quad (v1 v2 v3 v4 &key texture)
126   (let ((id-texture (getf (get-resource texture) :id-texture)))
127     (cond (id-texture
128            (progn-textures
129             (glBindTexture GL_TEXTURE_2D id-texture)
130             (begin-draw 4)
131             (draw-vertex v1 :texture-coord '(0 0))
132             (draw-vertex v2 :texture-coord '(1 0))
133             (draw-vertex v3 :texture-coord '(1 1))
134             (draw-vertex v4 :texture-coord '(0 1))
135             (glEnd)))
136           ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
137           (t (draw v1 v2 v3 v4)))))
138
139 (defun draw-rectangle (width height &key texture)
140   (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
141     (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
142
143 (defun draw-square (&key (size 1) texture)
144   (draw-rectangle size size :texture texture))
145
146 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
147   (let ((-size (neg size)))
148     (progn-textures
149      (glNormal3f 0 0 1)
150      (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
151      (glNormal3f 0 0 -1)
152      (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
153      (glNormal3f 0 1 0)
154      (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
155      (glNormal3f 0 -1 0)
156      (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
157      (glNormal3f 1 0 0)
158      (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
159      (glNormal3f -1 0 0)
160      (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
161
162 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
163   (init-lighting)
164   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
165   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
166   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
167   (and turn-on (glEnable id))
168   id)
169
170 (defun translate (x y &optional (z 0))
171   (glTranslatef x y z))
172
173 (defun rotate (&rest rot)
174   (cond ((3d-mode?) (apply #'3d-rotate rot))
175         (t (apply #'2d-rotate rot))))
176
177 (defun 3d-rotate (xrot yrot zrot)
178   (glRotatef xrot 1 0 0)
179   (glRotatef yrot 0 1 0)
180   (glRotatef zrot 0 0 1))
181
182 (defun 2d-rotate (rot)
183   (glRotatef rot 0 0 1))
184
185 (defun to-origin ()
186   (glLoadIdentity)
187   (cond ((3d-mode?) (camera-look))))
188
189 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
190   (defun set-camera (&key eye center up)
191     (cond (eye (setq camera-eye eye)))
192     (cond (center (setq camera-center center)))
193     (cond (up (setq camera-up up))))
194
195   (defun camera-look ()
196     (apply #'gluLookAt (concatenate 'list camera-eye camera-center camera-up))))