1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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)))
23 (defmacro with-color (color &body code)
25 `(let ((original-color (get-current-color))
27 (apply #'set-current-color ,color)
29 (apply #'set-current-color original-color)
35 (defmacro progn-textures (&body code)
38 (glEnable GL_TEXTURE_2D)
39 (setq values (multiple-value-list (progn ,@code)))
40 (glDisable GL_TEXTURE_2D)
41 (apply #'values values)))
43 (defun draw (&rest vertexes)
44 (begin-draw (length vertexes))
45 (draw-vertexes vertexes)
48 (defun begin-draw (number-of-points)
49 (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
50 ((= number-of-points 4) (glBegin GL_QUADS))))
52 (defun draw-vertexes (vertexes)
53 (cond ((null vertexes) nil)
54 (t (draw-vertex (car vertexes))
55 (draw-vertexes (cdr vertexes)))))
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))))
64 (defun simple-draw-vertex (x y &optional (z 0))
65 (cond ((3d-mode?) (glVertex3f x y z))
66 (t (glVertex2f x y))))
68 (defun load-image-for-texture (filename)
70 (let ((image (IMG_Load filename)))
72 (let* ((width (surface-w image)) (height (surface-h image))
73 (power-2 (nearest-power-of-two (min width height)))
74 (zoomx (/ power-2 width)) (zoomy (/ power-2 height))
76 (cond ((and (= zoomx 1) (= zoomy 1)) (values image width height))
77 (t (setq zoomed-image (zoomSurface image zoomx zoomy 0))
78 (SDL_FreeSurface image)
79 (cond ((/= zoomed-image 0) (values zoomed-image width height))))))))))
81 (defun load-texture (filename &key (min-filter GL_LINEAR) (mag-filter GL_LINEAR) static)
82 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
83 (cond ((get-resource key) key)
84 (t (true-load-texture filename min-filter mag-filter static)))))
86 (defun true-load-texture (filename min-filter mag-filter static)
87 (let ((key (make-resource-texture :filename filename :min-filter min-filter :mag-filter mag-filter)))
90 (image real-w real-h) (load-image-for-texture filename)
92 (let ((width (surface-w image)) (height (surface-h image))
93 (byteorder (if (= (SDL_ByteOrder) SDL_LIL_ENDIAN)
94 (if (= (surface-format-BytesPerPixel image) 3) GL_RGB GL_RGBA)
95 (if (= (surface-format-BytesPerPixel image) 3) GL_BGR GL_BGRA)))
96 (texture (car (glGenTextures 1))))
97 (glBindTexture GL_TEXTURE_2D texture)
98 (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 byteorder GL_UNSIGNED_BYTE (surface-pixels image))
99 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
100 (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
101 (SDL_FreeSurface image)
103 `(:id-texture ,texture :width ,real-w :height ,real-h)
104 (lambda () (true-load-texture filename min-filter mag-filter static))
105 (lambda () (glDeleteTextures 1 `(,texture)))
109 (defun draw-image-function (filename)
110 (let ((texture (load-texture filename)))
111 (lambda (&optional (f 1))
113 (let ((width (getf (get-resource texture) :width))
114 (height (getf (get-resource texture) :height)))
115 (draw-rectangle (* f width) (* f height) :texture texture)))))))
117 (defun draw-quad (v1 v2 v3 v4 &key texture)
118 (let ((id-texture (getf (get-resource texture) :id-texture)))
121 (glBindTexture GL_TEXTURE_2D id-texture)
123 (draw-vertex v1 :texture-coord '(0 0))
124 (draw-vertex v2 :texture-coord '(1 0))
125 (draw-vertex v3 :texture-coord '(1 1))
126 (draw-vertex v4 :texture-coord '(0 1))
128 ((consp texture) (with-color texture (draw v1 v2 v3 v4)))
129 (t (draw v1 v2 v3 v4)))))
131 (defun draw-rectangle (width height &key texture)
132 (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
133 (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture)))
135 (defun draw-square (&key (size 1) texture)
136 (draw-rectangle size size :texture texture))
138 (defun draw-cube (&key (size 1) texture texture-1 texture-2 texture-3 texture-4 texture-5 texture-6)
139 (let ((-size (neg size)))
142 (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture (or texture-1 texture))
144 (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture (or texture-2 texture))
146 (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture (or texture-3 texture))
148 (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture (or texture-4 texture))
150 (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture (or texture-5 texture))
152 (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture (or texture-6 texture)))))
154 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
156 (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
157 (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
158 (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
159 (and turn-on (glEnable id))
162 (defun translate (x y &optional (z 0))
163 (glTranslatef x y z))
165 (defun rotate (&rest rot)
166 (cond ((3d-mode?) (apply #'3d-rotate rot))
167 (t (apply #'2d-rotate rot))))
169 (defun 3d-rotate (xrot yrot zrot)
170 (glRotatef xrot 1 0 0)
171 (glRotatef yrot 0 1 0)
172 (glRotatef zrot 0 0 1))
174 (defun 2d-rotate (rot)
175 (glRotatef rot 0 0 1))
179 (cond ((3d-mode?) (camera-look))))
181 (let ((camera-eye '(0 0 0)) (camera-center '(0 0 -100)) (camera-up '(0 1 0)))
182 (defun set-camera (&key eye center up)
183 (cond (eye (setq camera-eye eye)))
184 (cond (center (setq camera-center center)))
185 (cond (up (setq camera-up up))))
187 (defun camera-look ()
188 (apply #'gluLookAt (concatenate 'list camera-eye camera-center camera-up))))