Advertisement
Guest User

Untitled

a guest
May 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. require 'bundler/setup'
  2. require 'opengl'
  3. require 'glut'
  4. include Gl, Glut
  5.  
  6. class BitMap
  7. def initialize(width, height, dpi = 96)
  8. @width = width
  9. @height = height
  10. @line_size = width * 3 + (4 - (width * 3) % 4) % 4
  11. @buf_size = @line_size * height
  12. @buf = ("\000" * @buf_size).encode('BINARY')
  13. @bit_count = 24
  14. @compression = 0 # 圧縮無し
  15. @size_image = 0
  16. @x_pix_per_meter = (39.375 * dpi).round
  17. @y_pix_per_meter = (39.375 * dpi).round
  18. @clr_used = 0
  19. @cir_important = 0
  20. end
  21.  
  22. attr_accessor :buf
  23. attr_reader :width, :height
  24.  
  25. # BMPファイルを出力する
  26. def write(filename)
  27. file_size = 14 + 40 + @buf_size
  28. data_offset = 14 + 40
  29.  
  30. open(filename, "wb") do |f|
  31. f.print 'BM'
  32. f.print [file_size, 0, data_offset].pack("l*")
  33. f.print [40, @width, @height].pack("l*")
  34. f.print [1, @bit_count].pack("S*")
  35. f.print [@compression, @size_image,
  36. @x_pix_per_meter, @y_pix_per_meter,
  37. @clr_used, @cir_important].pack("l*")
  38. f.print @buf
  39. end
  40. end
  41.  
  42. def self.gl_capture(fname)
  43. x = glutGet(GLUT_WINDOW_WIDTH)
  44. y = glutGet(GLUT_WINDOW_HEIGHT)
  45.  
  46. bitmap = BitMap.new(x, y)
  47. glPixelStorei(GL_PACK_ALIGNMENT, 1)
  48. imgdata = glReadPixels(0, 0, x, y, GL_BGR, GL_UNSIGNED_BYTE).unpack("H*")[0]
  49.  
  50. data = ""
  51. y.times do |j|
  52. (x * 3).times {|i| data += imgdata[6 * x * j + i * 2, 2].to_i(16).chr}
  53. data += "\x00" * ((4 - 3 * x % 4) % 4)
  54. end
  55.  
  56. bitmap.buf = data
  57. bitmap.write(fname)
  58. end
  59. end
  60.  
  61.  
  62. if __FILE__ == $0
  63. def init
  64. glClearColor(0, 0, 255, 1)
  65. end
  66.  
  67. display = proc do
  68. vertex = [-0.9, 0.9, 0.9, 0.9, 0, -0.9]
  69. glClear(GL_COLOR_BUFFER_BIT)
  70. glEnableClientState(GL_VERTEX_ARRAY)
  71. glVertexPointer(2, GL_FLOAT, 0, vertex)
  72. glLineWidth(3) #線の太さ
  73. glColor3f(0, 1, 0)
  74. glDrawArrays(GL_LINE_LOOP, 0, 3)
  75. BitMap.gl_capture("sample.bmp")
  76. glFlush
  77. end
  78.  
  79. Width = Height = 101
  80. glutInit
  81. glutInitWindowSize(Width, Height)
  82. glutInitWindowPosition(200, 100)
  83. glutCreateWindow("Triangle")
  84. init
  85. glutDisplayFunc(display)
  86. glutMainLoop
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement