Class: RubyGL::Shader

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygl/shader.rb

Instance Method Summary collapse

Constructor Details

#initialize(vert_src, frag_src) ⇒ Shader

Returns a new instance of Shader.



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

def initialize(vert_src, frag_src)
    @v_shader_id = Native.glCreateShader(Native::GL_VERTEX_SHADER)
    @f_shader_id = Native.glCreateShader(Native::GL_FRAGMENT_SHADER)
    @attrib_locs, @uniform_locs = {}, {}
    
    # Pointer Used To Point To The String Of Our Source Code
    shader_src_ptr = FFI::MemoryPointer.new(:pointer)
    shader_src_len = FFI::MemoryPointer.new(:int)
    
    v_shader_ptr = FFI::MemoryPointer.from_string(vert_src)
    f_shader_ptr = FFI::MemoryPointer.from_string(frag_src)
    
    shader_src_ptr.write_pointer(v_shader_ptr.address)
    shader_src_len.write_int(vert_src.size)
    Native.glShaderSource(@v_shader_id, 1, shader_src_ptr, shader_src_len)
    
    shader_src_ptr.write_pointer(f_shader_ptr.address)
    shader_src_len.write_int(frag_src.size)
    Native.glShaderSource(@f_shader_id, 1, shader_src_ptr, shader_src_len)
    
    Native.glCompileShader(@v_shader_id)
    Native.glCompileShader(@f_shader_id)
    
    # Throw Exception With Compile Error If Compilation Failed
    Shader.compile_status(@v_shader_id)
    Shader.compile_status(@f_shader_id)
    
    @program_id = Native.glCreateProgram()
    
    Native.glAttachShader(@program_id, @v_shader_id)
    Native.glAttachShader(@program_id, @f_shader_id)
    
    Native.glLinkProgram(@program_id)
    
    Shader.link_status(@program_id)
end

Instance Method Details

#attrib_location(var_name) ⇒ Object



63
64
65
# File 'lib/rubygl/shader.rb', line 63

def attrib_location(var_name)
    Native.glGetAttribLocation(@program_id, var_name)
end

#send_uniform(method_sym, var_name, data = nil, *args) ⇒ Object

Splat Operator Aggregates Incoming Parameters And Expands Outgoing Parameters If method_sym Does Not Accept A Pointer To The Data, Pass nil For data And Pass In The Normal Parameters Required For The Specific Method.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rubygl/shader.rb', line 50

def send_uniform(method_sym, var_name, data = nil, *args)
    loc = @uniform_locs[var_name] ||= uniform_location(var_name)
    
    if data != nil then
        data_ptr = FFI::MemoryPointer.new(:float, data.size)
        data_ptr.write_array_of_float(data)
        
        RubyGL::Native.send(method_sym, loc, *args, data_ptr)
    else
        RubyGL::Native.send(method_sym, loc, *args)
    end
end

#use_programObject



43
44
45
# File 'lib/rubygl/shader.rb', line 43

def use_program()
    Native.glUseProgram(@program_id)
end