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