Class: Mittsu::OpenGLState

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

Instance Method Summary collapse

Constructor Details

#initializeOpenGLState

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

def initialize
  @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



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

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



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

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

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

#init_attributesObject



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

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

#resetObject



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

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



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

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(GL_MITTSU_PARAMS[blend_equation], GL_MITTSU_PARAMS[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(GL_MITTSU_PARAMS[blend_src], GL_MITTSU_PARAMS[blend_dst], GL_MITTSU_PARAMS[blend_src_alpha], GL_MITTSU_PARAMS[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



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

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



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

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



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

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



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

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



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

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



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

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



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

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