Class: ShaderApp

Inherits:
RBGLox::App show all
Defined in:
lib/rbglox.rb

Instance Attribute Summary collapse

Attributes inherited from RBGLox::Window

#height, #multisample, #title, #vsync, #watch, #width

Instance Method Summary collapse

Methods inherited from RBGLox::App

#initialize

Methods inherited from RBGLox::Window

#begin_frame, #close, #end_frame, #exec, #initialize, #key?, #mouse?, #mouse_left?, #mouse_pos?, #mouse_right?, #mouse_wheel?, #resize, #time

Constructor Details

This class inherits a constructor from RBGLox::App

Instance Attribute Details

#modelObject

Returns the value of attribute model.



22
23
24
# File 'lib/rbglox.rb', line 22

def model
  @model
end

Instance Method Details

#drawObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbglox.rb', line 77

def draw
  glTranslatef(0,0, @z)
  glRotatef(@rotX, 1.0, 0.0, 0.0)
  glRotatef(@rotY, 0.0, 1.0, 0.0)
  glRotatef(@rotZ, 0.0, 0.0, 1.0)

  @textures.each_with_index do |texture, i|
    texture.bind i
  end
  
  @shader.bind
  @shader['time'] = time.to_f
  
  if @model.nil?
    glutSolidTeapot(8.0)
  else
    @model.draw
  end

  @shader.release

  @textures.each_with_index do |texture, i|
    texture.release i
  end
end

#initObject



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
# File 'lib/rbglox.rb', line 24

def init
  @textures = []
  (1..4).each do |i|
    filename = File.join watch, "texture#{i}.tga"
    @textures << RBGLox::Texture.new(filename) if File.exists? filename
  end
  
  @shader = RBGLox::Shader.new File.join watch, "shader.glsl"

  @z = -6.0
  if model == 'quad'
    @model = RBGLox::MeshQuad.new
  elsif ['cube', 'box'].include? model
    @model = RBGLox::MeshCube.new
  else
    @model = nil
    @z = -30.0
  end

  @lastX = 0
  @lastY = 0
  @lastM = 0

  @rotX = 0.0
  @rotY = 0.0
  @rotZ = 0.0
end

#reload(event) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rbglox.rb', line 111

def reload(event)
  if event.type == :modified
    puts "reloading #{event.path} ..."
    if event.path =~ /\.glsl$/i
      @shader.reload
    elsif event.path =~ /\.tga$/i
      @textures.each_with_index do |texture, i|
        if texture.filename == event.path
          texture.reload i
          break
        end
      end
    end
  end
end

#shutdownObject



103
104
105
106
107
108
109
# File 'lib/rbglox.rb', line 103

def shutdown
  @textures.each do |texture|
    texture.free
  end
  @shader.free
  @model.free unless @model.nil?
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbglox.rb', line 52

def update
  currM = mouse_wheel?

  if currM != 0
    @z -= (@lastM - currM)
    @lastM = currM
  end

  if mouse_left?
    currX, currY = mouse_pos?

    if @lastX > 0 and @lastY > 0
      @rotX += (currY - @lastY)
      @rotY += (currX - @lastX)
    end

    @lastX = currX
    @lastY = currY
  else
    @lastX = 0
    @lastY = 0
  end

end