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