danabr75-opengl 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +29 -0
  3. data/.gemtest +0 -0
  4. data/.gitignore +6 -0
  5. data/.travis.yml +19 -0
  6. data/Gemfile +8 -0
  7. data/History.md +101 -0
  8. data/MIT-LICENSE +18 -0
  9. data/Manifest.txt +142 -0
  10. data/README.rdoc +105 -0
  11. data/Rakefile +170 -0
  12. data/danabr75-opengl.gemspec +32 -0
  13. data/examples/NeHe/NeHe.png +0 -0
  14. data/examples/NeHe/crate.png +0 -0
  15. data/examples/NeHe/glass.png +0 -0
  16. data/examples/NeHe/nehe_lesson02.rb +117 -0
  17. data/examples/NeHe/nehe_lesson03.rb +123 -0
  18. data/examples/NeHe/nehe_lesson04.rb +132 -0
  19. data/examples/NeHe/nehe_lesson05.rb +182 -0
  20. data/examples/NeHe/nehe_lesson06.rb +186 -0
  21. data/examples/NeHe/nehe_lesson07.rb +240 -0
  22. data/examples/NeHe/nehe_lesson08.rb +255 -0
  23. data/examples/NeHe/nehe_lesson09.rb +203 -0
  24. data/examples/NeHe/nehe_lesson11.rb +176 -0
  25. data/examples/NeHe/nehe_lesson12.rb +203 -0
  26. data/examples/NeHe/nehe_lesson16.rb +211 -0
  27. data/examples/NeHe/nehe_lesson19.rb +209 -0
  28. data/examples/NeHe/nehe_lesson36.rb +308 -0
  29. data/examples/NeHe/particle.png +0 -0
  30. data/examples/NeHe/star.png +0 -0
  31. data/examples/NeHe/tim.png +0 -0
  32. data/examples/OrangeBook/3Dlabs-License.txt +33 -0
  33. data/examples/OrangeBook/brick.frag +36 -0
  34. data/examples/OrangeBook/brick.rb +378 -0
  35. data/examples/OrangeBook/brick.vert +41 -0
  36. data/examples/OrangeBook/particle.frag +17 -0
  37. data/examples/OrangeBook/particle.rb +408 -0
  38. data/examples/OrangeBook/particle.vert +38 -0
  39. data/examples/README +16 -0
  40. data/examples/RedBook/aapoly.rb +143 -0
  41. data/examples/RedBook/aargb.rb +121 -0
  42. data/examples/RedBook/accanti.rb +164 -0
  43. data/examples/RedBook/accpersp.rb +217 -0
  44. data/examples/RedBook/alpha.rb +125 -0
  45. data/examples/RedBook/alpha3D.rb +160 -0
  46. data/examples/RedBook/bezcurve.rb +107 -0
  47. data/examples/RedBook/bezmesh.rb +139 -0
  48. data/examples/RedBook/checker.rb +126 -0
  49. data/examples/RedBook/clip.rb +97 -0
  50. data/examples/RedBook/colormat.rb +137 -0
  51. data/examples/RedBook/cube.rb +71 -0
  52. data/examples/RedBook/depthcue.rb +101 -0
  53. data/examples/RedBook/dof.rb +206 -0
  54. data/examples/RedBook/double.rb +107 -0
  55. data/examples/RedBook/drawf.rb +93 -0
  56. data/examples/RedBook/feedback.rb +147 -0
  57. data/examples/RedBook/fog.rb +168 -0
  58. data/examples/RedBook/font.rb +153 -0
  59. data/examples/RedBook/hello.rb +81 -0
  60. data/examples/RedBook/image.rb +139 -0
  61. data/examples/RedBook/jitter.rb +207 -0
  62. data/examples/RedBook/light.rb +157 -0
  63. data/examples/RedBook/lines.rb +130 -0
  64. data/examples/RedBook/list.rb +113 -0
  65. data/examples/RedBook/material.rb +276 -0
  66. data/examples/RedBook/mipmap.rb +158 -0
  67. data/examples/RedBook/model.rb +115 -0
  68. data/examples/RedBook/movelight.rb +134 -0
  69. data/examples/RedBook/pickdepth.rb +181 -0
  70. data/examples/RedBook/planet.rb +110 -0
  71. data/examples/RedBook/quadric.rb +160 -0
  72. data/examples/RedBook/robot.rb +117 -0
  73. data/examples/RedBook/select.rb +198 -0
  74. data/examples/RedBook/smooth.rb +97 -0
  75. data/examples/RedBook/stencil.rb +165 -0
  76. data/examples/RedBook/stroke.rb +169 -0
  77. data/examples/RedBook/surface.rb +168 -0
  78. data/examples/RedBook/teaambient.rb +134 -0
  79. data/examples/RedBook/teapots.rb +184 -0
  80. data/examples/RedBook/tess.rb +185 -0
  81. data/examples/RedBook/texbind.rb +149 -0
  82. data/examples/RedBook/texgen.rb +171 -0
  83. data/examples/RedBook/texturesurf.rb +130 -0
  84. data/examples/RedBook/varray.rb +161 -0
  85. data/examples/RedBook/wrap.rb +150 -0
  86. data/examples/misc/OGLBench.rb +338 -0
  87. data/examples/misc/anisotropic.rb +196 -0
  88. data/examples/misc/fbo_test.rb +357 -0
  89. data/examples/misc/font-glut.rb +47 -0
  90. data/examples/misc/glfwtest.rb +30 -0
  91. data/examples/misc/plane.rb +161 -0
  92. data/examples/misc/readpixel.rb +66 -0
  93. data/examples/misc/sdltest.rb +36 -0
  94. data/examples/misc/trislam.rb +829 -0
  95. data/ext/opengl/GL/gl.h +2115 -0
  96. data/ext/opengl/GL/glext.h +11770 -0
  97. data/ext/opengl/common.h +372 -0
  98. data/ext/opengl/conv.h +247 -0
  99. data/ext/opengl/extconf.rb +19 -0
  100. data/ext/opengl/fptr_struct.h +912 -0
  101. data/ext/opengl/funcdef.h +279 -0
  102. data/ext/opengl/gl-1.0-1.1.c +3345 -0
  103. data/ext/opengl/gl-1.2.c +155 -0
  104. data/ext/opengl/gl-1.3.c +447 -0
  105. data/ext/opengl/gl-1.4.c +346 -0
  106. data/ext/opengl/gl-1.5.c +225 -0
  107. data/ext/opengl/gl-2.0.c +665 -0
  108. data/ext/opengl/gl-2.1.c +57 -0
  109. data/ext/opengl/gl-3.0.c +502 -0
  110. data/ext/opengl/gl-enums.c +4853 -0
  111. data/ext/opengl/gl-enums.h +14528 -0
  112. data/ext/opengl/gl-error.c +110 -0
  113. data/ext/opengl/gl-error.h +25 -0
  114. data/ext/opengl/gl-ext-3dfx.c +27 -0
  115. data/ext/opengl/gl-ext-arb.c +880 -0
  116. data/ext/opengl/gl-ext-ati.c +41 -0
  117. data/ext/opengl/gl-ext-ext.c +885 -0
  118. data/ext/opengl/gl-ext-gremedy.c +41 -0
  119. data/ext/opengl/gl-ext-nv.c +679 -0
  120. data/ext/opengl/gl.c +265 -0
  121. data/ext/opengl/gl_buffer.c +202 -0
  122. data/ext/opengl/glimpl.c +187 -0
  123. data/ext/opengl/glimpl.h +47 -0
  124. data/ext/opengl/opengl.c +9 -0
  125. data/lib/gl.rb +1 -0
  126. data/lib/opengl.rb +79 -0
  127. data/lib/opengl/bindings_version.rb +4 -0
  128. data/lib/opengl/implementation.rb +38 -0
  129. data/lib/opengl/test_case.rb +86 -0
  130. data/test/dummy.xorg.conf +140 -0
  131. data/test/test_gl.rb +45 -0
  132. data/test/test_gl_10_11.rb +1363 -0
  133. data/test/test_gl_12.rb +71 -0
  134. data/test/test_gl_13.rb +221 -0
  135. data/test/test_gl_14.rb +218 -0
  136. data/test/test_gl_15.rb +258 -0
  137. data/test/test_gl_20.rb +417 -0
  138. data/test/test_gl_21.rb +436 -0
  139. data/test/test_gl_ext_arb.rb +508 -0
  140. data/test/test_gl_ext_ati.rb +28 -0
  141. data/test/test_gl_ext_ext.rb +608 -0
  142. data/test/test_gl_ext_gremedy.rb +29 -0
  143. data/test/test_gl_ext_nv.rb +349 -0
  144. data/test/test_glimpl.rb +23 -0
  145. data/test/test_opengl_buffer.rb +122 -0
  146. data/utils/README +6 -0
  147. data/utils/enumgen.rb +108 -0
  148. data/utils/extlistgen.rb +90 -0
  149. metadata +326 -0
@@ -0,0 +1,372 @@
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
+ #ifndef GLFUNC_MAGIC_START
28
+ #include "extconf.h"
29
+ #endif
30
+
31
+ #include <ctype.h>
32
+
33
+ #include "GL/gl.h"
34
+
35
+ #include "gl-enums.h"
36
+ #include "gl-error.h"
37
+
38
+ #include "funcdef.h"
39
+ #include "conv.h"
40
+ #include "glimpl.h"
41
+
42
+ #ifndef APIENTRY
43
+ #define APIENTRY
44
+ #endif
45
+
46
+ #ifndef CALLBACK
47
+ #define CALLBACK
48
+ #endif
49
+
50
+ /* these two macros are cast to a 32 bit type in the places they are used */
51
+ #ifndef RARRAY_LENINT
52
+ #define RARRAY_LENINT RARRAY_LEN
53
+ #endif
54
+
55
+ #ifndef RSTRING_LENINT
56
+ #define RSTRING_LENINT RSTRING_LEN
57
+ #endif
58
+
59
+ /* */
60
+
61
+ /* For now we do not honor pixel store modes, so we need to
62
+ force them to defaults on each affected function call for
63
+ correct size requirement calculations */
64
+ #define FORCE_PIXEL_STORE_MODE \
65
+ { \
66
+ DECL_GL_FUNC_PTR(GLvoid,glPushClientAttrib,(GLbitfield)); \
67
+ DECL_GL_FUNC_PTR(GLvoid,glPixelStorei,(GLenum,GLint)); \
68
+ LOAD_GL_FUNC(glPushClientAttrib, NULL); \
69
+ LOAD_GL_FUNC(glPixelStorei, NULL); \
70
+ fptr_glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT); \
71
+ fptr_glPixelStorei(GL_PACK_ALIGNMENT, 1); \
72
+ fptr_glPixelStorei(GL_PACK_SKIP_PIXELS, 0); \
73
+ fptr_glPixelStorei(GL_PACK_SKIP_ROWS, 0); \
74
+ fptr_glPixelStorei(GL_PACK_ROW_LENGTH, 0); \
75
+ fptr_glPixelStorei(GL_PACK_SKIP_IMAGES, 0); \
76
+ fptr_glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0); \
77
+ if (CheckVersionExtension(obj, "GL_SGIS_texture4D")) { \
78
+ fptr_glPixelStorei(GL_PACK_SKIP_VOLUMES_SGIS, 0); \
79
+ fptr_glPixelStorei(GL_PACK_IMAGE_DEPTH_SGIS, 0); \
80
+ } \
81
+ }
82
+
83
+ #define RESTORE_PIXEL_STORE_MODE \
84
+ { \
85
+ DECL_GL_FUNC_PTR(GLvoid,glPopClientAttrib,(void)); \
86
+ LOAD_GL_FUNC(glPopClientAttrib, NULL); \
87
+ fptr_glPopClientAttrib(); \
88
+ }
89
+
90
+ #define CHECK_BUFFER_BINDING(_buffer_) CheckBufferBinding(obj, (_buffer_))
91
+
92
+ GLboolean CheckVersionExtension(VALUE self, const char *name);
93
+ void EnsureVersionExtension(VALUE obj, const char *verext);
94
+ GLint CheckBufferBinding(VALUE self, GLint buffer);
95
+ extern VALUE rb_cGlimpl;
96
+
97
+ /* -------------------------------------------------------------------- */
98
+
99
+ /* gets number of components for given format */
100
+ static inline int glformat_size(GLenum format)
101
+ {
102
+ switch(format)
103
+ {
104
+ case GL_COLOR_INDEX:
105
+ case GL_RED:
106
+ case GL_GREEN:
107
+ case GL_BLUE:
108
+ case GL_ALPHA:
109
+ case GL_RED_INTEGER_EXT:
110
+ case GL_GREEN_INTEGER_EXT:
111
+ case GL_BLUE_INTEGER_EXT:
112
+ case GL_ALPHA_INTEGER_EXT:
113
+ case GL_STENCIL_INDEX:
114
+ case GL_DEPTH_COMPONENT:
115
+ case GL_LUMINANCE:
116
+ case GL_LUMINANCE_INTEGER_EXT:
117
+ return 1;
118
+
119
+ case GL_LUMINANCE_ALPHA:
120
+ case GL_LUMINANCE_ALPHA_INTEGER_EXT:
121
+ case GL_422_EXT:
122
+ case GL_422_REV_EXT:
123
+ case GL_422_AVERAGE_EXT:
124
+ case GL_422_REV_AVERAGE_EXT:
125
+ case GL_YCRCB_422_SGIX:
126
+ case GL_YCBCR_422_APPLE:
127
+ case GL_YCBCR_MESA:
128
+ case GL_DEPTH_STENCIL_NV:
129
+ case GL_HILO_NV:
130
+ case GL_DSDT_NV:
131
+ case GL_DUDV_ATI:
132
+ case GL_DU8DV8_ATI:
133
+ case GL_FORMAT_SUBSAMPLE_24_24_OML:
134
+ return 2;
135
+
136
+ case GL_RGB:
137
+ case GL_RGB_INTEGER_EXT:
138
+ case GL_BGR_EXT:
139
+ case GL_BGR_INTEGER_EXT:
140
+ case GL_YCRCB_444_SGIX:
141
+ case GL_DSDT_MAG_NV:
142
+ case GL_FORMAT_SUBSAMPLE_244_244_OML:
143
+ return 3;
144
+
145
+ case GL_RGBA:
146
+ case GL_RGBA_INTEGER_EXT:
147
+ case GL_BGRA_EXT:
148
+ case GL_BGRA_INTEGER_EXT:
149
+ case GL_ABGR_EXT:
150
+ case GL_CMYK_EXT:
151
+ case GL_DSDT_MAG_VIB_NV:
152
+ return 4;
153
+
154
+ case GL_CMYKA_EXT:
155
+ return 5;
156
+
157
+ /* GL spec permits passing direct format size instead of enum (now obsolete) */
158
+ case 1:
159
+ case 2:
160
+ case 3:
161
+ case 4:
162
+ return format;
163
+
164
+ default:
165
+ rb_raise(rb_eArgError, "Unknown GL format enum %i",format);
166
+ return -1; /* not reached */
167
+ }
168
+ }
169
+
170
+ /* computes unit (pixel) size for given type and format */
171
+ static inline int gltype_glformat_unit_size(GLenum type,GLenum format)
172
+ {
173
+ unsigned int format_size;
174
+
175
+ format_size = glformat_size(format);
176
+
177
+ switch(type)
178
+ {
179
+ case GL_BYTE:
180
+ case GL_UNSIGNED_BYTE:
181
+ case GL_BITMAP:
182
+ return 1*format_size;
183
+
184
+ case GL_SHORT:
185
+ case GL_UNSIGNED_SHORT:
186
+ case GL_HALF_FLOAT_ARB:
187
+ return 2*format_size;
188
+
189
+ case GL_INT:
190
+ case GL_UNSIGNED_INT:
191
+ case GL_FLOAT:
192
+ return 4*format_size;
193
+
194
+ /* in packed formats all components are packed into/unpacked from single datatype,
195
+ so number of components(format_size) doesn't matter for total size calculation */
196
+ case GL_UNSIGNED_BYTE_3_3_2:
197
+ case GL_UNSIGNED_BYTE_2_3_3_REV:
198
+ return 1;
199
+
200
+ case GL_UNSIGNED_SHORT_5_6_5:
201
+ case GL_UNSIGNED_SHORT_5_6_5_REV:
202
+ case GL_UNSIGNED_SHORT_4_4_4_4:
203
+ case GL_UNSIGNED_SHORT_4_4_4_4_REV:
204
+ case GL_UNSIGNED_SHORT_5_5_5_1:
205
+ case GL_UNSIGNED_SHORT_1_5_5_5_REV:
206
+ case GL_UNSIGNED_SHORT_8_8_APPLE:
207
+ case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
208
+ return 2;
209
+
210
+ case GL_UNSIGNED_INT_8_8_8_8:
211
+ case GL_UNSIGNED_INT_8_8_8_8_REV:
212
+ case GL_UNSIGNED_INT_10_10_10_2:
213
+ case GL_UNSIGNED_INT_2_10_10_10_REV:
214
+ case GL_UNSIGNED_INT_24_8_NV:
215
+ case GL_UNSIGNED_INT_S8_S8_8_8_NV:
216
+ case GL_UNSIGNED_INT_8_8_S8_S8_REV_NV:
217
+ case GL_UNSIGNED_INT_10F_11F_11F_REV_EXT:
218
+ case GL_UNSIGNED_INT_5_9_9_9_REV_EXT:
219
+ case GL_FLOAT_32_UNSIGNED_INT_24_8_REV_NV:
220
+ return 4;
221
+
222
+ default:
223
+ rb_raise(rb_eArgError, "Unknown GL type enum %i",type);
224
+ return -1; /* not reached */
225
+ }
226
+ }
227
+
228
+ /* returns size(units) for given type of shader uniform var */
229
+ static inline int get_uniform_size(GLenum uniform_type)
230
+ {
231
+ int uniform_size = 0;
232
+
233
+ switch (uniform_type) {
234
+ case GL_FLOAT:
235
+ case GL_INT:
236
+ case GL_UNSIGNED_INT:
237
+ case GL_BOOL:
238
+ case GL_SAMPLER_1D:
239
+ case GL_SAMPLER_2D:
240
+ case GL_SAMPLER_3D:
241
+ case GL_SAMPLER_CUBE:
242
+ case GL_SAMPLER_1D_SHADOW:
243
+ case GL_SAMPLER_2D_SHADOW:
244
+ case GL_SAMPLER_2D_RECT_ARB:
245
+ case GL_SAMPLER_2D_RECT_SHADOW_ARB:
246
+ case GL_SAMPLER_1D_ARRAY_EXT:
247
+ case GL_SAMPLER_2D_ARRAY_EXT:
248
+ case GL_SAMPLER_BUFFER_EXT:
249
+ case GL_SAMPLER_1D_ARRAY_SHADOW_EXT:
250
+ case GL_SAMPLER_2D_ARRAY_SHADOW_EXT:
251
+ case GL_SAMPLER_CUBE_SHADOW_EXT:
252
+ case GL_INT_SAMPLER_1D_EXT:
253
+ case GL_INT_SAMPLER_2D_EXT:
254
+ case GL_INT_SAMPLER_3D_EXT:
255
+ case GL_INT_SAMPLER_CUBE_EXT:
256
+ case GL_INT_SAMPLER_2D_RECT_EXT:
257
+ case GL_INT_SAMPLER_1D_ARRAY_EXT:
258
+ case GL_INT_SAMPLER_2D_ARRAY_EXT:
259
+ case GL_INT_SAMPLER_BUFFER_EXT:
260
+ case GL_UNSIGNED_INT_SAMPLER_1D_EXT:
261
+ case GL_UNSIGNED_INT_SAMPLER_2D_EXT:
262
+ case GL_UNSIGNED_INT_SAMPLER_3D_EXT:
263
+ case GL_UNSIGNED_INT_SAMPLER_CUBE_EXT:
264
+ case GL_UNSIGNED_INT_SAMPLER_2D_RECT_EXT:
265
+ case GL_UNSIGNED_INT_SAMPLER_1D_ARRAY_EXT:
266
+ case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY_EXT:
267
+ case GL_UNSIGNED_INT_SAMPLER_BUFFER_EXT:
268
+ uniform_size = 1;
269
+ break;
270
+ case GL_FLOAT_VEC2:
271
+ case GL_INT_VEC2:
272
+ case GL_UNSIGNED_INT_VEC2_EXT:
273
+ case GL_BOOL_VEC2:
274
+ uniform_size = 2;
275
+ break;
276
+ case GL_FLOAT_VEC3:
277
+ case GL_INT_VEC3:
278
+ case GL_UNSIGNED_INT_VEC3_EXT:
279
+ case GL_BOOL_VEC3:
280
+ uniform_size = 3;
281
+ break;
282
+ case GL_FLOAT_VEC4:
283
+ case GL_INT_VEC4:
284
+ case GL_UNSIGNED_INT_VEC4_EXT:
285
+ case GL_BOOL_VEC4:
286
+ case GL_FLOAT_MAT2:
287
+ uniform_size = 4;
288
+ break;
289
+ case GL_FLOAT_MAT2x3:
290
+ case GL_FLOAT_MAT3x2:
291
+ uniform_size = 6;
292
+ break;
293
+ case GL_FLOAT_MAT2x4:
294
+ case GL_FLOAT_MAT4x2:
295
+ uniform_size = 8;
296
+ break;
297
+ case GL_FLOAT_MAT3:
298
+ uniform_size = 9;
299
+ break;
300
+ case GL_FLOAT_MAT4x3:
301
+ case GL_FLOAT_MAT3x4:
302
+ uniform_size = 12;
303
+ break;
304
+ case GL_FLOAT_MAT4:
305
+ uniform_size = 16;
306
+ break;
307
+ default:
308
+ rb_raise(rb_eTypeError, "Unsupported uniform type '%i'",uniform_type);
309
+ }
310
+ return uniform_size;
311
+ }
312
+
313
+ static inline int GetDataSize(GLenum type,GLenum format,int num)
314
+ {
315
+ int size;
316
+ int unit_size;
317
+
318
+ unit_size = gltype_glformat_unit_size(type,format);
319
+
320
+ if (type==GL_BITMAP)
321
+ size = unit_size*(num/8); /* FIXME account for alignment */
322
+ else
323
+ size = unit_size*num;
324
+
325
+ return size;
326
+ }
327
+
328
+ /* Checks if data size of 'data' string confirms to passed format values */
329
+ /* 'num' is number of elements, each of size 'format' * 'type' */
330
+ static inline void CheckDataSize(GLenum type,GLenum format,int num,VALUE data)
331
+ {
332
+ int size;
333
+
334
+ size = GetDataSize(type,format,num);
335
+
336
+ if (RSTRING_LEN(data) < size)
337
+ rb_raise(rb_eArgError, "Length of specified data doesn't correspond to format and type parameters passed. Calculated length: %i",size);
338
+ }
339
+
340
+ /* -------------------------------------------------------------------- */
341
+ static inline VALUE allocate_buffer_with_string( long size )
342
+ {
343
+ return rb_str_new(NULL, size);
344
+ }
345
+
346
+ static inline VALUE pack_array_or_pass_string(GLenum type,VALUE ary)
347
+ {
348
+ const char *type_str;
349
+
350
+ if (TYPE(ary)==T_STRING)
351
+ return ary;
352
+
353
+ Check_Type(ary,T_ARRAY);
354
+
355
+ switch(type) {
356
+ case GL_FLOAT: type_str = "f*"; break;
357
+ case GL_DOUBLE: type_str = "d*"; break;
358
+ case GL_BYTE: type_str = "c*"; break;
359
+ case GL_SHORT: type_str = "s*"; break;
360
+ case GL_INT: type_str = "i*"; break;
361
+ case GL_UNSIGNED_BYTE: type_str = "C*"; break;
362
+ case GL_UNSIGNED_SHORT: type_str = "S*"; break;
363
+ case GL_UNSIGNED_INT: type_str = "I*"; break;
364
+ default:
365
+ rb_raise(rb_eTypeError,"Unknown type %i",type);
366
+ return Qnil; /* not reached */
367
+ }
368
+
369
+ return rb_funcall(ary,rb_intern("pack"),1,rb_str_new2(type_str));
370
+ }
371
+
372
+ #endif
@@ -0,0 +1,247 @@
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 HAVE_STRUCT_RFLOAT_FLOAT_VALUE
31
+ #define FLOAT_VAL_ACCESS(val) RFLOAT(val)->float_value
32
+ #elif defined(RFLOAT)
33
+ #define FLOAT_VAL_ACCESS(val) RFLOAT(val)->value
34
+ #else
35
+ /* struct RFloat isn't available in ruby 2.2 */
36
+ #define FLOAT_VAL_ACCESS(val) RFLOAT_VALUE(val)
37
+ #endif
38
+
39
+ #define FASTCONV(_name_,_type_,_convfix_,_convfallback_) \
40
+ static inline _type_ _name_(val) \
41
+ VALUE val; \
42
+ { \
43
+ if (FIXNUM_P(val)) \
44
+ return (_type_) _convfix_(val); \
45
+ \
46
+ if (TYPE(val) == T_FLOAT) \
47
+ return (_type_)FLOAT_VAL_ACCESS(val); \
48
+ \
49
+ if ((val) == Qtrue) \
50
+ return (_type_)(GL_TRUE); \
51
+ \
52
+ if ((val) == Qfalse || (val) == Qnil) \
53
+ return (_type_)(GL_FALSE); \
54
+ \
55
+ return (_convfallback_(val)); \
56
+ }
57
+
58
+ FASTCONV(num2double,double,FIX2LONG,rb_num2dbl)
59
+ #if SIZEOF_INT < SIZEOF_LONG
60
+ /* For 64bit platforms with LP64 mode */
61
+ FASTCONV(num2int,long,FIX2LONG,rb_num2int)
62
+ FASTCONV(num2uint,unsigned long,FIX2ULONG,rb_num2uint)
63
+ #else
64
+ /* All other platforms */
65
+ FASTCONV(num2int,long,FIX2LONG,(int)NUM2LONG)
66
+ FASTCONV(num2uint,unsigned long,FIX2ULONG,(unsigned int)NUM2ULONG)
67
+ #endif
68
+
69
+ #undef FASTCONV
70
+
71
+ #define RUBY2GLENUM(x) \
72
+ (x) == Qtrue ? GL_TRUE : ((x) == Qfalse ? GL_FALSE : NUM2INT(x))
73
+
74
+ /* For conversion between ruby and GL boolean values */
75
+ #define GLBOOL2RUBY(x) \
76
+ (x) == GL_TRUE ? Qtrue : ((x)==GL_FALSE ? Qfalse : INT2NUM((x)))
77
+
78
+ #define RUBYBOOL2GL(x) \
79
+ (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
+ return GLBOOL2RUBY(value); \
106
+ default: \
107
+ return _conv_(value); \
108
+ } \
109
+ }
110
+
111
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY,GLint,INT2NUM)
112
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_U,GLuint,UINT2NUM)
113
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_LL,GLint64,LL2NUM)
114
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_ULL,GLuint64,ULL2NUM)
115
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_F,GLfloat,rb_float_new)
116
+ cond_GLBOOL2RUBY_FUNC(cond_GLBOOL2RUBY_D,GLdouble,rb_float_new)
117
+
118
+
119
+ /* For conversion between ruby array (or object that can be converted to
120
+ * array) and C array.
121
+ *
122
+ * The C array has to be preallocated by calling function. */
123
+ #define ARY2CTYPE(_type_,_convert_) \
124
+ static inline long ary2c##_type_( arg, cary, maxlen ) \
125
+ VALUE arg; \
126
+ GL##_type_ cary[]; \
127
+ long maxlen; \
128
+ { \
129
+ long i; \
130
+ VALUE ary = rb_Array(arg); \
131
+ if (maxlen < 1) \
132
+ maxlen = RARRAY_LEN(ary); \
133
+ else \
134
+ maxlen = maxlen < RARRAY_LEN(ary) ? maxlen : RARRAY_LEN(ary); \
135
+ for (i=0; i < maxlen; i++) \
136
+ cary[i] = (GL##_type_)_convert_(rb_ary_entry(ary,i)); \
137
+ return i; \
138
+ }
139
+
140
+ ARY2CTYPE(int,NUM2INT)
141
+ ARY2CTYPE(uint,NUM2UINT)
142
+ ARY2CTYPE(byte,NUM2INT)
143
+ ARY2CTYPE(ubyte,NUM2INT)
144
+ ARY2CTYPE(short,NUM2INT)
145
+ ARY2CTYPE(ushort,NUM2INT)
146
+ ARY2CTYPE(boolean,GLBOOL2RUBY)
147
+ ARY2CTYPE(float,NUM2DBL)
148
+ ARY2CTYPE(double,NUM2DBL)
149
+
150
+ #define ary2cflt ary2cfloat
151
+ #define ary2cdbl ary2cdouble
152
+
153
+ #undef ARY2CTYPE
154
+
155
+ /* Converts either array or object responding to #to_a to C-style array */
156
+ #define ARY2CMAT(_type_) \
157
+ static inline void ary2cmat##_type_(rary, cary, cols, rows) \
158
+ VALUE rary; \
159
+ _type_ cary[]; \
160
+ int cols,rows; \
161
+ { \
162
+ int i; \
163
+ \
164
+ rary = rb_Array(rary); \
165
+ rary = rb_funcall(rary,rb_intern("flatten"),0); \
166
+ \
167
+ if (RARRAY_LEN(rary) != cols*rows) \
168
+ rb_raise(rb_eArgError, "passed array/matrix must have %i*%i elements",cols,rows); \
169
+ \
170
+ for (i=0; i < cols*rows; i++) \
171
+ cary[i] = (_type_) NUM2DBL(rb_ary_entry(rary,i)); \
172
+ }
173
+
174
+ ARY2CMAT(double)
175
+ ARY2CMAT(float)
176
+ #undef ARY2CMAT
177
+
178
+ #define ARY2CMATCNT(_type_) \
179
+ static inline void ary2cmat##_type_##count(rary, cary, cols, rows) \
180
+ VALUE rary; \
181
+ _type_ cary[]; \
182
+ int cols,rows; \
183
+ { \
184
+ int i; \
185
+ \
186
+ rary = rb_Array(rary); \
187
+ rary = rb_funcall(rary,rb_intern("flatten"),0); \
188
+ \
189
+ if (RARRAY_LEN(rary)<1 || (RARRAY_LEN(rary) % (cols*rows) != 0)) {\
190
+ xfree(cary); \
191
+ rb_raise(rb_eArgError, "passed array/matrix must conatain n x (%i*%i) elements",cols,rows); \
192
+ } \
193
+ \
194
+ for (i=0; i < RARRAY_LEN(rary); i++) \
195
+ cary[i] = (_type_) NUM2DBL(rb_ary_entry(rary,i)); \
196
+ }
197
+
198
+ ARY2CMATCNT(double)
199
+ ARY2CMATCNT(float)
200
+ #undef ARY2CMATCNT
201
+
202
+ #define EMPTY
203
+ #define FREE(_x_) xfree(_x_);
204
+
205
+ #define RET_ARRAY_OR_SINGLE(_name_, _size_, _conv_, _params_) \
206
+ RET_ARRAY_OR_SINGLE_FUNC(_name_, _size_, _conv_, _params_, EMPTY)
207
+
208
+ #define RET_ARRAY_OR_SINGLE_FREE(_name, _size_, _conv_, _params_) \
209
+ RET_ARRAY_OR_SINGLE_FUNC(_name, _size_, _conv_, _params_, FREE(_params_))
210
+
211
+ #define RET_ARRAY_OR_SINGLE_FUNC(_name_, _size_, _conv_, _params_, _extra_) \
212
+ do { \
213
+ int iter; \
214
+ VALUE return_array; \
215
+ if (_size_ == 1) { \
216
+ return_array = _conv_(_params_[0]); \
217
+ } else { \
218
+ return_array = rb_ary_new2(_size_); \
219
+ for(iter=0;iter<_size_;iter++) \
220
+ rb_ary_push(return_array, _conv_(_params_[iter])); \
221
+ } \
222
+ _extra_ \
223
+ CHECK_GLERROR_FROM(_name_); \
224
+ return return_array; \
225
+ } while (0)
226
+
227
+ #define RET_ARRAY_OR_SINGLE_BOOL(_name_, _size_, _conv_, _enum_, _params_) \
228
+ RET_ARRAY_OR_SINGLE_BOOL_FUNC(_name_, _size_, _conv_, _enum_, _params_, EMPTY)
229
+
230
+ #define RET_ARRAY_OR_SINGLE_BOOL_FREE(_name_, _size_, _conv_, _enum_, _params_) \
231
+ RET_ARRAY_OR_SINGLE_BOOL_FUNC(_name_, _size_, _conv_, _enum_, _params_, FREE(_params_))
232
+
233
+ #define RET_ARRAY_OR_SINGLE_BOOL_FUNC(_name_, _size_, _conv_, _enum_, _params_, _extra_) \
234
+ do { \
235
+ int iter; \
236
+ VALUE return_array; \
237
+ if (_size_ == 1) { \
238
+ return_array = _conv_(_enum_,_params_[0]); \
239
+ } else { \
240
+ return_array = rb_ary_new2(_size_); \
241
+ for(iter=0;iter<_size_;iter++) \
242
+ rb_ary_push(return_array, _conv_(_enum_,_params_[iter])); \
243
+ } \
244
+ _extra_ \
245
+ CHECK_GLERROR_FROM(_name_); \
246
+ return return_array; \
247
+ } while (0)