File: readpixel.rb

package info (click to toggle)
libopengl-ruby 0.32f-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 640 kB
  • ctags: 595
  • sloc: ansic: 7,323; ruby: 3,966; makefile: 64
file content (57 lines) | stat: -rwxr-xr-x 1,320 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require "opengl"
require "glut"
begin
require "RMagick"
rescue Exception
    print "This sample needs RMagick Module.\n"
    exit
end

WIDTH = 200
HEIGHT = 200

display = Proc.new {
   GL.Clear(GL::COLOR_BUFFER_BIT);

   GL.Begin(GL::LINES);
      GL.Vertex(0.5, 0.5);
      GL.Vertex(-0.5, -0.5);
   GL.End

   GL.Flush();

   pixel_str = GL.ReadPixels(0, 0, WIDTH, HEIGHT, GL::RGBA, GL::SHORT)
   pixels = pixel_str.unpack("s*")
    
   image = Magick::Image.new(WIDTH, HEIGHT)
   image.import_pixels(0, 0, WIDTH, HEIGHT, "RGBA", pixels)
   image.flip!
#   image.write("opengl_window.gif")
}

reshape = Proc.new {|w, h|
   GL.Viewport(0, 0,  w,  h);
   GL.MatrixMode(GL::PROJECTION);
   GL.LoadIdentity();
   if (w <= h) 
      GLU.Ortho2D(-1.0, 1.0, -h.to_f/w.to_f, h.to_f/w.to_f);
   else 
      GLU.Ortho2D(w.to_f/h.to_f, w.to_f/h.to_f, -1.0, 1.0);
   end
   GL.MatrixMode(GL::MODELVIEW);
   GL.LoadIdentity();
}


#  Main Loop
#  Open window with initial window size, title bar, 
#  color index display mode, and handle input events.
#
   GLUT.Init
   GLUT.InitDisplayMode(GLUT::SINGLE | GLUT::RGB | GLUT::ALPHA);
   GLUT.InitWindowSize(WIDTH, HEIGHT);
   GLUT.CreateWindow($0);
   GLUT.ReshapeFunc(reshape);
   GLUT.DisplayFunc(display);
   GLUT.MainLoop