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