wxruby 2.0.0-x86-linux → 2.0.1-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,8 +22,13 @@ class ImageFrame < Wx::Frame
22
22
  def initialize
23
23
  super(nil, :title => 'Simple image demo')
24
24
 
25
+ @offset = 10
26
+ size = 256+2*@offset
27
+ self.client_size = [size, size]
28
+
25
29
  # Load a PNG bitmap from a file for drawing
26
- img_file = File.join( File.dirname(__FILE__), 'wxruby-logo.png')
30
+ img_file = File.join( File.dirname(__FILE__)+"/../../art",
31
+ 'wxruby-256x256.png')
27
32
  @bitmap = Wx::Bitmap.new(img_file)
28
33
 
29
34
  # Set up the drawing to be done when the frame needs re-painting
@@ -32,8 +37,8 @@ class ImageFrame < Wx::Frame
32
37
 
33
38
  def on_paint
34
39
  paint do | dc |
35
- # Draw the bitmap at offset 10px, 10px, with no transparency
36
- dc.draw_bitmap(@bitmap, 10, 10, false)
40
+ # Draw the bitmap at the specified offset with no transparency
41
+ dc.draw_bitmap(@bitmap, @offset, @offset, false)
37
42
  end
38
43
  end
39
44
  end
@@ -21,7 +21,7 @@ include Math
21
21
  # The sample demonstrates some uses of the Wx::Image class, a
22
22
  # platform-independent representation of an image which can be
23
23
  # manipulated (for example, resizing) and written to files in various
24
- # formats. It also shows how an image's data can be written directly, by
24
+ # formats. It also shows how an image's rgb data can be written directly, by
25
25
  # using Array#pack.
26
26
 
27
27
  # A canvas that draws and displays a mathematically generated image
@@ -36,7 +36,7 @@ class MathsDrawing < Window
36
36
  super(parent)
37
37
  # Create a dummy image
38
38
  @default_image = Image.new(1, 1)
39
- @default_image.data = [255, 255, 255].pack('CCC')
39
+ @default_image.rgb_data = [255, 255, 255].pack('CCC')
40
40
  @img = @default_image
41
41
 
42
42
  @red = lambda { | x, y | 1 }
@@ -88,7 +88,7 @@ class MathsDrawing < Window
88
88
  end
89
89
 
90
90
  start_time = Time.now
91
- # The string holding raw image data
91
+ # The string holding raw rgb data
92
92
  data = ''
93
93
  x_factor = size_x.to_f
94
94
  y_factor = size_y.to_f
@@ -105,7 +105,7 @@ class MathsDrawing < Window
105
105
  end
106
106
  end
107
107
  img = Image.new(size_x, size_y)
108
- img.data = data
108
+ img.rgb_data = data
109
109
  @render_time = Time.now - start_time
110
110
  img
111
111
  end
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+ # wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team
3
+ # Freely reusable code: see SAMPLES-LICENSE.TXT for details
4
+ begin
5
+ require 'rubygems'
6
+ rescue LoadError
7
+ end
8
+ require 'wx'
9
+ require 'RMagick'
10
+
11
+ # RMagick sample (written by Chauk-Mean Proum)
12
+
13
+ # This sample demonstrates how to convert directly a RMagick image to
14
+ # a wxRuby image (without saving and loading the image file).
15
+ # See the magick_to_wx method.
16
+
17
+ class ImageFrame < Wx::Frame
18
+ def initialize
19
+ super(nil, :title => 'RMagick sample', :size => [600, 600])
20
+
21
+ # Create the magick image from an image file
22
+ img_file = File.join( File.dirname(__FILE__)+"/../../art",
23
+ 'wxruby-256x256.png')
24
+ @magick_image = Magick::ImageList.new(img_file)
25
+
26
+ # Create some magick images with special effects
27
+ @magick_image1 = @magick_image.sketch
28
+ @magick_image2 = @magick_image.oil_paint(4)
29
+ @magick_image3 = @magick_image.shade
30
+
31
+ # Convert the magick images to wxRuby images
32
+ @image1 = magick_to_wx(@magick_image1)
33
+ @image2 = magick_to_wx(@magick_image2)
34
+ @image3 = magick_to_wx(@magick_image3)
35
+
36
+ # Create the corresponding bitmaps
37
+ compute_bitmaps
38
+
39
+ # Set up event handling
40
+ evt_size :on_size
41
+ evt_idle :on_idle
42
+ evt_paint :on_paint
43
+ end
44
+
45
+
46
+ # Convert the RMagick image to a WxRuby image
47
+ def magick_to_wx magick_img
48
+ wx_img = Wx::Image.new(magick_img.columns, magick_img.rows)
49
+
50
+ # Set the image data
51
+ wx_img.rgb_data = magick_img.to_blob { self.format = "RGB" }
52
+
53
+ # Set the alpha (transparency) if any
54
+ if magick_img.alpha?
55
+ wx_img.alpha_data = magick_img.to_blob { self.format = "A" }
56
+ end
57
+
58
+ wx_img
59
+ end
60
+
61
+ # Create a bitmap for the specified image and size
62
+ def compute_bitmap image, width, height
63
+ rescaled_image = Wx::Image.new(image).rescale(width, height)
64
+ rescaled_image.to_bitmap
65
+ end
66
+
67
+ # Create the bitmaps corresponding to the images and with half the size of the frame
68
+ def compute_bitmaps
69
+ width = client_size.x / 2
70
+ height = client_size.y / 2
71
+ @bitmap1 = compute_bitmap(@image1, width, height)
72
+ @bitmap2 = compute_bitmap(@image2, width, height)
73
+ @bitmap3 = compute_bitmap(@image3, width, height)
74
+ @done = true
75
+ end
76
+
77
+ # Note to recompute the bitmaps on a resize
78
+ def on_size(event)
79
+ @done = false
80
+ event.skip
81
+ end
82
+
83
+ # Recompute the bitmaps if needed, then do a refresh
84
+ def on_idle
85
+ if not @done
86
+ compute_bitmaps
87
+ refresh
88
+ end
89
+ end
90
+
91
+ # Paint the frame with the bitmaps
92
+ def on_paint
93
+ paint do | dc |
94
+
95
+ if @done
96
+ offset_x = client_size.x / 4
97
+ offset_y = client_size.y / 4
98
+ dc.draw_bitmap(@bitmap1, 0, 0, true)
99
+ dc.draw_bitmap(@bitmap2, offset_x, offset_y, true)
100
+ dc.draw_bitmap(@bitmap3, offset_x*2, offset_y*2, true)
101
+ end
102
+
103
+ end
104
+ end
105
+ end
106
+
107
+ Wx::App.run do
108
+ ImageFrame.new.show
109
+ end
110
+
@@ -20,7 +20,7 @@ class MinimalFrame < Wx::Frame
20
20
 
21
21
  # Give the frame an icon. PNG is a good choice of format for
22
22
  # cross-platform images. Note that OS X doesn't have "Frame" icons.
23
- icon_file = File.join( File.dirname(__FILE__), "mondrian.png")
23
+ icon_file = File.join( File.dirname(__FILE__)+"/../../art", "wxruby.png")
24
24
  self.icon = Wx::Icon.new(icon_file)
25
25
 
26
26
  menu_bar = Wx::MenuBar.new
@@ -14,13 +14,27 @@ include Glu
14
14
 
15
15
  class CubeFrame < Wx::Frame
16
16
  def initialize(title)
17
- super(nil, :title => title, :size => [ 400, 300 ])
17
+ super(nil, :title => title, :size => [ 600, 400 ])
18
+
19
+ sizer = Wx::VBoxSizer.new
20
+
18
21
  attrib = [Wx::GL_RGBA, Wx::GL_DOUBLEBUFFER, Wx::GL_DEPTH_SIZE, 24]
19
22
  @canvas = Wx::GLCanvas.new(self, -1, [-1, -1], [-1, -1],
20
- Wx::FULL_REPAINT_ON_RESIZE, 'GLCanvas', attrib)
21
- @canvas.evt_paint { @canvas.paint { render } }
23
+ Wx::FULL_REPAINT_ON_RESIZE, "GLCanvas", attrib)
24
+ sizer.add_item @canvas, :proportion => 1, :flag => Wx::EXPAND
25
+
26
+ text = Wx::StaticText.new(self, :label => "Use Up/Down/Left/Right keys to rotate the cube")
27
+ sizer.add_item text
28
+
29
+ self.sizer = sizer
30
+ self.show
31
+
32
+ @canvas.set_focus
22
33
  @canvas.evt_key_down {|evt| on_key_down(evt.get_key_code) }
23
- @rotate = [0.0, 0.0, 0.0]
34
+
35
+ @canvas.evt_paint { @canvas.paint { render } }
36
+ @rotate = [30.0, 0.0, -30.0]
37
+
24
38
  end
25
39
 
26
40
  def on_key_down(key)
@@ -39,9 +53,9 @@ class CubeFrame < Wx::Frame
39
53
 
40
54
  def render
41
55
  @canvas.set_current
42
- sz = @canvas.get_size
43
- w = sz.get_width
44
- h = sz.get_height
56
+ sz = @canvas.size
57
+ w = sz.width
58
+ h = sz.height
45
59
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
46
60
  glEnable(GL_DEPTH_TEST)
47
61
  glViewport(0, 0, w, h)
@@ -58,7 +72,7 @@ class CubeFrame < Wx::Frame
58
72
  glRotate(@rotate[2], 0.0, 0.0, 1.0)
59
73
  glBegin(GL_TRIANGLES)
60
74
  #left
61
- glColor3d(1.0, 1.0, 1.0)
75
+ glColor3d(1.0, 1.0, 0.0) # yellow
62
76
  glVertex3d(-1.0, 1.0, 1.0)
63
77
  glVertex3d(-1.0, 1.0,-1.0)
64
78
  glVertex3d(-1.0,-1.0, 1.0)
@@ -66,7 +80,7 @@ class CubeFrame < Wx::Frame
66
80
  glVertex3d(-1.0, 1.0,-1.0)
67
81
  glVertex3d(-1.0,-1.0,-1.0)
68
82
  #right
69
- glColor3d(0.0, 1.0, 1.0)
83
+ glColor3d(0.0, 1.0, 0.0) # green
70
84
  glVertex3d( 1.0,-1.0, 1.0)
71
85
  glVertex3d( 1.0,-1.0,-1.0)
72
86
  glVertex3d( 1.0, 1.0, 1.0)
@@ -74,7 +88,7 @@ class CubeFrame < Wx::Frame
74
88
  glVertex3d( 1.0,-1.0,-1.0)
75
89
  glVertex3d( 1.0, 1.0,-1.0)
76
90
  #up
77
- glColor3d(0.0, 0.0, 1.0)
91
+ glColor3d(0.0, 0.0, 1.0) # blue
78
92
  glVertex3d(-1.0, 1.0, 1.0)
79
93
  glVertex3d(-1.0,-1.0, 1.0)
80
94
  glVertex3d( 1.0, 1.0, 1.0)
@@ -82,7 +96,7 @@ class CubeFrame < Wx::Frame
82
96
  glVertex3d(-1.0,-1.0, 1.0)
83
97
  glVertex3d( 1.0,-1.0, 1.0)
84
98
  #down
85
- glColor3d(1.0, 0.0, 0.0)
99
+ glColor3d(0.0, 1.0, 1.0) # cyan
86
100
  glVertex3d(-1.0,-1.0,-1.0)
87
101
  glVertex3d(-1.0, 1.0,-1.0)
88
102
  glVertex3d( 1.0,-1.0,-1.0)
@@ -90,7 +104,7 @@ class CubeFrame < Wx::Frame
90
104
  glVertex3d(-1.0, 1.0,-1.0)
91
105
  glVertex3d( 1.0, 1.0,-1.0)
92
106
  #front
93
- glColor3d(1.0, 1.0, 0.0)
107
+ glColor3d(1.0, 0.0, 0.0) # red
94
108
  glVertex3d(-1.0,-1.0, 1.0)
95
109
  glVertex3d(-1.0,-1.0,-1.0)
96
110
  glVertex3d( 1.0,-1.0, 1.0)
@@ -98,7 +112,7 @@ class CubeFrame < Wx::Frame
98
112
  glVertex3d(-1.0,-1.0,-1.0)
99
113
  glVertex3d( 1.0,-1.0,-1.0)
100
114
  #back
101
- glColor3d(0.0, 1.0, 0.0)
115
+ glColor3d(1.0, 0.0, 1.0) # magenta
102
116
  glVertex3d( 1.0, 1.0, 1.0)
103
117
  glVertex3d( 1.0, 1.0,-1.0)
104
118
  glVertex3d(-1.0, 1.0, 1.0)
@@ -113,5 +127,4 @@ end
113
127
  Wx::App.run do
114
128
  self.app_name = 'GLCanvas Cube'
115
129
  frame = CubeFrame.new("OpenGL Canvas wxRuby App")
116
- frame.show
117
130
  end
@@ -0,0 +1,191 @@
1
+ #!/usr/bin/env ruby
2
+ # wxRuby2 Sample Code. Copyright (c) 2004-2008 wxRuby development team
3
+ # Freely reusable code: see SAMPLES-LICENSE.TXT for details
4
+
5
+ # OpenGL sample by Chauk-Mean Proum featuring an animated cube with lighting.
6
+ # OpenGL commands are structured following the Red Book :
7
+ # - init part (opengl_init)
8
+ # - resize part (opengl_resize)
9
+ # - render part (opengl_render)
10
+
11
+ begin
12
+ require 'rubygems'
13
+ rescue LoadError
14
+ end
15
+ require 'wx'
16
+ require 'gl'
17
+ require 'glu'
18
+
19
+ include Gl
20
+ include Glu
21
+
22
+ class CubeFrame < Wx::Frame
23
+ def initialize(title)
24
+ super(nil, :title => title)
25
+
26
+ sizer = Wx::VBoxSizer.new
27
+
28
+ attrib = [Wx::GL_RGBA, Wx::GL_DOUBLEBUFFER, Wx::GL_DEPTH_SIZE, 24]
29
+ # Use of keyword arguments for the GLCanvas initializer
30
+ @canvas = Wx::GLCanvas.new(self, :attrib_list => attrib, :size => [600, 600])
31
+ sizer.add_item @canvas, :proportion => 1, :flag => Wx::EXPAND
32
+
33
+ text = Wx::StaticText.new(self, :label => "Use Up/Down/Left/Right keys to change rotation direction")
34
+ sizer.add_item text
35
+
36
+ self.sizer = sizer
37
+ sizer.fit(self)
38
+
39
+ self.show
40
+ # A GL context can be set to a GL canvas only if the latter has been shown
41
+ opengl_init()
42
+ opengl_resize()
43
+
44
+ # set the focus on the GL canvas for key press
45
+ @canvas.set_focus
46
+ @canvas.evt_key_down {|evt| on_key_down(evt.get_key_code) }
47
+
48
+ @canvas.evt_paint { @canvas.paint { opengl_render } }
49
+ @canvas.evt_size do |e|
50
+ opengl_resize
51
+ e.skip()
52
+ end
53
+
54
+ # set up the animation
55
+ @rotate = [15.0, -30.0, 0.0]
56
+ @anim_step_x_axis = -1.0
57
+ @anim_step_y_axis = -2.0
58
+ anim_timer = Wx::Timer.new(self)
59
+ anim_timer.start(25) # start the timer with a period of 25 ms
60
+ evt_timer anim_timer, :animate
61
+
62
+ end
63
+
64
+ def animate
65
+ @rotate[0] += @anim_step_x_axis
66
+ @rotate[1] += @anim_step_y_axis
67
+ opengl_render()
68
+ end
69
+
70
+ def on_key_down(key)
71
+ case key
72
+ when Wx::K_UP
73
+ @anim_step_x_axis = -1.0
74
+ when Wx::K_DOWN
75
+ @anim_step_x_axis = 1.0
76
+ when Wx::K_LEFT
77
+ @anim_step_y_axis = -2.0
78
+ when Wx::K_RIGHT
79
+ @anim_step_y_axis = 2.0
80
+ end
81
+ opengl_render()
82
+ end
83
+
84
+
85
+ def opengl_init
86
+ # initialize the GL rendering
87
+ @canvas.set_current
88
+
89
+ mat_specular = [1.0, 1.0, 1.0, 1.0]
90
+ mat_shininess = [90.0]
91
+
92
+ light_ambient = [0.9, 0.9, 0.9, 1.0]
93
+ light_position = [-1.0, 4.0, 7.0, 0.0]
94
+
95
+ glLoadIdentity()
96
+
97
+ # glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular)
98
+ # glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess)
99
+
100
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_ambient)
101
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position)
102
+
103
+ glColorMaterial(GL_FRONT, GL_DIFFUSE)
104
+
105
+ glEnable(GL_LIGHTING)
106
+ glEnable(GL_LIGHT0)
107
+ glEnable(GL_COLOR_MATERIAL)
108
+
109
+ glDepthFunc(GL_LEQUAL)
110
+ glEnable(GL_DEPTH_TEST)
111
+ end
112
+
113
+ def opengl_resize
114
+ @canvas.set_current
115
+ sz = @canvas.size
116
+ w = sz.width
117
+ h = sz.height
118
+ glViewport(0, 0, w, h)
119
+ glMatrixMode(GL_PROJECTION)
120
+ glLoadIdentity()
121
+ gluPerspective(30.0, w.to_f/h.to_f, 1.0, 20.0)
122
+ glMatrixMode(GL_MODELVIEW)
123
+ end
124
+
125
+ def opengl_render
126
+ @canvas.set_current
127
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
128
+ glLoadIdentity()
129
+
130
+ gluLookAt(0.0, 3.0, 8.0, # position
131
+ 0.0, 0.0, 0.0, # target
132
+ 0.0, 1.0, 0.0) # up direction
133
+
134
+ glRotate(@rotate[0], 1.0, 0.0, 0.0)
135
+ glRotate(@rotate[1], 0.0, 1.0, 0.0)
136
+ glRotate(@rotate[2], 0.0, 0.0, 1.0)
137
+
138
+ glBegin(GL_QUADS)
139
+
140
+ #left
141
+ glNormal3d(-1.0, 0.0, 0.0) # normal vector is required for proper lighting
142
+ glColor3d(1.0, 1.0, 0.0) # yellow
143
+ glVertex3d(-1.0,-1.0,-1.0)
144
+ glVertex3d(-1.0, 1.0,-1.0)
145
+ glVertex3d(-1.0, 1.0, 1.0)
146
+ glVertex3d(-1.0,-1.0, 1.0)
147
+ #right
148
+ glNormal3d( 1.0, 0.0, 0.0)
149
+ glColor3d(0.0, 1.0, 0.0) # green
150
+ glVertex3d( 1.0,-1.0,-1.0)
151
+ glVertex3d( 1.0, 1.0,-1.0)
152
+ glVertex3d( 1.0, 1.0, 1.0)
153
+ glVertex3d( 1.0,-1.0, 1.0)
154
+ #up
155
+ glNormal3d( 0.0, 1.0, 0.0)
156
+ glColor3d(0.0, 0.0, 1.0) # blue
157
+ glVertex3d(-1.0, 1.0,-1.0)
158
+ glVertex3d( 1.0, 1.0,-1.0)
159
+ glVertex3d( 1.0, 1.0, 1.0)
160
+ glVertex3d(-1.0, 1.0, 1.0)
161
+ #down
162
+ glNormal3d( 0.0,-1.0, 0.0)
163
+ glColor3d(0.0, 1.0, 1.0) # cyan
164
+ glVertex3d(-1.0,-1.0,-1.0)
165
+ glVertex3d( 1.0,-1.0,-1.0)
166
+ glVertex3d( 1.0,-1.0, 1.0)
167
+ glVertex3d(-1.0,-1.0, 1.0)
168
+ #front
169
+ glNormal3d( 0.0, 0.0, 1.0)
170
+ glColor3d(1.0, 0.0, 0.0) # red
171
+ glVertex3d(-1.0,-1.0, 1.0)
172
+ glVertex3d(-1.0, 1.0, 1.0)
173
+ glVertex3d( 1.0, 1.0, 1.0)
174
+ glVertex3d( 1.0,-1.0, 1.0)
175
+ #back
176
+ glNormal3d( 0.0, 0.0,-1.0)
177
+ glColor3d(1.0, 0.0, 1.0) # magenta
178
+ glVertex3d(-1.0,-1.0,-1.0)
179
+ glVertex3d(-1.0, 1.0,-1.0)
180
+ glVertex3d( 1.0, 1.0,-1.0)
181
+ glVertex3d( 1.0,-1.0,-1.0)
182
+ glEnd()
183
+ glFlush()
184
+ @canvas.swap_buffers
185
+ end
186
+ end
187
+
188
+ Wx::App.run do
189
+ self.app_name = 'GLCanvas Cube'
190
+ frame = CubeFrame.new("OpenGL Canvas wxRuby : Animation and Lighting")
191
+ end