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