ffi-opengl 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *~
2
+ *.*~
3
+ generated/
4
+ announcement.txt
5
+
@@ -0,0 +1,22 @@
1
+ == 0.2.1 / 2009-06-17
2
+
3
+ * Minor improvements:
4
+ * Rework requires and cleanup.
5
+
6
+ == 0.2.0 / 2009-04-05
7
+
8
+ * Major improvements:
9
+ * Add support for MacOSX
10
+ * Add support for JRuby
11
+
12
+ * Minor improvements:
13
+ * Re-generate wrapper code fixing issues with callbacks
14
+ * Fix return type in functions that return string
15
+ * Add gears example
16
+ * Add a trivial benchmark that compares perfomance against
17
+ ruby-opengl
18
+
19
+ == 0.1.0 / 2009-03-06
20
+
21
+ * 1 major enhancement
22
+ * Birthday!
@@ -0,0 +1,76 @@
1
+ ffi-opengl
2
+ by Andrea Fazzi
3
+ http://github.com/remogatto/ffi-opengl/
4
+
5
+ == DESCRIPTION:
6
+
7
+ ffi-opengl is an autogenerated[1] ruby FFI interface towards OpenGL
8
+ libraries (GL, GLU, GLUT). The FFI layer ensures compatibility among
9
+ all Ruby implementation that support it. At the moment, this means
10
+ that you can run the same ffi-opengl script with MRI Ruby and JRuby.
11
+
12
+ ffi-opengl API aims to be a 1:1 tranposition of the OpenGL C API:
13
+ don't expect idiomatic ruby inside!
14
+
15
+ == FEATURES/PROBLEMS:
16
+
17
+ * Full [auto-generated] wrap of the OpenGL libraries.
18
+
19
+ * JRuby 1.3.0 compatible.
20
+
21
+ * No need of opengl development libraries.
22
+
23
+ * No compilation needed (once you have ruby-ffi installed or you are
24
+ using JRuby).
25
+
26
+ * Not yet tested on windows platform.
27
+
28
+ == SYNOPSIS:
29
+
30
+ See the example in 'examples/' folder. For the API documentation
31
+ please refer to OpenGL reference pages.
32
+
33
+ == REQUIREMENTS:
34
+
35
+ * ffi >= 0.3.0
36
+ * OpenGL libraries >= 1.2
37
+
38
+ == DOWNLOAD/INSTALL:
39
+
40
+ To download and install the gem from github:
41
+
42
+ [sudo] gem sources -a http://gems.github.com # needed only the first time
43
+ [sudo] gem install remogatto-ffi-opengl
44
+
45
+ To download the develpment trunk:
46
+
47
+ git clone http://github.com/remogatto/ffi-opengl.git
48
+
49
+ == LINKS:
50
+
51
+ 1. http://kenai.com/projects/ruby-ffi/sources/swig-generator/show
52
+
53
+ == LICENSE:
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2009 Andrea Fazzi
58
+
59
+ Permission is hereby granted, free of charge, to any person obtaining
60
+ a copy of this software and associated documentation files (the
61
+ 'Software'), to deal in the Software without restriction, including
62
+ without limitation the rights to use, copy, modify, merge, publish,
63
+ distribute, sublicense, and/or sell copies of the Software, and to
64
+ permit persons to whom the Software is furnished to do so, subject to
65
+ the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be
68
+ included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
71
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ begin
6
+ require 'bones'
7
+ Bones.setup
8
+ rescue LoadError
9
+ begin
10
+ load 'tasks/setup.rb'
11
+ rescue LoadError
12
+ raise RuntimeError, '### please install the "bones" gem ###'
13
+ end
14
+ end
15
+
16
+ ensure_in_path 'lib'
17
+ require 'ffi-opengl'
18
+
19
+ task :default => 'spec:run'
20
+
21
+ PROJ.name = 'ffi-opengl'
22
+ PROJ.authors = 'Andrea Fazzi'
23
+ PROJ.email = 'andrea.fazzi@alcacoop.it'
24
+ PROJ.url = 'http://github.com/remogatto/ffi-opengl'
25
+ PROJ.version = '0.2.1'
26
+ PROJ.rubyforge.name = 'ffi-opengl'
27
+
28
+ PROJ.readme_file = 'README.rdoc'
29
+
30
+ PROJ.ann.paragraphs << 'FEATURES/PROBLEMS' << 'SYNOPSIS' << 'REQUIREMENTS' << 'DOWNLOAD/INSTALL' << 'LINKS'
31
+ PROJ.ann.email[:to] << 'dev@ruby-ffi.kenai.com' << 'users@ruby-ffi.kenai.com'
32
+ PROJ.ann.email[:server] = 'smtp.gmail.com'
33
+
34
+ PROJ.spec.opts << '--color'
35
+ PROJ.ruby_opts = []
36
+
37
+ depend_on 'ffi', '0.3.0'
38
+
39
+ # EOF
@@ -0,0 +1,11 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require 'ffi-opengl'
3
+
4
+ require 'benchmark'
5
+
6
+ MAX_FRAMES = 30000
7
+
8
+ Benchmark.bm do |x|
9
+ x.report('ffi-opengl') { `ruby bench/ffi-opengl_spinning_cube.rb #{MAX_FRAMES}` }
10
+ x.report('ruby-opengl') { `ruby bench/ruby-opengl_spinning_cube.rb #{MAX_FRAMES}` }
11
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
2
+ require 'ffi-opengl'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. examples spinning_cube]))
4
+
5
+
6
+ include FFI, GL, GLU, GLUT
7
+ app = SpinningCube.new(ARGV[0].to_i)
8
+ app.light_diffuse = MemoryPointer.new(:float, 4).put_array_of_float(0, [1.0, 0.0, 0.0, 1.0])
9
+ app.light_position = MemoryPointer.new(:float, 4).put_array_of_float(0, [1.0, 1.0, 1.0, 0.0])
10
+ app.start
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'opengl'
3
+
4
+ require File.expand_path(File.join(File.dirname(__FILE__), %w[.. examples spinning_cube]))
5
+
6
+ class SpinningCube
7
+ def glut_init
8
+ glutInit
9
+ end
10
+ end
11
+
12
+ app = SpinningCube.new(ARGV[0].to_i)
13
+ app.light_diffuse = [1.0, 0.0, 0.0, 1.0]
14
+ app.light_position = [1.0, 1.0, 1.0, 0.0]
15
+ app.start
@@ -0,0 +1,330 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # 3-D gear wheels. This program is in the public domain.
4
+ #
5
+ # Command line options:
6
+ # -info print GL implementation information
7
+ # -exit automatically exit after 30 seconds
8
+ #
9
+ # 2005-05-01 Ruby version by Arto Bendiken based on gears.c rev 1.8.
10
+ # 2005-01-09 Original C version (gears.c) by Brian Paul et al.
11
+ # http://cvs.freedesktop.org/mesa/Mesa/progs/demos/gears.c?rev=1.8
12
+
13
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
14
+ require 'ffi-opengl'
15
+
16
+ class Gears
17
+ include FFI, GL, GLU, GLUT, Math
18
+
19
+ POS = MemoryPointer.new(:float, 4).put_array_of_float(0, [5.0, 5.0, 10.0, 0.0])
20
+ RED = MemoryPointer.new(:float, 4).put_array_of_float(0, [0.8, 0.1, 0.0, 1.0])
21
+ GREEN = MemoryPointer.new(:float, 4).put_array_of_float(0, [0.0, 0.8, 0.2, 1.0])
22
+ BLUE = MemoryPointer.new(:float, 4).put_array_of_float(0, [0.2, 0.2, 1.0, 1.0])
23
+
24
+ include Math
25
+
26
+ # Draw a gear wheel. You'll probably want to call this function when
27
+ # building a display list since we do a lot of trig here.
28
+ #
29
+ # Input: inner_radius - radius of hole at center
30
+ # outer_radius - radius at center of teeth
31
+ # width - width of gear
32
+ # teeth - number of teeth
33
+ # tooth_depth - depth of tooth
34
+ def gear(inner_radius, outer_radius, width, teeth, tooth_depth)
35
+ r0 = inner_radius
36
+ r1 = outer_radius - tooth_depth / 2.0
37
+ r2 = outer_radius + tooth_depth / 2.0
38
+
39
+ da = 2.0 * PI / teeth / 4.0
40
+
41
+ glShadeModel(GL_FLAT)
42
+
43
+ glNormal3f(0.0, 0.0, 1.0)
44
+
45
+ # Draw front face
46
+ glBegin(GL_QUAD_STRIP)
47
+ for i in 0..teeth
48
+ angle = i * 2.0 * PI / teeth
49
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5)
50
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5)
51
+ if i < teeth
52
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5)
53
+ glVertex3f(r1 * cos(angle + 3 * da),
54
+ r1 * sin(angle + 3 * da), width * 0.5)
55
+ end
56
+ end
57
+ glEnd()
58
+
59
+ # Draw front sides of teeth
60
+ glBegin(GL_QUADS)
61
+ for i in 0...teeth
62
+ angle = i * 2.0 * PI / teeth
63
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5)
64
+ glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5)
65
+ glVertex3f(r2 * cos(angle + 2 * da),
66
+ r2 * sin(angle + 2 * da), width * 0.5)
67
+ glVertex3f(r1 * cos(angle + 3 * da),
68
+ r1 * sin(angle + 3 * da), width * 0.5)
69
+ end
70
+ glEnd()
71
+
72
+ glNormal3f(0.0, 0.0, -1.0)
73
+
74
+ # Draw back face
75
+ glBegin(GL_QUAD_STRIP)
76
+ for i in 0..teeth
77
+ angle = i * 2.0 * PI / teeth
78
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5)
79
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5)
80
+ if i < teeth
81
+ glVertex3f(r1 * cos(angle + 3 * da),
82
+ r1 * sin(angle + 3 * da), -width * 0.5)
83
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5)
84
+ end
85
+ end
86
+ glEnd()
87
+
88
+ # Draw back sides of teeth
89
+ glBegin(GL_QUADS)
90
+ for i in 0...teeth
91
+ angle = i * 2.0 * PI / teeth
92
+ glVertex3f(r1 * cos(angle + 3 * da),
93
+ r1 * sin(angle + 3 * da), -width * 0.5)
94
+ glVertex3f(r2 * cos(angle + 2 * da),
95
+ r2 * sin(angle + 2 * da), -width * 0.5)
96
+ glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5)
97
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5)
98
+ end
99
+ glEnd()
100
+
101
+ # Draw outward faces of teeth
102
+ glBegin(GL_QUAD_STRIP)
103
+ for i in 0...teeth
104
+ angle = i * 2.0 * PI / teeth
105
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), width * 0.5)
106
+ glVertex3f(r1 * cos(angle), r1 * sin(angle), -width * 0.5)
107
+ u = r2 * cos(angle + da) - r1 * cos(angle)
108
+ v = r2 * sin(angle + da) - r1 * sin(angle)
109
+ len = sqrt(u * u + v * v)
110
+ u /= len
111
+ v /= len
112
+ glNormal3f(v, -u, 0.0)
113
+ glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), width * 0.5)
114
+ glVertex3f(r2 * cos(angle + da), r2 * sin(angle + da), -width * 0.5)
115
+ glNormal3f(cos(angle), sin(angle), 0.0)
116
+ glVertex3f(r2 * cos(angle + 2 * da),
117
+ r2 * sin(angle + 2 * da), width * 0.5)
118
+ glVertex3f(r2 * cos(angle + 2 * da),
119
+ r2 * sin(angle + 2 * da), -width * 0.5)
120
+ u = r1 * cos(angle + 3 * da) - r2 * cos(angle + 2 * da)
121
+ v = r1 * sin(angle + 3 * da) - r2 * sin(angle + 2 * da)
122
+ glNormal3f(v, -u, 0.0)
123
+ glVertex3f(r1 * cos(angle + 3 * da),
124
+ r1 * sin(angle + 3 * da), width * 0.5)
125
+ glVertex3f(r1 * cos(angle + 3 * da),
126
+ r1 * sin(angle + 3 * da), -width * 0.5)
127
+ glNormal3f(cos(angle), sin(angle), 0.0)
128
+ end
129
+ glVertex3f(r1 * cos(0), r1 * sin(0), width * 0.5)
130
+ glVertex3f(r1 * cos(0), r1 * sin(0), -width * 0.5)
131
+ glEnd()
132
+
133
+ glShadeModel(GL_SMOOTH)
134
+
135
+ # Draw inside radius cylinder
136
+ glBegin(GL_QUAD_STRIP)
137
+ for i in 0..teeth
138
+ angle = i * 2.0 * PI / teeth
139
+ glNormal3f(-cos(angle), -sin(angle), 0.0)
140
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), -width * 0.5)
141
+ glVertex3f(r0 * cos(angle), r0 * sin(angle), width * 0.5)
142
+ end
143
+ glEnd()
144
+ end
145
+
146
+ def draw
147
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
148
+
149
+ glPushMatrix()
150
+ glRotatef(@view_rotx, 1.0, 0.0, 0.0)
151
+ glRotatef(@view_roty, 0.0, 1.0, 0.0)
152
+ glRotatef(@view_rotz, 0.0, 0.0, 1.0)
153
+
154
+ glPushMatrix()
155
+ glTranslatef(-3.0, -2.0, 0.0)
156
+ glRotatef(@angle, 0.0, 0.0, 1.0)
157
+ glCallList(@gear1)
158
+ glPopMatrix()
159
+
160
+ glPushMatrix()
161
+ glTranslatef(3.1, -2.0, 0.0)
162
+ glRotatef(-2.0 * @angle - 9.0, 0.0, 0.0, 1.0)
163
+ glCallList(@gear2)
164
+ glPopMatrix()
165
+
166
+ glPushMatrix()
167
+ glTranslatef(-3.1, 4.2, 0.0)
168
+ glRotatef(-2.0 * @angle - 25.0, 0.0, 0.0, 1.0)
169
+ glCallList(@gear3)
170
+ glPopMatrix()
171
+
172
+ glPopMatrix()
173
+
174
+ glutSwapBuffers()
175
+
176
+ @frames = 0 if not defined? @frames
177
+ @t0 = 0 if not defined? @t0
178
+
179
+ @frames += 1
180
+ t = glutGet(GLUT_ELAPSED_TIME)
181
+ if t - @t0 >= 5000
182
+ seconds = (t - @t0) / 1000.0
183
+ fps = @frames / seconds
184
+ printf("%d frames in %6.3f seconds = %6.3f FPS\n",
185
+ @frames, seconds, fps)
186
+ @t0, @frames = t, 0
187
+ exit if defined? @autoexit and t >= 999.0 * @autoexit
188
+ end
189
+ end
190
+
191
+ def idle
192
+ t = glutGet(GLUT_ELAPSED_TIME) / 1000.0
193
+ @t0_idle = t if !defined? @t0_idle
194
+ # 90 degrees per second
195
+ @angle += 70.0 * (t - @t0_idle)
196
+ @t0_idle = t
197
+ glutPostRedisplay()
198
+ end
199
+
200
+ # Change view angle, exit upon ESC
201
+ def key(k, x, y)
202
+ case k
203
+ when ?z
204
+ @view_rotz += 5.0
205
+ when ?Z
206
+ @view_rotz -= 5.0
207
+ when 27 # Escape
208
+ exit
209
+ end
210
+ glutPostRedisplay()
211
+ end
212
+
213
+ # Change view angle
214
+ def special(k, x, y)
215
+ case k
216
+ when GLUT_KEY_UP
217
+ @view_rotx += 5.0
218
+ when GLUT_KEY_DOWN
219
+ @view_rotx -= 5.0
220
+ when GLUT_KEY_LEFT
221
+ @view_roty += 5.0
222
+ when GLUT_KEY_RIGHT
223
+ @view_roty -= 5.0
224
+ end
225
+ glutPostRedisplay()
226
+ end
227
+
228
+ # New window size or exposure
229
+ def reshape(width, height)
230
+ h = height.to_f / width.to_f
231
+ glViewport(0, 0, width, height)
232
+ glMatrixMode(GL_PROJECTION)
233
+ glLoadIdentity()
234
+ glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0)
235
+ glMatrixMode(GL_MODELVIEW)
236
+ glLoadIdentity()
237
+ glTranslatef(0.0, 0.0, -40.0)
238
+ end
239
+
240
+ def init
241
+ @angle = 0.0
242
+ @view_rotx, @view_roty, @view_rotz = 20.0, 30.0, 0.0
243
+
244
+ glLightfv(GL_LIGHT0, GL_POSITION, POS)
245
+ glEnable(GL_CULL_FACE)
246
+ glEnable(GL_LIGHTING)
247
+ glEnable(GL_LIGHT0)
248
+ glEnable(GL_DEPTH_TEST)
249
+
250
+ # Make the gears
251
+ @gear1 = glGenLists(1)
252
+ glNewList(@gear1, GL_COMPILE)
253
+ glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, RED)
254
+ gear(1.0, 4.0, 1.0, 20, 0.7)
255
+ glEndList()
256
+
257
+ @gear2 = glGenLists(1)
258
+ glNewList(@gear2, GL_COMPILE)
259
+ glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, GREEN)
260
+ gear(0.5, 2.0, 2.0, 10, 0.7)
261
+ glEndList()
262
+
263
+ @gear3 = glGenLists(1)
264
+ glNewList(@gear3, GL_COMPILE)
265
+ glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, BLUE)
266
+ gear(1.3, 2.0, 0.5, 10, 0.7)
267
+ glEndList()
268
+
269
+ glEnable(GL_NORMALIZE)
270
+
271
+ ARGV.each do |arg|
272
+ case arg
273
+ when '-info'
274
+ printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER))
275
+ printf("GL_VERSION = %s\n", glGetString(GL_VERSION))
276
+ printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR))
277
+ printf("GL_EXTENSIONS = %s\n", glGetString(GL_EXTENSIONS))
278
+ when '-exit'
279
+ @autoexit = 30
280
+ printf("Auto Exit after %i seconds.\n", @autoexit);
281
+ end
282
+ end
283
+ end
284
+
285
+ def visible(vis)
286
+ glutIdleFunc((vis == GLUT_VISIBLE ? method(:idle).to_proc : nil))
287
+ end
288
+
289
+ def mouse(button, state, x, y)
290
+ @mouse = state
291
+ @x0, @y0 = x, y
292
+ end
293
+
294
+ def motion(x, y)
295
+ if @mouse == GLUT_DOWN then
296
+ @view_roty += @x0 - x
297
+ @view_rotx += @y0 - y
298
+ end
299
+ @x0, @y0 = x, y
300
+ end
301
+
302
+ def initialize
303
+ # argc, argv parameters
304
+ glutInit(MemoryPointer.new(:int, 1).put_int(0, 0),
305
+ MemoryPointer.new(:pointer, 1).put_pointer(0, nil))
306
+ glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE)
307
+
308
+ glutInitWindowPosition(0, 0)
309
+ glutInitWindowSize(300, 300)
310
+ glutCreateWindow('Gears')
311
+ init()
312
+
313
+ glutDisplayFunc(method(:draw).to_proc)
314
+ glutReshapeFunc(method(:reshape).to_proc)
315
+ glutKeyboardFunc(method(:key).to_proc)
316
+ glutSpecialFunc(method(:special).to_proc)
317
+ glutVisibilityFunc(method(:visible).to_proc)
318
+ glutMouseFunc(method(:mouse).to_proc)
319
+ glutMotionFunc(method(:motion).to_proc)
320
+ end
321
+
322
+ def start
323
+ glutMainLoop()
324
+ end
325
+
326
+ end
327
+
328
+ Gears.new.start
329
+
330
+