Class: Ray::Effect::Generator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ray/effect/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version = 110) { ... } ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • version (Integer) (defaults to: 110)

    GLSL version to use

Yields:

  • Yields itself if a block is given



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/ray/effect/generator.rb', line 8

def initialize(version = 110)
  @effects = []
  @version = version

  if version >= 130
    @input = <<-input
in vec4 var_Color;
in vec2 var_TexCoord;
input
  else
    @input = <<-input
varying vec4 var_Color;
varying vec2 var_TexCoord;
input
  end

  @uniforms = <<-uniforms
uniform sampler2D in_Texture;
uniform bool in_TextureEnabled;
uniforms

  @color = "color"

  @default = <<-default
  /* Apply default value */
  vec4 color;
  if (in_TextureEnabled)
    color = texture#{"2D" if version<130}(in_Texture, var_TexCoord) * var_Color;
  else
    color = var_Color;
default

  yield self if block_given?
end

Instance Attribute Details

#colorString

Returns Name of the variable containing the color once the default code is run. “color” by default.

Returns:

  • (String)

    Name of the variable containing the color once the default code is run. “color” by default.



71
72
73
# File 'lib/ray/effect/generator.rb', line 71

def color
  @color
end

#defaultString

Returns Code to ste the default color. It should apply texturing, for example.

Returns:

  • (String)

    Code to ste the default color. It should apply texturing, for example.



75
76
77
# File 'lib/ray/effect/generator.rb', line 75

def default
  @default
end

#effectsArray<Ray::Effect> (readonly) Also known as: to_a

Returns All of the effects used by the generator.

Returns:



56
57
58
# File 'lib/ray/effect/generator.rb', line 56

def effects
  @effects
end

#inputString

Returns Code defining GLSL input (with varying or in, depending on the GLSL version).

Returns:

  • (String)

    Code defining GLSL input (with varying or in, depending on the GLSL version).



64
65
66
# File 'lib/ray/effect/generator.rb', line 64

def input
  @input
end

#uniformsString

Returns Code defining uniforms.

Returns:

  • (String)

    Code defining uniforms



67
68
69
# File 'lib/ray/effect/generator.rb', line 67

def uniforms
  @uniforms
end

#versionInteger (readonly)

Returns GLSL version number.

Returns:

  • (Integer)

    GLSL version number



60
61
62
# File 'lib/ray/effect/generator.rb', line 60

def version
  @version
end

Instance Method Details

#apply_defaults(shader) ⇒ Object

Apply generator defaults to a shader

Parameters:



140
141
142
# File 'lib/ray/effect/generator.rb', line 140

def apply_defaults(shader)
  each { |effect| effect.apply_defaults(shader) }
end

#build(shader = Ray::Shader.new) ⇒ Ray::Shader

Generates a shader

Parameters:

  • shader (Ray::Shader) (defaults to: Ray::Shader.new)

    Shader to compile and apply defaults to

Returns:



132
133
134
135
136
# File 'lib/ray/effect/generator.rb', line 132

def build(shader = Ray::Shader.new)
  shader.compile :frag => StringIO.new(code)
  apply_defaults shader
  shader
end

#codeString

Returns code of the pixel shader.

Returns:

  • (String)

    code of the pixel shader



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ray/effect/generator.rb', line 78

def code
  str  = "#version #@version\n"
  str << "\n"

  str << input    << "\n"
  str << uniforms << "\n"


  str << "out vec4 out_FragColor;\n" if version >= 130
  str << "\n"

  str << "/* Headers */\n"
  each do |effect|
    str << effect.header << "\n"
  end
  str << "\n"

  str << "/* Structs */\n\n"
  each do |effect|
    str << effect.struct << "\n"
  end

  str << "/* Effects parameters */\n"
  each do |effect|
    str << "uniform ray_#{effect.name} #{effect.name};\n"
  end
  str << "\n"

  str << "/* Functions */\n"
  each do |effect|
    str << effect.code << "\n"
  end

  str << "void main() {\n"
  str << default << "\n"

  each do |effect|
    str << "  if (#{effect.name}.enabled)\n"
    str << "    #@color = do_#{effect.name}(#{effect.name}, #@color);\n"
    str << "\n"
  end

  if version >= 130
    str << "  out_FragColor = #@color;\n"
  else
    str << "  gl_FragColor = #@color;\n"
  end

  str << "}\n"
end

#each(&block) ⇒ Object



51
52
53
# File 'lib/ray/effect/generator.rb', line 51

def each(&block)
  @effects.each(&block)
end

#push(*effects) ⇒ Object Also known as: <<

Parameters:

  • effects (Array<Ray:::Effect>)

    effects to add to the generator



44
45
46
47
# File 'lib/ray/effect/generator.rb', line 44

def push(*effects)
  @effects.concat effects
  self
end