]> git.jsancho.org Git - gacela.git/blob - src/gacela_GL.c
(no commit message)
[gacela.git] / src / gacela_GL.c
1 /* Gacela, a GNU Guile 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 #include <libguile.h>
19 #include <GL/gl.h>
20 #include <GL/glu.h>
21 #include "gacela_GL.h"
22
23
24 struct glTexture
25 {
26   GLuint texture_id;
27 };
28
29 static scm_t_bits glTexture_tag;
30
31 SCM
32 make_glTexture (GLuint texture_id)
33 {
34   SCM smob;
35   struct glTexture *glTexture;
36
37   glTexture = (struct glTexture *) scm_gc_malloc (sizeof (struct glTexture), "glTexture");
38
39   glTexture->texture_id = 0;
40
41   SCM_NEWSMOB (smob, glTexture_tag, glTexture);
42
43   glTexture->texture_id = texture_id;
44
45   return smob;
46 }
47
48 GLuint
49 get_glTexture_id (SCM glTexture_smob)
50 {
51   struct glTexture *glTexture;
52
53   scm_assert_smob_type (glTexture_tag, glTexture_smob);
54   glTexture = (struct glTexture *) SCM_SMOB_DATA (glTexture_smob);
55   return glTexture->texture_id;
56 }
57
58 size_t
59 free_glTexture (SCM glTexture_smob)
60 {
61   struct glTexture *glTexture = (struct glTexture *) SCM_SMOB_DATA (glTexture_smob);
62   GLuint text[1];
63
64   text[0] = glTexture->texture_id;
65   glDeleteTextures (1, &text[0]);
66   scm_gc_free (glTexture, sizeof (struct glTexture), "glTexture");
67
68   return 0;
69 }
70
71
72 SCM
73 gacela_glBegin (SCM mode)
74 {
75   glBegin (scm_to_int (mode));
76   return SCM_UNSPECIFIED;
77 }
78
79 SCM
80 gacela_glClear (SCM mask)
81 {
82   glClear (scm_to_int (mask));
83   return SCM_UNSPECIFIED;
84 }
85
86 SCM
87 gacela_glClearColor (SCM red, SCM green, SCM blue, SCM alpha)
88 {
89   glClearColor (scm_to_double (red), scm_to_double (green), scm_to_double (blue), scm_to_double (alpha));
90   return SCM_UNSPECIFIED;
91 }
92
93 SCM
94 gacela_glClearDepth (SCM depth)
95 {
96   glClearDepth (scm_to_double (depth));
97   return SCM_UNSPECIFIED;
98 }
99
100 SCM
101 gacela_glColor3f (SCM red, SCM green, SCM blue)
102 {
103   glColor3f (scm_to_double (red), scm_to_double (green), scm_to_double (blue));
104   return SCM_UNSPECIFIED;
105 }
106
107 SCM
108 gacela_glColor4f (SCM red, SCM green, SCM blue, SCM alpha)
109 {
110   glColor4f (scm_to_double (red), scm_to_double (green), scm_to_double (blue), scm_to_double (alpha));
111   return SCM_UNSPECIFIED;
112 }
113
114 SCM
115 gacela_glDepthFunc (SCM func)
116 {
117   glDepthFunc (scm_to_int (func));
118   return SCM_UNSPECIFIED;
119 }
120
121 SCM
122 gacela_glEnable (SCM cap)
123 {
124   glEnable (scm_to_int (cap));
125   return SCM_UNSPECIFIED;
126 }
127
128 SCM
129 gacela_glDisable (SCM cap)
130 {
131   glDisable (scm_to_int (cap));
132   return SCM_UNSPECIFIED;
133 }
134
135 SCM
136 gacela_glEnd (void)
137 {
138   glEnd ();
139   return SCM_UNSPECIFIED;
140 }
141
142 SCM
143 gacela_glHint (SCM target, SCM mode)
144 {
145   glHint (scm_to_int (target), scm_to_int (mode));
146   return SCM_UNSPECIFIED;
147 }
148
149 SCM
150 gacela_glLoadIdentity (void)
151 {
152   glLoadIdentity ();
153   return SCM_UNSPECIFIED;
154 }
155
156 SCM
157 gacela_glMatrixMode (SCM mode)
158 {
159   glMatrixMode (scm_to_int (mode));
160   return SCM_UNSPECIFIED;
161 }
162
163 SCM
164 gacela_glRotatef (SCM angle, SCM x, SCM y, SCM z)
165 {
166   glRotatef (scm_to_double (angle), scm_to_double (x), scm_to_double (y), scm_to_double (z));
167   return SCM_UNSPECIFIED;
168 }
169
170 SCM
171 gacela_glShadeModel (SCM mode)
172 {
173   glShadeModel (scm_to_int (mode));
174   return SCM_UNSPECIFIED;
175 }
176
177 SCM
178 gacela_glTranslatef (SCM x, SCM y, SCM z)
179 {
180   glTranslatef (scm_to_double (x), scm_to_double (y), scm_to_double (z));
181   return SCM_UNSPECIFIED;
182 }
183
184 SCM
185 gacela_glVertex2f (SCM x, SCM y)
186 {
187   glVertex2f (scm_to_double (x), scm_to_double (y));
188   return SCM_UNSPECIFIED;
189 }
190
191 SCM
192 gacela_glVertex3f (SCM x, SCM y, SCM z)
193 {
194   glVertex3f (scm_to_double (x), scm_to_double (y), scm_to_double (z));
195   return SCM_UNSPECIFIED;
196 }
197
198 SCM
199 gacela_glViewport (SCM x, SCM y, SCM width, SCM height)
200 {
201   glViewport (scm_to_int (x), scm_to_int (y), scm_to_int (width), scm_to_int (height));
202   return SCM_UNSPECIFIED;
203 }
204
205 SCM
206 gacela_glGenTextures (SCM n)
207 {
208   SCM textures;
209   int nint = scm_to_int (n);
210   GLuint text[nint];
211   int i;
212
213   textures = scm_list_n (SCM_UNDEFINED);
214   glGenTextures (nint, &text[0]);
215
216   for (i = nint - 1; i >= 0; i--) {
217     textures = scm_cons (scm_from_int (text[i]), textures);
218   }
219
220   return textures;
221 }
222
223 SCM
224 gacela_glDeleteTextures (SCM n, SCM textures)
225 {
226   int nint = scm_to_int (n);
227   GLuint text[nint];
228   int i;
229
230   for (i = 0; i < nint; i++) {
231     text[i] = scm_to_int (scm_list_ref (textures, scm_from_int (i)));
232   }
233
234   glDeleteTextures (nint, &text[0]);
235   return SCM_UNSPECIFIED;
236 }
237
238 SCM
239 gacela_glBindTexture (SCM target, SCM texture)
240 {
241   glBindTexture (scm_to_int (target), scm_to_int (texture));
242   return SCM_UNSPECIFIED;
243 }
244
245 SCM
246 gacela_glTexImage2D (SCM target, SCM level, SCM internalFormat, SCM width, SCM height, SCM border, SCM format, SCM type, SCM pixels)
247 {
248   glTexImage2D (scm_to_int (target), scm_to_int (level), scm_to_int (internalFormat), scm_to_int (width), \
249                 scm_to_int (height), scm_to_int (border), scm_to_int (format), scm_to_int (type), \
250                 (GLvoid *)scm_to_int (pixels));
251   return SCM_UNSPECIFIED;
252 }
253
254 SCM
255 gacela_glTexParameteri (SCM target, SCM pname, SCM param)
256 {
257   glTexParameteri (scm_to_int (target), scm_to_int (pname), scm_to_int (param));
258   return SCM_UNSPECIFIED;
259 }
260
261 SCM
262 gacela_glTexCoord2f (SCM s, SCM t)
263 {
264   glTexCoord2f (scm_to_double (s), scm_to_double (t));
265   return SCM_UNSPECIFIED;
266 }
267
268 SCM
269 gacela_glLightfv (SCM light, SCM pname, SCM params)
270 {
271   int n = scm_to_int (scm_length (params));
272   GLfloat gl_params[n];
273   int i;
274
275   for (i = 0; i < n; i++) {
276     gl_params[i] = scm_to_double (scm_list_ref (params, scm_from_int (i)));
277   }
278
279   glLightfv (scm_to_int (light), scm_to_int (pname), gl_params);
280   return SCM_UNSPECIFIED;
281 }
282
283 SCM
284 gacela_glNormal3f (SCM nx, SCM ny, SCM nz)
285 {
286   glNormal3f (scm_to_double (nx), scm_to_double (ny), scm_to_double (nz));
287   return SCM_UNSPECIFIED;
288 }
289
290 SCM
291 gacela_glBlendFunc (SCM sfactor, SCM dfactor)
292 {
293   glBlendFunc (scm_to_int (sfactor), scm_to_int (dfactor));
294   return SCM_UNSPECIFIED;
295 }
296
297 SCM
298 gacela_glOrtho (SCM left, SCM right, SCM bottom, SCM top, SCM near_val, SCM far_val)
299 {
300   glOrtho (scm_to_double (left), scm_to_double (right), scm_to_double (bottom), scm_to_double (top), \
301            scm_to_double (near_val), scm_to_double (far_val));
302   return SCM_UNSPECIFIED;
303 }
304
305 SCM
306 gacela_glPushMatrix (void)
307 {
308   glPushMatrix ();
309   return SCM_UNSPECIFIED;
310 }
311
312 SCM
313 gacela_glPopMatrix (void)
314 {
315   glPopMatrix ();
316   return SCM_UNSPECIFIED;
317 }
318
319 SCM
320 gacela_gluPerspective (SCM fovy, SCM aspect, SCM zNear, SCM zFar)
321 {
322   gluPerspective (scm_to_double (fovy), scm_to_double (aspect), scm_to_double (zNear), scm_to_double (zFar));
323   return SCM_UNSPECIFIED;
324 }
325
326 SCM
327 gacela_gluBuild2DMipmaps (SCM target, SCM internalFormat, SCM width, SCM height, SCM format, SCM type, SCM data)
328 {
329   return scm_from_int (gluBuild2DMipmaps (scm_to_int (target), scm_to_int (internalFormat), scm_to_int (width), \
330                                           scm_to_int (height), scm_to_int (format), scm_to_int (type), \
331                                           (void *)scm_to_int (data)));
332 }
333
334 SCM
335 gacela_gluLookAt (SCM eyeX, SCM eyeY, SCM eyeZ, SCM centerX, SCM centerY, SCM centerZ, SCM upX, SCM upY, SCM upZ)
336 {
337   gluLookAt (scm_to_double (eyeX), scm_to_double (eyeY), scm_to_double (eyeZ), \
338              scm_to_double (centerX), scm_to_double (centerY), scm_to_double (centerZ), \
339              scm_to_double (upX), scm_to_double (upY), scm_to_double (upZ));
340   return SCM_UNSPECIFIED;
341 }
342
343
344 void*
345 GL_register_functions (void* data)
346 {
347   glTexture_tag = scm_make_smob_type ("glTexture", sizeof (struct glTexture));
348   scm_set_smob_free (glTexture_tag, free_glTexture);
349
350   // Data types
351   scm_c_define ("GL_UNSIGNED_BYTE", scm_from_int (GL_UNSIGNED_BYTE));
352
353   // Primitives
354   scm_c_define ("GL_POINTS", scm_from_int (GL_POINTS));
355   scm_c_define ("GL_LINES", scm_from_int (GL_LINES));
356   scm_c_define ("GL_LINE_LOOP", scm_from_int (GL_LINE_LOOP));
357   scm_c_define ("GL_LINE_STRIP", scm_from_int (GL_LINE_STRIP));
358   scm_c_define ("GL_TRIANGLES", scm_from_int (GL_TRIANGLES));
359   scm_c_define ("GL_TRIANGLE_STRIP", scm_from_int (GL_TRIANGLE_STRIP));
360   scm_c_define ("GL_TRIANGLE_FAN", scm_from_int (GL_TRIANGLE_FAN));
361   scm_c_define ("GL_QUADS", scm_from_int (GL_QUADS));
362   scm_c_define ("GL_QUAD_STRIP", scm_from_int (GL_QUAD_STRIP));
363   scm_c_define ("GL_POLYGON", scm_from_int (GL_POLYGON));
364
365   // Matrix Mode
366   scm_c_define ("GL_MODELVIEW", scm_from_int (GL_MODELVIEW));
367   scm_c_define ("GL_PROJECTION", scm_from_int (GL_PROJECTION));
368
369   // Depth buffer
370   scm_c_define ("GL_LEQUAL", scm_from_int (GL_LEQUAL));
371   scm_c_define ("GL_DEPTH_TEST", scm_from_int (GL_DEPTH_TEST));
372
373   // Lighting
374   scm_c_define ("GL_LIGHTING", scm_from_int (GL_LIGHTING));
375   scm_c_define ("GL_LIGHT1", scm_from_int (GL_LIGHT1));
376   scm_c_define ("GL_AMBIENT", scm_from_int (GL_AMBIENT));
377   scm_c_define ("GL_DIFFUSE", scm_from_int (GL_DIFFUSE));
378   scm_c_define ("GL_POSITION", scm_from_int (GL_POSITION));
379   scm_c_define ("GL_SMOOTH", scm_from_int (GL_SMOOTH));
380
381   // Blending
382   scm_c_define ("GL_BLEND", scm_from_int (GL_BLEND));
383   scm_c_define ("GL_ONE", scm_from_int (GL_ONE));
384   scm_c_define ("GL_SRC_ALPHA", scm_from_int (GL_SRC_ALPHA));
385
386   // Fog
387   scm_c_define ("GL_LINEAR", scm_from_int (GL_LINEAR));
388
389   // Buffers, Pixel Drawing/Reading
390   scm_c_define ("GL_RGB", scm_from_int (GL_RGB));
391   scm_c_define ("GL_RGBA", scm_from_int (GL_RGBA));
392
393   // Hints
394   scm_c_define ("GL_PERSPECTIVE_CORRECTION_HINT", scm_from_int (GL_PERSPECTIVE_CORRECTION_HINT));
395   scm_c_define ("GL_NICEST", scm_from_int (GL_NICEST));
396
397   // Texture mapping
398   scm_c_define ("GL_TEXTURE_2D", scm_from_int (GL_TEXTURE_2D));
399   scm_c_define ("GL_TEXTURE_MAG_FILTER", scm_from_int (GL_TEXTURE_MAG_FILTER));
400   scm_c_define ("GL_TEXTURE_MIN_FILTER", scm_from_int (GL_TEXTURE_MIN_FILTER));
401   scm_c_define ("GL_LINEAR_MIPMAP_NEAREST", scm_from_int (GL_LINEAR_MIPMAP_NEAREST));
402   scm_c_define ("GL_NEAREST", scm_from_int (GL_NEAREST));
403
404   // glPush/PopAttrib bits
405   scm_c_define ("GL_DEPTH_BUFFER_BIT", scm_from_int (GL_DEPTH_BUFFER_BIT));
406   scm_c_define ("GL_COLOR_BUFFER_BIT", scm_from_int (GL_COLOR_BUFFER_BIT));
407
408   // OpenGL 1.2
409   scm_c_define ("GL_BGR", scm_from_int (GL_BGR));
410   scm_c_define ("GL_BGRA", scm_from_int (GL_BGRA));
411
412
413   scm_c_define_gsubr ("glBegin", 1, 0, 0, gacela_glBegin);
414   scm_c_define_gsubr ("glClear", 1, 0, 0, gacela_glClear);
415   scm_c_define_gsubr ("glClearColor", 4, 0, 0, gacela_glClearColor);
416   scm_c_define_gsubr ("glClearDepth", 1, 0, 0, gacela_glClearDepth);
417   scm_c_define_gsubr ("glColor3f", 3, 0, 0, gacela_glColor3f);
418   scm_c_define_gsubr ("glColor4f", 4, 0, 0, gacela_glColor4f);
419   scm_c_define_gsubr ("glDepthFunc", 1, 0, 0, gacela_glDepthFunc);
420   scm_c_define_gsubr ("glEnable", 1, 0, 0, gacela_glEnable);
421   scm_c_define_gsubr ("glDisable", 1, 0, 0, gacela_glDisable);
422   scm_c_define_gsubr ("glEnd", 0, 0, 0, gacela_glEnd);
423   scm_c_define_gsubr ("glHint", 2, 0, 0, gacela_glHint);
424   scm_c_define_gsubr ("glLoadIdentity", 0, 0, 0, gacela_glLoadIdentity);
425   scm_c_define_gsubr ("glMatrixMode", 1, 0, 0, gacela_glMatrixMode);
426   scm_c_define_gsubr ("glRotatef", 4, 0, 0, gacela_glRotatef);
427   scm_c_define_gsubr ("glShadeModel", 1, 0, 0, gacela_glShadeModel);
428   scm_c_define_gsubr ("glTranslatef", 3, 0, 0, gacela_glTranslatef);
429   scm_c_define_gsubr ("glVertex2f", 2, 0, 0, gacela_glVertex2f);
430   scm_c_define_gsubr ("glVertex3f", 3, 0, 0, gacela_glVertex3f);
431   scm_c_define_gsubr ("glViewport", 4, 0, 0, gacela_glViewport);
432   scm_c_define_gsubr ("glGenTextures", 1, 0, 0, gacela_glGenTextures);
433   scm_c_define_gsubr ("glDeleteTextures", 2, 0, 0, gacela_glDeleteTextures);
434   scm_c_define_gsubr ("glBindTexture", 2, 0, 0, gacela_glBindTexture);
435   scm_c_define_gsubr ("glTexImage2D", 9, 0, 0, gacela_glTexImage2D);
436   scm_c_define_gsubr ("glTexParameteri", 3, 0, 0, gacela_glTexParameteri);
437   scm_c_define_gsubr ("glTexCoord2f", 2, 0, 0, gacela_glTexCoord2f);
438   scm_c_define_gsubr ("glLightfv", 3, 0, 0, gacela_glLightfv);
439   scm_c_define_gsubr ("glNormal3f", 3, 0, 0, gacela_glNormal3f);
440   scm_c_define_gsubr ("glBlendFunc", 2, 0, 0, gacela_glBlendFunc);
441   scm_c_define_gsubr ("glOrtho", 6, 0, 0, gacela_glOrtho);
442   scm_c_define_gsubr ("glPushMatrix", 0, 0, 0, gacela_glPushMatrix);
443   scm_c_define_gsubr ("glPopMatrix", 0, 0, 0, gacela_glPopMatrix);
444
445   scm_c_define_gsubr ("gluPerspective", 4, 0, 0, gacela_gluPerspective);
446   scm_c_define_gsubr ("gluBuild2DMipmaps", 7, 0, 0, gacela_gluBuild2DMipmaps);
447   scm_c_define_gsubr ("gluLookAt", 9, 0, 0, gacela_gluLookAt);
448
449   return NULL;
450 }