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