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