opengl 0.9.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +0 -0
  4. data/.autotest +29 -0
  5. data/.gemtest +0 -0
  6. data/.gitignore +6 -0
  7. data/.travis.yml +19 -0
  8. data/History.rdoc +77 -0
  9. data/MIT-LICENSE +18 -0
  10. data/Manifest.txt +138 -0
  11. data/README.rdoc +102 -0
  12. data/Rakefile +60 -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 +155 -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/common.h +428 -0
  96. data/ext/opengl/conv.h +244 -0
  97. data/ext/opengl/extconf.rb +47 -0
  98. data/ext/opengl/funcdef.h +313 -0
  99. data/ext/opengl/gl-1.0-1.1.c +3075 -0
  100. data/ext/opengl/gl-1.2.c +155 -0
  101. data/ext/opengl/gl-1.3.c +443 -0
  102. data/ext/opengl/gl-1.4.c +348 -0
  103. data/ext/opengl/gl-1.5.c +224 -0
  104. data/ext/opengl/gl-2.0.c +667 -0
  105. data/ext/opengl/gl-2.1.c +57 -0
  106. data/ext/opengl/gl-3.0.c +493 -0
  107. data/ext/opengl/gl-enums.c +4873 -0
  108. data/ext/opengl/gl-enums.h +14588 -0
  109. data/ext/opengl/gl-error.c +112 -0
  110. data/ext/opengl/gl-error.h +28 -0
  111. data/ext/opengl/gl-ext-3dfx.c +27 -0
  112. data/ext/opengl/gl-ext-arb.c +875 -0
  113. data/ext/opengl/gl-ext-ati.c +41 -0
  114. data/ext/opengl/gl-ext-ext.c +889 -0
  115. data/ext/opengl/gl-ext-gremedy.c +41 -0
  116. data/ext/opengl/gl-ext-nv.c +680 -0
  117. data/ext/opengl/gl-types.h +67 -0
  118. data/ext/opengl/gl.c +220 -0
  119. data/ext/opengl/gl_buffer.c +177 -0
  120. data/ext/opengl/opengl.c +7 -0
  121. data/lib/gl.rb +1 -0
  122. data/lib/opengl.rb +53 -0
  123. data/lib/opengl/2.0/opengl.so +0 -0
  124. data/lib/opengl/2.1/opengl.so +0 -0
  125. data/lib/opengl/test_case.rb +86 -0
  126. data/test/dummy.xorg.conf +140 -0
  127. data/test/test_gl.rb +38 -0
  128. data/test/test_gl_10_11.rb +1363 -0
  129. data/test/test_gl_12.rb +71 -0
  130. data/test/test_gl_13.rb +221 -0
  131. data/test/test_gl_14.rb +218 -0
  132. data/test/test_gl_15.rb +258 -0
  133. data/test/test_gl_20.rb +417 -0
  134. data/test/test_gl_21.rb +436 -0
  135. data/test/test_gl_ext_arb.rb +508 -0
  136. data/test/test_gl_ext_ati.rb +28 -0
  137. data/test/test_gl_ext_ext.rb +608 -0
  138. data/test/test_gl_ext_gremedy.rb +29 -0
  139. data/test/test_gl_ext_nv.rb +349 -0
  140. data/test/test_opengl_buffer.rb +120 -0
  141. data/utils/README +6 -0
  142. data/utils/enumgen.rb +108 -0
  143. data/utils/extlistgen.rb +90 -0
  144. metadata +319 -0
  145. metadata.gz.sig +1 -0
@@ -0,0 +1,67 @@
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
+ /* GL types - define if system GLheaders are not recent
17
+ Note: must be included before gl-enums.h */
18
+
19
+ #if HAVE_STDINT_H
20
+ #include <stdint.h>
21
+ #endif
22
+
23
+ #ifndef HAVE_TYPE_INT64_T
24
+ typedef __int64 int64_t;
25
+ #endif
26
+ #ifndef HAVE_TYPE_UINT64_T
27
+ typedef unsigned __int64 uint64_t;
28
+ #endif
29
+
30
+ #if HAVE_INTTYPES_H
31
+ #include <inttypes.h>
32
+ #endif
33
+
34
+ /* GL base */
35
+ #ifndef GL_VERSION_1_5
36
+ typedef ptrdiff_t GLintptr;
37
+ typedef ptrdiff_t GLsizeiptr;
38
+ #endif
39
+
40
+ #ifndef GL_VERSION_2_0
41
+ typedef char GLchar;
42
+ #endif
43
+
44
+ /* new GL types introduced by ARB extensions */
45
+ #ifndef GL_ARB_half_float_pixel
46
+ typedef unsigned short GLhalfARB;
47
+ #endif
48
+
49
+ #ifndef GL_ARB_shader_objects
50
+ typedef char GLcharARB;
51
+ typedef unsigned int GLhandleARB;
52
+ #endif
53
+
54
+ #ifndef GL_ARB_vertex_buffer_object
55
+ typedef ptrdiff_t GLintptrARB;
56
+ typedef ptrdiff_t GLsizeiptrARB;
57
+ #endif
58
+
59
+ /* new GL types introduced by other extensions */
60
+ #ifndef GL_NV_half_float
61
+ typedef unsigned short GLhalfNV;
62
+ #endif
63
+
64
+ #ifndef GL_EXT_timer_query
65
+ typedef int64_t GLint64EXT;
66
+ typedef uint64_t GLuint64EXT;
67
+ #endif
data/ext/opengl/gl.c ADDED
@@ -0,0 +1,220 @@
1
+ /*
2
+ * Copyright (C) 1999 - 2005 Yoshi <yoshi@giganet.net>
3
+ * Copyright (C) 2006 John M. Gabriele <jmg3000@gmail.com>
4
+ * Copyright (C) 2007 Jan Dvorak <jan.dvorak@kraxnet.cz>
5
+ *
6
+ * This program is distributed under the terms of the MIT license.
7
+ * See the included MIT-LICENSE file for the terms of this license.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
13
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
14
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
15
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ */
17
+
18
+ #include "common.h"
19
+
20
+ static VALUE module;
21
+
22
+ void gl_init_enums(VALUE);
23
+ void gl_init_functions_1_0__1_1(VALUE);
24
+ void gl_init_functions_1_2(VALUE);
25
+ void gl_init_functions_1_3(VALUE);
26
+ void gl_init_functions_1_4(VALUE);
27
+ void gl_init_functions_1_5(VALUE);
28
+ void gl_init_functions_2_0(VALUE);
29
+ void gl_init_functions_2_1(VALUE);
30
+ void gl_init_functions_3_0(VALUE);
31
+ void gl_init_functions_ext_3dfx(VALUE);
32
+ void gl_init_functions_ext_arb(VALUE);
33
+ void gl_init_functions_ext_ati(VALUE);
34
+ void gl_init_functions_ext_ext(VALUE);
35
+ void gl_init_functions_ext_gremedy(VALUE);
36
+ void gl_init_functions_ext_nv(VALUE);
37
+ void gl_init_buffer(void);
38
+
39
+ static int opengl_version[2]; /* major, minor */
40
+ static char *opengl_extensions = NULL;
41
+
42
+ /* Returns current OpenGL version as major, minor or 0,0 if
43
+ * unknown (context not yet initialised etc.) The version is
44
+ * cached for subsequent calls.
45
+ */
46
+ const int *GetOpenglVersion(void)
47
+ {
48
+ if (opengl_version[0]==0) { /* not cached, query */
49
+ const char *vstr = (const char *) glGetString(GL_VERSION);
50
+ CHECK_GLERROR_FROM("glGetString");
51
+ if (vstr)
52
+ sscanf( vstr, "%d.%d", &opengl_version[0], &opengl_version[1] );
53
+ }
54
+ return opengl_version;
55
+ }
56
+
57
+ /* Checks if OpenGL version is at least the same or higher than
58
+ * major.minor
59
+ */
60
+ GLboolean CheckOpenglVersion(int major, int minor)
61
+ {
62
+ const int *version;
63
+
64
+ version = GetOpenglVersion();
65
+
66
+ if (version[0]>major || (version[0]==major && version[1] >=minor))
67
+ return GL_TRUE;
68
+ else
69
+ return GL_FALSE;
70
+ }
71
+
72
+ /* Returns supported OpenGL extensions as char* or NULL
73
+ * if unknown (context not yet initialised etc.) The list is
74
+ * cached for subsequent calls.
75
+ */
76
+ const char *GetOpenglExtensions(void)
77
+ {
78
+ if (opengl_extensions == NULL) {
79
+ const char *estr = (const char *) glGetString(GL_EXTENSIONS);
80
+ CHECK_GLERROR_FROM("glGetString");
81
+ if (estr) {
82
+ long len = strlen(estr);
83
+ opengl_extensions = ALLOC_N(GLchar,len+1+1); /* terminating null and added space */
84
+ strcpy(opengl_extensions,estr);
85
+ opengl_extensions[len] = ' '; /* add space char for easy searchs */
86
+ opengl_extensions[len+1] = '\0';
87
+ }
88
+ }
89
+ return opengl_extensions;
90
+ }
91
+
92
+ /* Checks if extension is supported by the current OpenGL implementation
93
+ */
94
+ GLboolean CheckExtension(const char *name)
95
+ {
96
+ const char *extensions;
97
+ char *name_tmp;
98
+ long name_len;
99
+ GLboolean res;
100
+
101
+ extensions = GetOpenglExtensions();
102
+
103
+ if(extensions==NULL)
104
+ return GL_FALSE;
105
+
106
+ /* add trailing space */
107
+ name_len = strlen(name);
108
+ name_tmp = ALLOC_N(GLchar,name_len+1+1); /* terminating null and added space */
109
+ strcpy(name_tmp,name);
110
+ name_tmp[name_len] = ' '; /* add space char for search */
111
+ name_tmp[name_len+1] = '\0';
112
+
113
+ if (strstr(extensions,name_tmp))
114
+ res = GL_TRUE;
115
+ else
116
+ res = GL_FALSE;
117
+
118
+ xfree(name_tmp);
119
+ return res;
120
+ }
121
+
122
+ /* wrapper for CheckOpenglVersion and CheckExtension, also used by macros
123
+ */
124
+ GLboolean CheckVersionExtension(const char *name)
125
+ {
126
+ if (name && name[0] && name[0]>='0' && name[0]<='9') { /* GL version query */
127
+ int major,minor;
128
+
129
+ if (sscanf( name, "%d.%d", &major, &minor ) != 2)
130
+ return GL_FALSE;
131
+
132
+ return (CheckOpenglVersion(major,minor));
133
+ } else {
134
+ return (CheckExtension(name));
135
+ }
136
+ }
137
+
138
+ /* Checks if given OpenGL version or extension is available
139
+ */
140
+ static VALUE
141
+ IsAvailable(obj,arg1)
142
+ VALUE obj,arg1;
143
+ {
144
+ char *name = NULL;
145
+ VALUE s;
146
+ GLboolean res;
147
+
148
+ s = rb_funcall(arg1, rb_intern("to_s"), 0);
149
+ name = RSTRING_PTR(s);
150
+
151
+ res = CheckVersionExtension(name);
152
+
153
+ return GLBOOL2RUBY(res);
154
+ }
155
+
156
+ /* Checks whether non-zero buffer of type $buffer is bound
157
+ * - this affects several functions that pass data from/to OpenGL.
158
+ */
159
+ GLint CheckBufferBinding(GLint buffer)
160
+ {
161
+ GLint result = 0;
162
+
163
+ /* check if the buffer functionality is supported */
164
+ switch(buffer) {
165
+ case GL_ARRAY_BUFFER_BINDING:
166
+ case GL_ELEMENT_ARRAY_BUFFER_BINDING:
167
+ if (!CheckOpenglVersion(1,5))
168
+ return 0;
169
+ break;
170
+ case GL_PIXEL_PACK_BUFFER_BINDING:
171
+ case GL_PIXEL_UNPACK_BUFFER_BINDING:
172
+ if (!CheckOpenglVersion(2,1))
173
+ return 0;
174
+ break;
175
+ default:
176
+ rb_raise(rb_eRuntimeError,"Internal Error: buffer type '%i' does not exist", buffer);
177
+ break;
178
+ }
179
+ glGetIntegerv(buffer,&result);
180
+ CHECK_GLERROR_FROM("glGetIntegerv");
181
+ return result;
182
+ }
183
+
184
+ DLLEXPORT void Init_gl()
185
+ {
186
+ VALUE mOpenGL = rb_path2class("OpenGL");
187
+ VALUE version = rb_const_get(mOpenGL, rb_intern("VERSION"));
188
+
189
+ gl_init_buffer();
190
+
191
+ module = rb_define_module("Gl");
192
+
193
+ /* TODO remove */
194
+ rb_define_const(module, "BINDINGS_VERSION", version);
195
+ rb_define_const(module, "RUBY_OPENGL_VERSION", version);
196
+
197
+ gl_init_error(module);
198
+ gl_init_enums(module);
199
+ gl_init_functions_1_0__1_1(module);
200
+ gl_init_functions_1_2(module);
201
+ gl_init_functions_1_3(module);
202
+ gl_init_functions_1_4(module);
203
+ gl_init_functions_1_5(module);
204
+ gl_init_functions_2_0(module);
205
+ gl_init_functions_2_1(module);
206
+ gl_init_functions_3_0(module);
207
+ gl_init_functions_ext_3dfx(module);
208
+ gl_init_functions_ext_arb(module);
209
+ gl_init_functions_ext_ati(module);
210
+ gl_init_functions_ext_ext(module);
211
+ gl_init_functions_ext_gremedy(module);
212
+ gl_init_functions_ext_nv(module);
213
+
214
+ rb_define_module_function(module, "is_available?", IsAvailable, 1);
215
+ rb_define_module_function(module, "is_supported?", IsAvailable, 1);
216
+ rb_define_module_function(module, "extension_available?", IsAvailable, 1);
217
+ rb_define_module_function(module, "extension_supported?", IsAvailable, 1);
218
+ rb_define_module_function(module, "version_available?", IsAvailable, 1);
219
+ rb_define_module_function(module, "version_supported?", IsAvailable, 1);
220
+ }
@@ -0,0 +1,177 @@
1
+ #include "common.h"
2
+
3
+ static GLvoid * (APIENTRY * fptr_glMapBuffer)(GLenum,GLenum);
4
+ static GLboolean (APIENTRY * fptr_glUnmapBuffer)(GLenum);
5
+
6
+ struct buffer {
7
+ GLenum target;
8
+ void *ptr;
9
+ GLsizeiptr len;
10
+ };
11
+
12
+ static void
13
+ buffer_free(void *ptr) {
14
+ struct buffer *buf = ptr;
15
+ LOAD_GL_FUNC(glUnmapBuffer, "1.5");
16
+
17
+ if (buf->ptr != NULL)
18
+ fptr_glUnmapBuffer(buf->target);
19
+ }
20
+
21
+ static size_t
22
+ buffer_memsize(const void *ptr) {
23
+ const struct buffer *buf = ptr;
24
+ return sizeof(struct buffer) + (size_t)buf->len;
25
+ }
26
+
27
+ static const rb_data_type_t buffer_type = {
28
+ "OpenGL/buffer",
29
+ { NULL, buffer_free, buffer_memsize, },
30
+ };
31
+
32
+ VALUE
33
+ rb_gl_buffer_s_map(VALUE klass, VALUE _target, VALUE _access) {
34
+ struct buffer *buf = ALLOC(struct buffer);
35
+ LOAD_GL_FUNC(glMapBuffer, "1.5");
36
+
37
+ buf->target = RUBY2GLENUM(_target);
38
+ buf->len = 0;
39
+
40
+ buf->ptr = fptr_glMapBuffer(buf->target, RUBY2GLENUM(_access));
41
+
42
+ if (buf->ptr == NULL) {
43
+ xfree(buf);
44
+ CHECK_GLERROR_FROM("glMapBuffer");
45
+ }
46
+
47
+ return TypedData_Wrap_Struct(klass, &buffer_type, buf);
48
+ }
49
+
50
+ static VALUE
51
+ rb_gl_buffer_addr(VALUE self) {
52
+ struct buffer *buf;
53
+
54
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
55
+
56
+ return SIZET2NUM((size_t)buf->ptr);
57
+ }
58
+
59
+ static VALUE
60
+ rb_gl_buffer_length(VALUE self) {
61
+ struct buffer *buf;
62
+
63
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
64
+
65
+ return RETCONV_GLsizeiptr(buf->len);
66
+ }
67
+
68
+ static VALUE
69
+ rb_gl_buffer_read(int argc, VALUE *argv, VALUE self) {
70
+ struct buffer *buf;
71
+ VALUE _length, _offset;
72
+ GLsizeiptr offset, length;
73
+
74
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
75
+
76
+ rb_scan_args(argc, argv, "02", &_length, &_offset);
77
+
78
+ if (buf->len == 0 && NIL_P(_length))
79
+ rb_raise(rb_eArgError, "length must be provided for unbounded buffer");
80
+
81
+ length = NUM2SIZET(_length);
82
+
83
+ if (NIL_P(_offset)) {
84
+ offset = 0;
85
+ } else {
86
+ offset = NUM2SIZET(_offset);
87
+ }
88
+
89
+ if (buf->len != 0 && length + offset > buf->len)
90
+ rb_raise(rb_eArgError, "read to %lu past end of buffer %lu",
91
+ (unsigned long)(length + offset), (unsigned long)buf->len);
92
+
93
+ return rb_str_new((char *)buf->ptr + offset, length);
94
+ }
95
+
96
+ static VALUE
97
+ rb_gl_buffer_target(VALUE self) {
98
+ struct buffer *buf;
99
+
100
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
101
+
102
+ return RETCONV_GLenum(buf->target);
103
+ }
104
+
105
+ static VALUE
106
+ rb_gl_buffer_unmap(VALUE self) {
107
+ struct buffer *buf;
108
+ LOAD_GL_FUNC(glUnmapBuffer, "1.5");
109
+
110
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
111
+
112
+ if (!buf->ptr)
113
+ return self;
114
+
115
+ fptr_glUnmapBuffer(buf->target);
116
+
117
+ CHECK_GLERROR_FROM("glUnmapBuffer");
118
+
119
+ buf->ptr = NULL;
120
+ buf->len = 0;
121
+ buf->target = 0;
122
+
123
+ return self;
124
+ }
125
+
126
+ static VALUE
127
+ rb_gl_buffer_write(int argc, VALUE *argv, VALUE self) {
128
+ struct buffer *buf;
129
+ VALUE _data, _offset;
130
+ GLsizeiptr offset;
131
+ long length;
132
+
133
+ TypedData_Get_Struct(self, struct buffer, &buffer_type, buf);
134
+
135
+ if (!buf->ptr)
136
+ rb_raise(rb_eArgError, "write to unmapped buffer");
137
+
138
+ rb_scan_args(argc, argv, "11", &_data, &_offset);
139
+
140
+ if (NIL_P(_data))
141
+ rb_raise(rb_eArgError, "cannot write nil to buffer");
142
+
143
+ _data = rb_String(_data);
144
+
145
+ length = RSTRING_LEN(_data);
146
+
147
+ if (NIL_P(_offset)) {
148
+ offset = 0;
149
+ } else {
150
+ offset = NUM2SIZET(_offset);
151
+ }
152
+
153
+ if (buf->len != 0 && length + offset > buf->len)
154
+ rb_raise(rb_eArgError, "write to %lu past end of buffer %lu",
155
+ (unsigned long)(length + offset), (unsigned long)buf->len);
156
+
157
+ memcpy((char *)buf->ptr + offset, RSTRING_PTR(_data), RSTRING_LEN(_data));
158
+
159
+ return self;
160
+ }
161
+
162
+ void
163
+ gl_init_buffer(void) {
164
+ VALUE mOpenGL = rb_path2class("OpenGL");
165
+ VALUE cBuffer = rb_define_class_under(mOpenGL, "Buffer", rb_cObject);
166
+
167
+ rb_undef_alloc_func(cBuffer);
168
+ rb_define_singleton_method(cBuffer, "map", rb_gl_buffer_s_map, 2);
169
+
170
+ rb_define_method(cBuffer, "addr", rb_gl_buffer_addr, 0);
171
+ rb_define_method(cBuffer, "length", rb_gl_buffer_length, 0);
172
+ rb_define_method(cBuffer, "read", rb_gl_buffer_read, -1);
173
+ rb_define_method(cBuffer, "target", rb_gl_buffer_target, 0);
174
+ rb_define_method(cBuffer, "unmap", rb_gl_buffer_unmap, 0);
175
+ rb_define_method(cBuffer, "write", rb_gl_buffer_write, -1);
176
+ }
177
+