Class: Shader

Inherits:
Object show all
Includes:
Gl
Defined in:
lib/ext/shader.rb

Constant Summary collapse

@@canvas_texture_id =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, shader_filename) ⇒ Shader

Returns a new instance of Shader.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ext/shader.rb', line 10

def initialize(window, shader_filename)
  @window = window
  @shader_filename = shader_filename
  
  @program_id = nil
  @vertex_shader_id = nil
  @fragment_shader_id = nil
  
  create_canvas unless @@canvas_texture_id
  compile
end

Instance Attribute Details

#fragment_shader_idObject (readonly)

Returns the value of attribute fragment_shader_id.



6
7
8
# File 'lib/ext/shader.rb', line 6

def fragment_shader_id
  @fragment_shader_id
end

#program_idObject (readonly)

Returns the value of attribute program_id.



6
7
8
# File 'lib/ext/shader.rb', line 6

def program_id
  @program_id
end

#shader_filenameObject (readonly)

Returns the value of attribute shader_filename.



5
6
7
# File 'lib/ext/shader.rb', line 5

def shader_filename
  @shader_filename
end

#vertex_shader_idObject (readonly)

Returns the value of attribute vertex_shader_id.



6
7
8
# File 'lib/ext/shader.rb', line 6

def vertex_shader_id
  @vertex_shader_id
end

#windowObject (readonly)

Returns the value of attribute window.



5
6
7
# File 'lib/ext/shader.rb', line 5

def window
  @window
end

Instance Method Details

#applyObject



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
# File 'lib/ext/shader.rb', line 22

def apply
  @window.gl do
    # copy frame buffer to canvas texture
    glBindTexture(GL_TEXTURE_2D, @@canvas_texture_id)
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, @window.width, @window.height, 0)
    
    # apply shader
    glUseProgram(@program_id)
    
    # clear screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glColor4f(1.0, 1.0, 1.0, 1.0)
    glMatrixMode(GL_PROJECTION)
    glPushMatrix
    glLoadIdentity
    glViewport(0, 0, @window.width, @window.height)
    glOrtho(0, @window.width, @window.height, 0, -1, 1)
    
    # draw processed canvas texture over the screen
    glBindTexture(GL_TEXTURE_2D, @@canvas_texture_id)
    
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 1.0); glVertex2f(0.0,           0.0)
    glTexCoord2f(1.0, 1.0); glVertex2f(@window.width, 0.0)
    glTexCoord2f(1.0, 0.0); glVertex2f(@window.width, @window.height)
    glTexCoord2f(0.0, 0.0); glVertex2f(0.0,           @window.height)
    glEnd
    
    # done, disable shader
    glUseProgram(0)
    
    # and out
    glPopMatrix
  end
end

#uniform(name, value) ⇒ Object Also known as: []=



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ext/shader.rb', line 58

def uniform(name, value)
  glUseProgram(@program_id)
  if value.is_a?(Float)
    glUniform1f(glGetUniformLocation(@program_id, name), value)
  elsif value.is_a?(Integer)
    glUniform1i(glGetUniformLocation(@program_id, name), value)
  else
    raise ArgumentError, "Uniform data type not supported"
  end
  glUseProgram(0)
end