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