class GlareaDemo

Constants

VERTEX_DATA

The object we are drawing

Z_AXIS

Public Class Methods

new(main_window) click to toggle source
# File gtk3/sample/gtk-demo/glarea.rb, line 20
def initialize(main_window)
  # Rotation angles on each axis
  @rotation_angles = [0.0, 0.0, 0.0]
  @window = Gtk::Window.new(:toplevel)
  @window.screen = main_window.screen
  @window.title = "OpenGL Area"
  @window.set_default_size(400, 600)

  box = Gtk::Box.new(:vertical, false)
  box.margin = 12
  box.spacing = 6
  @window.add(box)

  @gl_area = Gtk::GLArea.new
  @gl_area.hexpand = true
  @gl_area.vexpand = true
  box.add(@gl_area)

  # We need to initialize and free GL resources, so we use
  # the realize and unrealize signals on the widget
  @gl_area.signal_connect "realize" do |widget|
    # We need to set up our state when we realize the GtkGLArea widget
    widget.make_current
    unless widget.error
      init_buffers
      @program, @mvp_location = init_shaders(widget)
    end
  end

  @gl_area.signal_connect "unrealize" do |widget|
    widget.make_current
    unless widget.error
      # We should tear down the state when unrealizing
      glDeleteProgram(@program)
    end
  end

  # The main "draw" call for GtkGLArea
  @gl_area.signal_connect "render" do |area, _context|
    return false if area.error

    # Clear the viewport
    glClearColor(0.5, 0.5, 0.5, 1.0)
    glClear(GL_COLOR_BUFFER_BIT)
    draw_triangle
    glFlush
    true
  end

  controls = Gtk::Box.new(:vertical, false)
  box.add(controls)
  controls.hexpand = true
  (0..2).each do |i|
    controls.add(create_axis_slider(i))
  end

  button = Gtk::Button.new(:label => "Quit")
  button.hexpand = true
  box.add(button)
  button.signal_connect "clicked" do
    @window.destroy
  end
end

Public Instance Methods

run() click to toggle source
# File gtk3/sample/gtk-demo/glarea.rb, line 84
def run
  if !@window.visible?
    @window.show_all
  else
    @window.destroy
  end
  @window
end