opengl3 0.0.1.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +17 -0
- data/examples/math3d.rb +65 -0
- data/examples/ogl-2_1-basic.rb +107 -0
- data/examples/ogl-3_2-basic.rb +128 -0
- data/examples/ogl-3_2-cube.rb +203 -0
- data/examples/ogl-4_0-quad_tessellation.rb +283 -0
- data/examples/ogl-4_0-triangle_tessellation.rb +262 -0
- data/lib/glut.rb +125 -0
- data/lib/opengl.rb +7 -0
- data/lib/opengl/constants.rb +15 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_1_1.yml +230 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_1_2.yml +36 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_1_3.yml +59 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_1_4.yml +16 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_1_5.yml +28 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_2_0.yml +80 -0
- data/lib/opengl/constants/core/2_x/GL_VERSION_2_1.yml +16 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_depth_buffer_float.yml +3 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_framebuffer_object.yml +77 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_framebuffer_sRGB.yml +1 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_half_float_vertex.yml +1 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_map_buffer_range.yml +6 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_texture_compression_rgtc.yml +4 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_texture_rg.yml +22 -0
- data/lib/opengl/constants/core/3_0/GL_ARB_vertex_array_object.yml +1 -0
- data/lib/opengl/constants/core/3_0/GL_VERSION_3_0.yml +224 -0
- data/lib/opengl/constants/core/3_1/GL_ARB_copy_buffer.yml +4 -0
- data/lib/opengl/constants/core/3_1/GL_ARB_uniform_buffer_object.yml +33 -0
- data/lib/opengl/constants/core/3_1/GL_VERSION_3_1.yml +65 -0
- data/lib/opengl/constants/core/3_2/GL_ARB_depth_clamp.yml +1 -0
- data/lib/opengl/constants/core/3_2/GL_ARB_provoking_vertex.yml +4 -0
- data/lib/opengl/constants/core/3_2/GL_ARB_seamless_cube_map.yml +1 -0
- data/lib/opengl/constants/core/3_2/GL_ARB_sync.yml +15 -0
- data/lib/opengl/constants/core/3_2/GL_ARB_texture_multisample.yml +21 -0
- data/lib/opengl/constants/core/3_2/GL_VERSION_3_2.yml +76 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_blend_func_extended.yml +5 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_occlusion_query2.yml +1 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_sampler_objects.yml +1 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_texture_rgb10_a2ui.yml +1 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_texture_swizzle.yml +5 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_timer_query.yml +2 -0
- data/lib/opengl/constants/core/3_3/GL_ARB_vertex_type_2_10_10_10_rev.yml +2 -0
- data/lib/opengl/constants/core/3_3/GL_VERSION_3_3.yml +25 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_draw_indirect.yml +2 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_gpu_shader5.yml +6 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_gpu_shader_fp64.yml +13 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_shader_subroutine.yml +11 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_tessellation_shader.yml +37 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_transform_feedback2.yml +6 -0
- data/lib/opengl/constants/core/4_0/GL_ARB_transform_feedback3.yml +2 -0
- data/lib/opengl/constants/core/4_0/GL_VERSION_4_0.yml +88 -0
- data/lib/opengl/constants/core/4_1/GL_ARB_ES2_compatibility.yml +16 -0
- data/lib/opengl/constants/core/4_1/GL_ARB_get_program_binary.yml +4 -0
- data/lib/opengl/constants/core/4_1/GL_ARB_separate_shader_objects.yml +9 -0
- data/lib/opengl/constants/core/4_1/GL_ARB_viewport_array.yml +13 -0
- data/lib/opengl/constants/core/4_1/GL_VERSION_4_1.yml +44 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_compressed_texture_pixel_storage.yml +8 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_internalformat_query.yml +1 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_map_buffer_alignment.yml +1 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_shader_atomic_counters.yml +29 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_shader_image_load_store.yml +64 -0
- data/lib/opengl/constants/core/4_2/GL_ARB_texture_storage.yml +1 -0
- data/lib/opengl/constants/core/4_2/GL_VERSION_4_2.yml +116 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_ES3_compatibility.yml +13 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_compute_shader.yml +18 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_explicit_uniform_location.yml +1 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_framebuffer_no_attachments.yml +9 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_internalformat_query2.yml +115 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_program_interface_query.yml +50 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_shader_storage_buffer_object.yml +18 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_stencil_texturing.yml +1 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_texture_buffer_range.yml +3 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_texture_view.yml +5 -0
- data/lib/opengl/constants/core/4_3/GL_ARB_vertex_attrib_binding.yml +7 -0
- data/lib/opengl/constants/core/4_3/GL_KHR_debug.yml +41 -0
- data/lib/opengl/constants/core/4_3/GL_VERSION_4_3.yml +281 -0
- data/lib/opengl/constants/ext/GL_EXT_texture_compression_s3tc.yml +4 -0
- data/lib/opengl/constants/ext/GL_EXT_texture_filter_anisotropic.yml +2 -0
- data/lib/opengl/constants/ext/GL_EXT_texture_sRGB.yml +16 -0
- data/lib/opengl/debugging.rb +38 -0
- data/lib/opengl/gl.rb +26 -0
- data/lib/opengl/platform.rb +4 -0
- data/lib/opengl/platform/base.rb +30 -0
- data/lib/opengl/platform/darwin.rb +24 -0
- data/lib/opengl/platform/linux.rb +25 -0
- data/lib/opengl/platform/types/darwin.yml +2 -0
- data/lib/opengl/platform/types/default.yml +27 -0
- data/lib/opengl/platform/types/linux.yml +2 -0
- data/lib/opengl/raw.rb +44 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_0.yml +259 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_1.yml +106 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_2.yml +60 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_3.yml +85 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_4.yml +43 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_1_5.yml +103 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_2_0.yml +537 -0
- data/lib/opengl/raw/core/2_x/GL_VERSION_2_1.yml +43 -0
- data/lib/opengl/raw/core/3_0/GL_ARB_framebuffer_object.yml +128 -0
- data/lib/opengl/raw/core/3_0/GL_ARB_map_buffer_range.yml +14 -0
- data/lib/opengl/raw/core/3_0/GL_ARB_vertex_array_object.yml +19 -0
- data/lib/opengl/raw/core/3_0/GL_VERSION_3_0.yml +340 -0
- data/lib/opengl/raw/core/3_1/GL_ARB_copy_buffer.yml +9 -0
- data/lib/opengl/raw/core/3_1/GL_ARB_uniform_buffer_object.yml +50 -0
- data/lib/opengl/raw/core/3_1/GL_VERSION_3_1.yml +26 -0
- data/lib/opengl/raw/core/3_2/GL_ARB_draw_elements_base_vertex.yml +37 -0
- data/lib/opengl/raw/core/3_2/GL_ARB_provoking_vertex.yml +5 -0
- data/lib/opengl/raw/core/3_2/GL_ARB_sync.yml +39 -0
- data/lib/opengl/raw/core/3_2/GL_ARB_texture_multisample.yml +31 -0
- data/lib/opengl/raw/core/3_2/GL_VERSION_3_2.yml +20 -0
- data/lib/opengl/raw/core/3_3/GL_ARB_blend_func_extended.yml +13 -0
- data/lib/opengl/raw/core/3_3/GL_ARB_sampler_objects.yml +80 -0
- data/lib/opengl/raw/core/3_3/GL_ARB_timer_query.yml +18 -0
- data/lib/opengl/raw/core/3_3/GL_ARB_vertex_type_2_10_10_10_rev.yml +57 -0
- data/lib/opengl/raw/core/3_3/GL_VERSION_3_3.yml +6 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_draw_indirect.yml +12 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_gpu_shader_fp64.yml +120 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_shader_subroutine.yml +58 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_tessellation_shader.yml +11 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_transform_feedback2.yml +31 -0
- data/lib/opengl/raw/core/4_0/GL_ARB_transform_feedback3.yml +25 -0
- data/lib/opengl/raw/core/4_0/GL_VERSION_4_0.yml +30 -0
- data/lib/opengl/raw/core/4_1/GL_ARB_ES2_compatibility.yml +28 -0
- data/lib/opengl/raw/core/4_1/GL_ARB_get_program_binary.yml +22 -0
- data/lib/opengl/raw/core/4_1/GL_ARB_separate_shader_objects.yml +429 -0
- data/lib/opengl/raw/core/4_1/GL_ARB_vertex_attrib_64bit.yml +61 -0
- data/lib/opengl/raw/core/4_1/GL_ARB_viewport_array.yml +63 -0
- data/lib/opengl/raw/core/4_1/GL_VERSION_4_1.yml +1 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_base_instance.yml +28 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_internalformat_query.yml +9 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_shader_atomic_counters.yml +8 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_shader_image_load_store.yml +15 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_texture_storage.yml +52 -0
- data/lib/opengl/raw/core/4_2/GL_ARB_transform_feedback_instanced.yml +14 -0
- data/lib/opengl/raw/core/4_2/GL_VERSION_4_2.yml +1 -0
- data/lib/opengl/type.rb +74 -0
- data/lib/opengl/version.rb +3 -0
- data/lib/opengl/wrapper.rb +13 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_0.rb +165 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_1.rb +29 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_2.rb +13 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_3.rb +16 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_4.rb +18 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_1_5.rb +49 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_2_0.rb +184 -0
- data/lib/opengl/wrapper/core/2_x/GL_VERSION_2_1.rb +13 -0
- data/lib/opengl/wrapper/core/3_0/GL_ARB_framebuffer_object.rb +43 -0
- data/lib/opengl/wrapper/core/3_0/GL_ARB_map_buffer_range.rb +11 -0
- data/lib/opengl/wrapper/core/3_0/GL_ARB_vertex_array_object.rb +16 -0
- data/lib/opengl/wrapper/core/3_0/GL_VERSION_3_0.rb +87 -0
- data/lib/opengl/wrapper/macros/base.rb +80 -0
- data/opengl.gemspec +25 -0
- data/spec/spec_helper.rb +8 -0
- data/test/test_helper.rb +8 -0
- metadata +269 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 David Lin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# OpenGL (opengl3)
|
2
|
+
|
3
|
+
OpenGL wrapper library for Ruby
|
4
|
+
|
5
|
+
* Pure Ruby, no C/C++/Java extension included (thanks to ffi gem)
|
6
|
+
* OpenGL 2.1 ~ 4.2
|
7
|
+
* automatic error checking each function call
|
8
|
+
* Ruby-style wrapper (not complete)
|
9
|
+
* Works on OS X 10.8 and Linux (Windows will be supported in the future)
|
10
|
+
* Inspired by PyOpenGL
|
11
|
+
|
12
|
+
## Notice
|
13
|
+
|
14
|
+
Since the project is not complete, many functions are still C-style.
|
15
|
+
But don't worry, the gem makes use of `ffi` gem to import functions, and
|
16
|
+
therefore you can use ffi objects like `FFI::MemoryPointer` to work
|
17
|
+
with C-style OpenGL entrypoints.
|
18
|
+
|
19
|
+
## Ruby-Style OpenGL APIs
|
20
|
+
|
21
|
+
Please take a look at `lib/opengl/wrapper/**/*.rb`
|
22
|
+
to see if entrypoints are wrapped.
|
23
|
+
|
24
|
+
* glGetString() -> String
|
25
|
+
* glGen__Objects__(count) -> Array of Integer
|
26
|
+
* glDelete__Objects__(Array of Integer) -> nil
|
27
|
+
* glGet__Value__fv(\*args, count) -> Array of Float
|
28
|
+
* glGet__Value__iv(\*args, count) -> Array of Integer
|
29
|
+
* glGet__Value__uiv(\*args, count) -> Array of Integer
|
30
|
+
* glShaderSource(shader, Array of String) -> nil
|
31
|
+
* glGetShaderInfoLog(shader) / glGetProgramInfoLog(program) -> String
|
32
|
+
* More will be added
|
33
|
+
|
34
|
+
block-style glMapBuffer / glMapBufferRange:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
38
|
+
ptr.put_array_of_GLfloat(offset_in_bytes, [ 1.0, 2.0, 3.0 ])
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
And helper functions
|
43
|
+
|
44
|
+
* glGetVersion() -> Array of Integer
|
45
|
+
* glGetShadingLanguageVersion() -> Array of Integer
|
46
|
+
* glGetAvailableExtensions() -> Array of String
|
47
|
+
* glIsShaderCompiled?(shader) -> boolean
|
48
|
+
* glIsProgramLinked?(program) -> boolean
|
49
|
+
* More will be added
|
50
|
+
|
51
|
+
## Installation
|
52
|
+
|
53
|
+
Add this line to your application's Gemfile:
|
54
|
+
|
55
|
+
gem 'opengl3', :require => 'opengl'
|
56
|
+
|
57
|
+
And then execute:
|
58
|
+
|
59
|
+
$ bundle
|
60
|
+
|
61
|
+
Or install it yourself as:
|
62
|
+
|
63
|
+
$ gem install opengl3
|
64
|
+
|
65
|
+
## Usage
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
require 'opengl'
|
69
|
+
class App
|
70
|
+
include OpenGL::Constants
|
71
|
+
|
72
|
+
def initialize
|
73
|
+
# ... (window created)
|
74
|
+
self.extend OpenGL::GL.entrypoints
|
75
|
+
# OK, you can use OpenGL now
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
1. Fork it
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new :spec do |t|
|
5
|
+
t.libs << "spec"
|
6
|
+
t.pattern = "spec/**/*_spec.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
Rake::TestTask.new :test do |t|
|
10
|
+
t.libs << "test"
|
11
|
+
t.pattern = "test/**/*_test.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Open an irb session preloaded with this library"
|
15
|
+
task :console do
|
16
|
+
sh "irb -rubygems -I lib -r opengl"
|
17
|
+
end
|
data/examples/math3d.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
class Matrix
|
4
|
+
|
5
|
+
def self.perspective(fovy, aspect, z_near, z_far)
|
6
|
+
f, z_diff = Math.tan(Math::PI * 0.5 - fovy * 0.5), (z_near-z_far)
|
7
|
+
return self[
|
8
|
+
[ f/aspect, 0.0, 0.0, 0.0 ],
|
9
|
+
[ 0.0, f, 0.0, 0.0 ],
|
10
|
+
[ 0.0, 0.0, (z_far+z_near)/z_diff, (2.0*z_far*z_near)/z_diff ],
|
11
|
+
[ 0.0, 0.0, -1.0, 0.0 ]
|
12
|
+
]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.translate(x, y, z)
|
16
|
+
x, y, z = Float(x), Float(y), Float(z)
|
17
|
+
return self[
|
18
|
+
[ 1.0, 0.0, 0.0, x ],
|
19
|
+
[ 0.0, 1.0, 0.0, y ],
|
20
|
+
[ 0.0, 0.0, 1.0, z ],
|
21
|
+
[ 0.0, 0.0, 0.0, 1.0 ]
|
22
|
+
]
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.scale(x, y, z)
|
26
|
+
x, y, z = Float(x), Float(y), Float(z)
|
27
|
+
return self[
|
28
|
+
[ x, 0.0, 0.0, 0.0 ],
|
29
|
+
[ 0.0, y, 0.0, 0.0 ],
|
30
|
+
[ 0.0, 0.0, z, 0.0 ],
|
31
|
+
[ 0.0, 0.0, 0.0, 1.0 ]
|
32
|
+
]
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.rotate_x(angle)
|
36
|
+
c, s = Math.cos(angle), Math.sin(angle)
|
37
|
+
return self[
|
38
|
+
[ 1.0, 0.0, 0.0, 0.0 ],
|
39
|
+
[ 0.0, c, -s, 0.0 ],
|
40
|
+
[ 0.0, s, c, 0.0 ],
|
41
|
+
[ 0.0, 0.0, 0.0, 1.0 ]
|
42
|
+
]
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.rotate_y(angle)
|
46
|
+
c, s = Math.cos(angle), Math.sin(angle)
|
47
|
+
return self[
|
48
|
+
[ c, 0.0, s, 0.0 ],
|
49
|
+
[ 0.0, 1.0, 0.0, 0.0 ],
|
50
|
+
[ -s, 0.0, c, 0.0 ],
|
51
|
+
[ 0.0, 0.0, 0.0, 1.0 ]
|
52
|
+
]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.rotate_z(angle)
|
56
|
+
c, s = Math.cos(angle), Math.sin(angle)
|
57
|
+
return self[
|
58
|
+
[ c, -s, 0.0, 0.0 ],
|
59
|
+
[ s, c, 0.0, 0.0 ],
|
60
|
+
[ 0.0, 0.0, 1.0, 0.0 ],
|
61
|
+
[ 0.0, 0.0, 0.0, 1.0 ]
|
62
|
+
]
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'opengl'
|
4
|
+
require 'glut'
|
5
|
+
|
6
|
+
class TestApp
|
7
|
+
include GLUT
|
8
|
+
include OpenGL::Constants
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
caps = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE
|
12
|
+
glutInitDisplayMode(caps)
|
13
|
+
glutInitWindowPosition(300, 200)
|
14
|
+
glutInitWindowSize(512, 512)
|
15
|
+
glutCreateWindow('test')
|
16
|
+
|
17
|
+
glutDisplayFunc(@draw_method ||= method(:draw)) # prevent from being GCed
|
18
|
+
end
|
19
|
+
|
20
|
+
def start
|
21
|
+
self.extend OpenGL::GL.entrypoints
|
22
|
+
puts glGetString(GL_VERSION)
|
23
|
+
puts glGetVersion().inspect
|
24
|
+
puts glGetShadingLanguageVersion().inspect
|
25
|
+
puts glGetAvailableExtensions().inspect
|
26
|
+
|
27
|
+
@quad = begin
|
28
|
+
buffer = glGenBuffers(1)[0]
|
29
|
+
glBindBuffer(GL_ARRAY_BUFFER, buffer)
|
30
|
+
glBufferData(GL_ARRAY_BUFFER, 3 * 4 * 4, nil, GL_STATIC_DRAW)
|
31
|
+
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
32
|
+
ptr.put_array_of_GLfloat( 0, [-0.5, 0.5, 0.0])
|
33
|
+
ptr.put_array_of_GLfloat(12, [-0.5,-0.5, 0.0])
|
34
|
+
ptr.put_array_of_GLfloat(24, [ 0.5, 0.5, 0.0])
|
35
|
+
ptr.put_array_of_GLfloat(36, [ 0.5,-0.5, 0.0])
|
36
|
+
end
|
37
|
+
|
38
|
+
buffer
|
39
|
+
end
|
40
|
+
|
41
|
+
@program = begin
|
42
|
+
vertex_shader = glCreateShader(:vertex)
|
43
|
+
glShaderSource(vertex_shader, VS_CODE)
|
44
|
+
glCompileShader(vertex_shader)
|
45
|
+
unless glIsShaderCompiled?(vertex_shader)
|
46
|
+
msg = glGetShaderInfoLog(vertex_shader)
|
47
|
+
raise RuntimeError, "vertex_shader:\n#{msg}"
|
48
|
+
end
|
49
|
+
|
50
|
+
fragment_shader = glCreateShader(:fragment)
|
51
|
+
glShaderSource(fragment_shader, FS_CODE)
|
52
|
+
glCompileShader(fragment_shader)
|
53
|
+
unless glIsShaderCompiled?(fragment_shader)
|
54
|
+
msg = glGetShaderInfoLog(fragment_shader)
|
55
|
+
raise RuntimeError, "fragment_shader:\n#{msg}"
|
56
|
+
end
|
57
|
+
|
58
|
+
program = glCreateProgram()
|
59
|
+
glAttachShader(program, vertex_shader)
|
60
|
+
glAttachShader(program, fragment_shader)
|
61
|
+
glBindAttribLocation(program, 0, "in_Position")
|
62
|
+
glLinkProgram(program)
|
63
|
+
unless glIsProgramLinked?(program)
|
64
|
+
msg = glGetProgramInfoLog(program)
|
65
|
+
raise RuntimeError, "program:\n#{msg}"
|
66
|
+
end
|
67
|
+
glDeleteShader(vertex_shader)
|
68
|
+
glDeleteShader(fragment_shader)
|
69
|
+
program
|
70
|
+
end
|
71
|
+
|
72
|
+
glutMainLoop()
|
73
|
+
end
|
74
|
+
|
75
|
+
def draw
|
76
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
77
|
+
|
78
|
+
glUseProgram(@program)
|
79
|
+
glBindBuffer(GL_ARRAY_BUFFER, @quad)
|
80
|
+
glEnableVertexAttribArray(0)
|
81
|
+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
82
|
+
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
|
83
|
+
|
84
|
+
glutSwapBuffers()
|
85
|
+
end
|
86
|
+
|
87
|
+
VS_CODE =<<-GLSL
|
88
|
+
attribute vec4 in_Position;
|
89
|
+
varying vec2 vs_TexCoord;
|
90
|
+
void main()
|
91
|
+
{
|
92
|
+
gl_Position = in_Position;
|
93
|
+
vs_TexCoord = in_Position.xy + 0.5;
|
94
|
+
}
|
95
|
+
GLSL
|
96
|
+
|
97
|
+
FS_CODE =<<-GLSL
|
98
|
+
varying vec2 vs_TexCoord;
|
99
|
+
void main()
|
100
|
+
{
|
101
|
+
gl_FragColor = vec4(vs_TexCoord, 0.0, 1.0);
|
102
|
+
}
|
103
|
+
GLSL
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
TestApp.new.start
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'opengl'
|
4
|
+
require 'glut'
|
5
|
+
|
6
|
+
class TestApp
|
7
|
+
include GLUT
|
8
|
+
include OpenGL::Constants
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
caps = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE
|
12
|
+
if FFI::Platform.mac?
|
13
|
+
caps |= GLUT_3_2_CORE_PROFILE
|
14
|
+
else
|
15
|
+
glutInitContextVersion(3, 2)
|
16
|
+
glutInitContextProfile(:core)
|
17
|
+
end
|
18
|
+
glutInitDisplayMode(caps)
|
19
|
+
glutInitWindowPosition(300, 200)
|
20
|
+
glutInitWindowSize(512, 512)
|
21
|
+
glutCreateWindow('test')
|
22
|
+
|
23
|
+
glutDisplayFunc(@draw_method ||= method(:draw)) # prevent from being GCed
|
24
|
+
end
|
25
|
+
|
26
|
+
def start
|
27
|
+
self.extend OpenGL::GL.entrypoints
|
28
|
+
puts glGetString(GL_VERSION)
|
29
|
+
puts glGetBooleanv(GL_DEPTH_TEST).inspect
|
30
|
+
puts glGetDoublev(GL_COLOR_CLEAR_VALUE, 4).inspect
|
31
|
+
puts glGetFloatv(GL_LINE_WIDTH).inspect
|
32
|
+
puts glGetIntegerv(GL_VIEWPORT, 4).inspect
|
33
|
+
puts glGetVersion().inspect
|
34
|
+
puts glGetShadingLanguageVersion().inspect
|
35
|
+
puts glGetAvailableExtensions().inspect
|
36
|
+
|
37
|
+
@quad = begin
|
38
|
+
buffer = glGenBuffers(1)[0]
|
39
|
+
glBindBuffer(GL_ARRAY_BUFFER, buffer)
|
40
|
+
glBufferData(GL_ARRAY_BUFFER, 3 * 4 * 4, nil, GL_STATIC_DRAW)
|
41
|
+
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
42
|
+
ptr.put_array_of_GLfloat( 0, [-0.5, 0.5, 0.0])
|
43
|
+
ptr.put_array_of_GLfloat(12, [-0.5,-0.5, 0.0])
|
44
|
+
ptr.put_array_of_GLfloat(24, [ 0.5, 0.5, 0.0])
|
45
|
+
ptr.put_array_of_GLfloat(36, [ 0.5,-0.5, 0.0])
|
46
|
+
end
|
47
|
+
|
48
|
+
array = glGenVertexArrays(1)[0]
|
49
|
+
glBindVertexArray(array)
|
50
|
+
glBindBuffer(GL_ARRAY_BUFFER, buffer)
|
51
|
+
glEnableVertexAttribArray(0)
|
52
|
+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
53
|
+
|
54
|
+
glBindVertexArray(0)
|
55
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
56
|
+
|
57
|
+
array
|
58
|
+
end
|
59
|
+
|
60
|
+
@program = begin
|
61
|
+
vertex_shader = glCreateShader(:vertex)
|
62
|
+
glShaderSource(vertex_shader, VS_CODE)
|
63
|
+
glCompileShader(vertex_shader)
|
64
|
+
unless glIsShaderCompiled?(vertex_shader)
|
65
|
+
msg = glGetShaderInfoLog(vertex_shader)
|
66
|
+
raise RuntimeError, "vertex_shader:\n#{msg}"
|
67
|
+
end
|
68
|
+
|
69
|
+
fragment_shader = glCreateShader(:fragment)
|
70
|
+
glShaderSource(fragment_shader, FS_CODE)
|
71
|
+
glCompileShader(fragment_shader)
|
72
|
+
unless glIsShaderCompiled?(fragment_shader)
|
73
|
+
msg = glGetShaderInfoLog(fragment_shader)
|
74
|
+
raise RuntimeError, "fragment_shader:\n#{msg}"
|
75
|
+
end
|
76
|
+
|
77
|
+
program = glCreateProgram()
|
78
|
+
glAttachShader(program, vertex_shader)
|
79
|
+
glAttachShader(program, fragment_shader)
|
80
|
+
glBindAttribLocation(program, 0, "in_Position")
|
81
|
+
glBindFragDataLocation(program, 0, "fs_FragColor")
|
82
|
+
glLinkProgram(program)
|
83
|
+
unless glIsProgramLinked?(program)
|
84
|
+
msg = glGetProgramInfoLog(program)
|
85
|
+
raise RuntimeError, "program:\n#{msg}"
|
86
|
+
end
|
87
|
+
glDeleteShader(vertex_shader)
|
88
|
+
glDeleteShader(fragment_shader)
|
89
|
+
program
|
90
|
+
end
|
91
|
+
|
92
|
+
glutMainLoop()
|
93
|
+
end
|
94
|
+
|
95
|
+
def draw
|
96
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
97
|
+
|
98
|
+
glUseProgram(@program)
|
99
|
+
glBindVertexArray(@quad)
|
100
|
+
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)
|
101
|
+
|
102
|
+
glutSwapBuffers()
|
103
|
+
end
|
104
|
+
|
105
|
+
VS_CODE =<<-GLSL
|
106
|
+
#version 150 core
|
107
|
+
in vec4 in_Position;
|
108
|
+
out vec2 vs_TexCoord;
|
109
|
+
void main()
|
110
|
+
{
|
111
|
+
gl_Position = in_Position;
|
112
|
+
vs_TexCoord = in_Position.xy + 0.5;
|
113
|
+
}
|
114
|
+
GLSL
|
115
|
+
|
116
|
+
FS_CODE =<<-GLSL
|
117
|
+
#version 150 core
|
118
|
+
in vec2 vs_TexCoord;
|
119
|
+
out vec4 fs_FragColor;
|
120
|
+
void main()
|
121
|
+
{
|
122
|
+
fs_FragColor = vec4(vs_TexCoord, 0.0, 1.0);
|
123
|
+
}
|
124
|
+
GLSL
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
TestApp.new.start
|