ruby-opengl2 0.60.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/Rakefile +80 -0
  2. data/examples/NeHe/nehe_lesson02.rb +117 -0
  3. data/examples/NeHe/nehe_lesson03.rb +122 -0
  4. data/examples/NeHe/nehe_lesson04.rb +133 -0
  5. data/examples/NeHe/nehe_lesson05.rb +186 -0
  6. data/examples/NeHe/nehe_lesson36.rb +303 -0
  7. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  8. data/examples/OrangeBook/brick.frag +36 -0
  9. data/examples/OrangeBook/brick.rb +376 -0
  10. data/examples/OrangeBook/brick.vert +41 -0
  11. data/examples/OrangeBook/particle.frag +17 -0
  12. data/examples/OrangeBook/particle.rb +406 -0
  13. data/examples/OrangeBook/particle.vert +38 -0
  14. data/examples/README +16 -0
  15. data/examples/RedBook/aapoly.rb +142 -0
  16. data/examples/RedBook/aargb.rb +119 -0
  17. data/examples/RedBook/accanti.rb +162 -0
  18. data/examples/RedBook/accpersp.rb +215 -0
  19. data/examples/RedBook/alpha.rb +123 -0
  20. data/examples/RedBook/alpha3D.rb +158 -0
  21. data/examples/RedBook/bezcurve.rb +105 -0
  22. data/examples/RedBook/bezmesh.rb +137 -0
  23. data/examples/RedBook/checker.rb +124 -0
  24. data/examples/RedBook/clip.rb +95 -0
  25. data/examples/RedBook/colormat.rb +135 -0
  26. data/examples/RedBook/cube.rb +69 -0
  27. data/examples/RedBook/depthcue.rb +99 -0
  28. data/examples/RedBook/dof.rb +205 -0
  29. data/examples/RedBook/double.rb +105 -0
  30. data/examples/RedBook/drawf.rb +91 -0
  31. data/examples/RedBook/feedback.rb +145 -0
  32. data/examples/RedBook/fog.rb +167 -0
  33. data/examples/RedBook/font.rb +151 -0
  34. data/examples/RedBook/hello.rb +79 -0
  35. data/examples/RedBook/image.rb +137 -0
  36. data/examples/RedBook/jitter.rb +207 -0
  37. data/examples/RedBook/lines.rb +128 -0
  38. data/examples/RedBook/list.rb +111 -0
  39. data/examples/RedBook/material.rb +275 -0
  40. data/examples/RedBook/mipmap.rb +156 -0
  41. data/examples/RedBook/model.rb +113 -0
  42. data/examples/RedBook/movelight.rb +132 -0
  43. data/examples/RedBook/pickdepth.rb +179 -0
  44. data/examples/RedBook/planet.rb +108 -0
  45. data/examples/RedBook/quadric.rb +158 -0
  46. data/examples/RedBook/robot.rb +115 -0
  47. data/examples/RedBook/select.rb +196 -0
  48. data/examples/RedBook/smooth.rb +95 -0
  49. data/examples/RedBook/stencil.rb +163 -0
  50. data/examples/RedBook/stroke.rb +167 -0
  51. data/examples/RedBook/surface.rb +166 -0
  52. data/examples/RedBook/teaambient.rb +132 -0
  53. data/examples/RedBook/teapots.rb +182 -0
  54. data/examples/RedBook/tess.rb +183 -0
  55. data/examples/RedBook/texbind.rb +147 -0
  56. data/examples/RedBook/texgen.rb +169 -0
  57. data/examples/RedBook/texturesurf.rb +128 -0
  58. data/examples/RedBook/varray.rb +159 -0
  59. data/examples/RedBook/wrap.rb +148 -0
  60. data/examples/misc/OGLBench.rb +337 -0
  61. data/examples/misc/anisotropic.rb +194 -0
  62. data/examples/misc/fbo_test.rb +356 -0
  63. data/examples/misc/font-glut.rb +46 -0
  64. data/examples/misc/glfwtest.rb +30 -0
  65. data/examples/misc/plane.rb +161 -0
  66. data/examples/misc/readpixel.rb +65 -0
  67. data/examples/misc/sdltest.rb +34 -0
  68. data/examples/misc/trislam.rb +828 -0
  69. data/ext/common/common.h +423 -0
  70. data/ext/common/conv.h +243 -0
  71. data/ext/common/funcdef.h +280 -0
  72. data/ext/common/gl-enums.h +10031 -0
  73. data/ext/common/gl-error.h +23 -0
  74. data/ext/common/gl-types.h +63 -0
  75. data/ext/common/glu-enums.h +463 -0
  76. data/ext/gl/gl-1.0-1.1.c +2811 -0
  77. data/ext/gl/gl-1.2.c +814 -0
  78. data/ext/gl/gl-1.3.c +443 -0
  79. data/ext/gl/gl-1.4.c +348 -0
  80. data/ext/gl/gl-1.5.c +225 -0
  81. data/ext/gl/gl-2.0.c +657 -0
  82. data/ext/gl/gl-2.1.c +57 -0
  83. data/ext/gl/gl-enums.c +3354 -0
  84. data/ext/gl/gl-error.c +104 -0
  85. data/ext/gl/gl-ext-3dfx.c +27 -0
  86. data/ext/gl/gl-ext-arb.c +866 -0
  87. data/ext/gl/gl-ext-ati.c +41 -0
  88. data/ext/gl/gl-ext-ext.c +889 -0
  89. data/ext/gl/gl-ext-gremedy.c +41 -0
  90. data/ext/gl/gl-ext-nv.c +679 -0
  91. data/ext/gl/gl.c +214 -0
  92. data/ext/gl/mkrf_conf.rb +37 -0
  93. data/ext/glu/glu-enums.c +164 -0
  94. data/ext/glu/glu.c +1530 -0
  95. data/ext/glu/mkrf_conf.rb +39 -0
  96. data/ext/glut/glut.c +1627 -0
  97. data/ext/glut/mkrf_conf.rb +37 -0
  98. data/lib/opengl.rb +84 -0
  99. data/test/README +10 -0
  100. data/test/tc_common.rb +98 -0
  101. data/test/tc_ext_arb.rb +467 -0
  102. data/test/tc_ext_ati.rb +33 -0
  103. data/test/tc_ext_ext.rb +551 -0
  104. data/test/tc_ext_gremedy.rb +36 -0
  105. data/test/tc_ext_nv.rb +357 -0
  106. data/test/tc_func_10_11.rb +1281 -0
  107. data/test/tc_func_12.rb +186 -0
  108. data/test/tc_func_13.rb +229 -0
  109. data/test/tc_func_14.rb +197 -0
  110. data/test/tc_func_15.rb +270 -0
  111. data/test/tc_func_20.rb +346 -0
  112. data/test/tc_func_21.rb +541 -0
  113. data/test/tc_glu.rb +310 -0
  114. data/test/tc_include_gl.rb +35 -0
  115. data/test/tc_misc.rb +54 -0
  116. data/test/tc_require_gl.rb +34 -0
  117. metadata +164 -0
@@ -0,0 +1,423 @@
1
+ /*
2
+ * Last edit by previous maintainer:
3
+ * 2000/01/06 16:37:43, kusano
4
+ *
5
+ * Copyright (C) 1999 - 2005 Yoshi <yoshi@giganet.net>
6
+ * Copyright (C) 2006 John M. Gabriele <jmg3000@gmail.com>
7
+ * Copyright (C) 2007 James Adam <james@lazyatom.com>
8
+ * Copyright (C) 2007 Jan Dvorak <jan.dvorak@kraxnet.cz>
9
+ *
10
+ * This program is distributed under the terms of the MIT license.
11
+ * See the included MIT-LICENSE file for the terms of this license.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ */
21
+
22
+ #ifndef _COMMON_H_
23
+ #define _COMMON_H_
24
+
25
+ #include <ruby.h>
26
+
27
+ #include <ctype.h>
28
+
29
+ #ifdef __APPLE__
30
+ #include <OpenGL/gl.h>
31
+ #include <OpenGL/glu.h>
32
+ #include <GLUT/glut.h>
33
+ #include <mach-o/dyld.h>
34
+ #include <stdlib.h>
35
+ #include <string.h>
36
+ #elif defined WIN32
37
+ #include <windows.h>
38
+ #include <GL/gl.h>
39
+ #include <GL/glu.h>
40
+ #include <GL/glut.h>
41
+ #else
42
+ #include <GL/gl.h>
43
+ #include <GL/glx.h>
44
+ #include <GL/glu.h>
45
+ #include <GL/glut.h>
46
+ #endif
47
+
48
+ #include "gl-types.h"
49
+ #include "gl-enums.h"
50
+ #include "glu-enums.h"
51
+ #include "gl-error.h"
52
+
53
+ #include "funcdef.h"
54
+ #include "conv.h"
55
+
56
+ #ifndef APIENTRY
57
+ #define APIENTRY
58
+ #endif
59
+
60
+ #ifndef CALLBACK
61
+ #define CALLBACK
62
+ #endif
63
+
64
+ #ifndef GLUTCALLBACK
65
+ #define GLUTCALLBACK
66
+ #endif
67
+
68
+ #ifdef WIN32
69
+ #define DLLEXPORT __declspec(dllexport)
70
+ #else
71
+ #define DLLEXPORT
72
+ #endif
73
+
74
+ /* */
75
+
76
+ /* at least GL_MAX_VERTEX_ATTRIBS - usually 16 or 32 on today's high-end cards */
77
+ #define _MAX_VERTEX_ATTRIBS 64
78
+
79
+ typedef struct RArray RArray;
80
+
81
+ extern VALUE cProc;
82
+
83
+ /* For now we do not honor pixel store modes, so we need to
84
+ force them to defaults on each affected function call for
85
+ correct size requirement calculations */
86
+ #define FORCE_PIXEL_STORE_MODE \
87
+ glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); \
88
+ glPixelStorei(GL_PACK_ALIGNMENT, 1); \
89
+ glPixelStorei(GL_PACK_SKIP_PIXELS, 0); \
90
+ glPixelStorei(GL_PACK_SKIP_ROWS, 0); \
91
+ glPixelStorei(GL_PACK_ROW_LENGTH, 0); \
92
+ glPixelStorei(GL_PACK_SKIP_IMAGES, 0); \
93
+ glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0); \
94
+ if (CheckVersionExtension("GL_SGIS_texture4D")) { \
95
+ glPixelStorei(GL_PACK_SKIP_VOLUMES_SGIS, 0); \
96
+ glPixelStorei(GL_PACK_IMAGE_DEPTH_SGIS, 0); \
97
+ }
98
+
99
+ #define RESTORE_PIXEL_STORE_MODE \
100
+ glPopClientAttrib();
101
+
102
+ GLboolean CheckVersionExtension(const char *name);
103
+ GLint CheckBufferBinding(GLint buffer);
104
+
105
+ /* -------------------------------------------------------------------- */
106
+
107
+ /* gets number of components for given format */
108
+ static inline int glformat_size(GLenum format)
109
+ {
110
+ switch(format)
111
+ {
112
+ case GL_COLOR_INDEX:
113
+ case GL_RED:
114
+ case GL_GREEN:
115
+ case GL_BLUE:
116
+ case GL_ALPHA:
117
+ case GL_RED_INTEGER_EXT:
118
+ case GL_GREEN_INTEGER_EXT:
119
+ case GL_BLUE_INTEGER_EXT:
120
+ case GL_ALPHA_INTEGER_EXT:
121
+ case GL_STENCIL_INDEX:
122
+ case GL_DEPTH_COMPONENT:
123
+ case GL_LUMINANCE:
124
+ case GL_LUMINANCE_INTEGER_EXT:
125
+ return 1;
126
+
127
+ case GL_LUMINANCE_ALPHA:
128
+ case GL_LUMINANCE_ALPHA_INTEGER_EXT:
129
+ case GL_422_EXT:
130
+ case GL_422_REV_EXT:
131
+ case GL_422_AVERAGE_EXT:
132
+ case GL_422_REV_AVERAGE_EXT:
133
+ case GL_YCRCB_422_SGIX:
134
+ case GL_YCBCR_422_APPLE:
135
+ case GL_YCBCR_MESA:
136
+ case GL_DEPTH_STENCIL_NV:
137
+ case GL_HILO_NV:
138
+ case GL_DSDT_NV:
139
+ case GL_DUDV_ATI:
140
+ case GL_DU8DV8_ATI:
141
+ case GL_FORMAT_SUBSAMPLE_24_24_OML:
142
+ return 2;
143
+
144
+ case GL_RGB:
145
+ case GL_RGB_INTEGER_EXT:
146
+ case GL_BGR_EXT:
147
+ case GL_BGR_INTEGER_EXT:
148
+ case GL_YCRCB_444_SGIX:
149
+ case GL_DSDT_MAG_NV:
150
+ case GL_FORMAT_SUBSAMPLE_244_244_OML:
151
+ return 3;
152
+
153
+ case GL_RGBA:
154
+ case GL_RGBA_INTEGER_EXT:
155
+ case GL_BGRA_EXT:
156
+ case GL_BGRA_INTEGER_EXT:
157
+ case GL_ABGR_EXT:
158
+ case GL_CMYK_EXT:
159
+ case GL_DSDT_MAG_VIB_NV:
160
+ return 4;
161
+
162
+ case GL_CMYKA_EXT:
163
+ return 5;
164
+
165
+ /* GL spec permits passing direct format size instead of enum (now obsolete) */
166
+ case 1:
167
+ case 2:
168
+ case 3:
169
+ case 4:
170
+ return format;
171
+
172
+ default:
173
+ rb_raise(rb_eArgError, "Unknown GL format enum %i",format);
174
+ return -1; /* not reached */
175
+ }
176
+ }
177
+
178
+ /* computes unit (pixel) size for given type and format */
179
+ static inline int gltype_glformat_unit_size(GLenum type,GLenum format)
180
+ {
181
+ unsigned int format_size;
182
+
183
+ format_size = glformat_size(format);
184
+
185
+ switch(type)
186
+ {
187
+ case GL_BYTE:
188
+ case GL_UNSIGNED_BYTE:
189
+ case GL_BITMAP:
190
+ return 1*format_size;
191
+
192
+ case GL_SHORT:
193
+ case GL_UNSIGNED_SHORT:
194
+ case GL_HALF_FLOAT_ARB:
195
+ return 2*format_size;
196
+
197
+ case GL_INT:
198
+ case GL_UNSIGNED_INT:
199
+ case GL_FLOAT:
200
+ return 4*format_size;
201
+
202
+ /* in packed formats all components are packed into/unpacked from single datatype,
203
+ so number of components(format_size) doesn't matter for total size calculation */
204
+ case GL_UNSIGNED_BYTE_3_3_2:
205
+ case GL_UNSIGNED_BYTE_2_3_3_REV:
206
+ return 1;
207
+
208
+ case GL_UNSIGNED_SHORT_5_6_5:
209
+ case GL_UNSIGNED_SHORT_5_6_5_REV:
210
+ case GL_UNSIGNED_SHORT_4_4_4_4:
211
+ case GL_UNSIGNED_SHORT_4_4_4_4_REV:
212
+ case GL_UNSIGNED_SHORT_5_5_5_1:
213
+ case GL_UNSIGNED_SHORT_1_5_5_5_REV:
214
+ case GL_UNSIGNED_SHORT_8_8_APPLE:
215
+ case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
216
+ return 2;
217
+
218
+ case GL_UNSIGNED_INT_8_8_8_8:
219
+ case GL_UNSIGNED_INT_8_8_8_8_REV:
220
+ case GL_UNSIGNED_INT_10_10_10_2:
221
+ case GL_UNSIGNED_INT_2_10_10_10_REV:
222
+ case GL_UNSIGNED_INT_24_8_NV:
223
+ case GL_UNSIGNED_INT_S8_S8_8_8_NV:
224
+ case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
225
+ case GL_UNSIGNED_INT_10F_11F_11F_REV_EXT:
226
+ case GL_UNSIGNED_INT_5_9_9_9_REV_EXT:
227
+ case GL_FLOAT_32_UNSIGNED_INT_24_8_REV_NV:
228
+ return 4;
229
+
230
+ default:
231
+ rb_raise(rb_eArgError, "Unknown GL type enum %i",type);
232
+ return -1; /* not reached */
233
+ }
234
+ }
235
+
236
+ /* returns size(units) for given type of shader uniform var */
237
+ static inline int get_uniform_size(GLenum uniform_type)
238
+ {
239
+ int uniform_size = 0;
240
+
241
+ switch (uniform_type) {
242
+ case GL_FLOAT:
243
+ case GL_INT:
244
+ case GL_UNSIGNED_INT:
245
+ case GL_BOOL:
246
+ case GL_SAMPLER_1D:
247
+ case GL_SAMPLER_2D:
248
+ case GL_SAMPLER_3D:
249
+ case GL_SAMPLER_CUBE:
250
+ case GL_SAMPLER_1D_SHADOW:
251
+ case GL_SAMPLER_2D_SHADOW:
252
+ case GL_SAMPLER_2D_RECT_ARB:
253
+ case GL_SAMPLER_2D_RECT_SHADOW_ARB:
254
+ case GL_SAMPLER_1D_ARRAY_EXT:
255
+ case GL_SAMPLER_2D_ARRAY_EXT:
256
+ case GL_SAMPLER_BUFFER_EXT:
257
+ case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
258
+ case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
259
+ case GL_SAMPLER_CUBE_SHADOW_EXT:
260
+ case GL_INT_SAMPLER_1D_EXT:
261
+ case GL_INT_SAMPLER_2D_EXT:
262
+ case GL_INT_SAMPLER_3D_EXT:
263
+ case GL_INT_SAMPLER_CUBE_EXT:
264
+ case GL_INT_SAMPLER_2D_RECT_EXT:
265
+ case GL_INT_SAMPLER_1D_ARRAY_EXT:
266
+ case GL_INT_SAMPLER_2D_ARRAY_EXT:
267
+ case GL_INT_SAMPLER_BUFFER_EXT:
268
+ case GL_UNSIGNED_INT_SAMPLER_1D_EXT:
269
+ case GL_UNSIGNED_INT_SAMPLER_2D_EXT:
270
+ case GL_UNSIGNED_INT_SAMPLER_3D_EXT:
271
+ case GL_UNSIGNED_INT_SAMPLER_CUBE_EXT:
272
+ case GL_UNSIGNED_INT_SAMPLER_2D_RECT_EXT:
273
+ case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY_EXT:
274
+ case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY_EXT:
275
+ case GL_UNSIGNED_INT_SAMPLER_BUFFER_EXT:
276
+ uniform_size = 1;
277
+ break;
278
+ case GL_FLOAT_VEC2:
279
+ case GL_INT_VEC2:
280
+ case GL_UNSIGNED_INT_VEC2_EXT:
281
+ case GL_BOOL_VEC2:
282
+ uniform_size = 2;
283
+ break;
284
+ case GL_FLOAT_VEC3:
285
+ case GL_INT_VEC3:
286
+ case GL_UNSIGNED_INT_VEC3_EXT:
287
+ case GL_BOOL_VEC3:
288
+ uniform_size = 3;
289
+ break;
290
+ case GL_FLOAT_VEC4:
291
+ case GL_INT_VEC4:
292
+ case GL_UNSIGNED_INT_VEC4_EXT:
293
+ case GL_BOOL_VEC4:
294
+ case GL_FLOAT_MAT2:
295
+ uniform_size = 4;
296
+ break;
297
+ case GL_FLOAT_MAT2x3:
298
+ case GL_FLOAT_MAT3x2:
299
+ uniform_size = 6;
300
+ break;
301
+ case GL_FLOAT_MAT2x4:
302
+ case GL_FLOAT_MAT4x2:
303
+ uniform_size = 8;
304
+ break;
305
+ case GL_FLOAT_MAT3:
306
+ uniform_size = 9;
307
+ break;
308
+ case GL_FLOAT_MAT4x3:
309
+ case GL_FLOAT_MAT3x4:
310
+ uniform_size = 12;
311
+ break;
312
+ case GL_FLOAT_MAT4:
313
+ uniform_size = 16;
314
+ break;
315
+ default:
316
+ rb_raise(rb_eTypeError, "Unsupported uniform type '%i'",uniform_type);
317
+ }
318
+ return uniform_size;
319
+ }
320
+
321
+ static inline int GetDataSize(GLenum type,GLenum format,int num)
322
+ {
323
+ int size;
324
+ int unit_size;
325
+
326
+ unit_size = gltype_glformat_unit_size(type,format);
327
+
328
+ if (type==GL_BITMAP)
329
+ size = unit_size*(num/8); /* FIXME account for alignment */
330
+ else
331
+ size = unit_size*num;
332
+
333
+ return size;
334
+ }
335
+
336
+ /* Checks if data size of 'data' string confirms to passed format values */
337
+ /* 'num' is number of elements, each of size 'format' * 'type' */
338
+ static inline void CheckDataSize(GLenum type,GLenum format,int num,VALUE data)
339
+ {
340
+ int size;
341
+
342
+ size = GetDataSize(type,format,num);
343
+
344
+ if (RSTRING_LEN(data) < size)
345
+ rb_raise(rb_eArgError, "Length of specified data doesn't correspond to format and type parameters passed. Calculated length: %i",size);
346
+ }
347
+
348
+ /* -------------------------------------------------------------------- */
349
+ static inline VALUE allocate_buffer_with_string( int size )
350
+ {
351
+ return rb_str_new(NULL, size);
352
+ }
353
+
354
+ /* -------------------------------------------------------------------- */
355
+ static inline void *load_gl_function(const char *name,int raise)
356
+ {
357
+ void *func_ptr = NULL;
358
+
359
+ #if defined(__APPLE__)
360
+ static const struct mach_header* library = NULL;
361
+ char* symbolName;
362
+ NSSymbol symbol;
363
+ if (library == NULL)
364
+ library = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL",NSADDIMAGE_OPTION_RETURN_ON_ERROR);
365
+
366
+ if (library == NULL)
367
+ rb_raise(rb_eRuntimeError,"Can't load OpenGL library for dynamic loading");
368
+
369
+ /* prepend a '_' for the Unix C symbol mangling convention */
370
+ symbolName = ALLOC_N(char,strlen(name) + 2);
371
+ symbolName[0] = '_';
372
+ strcpy(symbolName+1, name);
373
+
374
+ symbol = NSLookupSymbolInImage(library,symbolName,NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
375
+ xfree(symbolName);
376
+
377
+ if (symbol == NULL)
378
+ func_ptr = NULL;
379
+ else
380
+ func_ptr = NSAddressOfSymbol(symbol);
381
+ #elif defined(WIN32) || defined(_WIN32)
382
+ func_ptr = wglGetProcAddress((LPCSTR)name);
383
+ #elif defined(GLX_VERSION_1_4)
384
+ func_ptr = glXGetProcAddress((const GLubyte *)name);
385
+ #else
386
+ func_ptr = glXGetProcAddressARB((const GLubyte *)name);
387
+ #endif
388
+
389
+ if (func_ptr == NULL && raise == 1)
390
+ rb_raise(rb_eNotImpError,"Function %s is not available on this system",name);
391
+
392
+ return func_ptr;
393
+ }
394
+
395
+ static inline VALUE pack_array_or_pass_string(GLenum type,VALUE ary)
396
+ {
397
+ char *type_str;
398
+
399
+ if (TYPE(ary)==T_STRING)
400
+ return ary;
401
+
402
+ Check_Type(ary,T_ARRAY);
403
+
404
+ switch(type) {
405
+ case GL_FLOAT: type_str = "f*"; break;
406
+ case GL_DOUBLE: type_str = "d*"; break;
407
+ case GL_BYTE: type_str = "c*"; break;
408
+ case GL_SHORT: type_str = "s*"; break;
409
+ case GL_INT: type_str = "i*"; break;
410
+ case GL_UNSIGNED_BYTE: type_str = "C*"; break;
411
+ case GL_UNSIGNED_SHORT: type_str = "S*"; break;
412
+ case GL_UNSIGNED_INT: type_str = "I*"; break;
413
+ default:
414
+ rb_raise(rb_eTypeError,"Unknown type %i",type);
415
+ return Qnil; /* not reached */
416
+ }
417
+
418
+ return rb_funcall(ary,rb_intern("pack"),1,rb_str_new2(type_str));
419
+ }
420
+
421
+ /* -------------------------------------------------------------------- */
422
+
423
+ #endif /* _COMMON_H_ */
@@ -0,0 +1,243 @@
1
+ /*
2
+ * Copyright (C) 2007 Jan Dvorak <jan.dvorak@kraxnet.cz>
3
+ *
4
+ * This program is distributed under the terms of the MIT license.
5
+ * See the included MIT-LICENSE file for the terms of this license.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
9
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
10
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
11
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
12
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
13
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ */
15
+
16
+ /* Functions and macros for datatype conversion between Ruby and C */
17
+
18
+ /*
19
+ Fast inline conversion functions as a replacement for the ones in libruby.
20
+ FIXNUM_P is simple logical AND check so it comes first, TYPE() is simple function,
21
+ and specified in header file so it can be inlined. For conversion, FIX2LONG is
22
+ simple right shift, and RFLOAT()-> just pointer dereference. For converting
23
+ Fixnum and Float types (which accounts for 99.9% of things you would want to pass
24
+ to OpenGL), there is large performance boost as result.
25
+
26
+ Also ruby 'true' and 'false' are converted to GL_TRUE/GL_FALSE for compatibility, and
27
+ finally, we fallback to library functions for any other data types (and error handling).
28
+ */
29
+
30
+ #if RUBY_VERSION <190
31
+ #define FLOAT_VAL_ACCESS(val) RFLOAT(val)->value
32
+ #else
33
+ #define FLOAT_VAL_ACCESS(val) RFLOAT(val)->float_value
34
+ #endif
35
+
36
+ #define FASTCONV(_name_,_type_,_convfix_,_convfallback_) \
37
+ static inline _type_ _name_(val) \
38
+ VALUE val; \
39
+ { \
40
+ if (FIXNUM_P(val)) \
41
+ return (_type_) _convfix_(val); \
42
+ \
43
+ if (TYPE(val) == T_FLOAT) \
44
+ return (_type_)FLOAT_VAL_ACCESS(val); \
45
+ \
46
+ if ((val) == Qtrue) \
47
+ return (_type_)(GL_TRUE); \
48
+ \
49
+ if ((val) == Qfalse || (val) == Qnil) \
50
+ return (_type_)(GL_FALSE); \
51
+ \
52
+ return (_convfallback_(val)); \
53
+ }
54
+
55
+ FASTCONV(num2double,double,FIX2LONG,rb_num2dbl)
56
+ #if SIZEOF_INT < SIZEOF_LONG
57
+ /* For 64bit platforms with LP64 mode */
58
+ FASTCONV(num2int,long,FIX2LONG,rb_num2int)
59
+ FASTCONV(num2uint,unsigned long,FIX2ULONG,rb_num2uint)
60
+ #else
61
+ /* All other platforms */
62
+ FASTCONV(num2int,long,FIX2LONG,(int)NUM2LONG)
63
+ FASTCONV(num2uint,unsigned long,FIX2ULONG,(unsigned int)NUM2ULONG)
64
+ #endif
65
+
66
+ #undef NUM2DBL
67
+ #define NUM2DBL num2double
68
+
69
+ #undef NUM2INT
70
+ #define NUM2INT num2int
71
+
72
+ #undef NUM2UINT
73
+ #define NUM2UINT num2uint
74
+
75
+ #undef FASTCONV
76
+
77
+ /* For conversion between ruby and GL boolean values */
78
+ #define GLBOOL2RUBY(x) (x)==GL_TRUE? Qtrue :( (x)==GL_FALSE? Qfalse : INT2NUM((x)))
79
+ #define RUBYBOOL2GL(x) (x)==Qtrue? GL_TRUE : GL_FALSE
80
+
81
+ #define cond_GLBOOL2RUBY_FUNC(_name_,_type_,_conv_) \
82
+ static inline VALUE _name_(GLenum pname,_type_ value) \
83
+ { \
84
+ switch (pname) { \
85
+ case GL_DELETE_STATUS: \
86
+ case GL_LINK_STATUS: \
87
+ case GL_VALIDATE_STATUS: \
88
+ case GL_COMPILE_STATUS: \
89
+ case GL_MINMAX_SINK: \
90
+ case GL_HISTOGRAM_SINK: \
91
+ case GL_COORD_REPLACE: \
92
+ case GL_TEXTURE_COMPRESSED: \
93
+ case GL_GENERATE_MIPMAP: \
94
+ case GL_TEXTURE_RESIDENT: \
95
+ case GL_BUFFER_MAPPED: \
96
+ case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED: \
97
+ case GL_VERTEX_ATTRIB_ARRAY_ENABLED: \
98
+ case GL_QUERY_RESULT_AVAILABLE: \
99
+ case GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB: \
100
+ case GL_FRAMEBUFFER_ATTACHMENT_LAYERED_EXT: \
101
+ case GL_FENCE_STATUS_NV: \
102
+ case GL_TEXTURE_FLOAT_COMPONENTS_NV: \
103
+ case GL_SHADER_CONSISTENT_NV: \
104
+ case GL_TEXTURE_COMPARE_SGIX: \
105
+ case GLU_TESS_BOUNDARY_ONLY: \
106
+ case GLU_CULLING: \
107
+ case GLU_AUTO_LOAD_MATRIX: \
108
+ return GLBOOL2RUBY(value); \
109
+ default: \
110
+ return _conv_(value); \
111
+ } \
112
+ }
113
+
114
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY,GLint,INT2NUM)
115
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_U,GLuint,UINT2NUM)
116
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_LL,GLint64EXT,LL2NUM)
117
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_ULL,GLuint64EXT,ULL2NUM)
118
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_F,GLfloat,rb_float_new)
119
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_D,GLdouble,rb_float_new)
120
+
121
+
122
+ /* For conversion between ruby array (or object that can be converted to array) and C array.
123
+ The C array has to be preallocated by calling function. */
124
+ #define ARY2CTYPE(_type_,_convert_) \
125
+ static inline int ary2c##_type_( arg, cary, maxlen ) \
126
+ VALUE arg; \
127
+ GL##_type_ cary[]; \
128
+ int maxlen; \
129
+ { \
130
+ int i; \
131
+ VALUE ary = rb_Array(arg); \
132
+ if (maxlen < 1) \
133
+ maxlen = RARRAY_LEN(ary); \
134
+ else \
135
+ maxlen = maxlen < RARRAY_LEN(ary) ? maxlen : RARRAY_LEN(ary); \
136
+ for (i=0; i < maxlen; i++) \
137
+ cary[i] = (GL##_type_)_convert_(rb_ary_entry(ary,i)); \
138
+ return i; \
139
+ }
140
+
141
+ ARY2CTYPE(int,NUM2INT)
142
+ ARY2CTYPE(uint,NUM2UINT)
143
+ ARY2CTYPE(byte,NUM2INT)
144
+ ARY2CTYPE(ubyte,NUM2INT)
145
+ ARY2CTYPE(short,NUM2INT)
146
+ ARY2CTYPE(ushort,NUM2INT)
147
+ ARY2CTYPE(boolean,NUM2INT)
148
+ ARY2CTYPE(float,NUM2DBL)
149
+ ARY2CTYPE(double,NUM2DBL)
150
+
151
+ #define ary2cflt ary2cfloat
152
+ #define ary2cdbl ary2cdouble
153
+
154
+ #undef ARY2CTYPE
155
+
156
+ /* Converts either array or object responding to #to_a to C-style array */
157
+ #define ARY2CMAT(_type_) \
158
+ static inline void ary2cmat##_type_(rary, cary, cols, rows, count) \
159
+ VALUE rary; \
160
+ _type_ cary[]; \
161
+ int cols,rows; \
162
+ { \
163
+ int i; \
164
+ \
165
+ rary = rb_Array(rary); \
166
+ rary = rb_funcall(rary,rb_intern("flatten"),0); \
167
+ \
168
+ if (RARRAY_LEN(rary) != cols*rows) \
169
+ rb_raise(rb_eArgError, "passed array/matrix must have %i*%i elements",cols,rows); \
170
+ \
171
+ for (i=0; i < cols*rows; i++) \
172
+ cary[i] = (_type_) NUM2DBL(rb_ary_entry(rary,i)); \
173
+ }
174
+
175
+ ARY2CMAT(double)
176
+ ARY2CMAT(float)
177
+ #undef ARY2CMAT
178
+
179
+ #define ARY2CMATCNT(_type_) \
180
+ static inline void ary2cmat##_type_##count(rary, cary, cols, rows) \
181
+ VALUE rary; \
182
+ _type_ cary[]; \
183
+ int cols,rows; \
184
+ { \
185
+ int i; \
186
+ \
187
+ rary = rb_Array(rary); \
188
+ rary = rb_funcall(rary,rb_intern("flatten"),0); \
189
+ \
190
+ if (RARRAY_LEN(rary)<1 || (RARRAY_LEN(rary) % (cols*rows) != 0)) {\
191
+ xfree(cary); \
192
+ rb_raise(rb_eArgError, "passed array/matrix must conatain n x (%i*%i) elements",cols,rows); \
193
+ } \
194
+ \
195
+ for (i=0; i < RARRAY_LEN(rary); i++) \
196
+ cary[i] = (_type_) NUM2DBL(rb_ary_entry(rary,i)); \
197
+ }
198
+
199
+ ARY2CMATCNT(double)
200
+ ARY2CMATCNT(float)
201
+ #undef ARY2CMATCNT
202
+
203
+
204
+ #define EMPTY
205
+ #define FREE(_x_) xfree(_x_);
206
+
207
+ #define RET_ARRAY_OR_SINGLE(_size_,_conv_,_params_) RET_ARRAY_OR_SINGLE_FUNC(_size_,_conv_,_params_,EMPTY)
208
+ #define RET_ARRAY_OR_SINGLE_FREE(_size_,_conv_,_params_) RET_ARRAY_OR_SINGLE_FUNC(_size_,_conv_,_params_,FREE(_params_))
209
+
210
+ #define RET_ARRAY_OR_SINGLE_FUNC(_size_,_conv_,_params_,_extra_) \
211
+ { \
212
+ int iter; \
213
+ VALUE return_array; \
214
+ if (_size_ == 1) { \
215
+ return_array = _conv_(_params_[0]); \
216
+ } else { \
217
+ return_array = rb_ary_new2(_size_); \
218
+ for(iter=0;iter<_size_;iter++) \
219
+ rb_ary_push(return_array, _conv_(_params_[iter])); \
220
+ } \
221
+ _extra_ \
222
+ CHECK_GLERROR \
223
+ return return_array; \
224
+ }
225
+
226
+ #define RET_ARRAY_OR_SINGLE_BOOL(_size_,_conv_,_enum_,_params_) RET_ARRAY_OR_SINGLE_BOOL_FUNC(_size_,_conv_,_enum_,_params_,EMPTY)
227
+ #define RET_ARRAY_OR_SINGLE_BOOL_FREE(_size_,_conv_,_enum_,_params_) RET_ARRAY_OR_SINGLE_BOOL_FUNC(_size_,_conv_,_enum_,_params_,FREE(_params_))
228
+
229
+ #define RET_ARRAY_OR_SINGLE_BOOL_FUNC(_size_,_conv_,_enum_,_params_,_extra_) \
230
+ { \
231
+ int iter; \
232
+ VALUE return_array; \
233
+ if (_size_ == 1) { \
234
+ return_array = _conv_(_enum_,_params_[0]); \
235
+ } else { \
236
+ return_array = rb_ary_new2(_size_); \
237
+ for(iter=0;iter<_size_;iter++) \
238
+ rb_ary_push(return_array, _conv_(_enum_,_params_[iter])); \
239
+ } \
240
+ _extra_ \
241
+ CHECK_GLERROR \
242
+ return return_array; \
243
+ }