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
@@ -0,0 +1,203 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'opengl'
|
4
|
+
require 'glut'
|
5
|
+
require_relative 'math3d'
|
6
|
+
|
7
|
+
class TestApp
|
8
|
+
include GLUT
|
9
|
+
include OpenGL::Constants
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
caps = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE
|
13
|
+
if FFI::Platform.mac?
|
14
|
+
caps |= GLUT_3_2_CORE_PROFILE
|
15
|
+
else
|
16
|
+
glutInitContextVersion(3, 2)
|
17
|
+
glutInitContextProfile(:core)
|
18
|
+
end
|
19
|
+
glutInitDisplayMode(caps)
|
20
|
+
glutInitWindowPosition(300, 200)
|
21
|
+
glutInitWindowSize(512, 512)
|
22
|
+
glutCreateWindow('cube')
|
23
|
+
|
24
|
+
glutDisplayFunc(@draw_method ||= method(:draw))
|
25
|
+
glutReshapeFunc(@resize_method ||= method(:resize))
|
26
|
+
glutMouseFunc(@mouse_press_method ||= method(:mouse_press))
|
27
|
+
glutMotionFunc(@mouse_track_method ||= method(:mouse_track))
|
28
|
+
end
|
29
|
+
|
30
|
+
def start
|
31
|
+
self.extend OpenGL::GL.entrypoints
|
32
|
+
puts glGetString(GL_VERSION)
|
33
|
+
|
34
|
+
@cube = begin
|
35
|
+
vertex_buffer, index_buffer = glGenBuffers(2)
|
36
|
+
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
|
37
|
+
glBufferData(GL_ARRAY_BUFFER, 3 * 4 * 8, nil, GL_STATIC_DRAW)
|
38
|
+
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
39
|
+
ptr.put_array_of_GLfloat( 0, [-1.0, 1.0, 1.0]) # 0
|
40
|
+
ptr.put_array_of_GLfloat(12, [-1.0,-1.0, 1.0]) # 1
|
41
|
+
ptr.put_array_of_GLfloat(24, [ 1.0, 1.0, 1.0]) # 2
|
42
|
+
ptr.put_array_of_GLfloat(36, [ 1.0,-1.0, 1.0]) # 3
|
43
|
+
ptr.put_array_of_GLfloat(48, [ 1.0, 1.0,-1.0]) # 4
|
44
|
+
ptr.put_array_of_GLfloat(60, [ 1.0,-1.0,-1.0]) # 5
|
45
|
+
ptr.put_array_of_GLfloat(72, [-1.0, 1.0,-1.0]) # 6
|
46
|
+
ptr.put_array_of_GLfloat(84, [-1.0,-1.0,-1.0]) # 7
|
47
|
+
end
|
48
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer)
|
49
|
+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * 4 * 6, nil, GL_STATIC_DRAW)
|
50
|
+
glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
51
|
+
ptr.write_array_of_GLushort [0,1,2,3,4,5,6,7,0,1,0xffff]
|
52
|
+
ptr.put_array_of_GLushort(22, [6,0,4,2,0xffff,1,7,3,5])
|
53
|
+
#ptr.put_array_of_GLushort( 0, [0, 1, 2, 3]) # front
|
54
|
+
#ptr.put_array_of_GLushort( 8, [2, 3, 4, 5]) # right
|
55
|
+
#ptr.put_array_of_GLushort(16, [4, 5, 6, 7]) # back
|
56
|
+
#ptr.put_array_of_GLushort(24, [6, 7, 0, 1]) # left
|
57
|
+
#ptr.put_array_of_GLushort(32, [6, 0, 4, 2]) # top
|
58
|
+
#ptr.put_array_of_GLushort(40, [1, 7, 3, 5]) # bottom
|
59
|
+
end
|
60
|
+
|
61
|
+
array = glGenVertexArrays(1)[0]
|
62
|
+
glBindVertexArray(array)
|
63
|
+
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
|
64
|
+
glEnableVertexAttribArray(0)
|
65
|
+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
66
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer)
|
67
|
+
|
68
|
+
glBindVertexArray(0)
|
69
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
70
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
|
71
|
+
|
72
|
+
array
|
73
|
+
end
|
74
|
+
|
75
|
+
@program = begin
|
76
|
+
vertex_shader = glCreateShader(:vertex)
|
77
|
+
glShaderSource(vertex_shader, VS_CODE)
|
78
|
+
glCompileShader(vertex_shader)
|
79
|
+
unless glIsShaderCompiled?(vertex_shader)
|
80
|
+
msg = glGetShaderInfoLog(vertex_shader)
|
81
|
+
raise RuntimeError, "vertex_shader:\n#{msg}"
|
82
|
+
end
|
83
|
+
|
84
|
+
fragment_shader = glCreateShader(:fragment)
|
85
|
+
glShaderSource(fragment_shader, FS_CODE)
|
86
|
+
glCompileShader(fragment_shader)
|
87
|
+
unless glIsShaderCompiled?(fragment_shader)
|
88
|
+
msg = glGetShaderInfoLog(fragment_shader)
|
89
|
+
raise RuntimeError, "fragment_shader:\n#{msg}"
|
90
|
+
end
|
91
|
+
|
92
|
+
program = glCreateProgram()
|
93
|
+
glAttachShader(program, vertex_shader)
|
94
|
+
glAttachShader(program, fragment_shader)
|
95
|
+
glBindAttribLocation(program, 0, "in_Position")
|
96
|
+
glBindFragDataLocation(program, 0, "fs_FragColor")
|
97
|
+
glLinkProgram(program)
|
98
|
+
unless glIsProgramLinked?(program)
|
99
|
+
msg = glGetProgramInfoLog(program)
|
100
|
+
raise RuntimeError, "program:\n#{msg}"
|
101
|
+
end
|
102
|
+
glDeleteShader(vertex_shader)
|
103
|
+
glDeleteShader(fragment_shader)
|
104
|
+
program
|
105
|
+
end
|
106
|
+
|
107
|
+
@view = Matrix.translate(0, 0, -5)
|
108
|
+
@world = Matrix.identity(4)
|
109
|
+
|
110
|
+
glutMainLoop()
|
111
|
+
end
|
112
|
+
|
113
|
+
def draw
|
114
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
115
|
+
|
116
|
+
glEnable(GL_CULL_FACE)
|
117
|
+
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
118
|
+
glEnable(GL_DEPTH_TEST)
|
119
|
+
|
120
|
+
glUseProgram(@program)
|
121
|
+
|
122
|
+
if (loc = glGetUniformLocation(@program, "u_Projection")) != -1
|
123
|
+
#glUniformMatrix4fv(loc, [ @projection ], true)
|
124
|
+
OpenGL::Type.GLfloat(16) do |ptr|
|
125
|
+
ptr.write_array_of_GLfloat(@projection.to_a.flatten)
|
126
|
+
glUniformMatrix4fv(loc, 1, GL_TRUE, ptr)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
if (loc = glGetUniformLocation(@program, "u_ModelView")) != -1
|
131
|
+
OpenGL::Type.GLfloat(16) do |ptr|
|
132
|
+
modelview = @view * @world
|
133
|
+
ptr.write_array_of_GLfloat(modelview.to_a.flatten)
|
134
|
+
glUniformMatrix4fv(loc, 1, GL_TRUE, ptr)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
glBindVertexArray(@cube)
|
139
|
+
glEnable(GL_PRIMITIVE_RESTART)
|
140
|
+
glPrimitiveRestartIndex(0xffff)
|
141
|
+
glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_SHORT, nil)
|
142
|
+
|
143
|
+
glutSwapBuffers()
|
144
|
+
end
|
145
|
+
|
146
|
+
def resize(width, height)
|
147
|
+
glViewport(0, 0, width, height)
|
148
|
+
|
149
|
+
fovy = 45.0 / 180.0 * Math::PI
|
150
|
+
aspect = Float(width) / Float(height)
|
151
|
+
z_near, z_far = 1.0, 100.0
|
152
|
+
@projection = Matrix.perspective(fovy, aspect, z_near, z_far)
|
153
|
+
end
|
154
|
+
|
155
|
+
def mouse_press(button, button_state, x, y)
|
156
|
+
if button == :LEFT
|
157
|
+
case button_state
|
158
|
+
when :DOWN
|
159
|
+
@mouse_tracking_pos = [x, y]
|
160
|
+
when :UP
|
161
|
+
remove_instance_variable(:@mouse_tracking_pos)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def mouse_track(x, y)
|
167
|
+
if defined? @mouse_tracking_pos
|
168
|
+
delta_x = (x - @mouse_tracking_pos[0]) * 0.01
|
169
|
+
delta_y = (y - @mouse_tracking_pos[1]) * 0.01
|
170
|
+
|
171
|
+
@world = Matrix.rotate_x(delta_y) * Matrix.rotate_y(delta_x) * @world
|
172
|
+
|
173
|
+
@mouse_tracking_pos = [x, y]
|
174
|
+
glutPostRedisplay()
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
VS_CODE =<<-GLSL
|
179
|
+
#version 150 core
|
180
|
+
uniform mat4 u_Projection;
|
181
|
+
uniform mat4 u_ModelView;
|
182
|
+
in vec4 in_Position;
|
183
|
+
out vec3 vs_Vertex;
|
184
|
+
void main()
|
185
|
+
{
|
186
|
+
gl_Position = u_Projection * u_ModelView * in_Position;
|
187
|
+
vs_Vertex = in_Position.xyz;
|
188
|
+
}
|
189
|
+
GLSL
|
190
|
+
|
191
|
+
FS_CODE =<<-GLSL
|
192
|
+
#version 150 core
|
193
|
+
in vec3 vs_Vertex;
|
194
|
+
out vec4 fs_FragColor;
|
195
|
+
void main()
|
196
|
+
{
|
197
|
+
fs_FragColor = vec4(vs_Vertex * 0.5 + 0.5, 1.0);
|
198
|
+
}
|
199
|
+
GLSL
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
TestApp.new.start
|
@@ -0,0 +1,283 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'opengl'
|
4
|
+
require 'glut'
|
5
|
+
require_relative 'math3d'
|
6
|
+
|
7
|
+
class TestApp
|
8
|
+
include GLUT
|
9
|
+
include OpenGL::Constants
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
caps = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE
|
13
|
+
if FFI::Platform.mac?
|
14
|
+
caps |= GLUT_4_0_CORE_PROFILE
|
15
|
+
else
|
16
|
+
glutInitContextVersion(4, 0)
|
17
|
+
glutInitContextProfile(:core)
|
18
|
+
end
|
19
|
+
glutInitDisplayMode(caps)
|
20
|
+
glutInitWindowPosition(300, 200)
|
21
|
+
glutInitWindowSize(512, 512)
|
22
|
+
glutCreateWindow('tessellation')
|
23
|
+
|
24
|
+
glutDisplayFunc(@draw_method ||= method(:draw))
|
25
|
+
glutReshapeFunc(@resize_method ||= method(:resize))
|
26
|
+
glutMouseFunc(@mouse_press_method ||= method(:mouse_press))
|
27
|
+
glutMotionFunc(@mouse_track_method ||= method(:mouse_track))
|
28
|
+
end
|
29
|
+
|
30
|
+
def start
|
31
|
+
self.extend OpenGL::GL.entrypoints
|
32
|
+
puts glGetString(GL_VERSION)
|
33
|
+
|
34
|
+
@quad = begin
|
35
|
+
vertex_buffer, index_buffer = glGenBuffers(2)
|
36
|
+
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
|
37
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer)
|
38
|
+
n, m = 4, 4
|
39
|
+
@quad_count = 4*m*n
|
40
|
+
glBufferData(GL_ARRAY_BUFFER, 4*2*(n+1)*(m+1), nil, GL_STATIC_DRAW)
|
41
|
+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*@quad_count, nil, GL_STATIC_DRAW)
|
42
|
+
glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
43
|
+
inv_n, inv_m, off = 1.0/n, 1.0/m, 0
|
44
|
+
0.upto(m) do |i|
|
45
|
+
y = 0.5 - i * inv_m
|
46
|
+
0.upto(n) do |j|
|
47
|
+
x = j * inv_n - 0.5
|
48
|
+
ptr.put_array_of_GLfloat(off, [x, y])
|
49
|
+
off += 4*2
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY) do |ptr|
|
54
|
+
off = 0
|
55
|
+
0.upto(m-1) do |i|
|
56
|
+
0.upto(n-1) do |j|
|
57
|
+
v0 = i*(n+1)+j
|
58
|
+
ptr.put_array_of_GLushort(off, [v0, v0+n+1, v0+n+2, v0+1])
|
59
|
+
off += 2*4
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
array = glGenVertexArrays(1)[0]
|
65
|
+
glBindVertexArray(array)
|
66
|
+
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer)
|
67
|
+
glEnableVertexAttribArray(0)
|
68
|
+
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nil)
|
69
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer)
|
70
|
+
|
71
|
+
glBindVertexArray(0)
|
72
|
+
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
73
|
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
|
74
|
+
|
75
|
+
array
|
76
|
+
end
|
77
|
+
|
78
|
+
@program = begin
|
79
|
+
vertex_shader = glCreateShader(:vertex)
|
80
|
+
glShaderSource(vertex_shader, VS_CODE)
|
81
|
+
glCompileShader(vertex_shader)
|
82
|
+
unless glIsShaderCompiled?(vertex_shader)
|
83
|
+
msg = glGetShaderInfoLog(vertex_shader)
|
84
|
+
raise RuntimeError, "vertex_shader:\n#{msg}"
|
85
|
+
end
|
86
|
+
|
87
|
+
tess_control_shader = glCreateShader(:tess_control)
|
88
|
+
glShaderSource(tess_control_shader, TC_CODE)
|
89
|
+
glCompileShader(tess_control_shader)
|
90
|
+
unless glIsShaderCompiled?(tess_control_shader)
|
91
|
+
msg = glGetShaderInfoLog(tess_control_shader)
|
92
|
+
raise RuntimeError, "tess_control_shader:\n#{msg}"
|
93
|
+
end
|
94
|
+
|
95
|
+
tess_eval_shader = glCreateShader(:tess_eval)
|
96
|
+
glShaderSource(tess_eval_shader, TE_CODE)
|
97
|
+
glCompileShader(tess_eval_shader)
|
98
|
+
unless glIsShaderCompiled?(tess_eval_shader)
|
99
|
+
msg = glGetShaderInfoLog(tess_eval_shader)
|
100
|
+
raise RuntimeError, "tess_eval_shader:\n#{msg}"
|
101
|
+
end
|
102
|
+
|
103
|
+
geometry_shader = glCreateShader(:geometry)
|
104
|
+
glShaderSource(geometry_shader, GS_CODE)
|
105
|
+
glCompileShader(geometry_shader)
|
106
|
+
unless glIsShaderCompiled?(geometry_shader)
|
107
|
+
msg = glGetShaderInfoLog(geometry_shader)
|
108
|
+
raise RuntimeError, "geometry_shader:\n#{msg}"
|
109
|
+
end
|
110
|
+
|
111
|
+
fragment_shader = glCreateShader(:fragment)
|
112
|
+
glShaderSource(fragment_shader, FS_CODE)
|
113
|
+
glCompileShader(fragment_shader)
|
114
|
+
unless glIsShaderCompiled?(fragment_shader)
|
115
|
+
msg = glGetShaderInfoLog(fragment_shader)
|
116
|
+
raise RuntimeError, "fragment_shader:\n#{msg}"
|
117
|
+
end
|
118
|
+
|
119
|
+
program = glCreateProgram()
|
120
|
+
glAttachShader(program, vertex_shader)
|
121
|
+
glAttachShader(program, tess_control_shader)
|
122
|
+
glAttachShader(program, tess_eval_shader)
|
123
|
+
glAttachShader(program, geometry_shader)
|
124
|
+
glAttachShader(program, fragment_shader)
|
125
|
+
glLinkProgram(program)
|
126
|
+
unless glIsProgramLinked?(program)
|
127
|
+
msg = glGetProgramInfoLog(program)
|
128
|
+
raise RuntimeError, "program:\n#{msg}"
|
129
|
+
end
|
130
|
+
glDeleteShader(vertex_shader)
|
131
|
+
glDeleteShader(tess_control_shader)
|
132
|
+
glDeleteShader(tess_eval_shader)
|
133
|
+
glDeleteShader(geometry_shader)
|
134
|
+
glDeleteShader(fragment_shader)
|
135
|
+
program
|
136
|
+
end
|
137
|
+
|
138
|
+
@view = Matrix.translate(0, 0, -5)
|
139
|
+
@world = Matrix.scale(5, 5, 5)
|
140
|
+
|
141
|
+
glutMainLoop()
|
142
|
+
end
|
143
|
+
|
144
|
+
def draw
|
145
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
146
|
+
|
147
|
+
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
|
148
|
+
|
149
|
+
glUseProgram(@program)
|
150
|
+
if (loc = glGetUniformLocation(@program, "u_Projection")) != -1
|
151
|
+
OpenGL::Type.GLfloat(16) do |ptr|
|
152
|
+
ptr.write_array_of_GLfloat(@projection.to_a.flatten)
|
153
|
+
glUniformMatrix4fv(loc, 1, GL_TRUE, ptr)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
if (loc = glGetUniformLocation(@program, "u_ModelView")) != -1
|
157
|
+
OpenGL::Type.GLfloat(16) do |ptr|
|
158
|
+
modelview = @view * @world
|
159
|
+
ptr.write_array_of_GLfloat(modelview.to_a.flatten)
|
160
|
+
glUniformMatrix4fv(loc, 1, GL_TRUE, ptr)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
glPatchParameteri(GL_PATCH_VERTICES, 4)
|
164
|
+
|
165
|
+
glBindVertexArray(@quad)
|
166
|
+
glDrawElements(GL_PATCHES, @quad_count, GL_UNSIGNED_SHORT, nil)
|
167
|
+
|
168
|
+
glutSwapBuffers()
|
169
|
+
end
|
170
|
+
|
171
|
+
def resize(width, height)
|
172
|
+
glViewport(0, 0, width, height)
|
173
|
+
|
174
|
+
fovy = 45.0 / 180.0 * Math::PI
|
175
|
+
aspect = Float(width) / Float(height)
|
176
|
+
z_near, z_far = 1.0, 100.0
|
177
|
+
@projection = Matrix.perspective(fovy, aspect, z_near, z_far)
|
178
|
+
end
|
179
|
+
|
180
|
+
def mouse_press(button, button_state, x, y)
|
181
|
+
if button == :LEFT
|
182
|
+
case button_state
|
183
|
+
when :DOWN
|
184
|
+
@mouse_tracking_pos = [x, y]
|
185
|
+
when :UP
|
186
|
+
remove_instance_variable(:@mouse_tracking_pos)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def mouse_track(x, y)
|
192
|
+
if defined? @mouse_tracking_pos
|
193
|
+
delta_x = (x - @mouse_tracking_pos[0]) * 0.01
|
194
|
+
delta_y = (y - @mouse_tracking_pos[1]) * 0.01
|
195
|
+
|
196
|
+
#@world = Matrix.rotate_x(delta_y) * Matrix.rotate_y(delta_x) * @world
|
197
|
+
@world = Matrix.translate(0,0,delta_y) * @world
|
198
|
+
|
199
|
+
@mouse_tracking_pos = [x, y]
|
200
|
+
glutPostRedisplay()
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
VS_CODE =<<-GLSL
|
205
|
+
#version 400 core
|
206
|
+
uniform mat4 u_Projection;
|
207
|
+
uniform mat4 u_ModelView;
|
208
|
+
layout(location = 0) in vec4 in_Position;
|
209
|
+
//out vec3 vs_Position;
|
210
|
+
//out vec2 vs_TexCoord;
|
211
|
+
void main()
|
212
|
+
{
|
213
|
+
gl_Position = u_Projection * u_ModelView * in_Position;
|
214
|
+
//vs_Position = in_Position.xyz;
|
215
|
+
//vs_TexCoord = in_Position.xy + 0.5;
|
216
|
+
}
|
217
|
+
GLSL
|
218
|
+
|
219
|
+
TC_CODE =<<-GLSL
|
220
|
+
#version 400 core
|
221
|
+
layout(vertices=4) out;
|
222
|
+
#define ID gl_InvocationID
|
223
|
+
void main()
|
224
|
+
{
|
225
|
+
gl_out[ID].gl_Position = gl_in[ID].gl_Position;
|
226
|
+
if (ID == 0) {
|
227
|
+
float sum = 0.0;
|
228
|
+
for (int i = 0; i < 4; ++i) {
|
229
|
+
vec4 v = gl_in[i].gl_Position;
|
230
|
+
sum += v.z / v.w;
|
231
|
+
}
|
232
|
+
sum *= 0.25;
|
233
|
+
float k = 20 * clamp(1.0 - sum, 0.0, 2.0);
|
234
|
+
gl_TessLevelInner[0] = k;
|
235
|
+
gl_TessLevelInner[1] = k;
|
236
|
+
gl_TessLevelOuter[0] = k;
|
237
|
+
gl_TessLevelOuter[1] = k;
|
238
|
+
gl_TessLevelOuter[2] = k;
|
239
|
+
gl_TessLevelOuter[3] = k;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
GLSL
|
243
|
+
|
244
|
+
TE_CODE =<<-GLSL
|
245
|
+
#version 400 core
|
246
|
+
layout(quads, fractional_even_spacing, ccw) in;
|
247
|
+
void main()
|
248
|
+
{
|
249
|
+
float u = gl_TessCoord.x, v = gl_TessCoord.y;
|
250
|
+
vec4 upper = mix(gl_in[0].gl_Position, gl_in[3].gl_Position, v);
|
251
|
+
vec4 lower = mix(gl_in[1].gl_Position, gl_in[2].gl_Position, v);
|
252
|
+
gl_Position = mix(upper, lower, u);
|
253
|
+
}
|
254
|
+
GLSL
|
255
|
+
|
256
|
+
GS_CODE =<<-GLSL
|
257
|
+
#version 400 core
|
258
|
+
layout(triangles) in;
|
259
|
+
layout(triangle_strip, max_vertices=3) out;
|
260
|
+
void main()
|
261
|
+
{
|
262
|
+
for (int i = 0; i < 3; ++i) {
|
263
|
+
gl_Position = gl_in[i].gl_Position;
|
264
|
+
EmitVertex();
|
265
|
+
}
|
266
|
+
EndPrimitive();
|
267
|
+
}
|
268
|
+
GLSL
|
269
|
+
|
270
|
+
FS_CODE =<<-GLSL
|
271
|
+
#version 400 core
|
272
|
+
//in vec2 vs_TexCoord;
|
273
|
+
layout(location = 0) out vec4 fs_FragColor;
|
274
|
+
void main()
|
275
|
+
{
|
276
|
+
//fs_FragColor = vec4(vs_TexCoord, 0.0, 1.0);
|
277
|
+
fs_FragColor = vec4(1.0);
|
278
|
+
}
|
279
|
+
GLSL
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
TestApp.new.start
|