]> 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
18 (in-package :gacela)
19
20 (defmacro set-dimension (&key 2d 3d))
21
22 (defun draw (&rest vertexes)
23   (begin-draw (length vertexes))
24   (draw-vertexes vertexes)
25   (glEnd))
26
27 (defun begin-draw (number-of-points)
28   (cond ((= number-of-points 3) (glBegin GL_TRIANGLES))
29         ((= number-of-points 4) (glBegin GL_QUADS))))
30
31 (defun draw-vertexes (vertexes)
32   (cond ((null vertexes) nil)
33         (t (draw-vertex (car vertexes))
34            (draw-vertexes (cdr vertexes)))))
35
36 (defun draw-vertex (vertex &key texture-coord)
37   (cond ((consp (car vertex)) (draw-color (car vertex)) (apply #'glVertex3f (cadr vertex)))
38         (t (cond (texture-coord (apply #'glTexCoord2f texture-coord)))
39            (apply #'glVertex3f vertex))))
40
41 (defun draw-color (color)
42   (apply #'glColor3f color))
43
44 (defun load-texture (filename &optional (min-filter GL_LINEAR) (mag-filter GL_LINEAR))
45   (init-textures)
46   (init-video-mode)
47   (let ((image (IMG_Load filename)))
48     (cond ((/= image 0)
49            (let ((width (surface-w image)) (height (surface-h image))
50                  (texture (car (glGenTextures 1))))
51              (glBindTexture GL_TEXTURE_2D texture)
52              (glTexImage2D GL_TEXTURE_2D 0 3 width height 0 GL_RGB GL_UNSIGNED_BYTE (surface-pixels image))
53              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER min-filter)
54              (glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER mag-filter)
55              (SDL_FreeSurface image)
56              (values texture width height))))))
57
58 (defun draw-image-function (filename)
59   (multiple-value-bind
60    (texture width height) (load-texture filename)
61         (cond (texture
62                (lambda (&optional (f 1))
63                  (draw-rectangle (* f width) (* f height) :texture texture))))))
64
65 (defun draw-quad (v1 v2 v3 v4 &key texture color)
66   (cond (texture (glBindTexture GL_TEXTURE_2D texture)
67                  (begin-draw 4)
68                  (draw-vertex v1 :texture-coord '(0 0))
69                  (draw-vertex v2 :texture-coord '(1 0))
70                  (draw-vertex v3 :texture-coord '(1 1))
71                  (draw-vertex v4 :texture-coord '(0 1))
72                  (glEnd))
73         (t (cond (color (draw-color color)))
74            (draw v1 v2 v3 v4))))
75
76 (defun draw-rectangle (width height &key texture color)
77   (let* ((w (/ width 2)) (-w (neg w)) (h (/ height 2)) (-h (neg h)))
78     (draw-quad (list -w h 0) (list w h 0) (list w -h 0) (list -w -h 0) :texture texture :color color)))
79
80 (defun draw-square (&key (size 1) texture color)
81   (draw-rectangle size size :texture texture :color color))
82
83 (defun draw-cube (&key size texture color)
84   (let ((-size (neg size)))
85     (enable :textures texture)
86     (glNormal3f 0 0 1)
87     (draw-quad (list -size size size) (list size size size) (list size -size size) (list -size -size size) :texture texture :color color)
88     (glNormal3f 0 0 -1)
89     (draw-quad (list -size -size -size) (list size -size -size) (list size size -size) (list -size size -size) :texture texture :color color)
90     (glNormal3f 0 1 0)
91     (draw-quad (list size size size) (list -size size size) (list -size size -size) (list size size -size) :texture texture :color color)
92     (glNormal3f 0 -1 0)
93     (draw-quad (list -size -size size) (list size -size size) (list size -size -size) (list -size -size -size) :texture texture :color color)
94     (glNormal3f 1 0 0)
95     (draw-quad (list size -size -size) (list size -size size) (list size size size) (list size size -size) :texture texture :color color)
96     (glNormal3f -1 0 0)
97     (draw-quad (list -size -size size) (list -size -size -size) (list -size size -size) (list -size size size) :texture texture :color color)))
98
99 (defun add-light (&key light position ambient (id GL_LIGHT1) (turn-on t))
100   (init-lighting)
101   (and light (glLightfv id GL_DIFFUSE (first light) (second light) (third light) (fourth light)))
102   (and light position (glLightfv GL_POSITION (first position) (second position) (third position) (fourth position)))
103   (and ambient (glLightfv id GL_AMBIENT (first ambient) (second ambient) (third ambient) (fourth ambient)))
104   (and turn-on (glEnable id))
105   id)
106
107 (defun translate (x y &optional (z 0))
108   (glTranslatef x y z))
109
110 (defun rotate (xrot yrot &optional zrot)
111   (glRotatef xrot 1 0 0)
112   (glRotatef yrot 0 1 0)
113   (cond (zrot (glRotatef zrot 0 0 1))))
114
115 (defun enable (&key textures)
116   (cond (textures (glEnable GL_TEXTURE_2D))))