rglviewer 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/simpleviewer.rb +5 -2
- data/lib/rglviewer/camera.rb +90 -0
- data/lib/rglviewer/frame.rb +48 -0
- data/lib/rglviewer/gl2dviewer.rb +91 -0
- data/lib/rglviewer/glviewer.rb +114 -0
- data/lib/rglviewer/version.rb +1 -1
- data/rglviewer.gemspec +2 -0
- metadata +21 -6
data/examples/simpleviewer.rb
CHANGED
@@ -6,6 +6,8 @@ require 'opengl'
|
|
6
6
|
|
7
7
|
include GLViewer,Gl
|
8
8
|
|
9
|
+
@start_angle = 0.0
|
10
|
+
|
9
11
|
init = Proc.new do |v|
|
10
12
|
v.display_fps = true
|
11
13
|
v.display_grid = true
|
@@ -18,10 +20,10 @@ paint = Proc.new do |v|
|
|
18
20
|
up = Math.sqrt(1.0 - nor * nor )
|
19
21
|
|
20
22
|
glBegin(GL_QUAD_STRIP)
|
21
|
-
|
23
|
+
@start_angle += Math::PI / 500
|
22
24
|
(1..nb_steps).each do |i|
|
23
25
|
ratio = i.to_f/nb_steps;
|
24
|
-
angle = 21.0 * ratio;
|
26
|
+
angle = 21.0 * ratio + @start_angle;
|
25
27
|
c = Math.cos(angle)
|
26
28
|
s = Math.sin(angle)
|
27
29
|
r_1 = 1.0 - 0.8 * ratio
|
@@ -37,6 +39,7 @@ paint = Proc.new do |v|
|
|
37
39
|
|
38
40
|
end
|
39
41
|
|
42
|
+
|
40
43
|
viewer = GLViewer::GLViewer.new :render => paint , :init => init
|
41
44
|
|
42
45
|
viewer.main_loop
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'opengl'
|
2
|
+
require 'gslng'
|
3
|
+
require 'rglviewer/frame'
|
4
|
+
|
5
|
+
|
6
|
+
include Gl,Glu,GSLng
|
7
|
+
|
8
|
+
module GLViewer
|
9
|
+
class Camera
|
10
|
+
|
11
|
+
|
12
|
+
attr_reader :width, :height
|
13
|
+
attr_accessor :scene_radius, :scene_center, :type, :field_of_view
|
14
|
+
|
15
|
+
def initialize args = {}
|
16
|
+
@width = args[:width] || 640
|
17
|
+
@height = args[:height] || 480
|
18
|
+
@scene_radius = args[:scene_radius] || 1.0
|
19
|
+
@scene_center = args[:scene_center] || [0.0,0.0,0.0]
|
20
|
+
@type = args[:type] || :perspective
|
21
|
+
@field_of_view = args[:field_of_view] || Math::PI / 4.0
|
22
|
+
@z_clipping_coefficient = args[:z_clipping_coefficient] || Math.sqrt(3.0)
|
23
|
+
@z_near_coefficient = args[:z_near_corefficient] || 0.005
|
24
|
+
#@frame = args[:frame]
|
25
|
+
#unless @frame
|
26
|
+
# t = Vector.from_array [0.0, 0.0, 2 * @scene_radius]
|
27
|
+
# @frame.new :translation => t
|
28
|
+
#end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_projection_matrix
|
32
|
+
glMatrixMode(GL_PROJECTION)
|
33
|
+
glLoadIdentity
|
34
|
+
method("load_" + @type.to_s).call
|
35
|
+
end
|
36
|
+
|
37
|
+
def load_model_view_matrix
|
38
|
+
glMatrixMode(GL_MODELVIEW)
|
39
|
+
glLoadIdentity
|
40
|
+
glTranslatef(0.0,0.0, -5.0 * @scene_radius)
|
41
|
+
end
|
42
|
+
|
43
|
+
def width= w
|
44
|
+
@width = w > 0 ? w : 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def height= h
|
48
|
+
@height = h > 0 ? h : 1
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def load_perspective
|
54
|
+
gluPerspective(180.0 * @field_of_view / Math::PI,
|
55
|
+
@width / @height,
|
56
|
+
z_near,
|
57
|
+
z_far)
|
58
|
+
end
|
59
|
+
|
60
|
+
def load_ortho
|
61
|
+
raise RuntimeError, "Unimplemented yet"
|
62
|
+
end
|
63
|
+
|
64
|
+
def distance_to_scene_center
|
65
|
+
2.0
|
66
|
+
end
|
67
|
+
|
68
|
+
def z_near
|
69
|
+
z = self.distance_to_scene_center - @z_clipping_coefficient * @scene_radius
|
70
|
+
|
71
|
+
z_min = @z_near_coefficient * @z_clipping_coefficient * @scene_radius
|
72
|
+
|
73
|
+
if z < z_min
|
74
|
+
if @type == :perspective
|
75
|
+
z = z_min
|
76
|
+
end
|
77
|
+
if @type == :orthographic
|
78
|
+
z = 0.0
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def z_far
|
84
|
+
self.distance_to_scene_center + @z_clipping_coefficient * @scene_radius
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'gslng'
|
2
|
+
|
3
|
+
include GSLng
|
4
|
+
|
5
|
+
module GLViewer
|
6
|
+
class Frame
|
7
|
+
def initialize args = {}
|
8
|
+
translation = args[:translation] || Vector.new(3,true)
|
9
|
+
rotation = args[:rotation] || Matrix.new(3,3)
|
10
|
+
@direct = Matrix.new(4,4,false).identity
|
11
|
+
@indirect = @direct.clone
|
12
|
+
#@direct.view(0,3,3,1) = translation
|
13
|
+
#@direct.view(0,0,3,3) = rotation
|
14
|
+
#t_rotation = rotation.transpose
|
15
|
+
#@indirect.view(0,0,3,3) = t_rotation
|
16
|
+
#@indirect.view(0,3,3,1) = - t_rotation * translation
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def direct_transform vec
|
21
|
+
@direct * vec
|
22
|
+
end
|
23
|
+
|
24
|
+
def reverse_transform vec
|
25
|
+
@indirect * vec
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.rotate_x a
|
29
|
+
Matrix.from_array [1.0 , 0.0 , 0.0 ,
|
30
|
+
0.0 , Math.cos(a), - Math.sin(a),
|
31
|
+
0.0 , Math.sin(a), Math.cos(a)]
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.rotate_y a
|
35
|
+
Matrix.from_array [ Math.cos(a) , 0.0 , Math.sin(a),
|
36
|
+
0.0 , 1.0 , 0.0 ,
|
37
|
+
-Math.sin(a) , 0.0 , Math.cos(a)]
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.rotate_z a
|
41
|
+
Matrix.from_array [Math.cos(a) , - Math.sin(a) , 0.0 ,
|
42
|
+
Math.sin(a) , Math.cos(a) , 0.0 ,
|
43
|
+
0.0 , 0.0 , 1.0 ]
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'opengl'
|
2
|
+
|
3
|
+
include Gl,Glu,Glut
|
4
|
+
|
5
|
+
module GLViewer
|
6
|
+
class GL2DViewer
|
7
|
+
|
8
|
+
attr_accessor :width,:height, :init,:render, :background
|
9
|
+
|
10
|
+
def initialize args = {}
|
11
|
+
@width = args[:width] || 640
|
12
|
+
@height = args[:height] || 480
|
13
|
+
@background = args[:background] || [255,255,255]
|
14
|
+
@init = args[:init] || Proc.new {}
|
15
|
+
@render = args[:render] || Proc.new {}
|
16
|
+
@width = 1 if @width <= 0
|
17
|
+
@height = 1 if @height <= 0
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
def main_loop
|
23
|
+
glutInit
|
24
|
+
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA )
|
25
|
+
glutInitWindowSize(@width,@height)
|
26
|
+
glutInitWindowPosition(0,0)
|
27
|
+
@window = glutCreateWindow("RGLViewer")
|
28
|
+
glutDisplayFunc(method(:gl_draw).to_proc)
|
29
|
+
glutIdleFunc(method(:gl_idle).to_proc)
|
30
|
+
glutReshapeFunc(method(:gl_reshape).to_proc)
|
31
|
+
|
32
|
+
gl_init
|
33
|
+
|
34
|
+
glutMainLoop
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
|
41
|
+
def gl_reshape width, height
|
42
|
+
@width = [1,width ].max
|
43
|
+
@height = [1,height].max
|
44
|
+
|
45
|
+
glViewport(0, 0, @width, @height)
|
46
|
+
glMatrixMode(GL_PROJECTION)
|
47
|
+
|
48
|
+
glLoadIdentity
|
49
|
+
glOrtho(0 , @width, @height, 0, 0, 1);
|
50
|
+
glMatrixMode(GL_MODELVIEW)
|
51
|
+
glLoadIdentity
|
52
|
+
|
53
|
+
gl_draw
|
54
|
+
end
|
55
|
+
|
56
|
+
def gl_init
|
57
|
+
glClearColor(*(@background.collect{|c| c / 255.0}),1)
|
58
|
+
|
59
|
+
glDisable(GL_DEPTH_TEST)
|
60
|
+
|
61
|
+
gl_reshape @width, @height
|
62
|
+
|
63
|
+
@init.call self
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
def gl_draw
|
68
|
+
gl_pre_draw
|
69
|
+
|
70
|
+
@render.call(self)
|
71
|
+
|
72
|
+
gl_post_draw
|
73
|
+
end
|
74
|
+
|
75
|
+
def gl_idle
|
76
|
+
glutPostRedisplay
|
77
|
+
end
|
78
|
+
|
79
|
+
def gl_pre_draw
|
80
|
+
glClear(GL_COLOR_BUFFER_BIT)
|
81
|
+
|
82
|
+
glMatrixMode(GL_MODELVIEW)
|
83
|
+
glLoadIdentity
|
84
|
+
end
|
85
|
+
|
86
|
+
def gl_post_draw
|
87
|
+
glutSwapBuffers
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'opengl'
|
2
|
+
|
3
|
+
include Gl, Glu, Glut
|
4
|
+
|
5
|
+
module GLViewer
|
6
|
+
class GLViewer
|
7
|
+
attr_accessor :camera, :render, :init
|
8
|
+
attr_writer :display_grid,:display_frame, :display_fps
|
9
|
+
|
10
|
+
def initialize args = {}
|
11
|
+
@background_color = args[:background_color] || [180, 180, 180]
|
12
|
+
@foreground_color = args[:background_color] || [51, 51, 51]
|
13
|
+
@display_grid = args[:display_grid] || false
|
14
|
+
@display_fps = args[:display_fps] || false
|
15
|
+
@display_frame = args[:display_frame] || false
|
16
|
+
@init = args[:init] || Proc.new {}
|
17
|
+
@render = args[:render] || Proc.new {}
|
18
|
+
self.camera = args[:camera] || Camera.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def camera= cam
|
22
|
+
unless cam.is_a?(Camera)
|
23
|
+
raise ArgumentError "You should pass a GLViewer::Camera object as a camera."
|
24
|
+
end
|
25
|
+
@camera = cam
|
26
|
+
end
|
27
|
+
def display_grid?
|
28
|
+
@display_grid
|
29
|
+
end
|
30
|
+
|
31
|
+
def display_frame?
|
32
|
+
@display_frame
|
33
|
+
end
|
34
|
+
|
35
|
+
def display_fps?
|
36
|
+
@display_fps
|
37
|
+
end
|
38
|
+
|
39
|
+
def main_loop
|
40
|
+
glutInit
|
41
|
+
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
|
42
|
+
glutInitWindowSize(@camera.width,@camera.height)
|
43
|
+
glutInitWindowPosition(0,0)
|
44
|
+
@window = glutCreateWindow("RGLViewer")
|
45
|
+
glutDisplayFunc(method(:gl_draw).to_proc)
|
46
|
+
glutIdleFunc(method(:gl_idle).to_proc)
|
47
|
+
glutReshapeFunc(method(:gl_reshape).to_proc)
|
48
|
+
|
49
|
+
gl_init
|
50
|
+
|
51
|
+
glutMainLoop
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
def gl_init
|
56
|
+
|
57
|
+
glClearColor(*(@background_color.collect{|c| c / 255.0}),1)
|
58
|
+
# Enables clearing of depth buffer
|
59
|
+
glClearDepth(1.0)
|
60
|
+
# Set type of depth test
|
61
|
+
glDepthFunc(GL_LEQUAL)
|
62
|
+
# Enable depth testing
|
63
|
+
glEnable(GL_DEPTH_TEST)
|
64
|
+
# Enable smooth color shading
|
65
|
+
glShadeModel(GL_SMOOTH)
|
66
|
+
|
67
|
+
glEnable(GL_COLOR_MATERIAL)
|
68
|
+
|
69
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
70
|
+
|
71
|
+
gl_reshape @camera.width, @camera.height
|
72
|
+
|
73
|
+
@init.call(self)
|
74
|
+
|
75
|
+
gl_draw
|
76
|
+
end
|
77
|
+
|
78
|
+
def gl_pre_draw
|
79
|
+
|
80
|
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
81
|
+
|
82
|
+
@camera.load_projection_matrix
|
83
|
+
@camera.load_model_view_matrix
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
def gl_post_draw
|
88
|
+
glutSwapBuffers
|
89
|
+
end
|
90
|
+
|
91
|
+
def gl_draw
|
92
|
+
|
93
|
+
gl_pre_draw
|
94
|
+
|
95
|
+
@render.call(self)
|
96
|
+
|
97
|
+
gl_post_draw
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
def gl_idle
|
102
|
+
glutPostRedisplay
|
103
|
+
end
|
104
|
+
|
105
|
+
def gl_reshape width, height
|
106
|
+
@camera.width = width
|
107
|
+
@camera.height = height
|
108
|
+
|
109
|
+
glViewport(0,0,@camera.width,@camera.height)
|
110
|
+
glutPostRedisplay
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
data/lib/rglviewer/version.rb
CHANGED
data/rglviewer.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rglviewer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-25 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &9686040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *9686040
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: ruby-opengl2
|
27
|
-
requirement: &
|
27
|
+
requirement: &9685620 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,18 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *9685620
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ruby-gsl-ng
|
38
|
+
requirement: &9685080 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *9685080
|
36
47
|
description: libQGLViewer clone using glut
|
37
48
|
email:
|
38
49
|
- alexandre.tuleu.2005@polytechnique.org
|
@@ -51,6 +62,10 @@ files:
|
|
51
62
|
- examples/simpleviewer.rb
|
52
63
|
- examples/test.rb
|
53
64
|
- lib/rglviewer.rb
|
65
|
+
- lib/rglviewer/camera.rb
|
66
|
+
- lib/rglviewer/frame.rb
|
67
|
+
- lib/rglviewer/gl2dviewer.rb
|
68
|
+
- lib/rglviewer/glviewer.rb
|
54
69
|
- lib/rglviewer/version.rb
|
55
70
|
- rglviewer.gemspec
|
56
71
|
- spec/glviewer_spec.rb
|