Class: Mittsu::OpenGLRenderTarget

Inherits:
Texture
  • Object
show all
Includes:
EventDispatcher
Defined in:
lib/mittsu/renderers/opengl_render_target.rb

Constant Summary

Constants inherited from Texture

Texture::DEFAULT_IMAGE, Texture::DEFAULT_MAPPING

Instance Attribute Summary collapse

Attributes inherited from Texture

#filp_y, #id, #image, #mapping, #mipmaps, #name, #on_update, #opengl_texture, #premultiply_alpha, #source_file, #unpack_alignment, #uuid

Instance Method Summary collapse

Methods included from EventDispatcher

#add_event_listener, #dispatch_event, #has_event_listener, #remove_event_listener

Methods inherited from Texture

#needs_update=, #set, #update, #update_opengl

Constructor Details

#initialize(width, height, options = {}) ⇒ OpenGLRenderTarget

Returns a new instance of OpenGLRenderTarget.



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
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 25

def initialize(width, height, options = {})
  super(self, nil)

  @width = width
  @height = height

  @wrap_s = options.fetch(:wrap_s, ClampToEdgeWrapping)
  @wrap_t = options.fetch(:wrap_t, ClampToEdgeWrapping)

  @mag_filter = options.fetch(:mag_filter, LinearFilter)
  @min_filter = options.fetch(:min_filter, LinearMipMapLinearFilter)

  @anisotropy = options.fetch(:anisotropy, 1.0);

  @offset = Vector2.new(0.0, 0.0)
  @repeat = Vector2.new(1.0, 1.0)

  @format = options.fetch(:format, RGBAFormat)
  @type = options.fetch(:type, UnsignedByteType)

  @depth_buffer = options.fetch(:depth_buffer, true)
  @stencil_buffer = options.fetch(:stencil_buffer, true)

  @generate_mipmaps = true

  @share_depth_from = options.fetch(:share_depth_from, nil)
end

Instance Attribute Details

#anisotropyObject

Returns the value of attribute anisotropy.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def anisotropy
  @anisotropy
end

#depth_bufferObject

Returns the value of attribute depth_buffer.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def depth_buffer
  @depth_buffer
end

#formatObject

Returns the value of attribute format.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def format
  @format
end

#framebufferObject (readonly)

Returns the value of attribute framebuffer.



21
22
23
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 21

def framebuffer
  @framebuffer
end

#generate_mipmapsObject

Returns the value of attribute generate_mipmaps.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def generate_mipmaps
  @generate_mipmaps
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def height
  @height
end

#mag_filterObject

Returns the value of attribute mag_filter.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def mag_filter
  @mag_filter
end

#min_filterObject

Returns the value of attribute min_filter.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def min_filter
  @min_filter
end

#offsetObject

Returns the value of attribute offset.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def offset
  @offset
end

#renderer=(value) ⇒ Object (writeonly)

Sets the attribute renderer

Parameters:

  • value

    the value to set the attribute renderer to.



23
24
25
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 23

def renderer=(value)
  @renderer = value
end

#repeatObject

Returns the value of attribute repeat.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def repeat
  @repeat
end

#share_depth_fromObject

Returns the value of attribute share_depth_from.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def share_depth_from
  @share_depth_from
end

#stencil_bufferObject

Returns the value of attribute stencil_buffer.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def stencil_buffer
  @stencil_buffer
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def type
  @type
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def width
  @width
end

#wrap_sObject

Returns the value of attribute wrap_s.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def wrap_s
  @wrap_s
end

#wrap_tObject

Returns the value of attribute wrap_t.



5
6
7
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 5

def wrap_t
  @wrap_t
end

Instance Method Details

#cloneObject



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
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 62

def clone
  OpenGLRenderTarget.new(@width, @height).tap do |tmp|
    tmp.renderer = @renderer

		tmp.wrap_s = @wrap_s
		tmp.wrap_t = @wrap_t

		tmp.mag_filter = @mag_filter
		tmp.min_filter = @min_filter

		tmp.anisotropy = @anisotropy

		tmp.offset.copy(@offset)
		tmp.repeat.copy(@repeat)

		tmp.format = @format
		tmp.type = @type

		tmp.depth_buffer = @depth_buffer
		tmp.stencil_buffer = @stencil_buffer

		tmp.generate_mipmaps = @generate_mipmaps

		tmp.share_depth_from = @share_depth_from
  end
end

#disposeObject



158
159
160
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 158

def dispose
  dispatch_event(type: :dispose)
end

#needs_update?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 58

def needs_update?
  false
end

#set_size(width, height) ⇒ Object



53
54
55
56
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 53

def set_size(width, height)
  @width = width
  @height = height
end

#setup_buffersObject



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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 89

def setup_buffers
  return unless @framebuffer.nil?

  # TODO: when OpenGLRenderTargetCube exists
  is_cube = false # render_target.is_a? OpenGLRenderTargetCube

  @depth_buffer = true if @depth_buffer.nil?
  @stencil_buffer = true if @stencil_buffer.nil?

  add_event_listener(:dispose, @renderer.method(:on_render_target_dispose))

  @opengl_texture = glCreateTexture

  @renderer.info[:memory][:textures] += 1

  # Setup texture, create render and frame buffers

  is_target_power_of_two = Math.power_of_two?(@width) && Math.power_of_two?(@height)
  gl_format = GL_MITTSU_PARAMS[@format]
  gl_type = GL_MITTSU_PARAMS[@type]

  if is_cube
    # TODO
  else
    @framebuffer = glCreateFramebuffer

    if @share_depth_from
      @renderbuffer = share_depth_from.renderbuffer
    else
      @renderbuffer = glCreateRenderbuffer
    end

    glBindTexture(GL_TEXTURE_2D, @opengl_texture)
    set_parameters(GL_TEXTURE_2D, is_target_power_of_two)

    glTexImage2D(GL_TEXTURE_2D, 0, gl_format, @width, @height, 0, gl_format, gl_type, nil)

    setup_framebuffer(GL_TEXTURE_2D)

    if @share_depth_from
      if @depth_buffer && !@stencil_buffer
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, @renderbuffer)
      elsif @depth_buffer && @stencil_buffer
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, @renderbuffer)
      end
    else
      setup_renderbuffer
    end

    glGenerateMipmap(GL_TEXTURE_2D) if is_target_power_of_two
  end

  # Release everything

  if is_cube
    # TODO
  else
    glBindTexture(GL_TEXTURE_2D, 0)
  end

  glBindRenderbuffer(GL_RENDERBUFFER, 0)
  glBindFramebuffer(GL_FRAMEBUFFER, 0)
end

#update_mipmapObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 162

def update_mipmap
  return if !@generate_mipmaps || @min_filter == NearestFilter || @min_filter == LinearFilter
  # TODO: when OpenGLRenderTargetCube exists
# 	glBindTexture(GL_TEXTURE_CUBE_MAP, @opengl_texture)
# 	glGenerateMipmap(GL_TEXTURE_CUBE_MAP)
# 	glBindTexture(GL_TEXTURE_CUBE_MAP, nil)
			glBindTexture(GL_TEXTURE_2D, @opengl_texture)
			glGenerateMipmap(GL_TEXTURE_2D)
			glBindTexture(GL_TEXTURE_2D, nil)
end

#useObject



153
154
155
156
# File 'lib/mittsu/renderers/opengl_render_target.rb', line 153

def use
  glBindFramebuffer(GL_FRAMEBUFFER, @framebuffer)
  glViewport(0, 0, @width, @height)
end