Class: Mittsu::OpenGLState

Inherits:
Object
  • Object
show all
Defined in:
lib/mittsu/renderers/opengl/opengl_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(param_mittsu_to_gl) ⇒ OpenGLState

Returns a new instance of OpenGLState.



3
4
5
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
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 3

def initialize(param_mittsu_to_gl)
  @param_mittsu_to_gl = param_mittsu_to_gl

  @new_attributes = Array.new(16) # Uint8Array
  @enabled_attributes = Array.new(16) # Uint8Array

  @current_blending = nil
  @current_blend_equation = nil
  @current_blend_src = nil
  @current_blend_dst = nil
  @current_blend_equation_alpha = nil
  @current_blend_src_alpha = nil
  @current_blend_dst_alpha = nil

  @current_depth_test = nil
  @current_depth_write = nil

  @current_color_write = nil

  @current_double_sided = nil
  @current_flip_sided = nil

  @current_line_width = nil

  @current_polygon_offset = nil
  @current_polygon_offset_factor = nil
  @current_polygon_offset_units = nil
end

Instance Method Details

#disable_unused_attributesObject



48
49
50
51
52
53
54
55
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 48

def disable_unused_attributes
  @enabled_attributes.length.times do |i|
    if @enabled_attributes[i] && !@new_attributes[i]
      glDisableVertexAttribArray(i)
      @enabled_attributes[i] = false
    end
  end
end

#enable_attribute(attribute) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 38

def enable_attribute(attribute)
  glEnableVertexAttribArray(attribute)
  @new_attributes[attribute] = true

  if !@enabled_attributes[attribute]
    # glEnableVertexAttribArray(attribute)
    @enabled_attributes[attribute] = true
  end
end

#init_attributesObject



32
33
34
35
36
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 32

def init_attributes
  @new_attributes.length.times do |i|
    @new_attributes[i] = false
  end
end

#resetObject



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 194

def reset
  @enabled_attributes.length.times do |i|
    @enabled_attributes[i] = false
  end

  @current_blending = nil
  @current_depth_test = nil
  @current_depth_write = nil
  @current_color_write = nil
  @current_double_sided = nil
  @current_flip_sided = nil
end

#set_blending(blending, blend_equation = nil, blend_src = nil, blend_dst = nil, blend_equation_alpha = nil, blend_src_alpha = nil, blend_dst_alpha = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 57

def set_blending(blending, blend_equation = nil, blend_src = nil, blend_dst = nil, blend_equation_alpha = nil, blend_src_alpha = nil, blend_dst_alpha = nil)
  if blending != @current_blending
    case blending
    when NoBlending
      glDisable(GL_BLEND)
    when AdditiveBlending
      glEnable(GL_BLEND)
      glBlendEquation(GL_FUNC_ADD)
      glBlendFunc(GL_SRC_ALPHA, GL_ONE)
    when SubtractiveBlending
      # TODO: Find blendFuncSeparate() combination ???
      glEnable(GL_BLEND)
      glBlendEquation(GL_FUNC_ADD)
      glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR)
    when MultiplyBlending
      # TODO: Find blendFuncSeparate() combination ???
      glEnable(GL_BLEND)
      glBlendEquation(GL_FUNC_ADD)
      glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR)
    when CustomBlending
      glEnable(GL_BLEND)
    else
      glEnable(GL_BLEND)
      glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD)
      glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
    end

    @current_blending = blending
  end

  if blending == CustomBlending
    blend_equation_alpha ||= blend_equation
    blend_src_alpha ||= blend_src
    blend_dst_alpha ||= blend_dst

    if blend_equation != @current_blend_equation || blend_equation_alpha != @current_blend_equation_alpha
      glBlendEquationSeparate(@param_mittsu_to_gl[blend_equation], @param_mittsu_to_gl[blend_equation_alpha])

      @current_blend_equation = blend_equation
      @current_blend_equation_alpha = blend_equation_alpha
    end

    if blend_src != @current_blend_src || blend_dst != @current_blend_dst || blend_src_alpha != @current_blend_src_alpha || blend_dst_alpha != @current_blend_dst_alpha
      glBlendFuncSeparate(@param_mittsu_to_gl[blend_src], @param_mittsu_to_gl[blend_dst], @param_mittsu_to_gl[blend_src_alpha], @param_mittsu_to_gl[blend_dst_alpha])

      @current_blend_src = nil
      @current_blend_dst = nil
      @current_blend_src_alpha = nil
      @current_blend_dst_alpha = nil
    end
  else
    @current_blend_equation = nil
    @current_blend_src = nil
    @current_blend_dst = nil
    @current_blend_equation_alpha = nil
    @current_blend_src_alpha = nil
    @current_blend_dst_alpha = nil
  end
end

#set_color_write(color_write) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 136

def set_color_write(color_write)
  if @current_color_write != color_write
    gl_color_write = color_write ? GL_TRUE : GL_FALSE
    glColorMask(gl_color_write, gl_color_write, gl_color_write, gl_color_write)
    @current_color_write = color_write
  end
end

#set_depth_test(depth_test) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 117

def set_depth_test(depth_test)
  if @current_depth_test != depth_test
    if depth_test
      glEnable(GL_DEPTH_TEST)
    else
      glDisable(GL_DEPTH_TEST)
    end

    @current_depth_test = depth_test
  end
end

#set_depth_write(depth_write) ⇒ Object



129
130
131
132
133
134
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 129

def set_depth_write(depth_write)
  if @current_depth_write != depth_write
    glDepthMask(depth_write ? GL_TRUE : GL_FALSE)
    @current_depth_write = depth_write
  end
end

#set_double_sided(double_sided) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 144

def set_double_sided(double_sided)
  if @current_double_sided != double_sided
    if double_sided
      glDisable(GL_CULL_FACE)
    else
      glEnable(GL_CULL_FACE)
    end

    @current_double_sided = double_sided
  end
end

#set_flip_sided(flip_sided) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 156

def set_flip_sided(flip_sided)
  if @current_flip_sided != flip_sided
    if flip_sided
      glFrontFace(GL_CW)
    else
      glFrontFace(GL_CCW)
    end

    @current_flip_sided = flip_sided
  end
end

#set_line_width(width) ⇒ Object



168
169
170
171
172
173
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 168

def set_line_width(width)
  if width != @current_line_width
    glLineWidth(width)
    @current_line_width = width
  end
end

#set_polygon_offset(polygon_offset, factor, units) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mittsu/renderers/opengl/opengl_state.rb', line 175

def set_polygon_offset(polygon_offset, factor, units)
  if @current_polygon_offset != polygon_offset
    if polygon_offset
      glEnable(GL_POLYGON_OFFSET_FILL)
    else
      glDisable(GL_POLYGON_OFFSET_FILL)
    end

    @current_polygon_offset = polygon_offset
  end

  if polygon_offset && (@current_polygon_offset_factor != factor || @current_polygon_offset_units != units)
    glPolygonOffset(factor, units)

    @current_polygon_offset_factor = factor
    @current_polygon_offset_units = units
  end
end