Class: Mittsu::OpenGLCubeTexture

Inherits:
OpenGLTexture show all
Defined in:
lib/mittsu/renderers/opengl/textures/opengl_cube_texture.rb

Instance Attribute Summary

Attributes inherited from OpenGLTexture

#opengl_texture

Instance Method Summary collapse

Methods inherited from OpenGLTexture

#initialize, #update

Constructor Details

This class inherits a constructor from Mittsu::OpenGLTexture

Instance Method Details

#set(slot) ⇒ Object



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mittsu/renderers/opengl/textures/opengl_cube_texture.rb', line 3

def set(slot)
  if @texture.image.length == 6
    if @texture.needs_update?
      if !@texture.image[:_opengl_texture_cube]
        @texture.add_event_listener(:dispose, @renderer.method(:on_texture_dispose))
        @texture.image[:_opengl_texture_cube] = glCreateTexture
        @renderer.info[:memory][:textures] += 1
      end

      glActiveTexture(GL_TEXTURE0 + slot)
      glBindTexture(GL_TEXTURE_CUBE_MAP, @texture.image[:_opengl_texture_cube])

      # glPixelStorei(GL_UNPACK_FLIP_Y_WEBGL, texture.flip_y)

      is_compressed = @texture.is_a?(CompressedTexture)
      is_data_texture = @texture.image[0].is_a?(DataTexture)

      cube_image = [];

      6.times do |i|
        if @auto_scale_cubemaps && !is_compressed && !is_data_texture
          cube_image[i] = clamp_to_max_size(@texture.image[i], @_max_cubemap_size)
        else
          cube_image[i] = is_data_texture ? @texture.image[i].image : @texture.image[i];
        end
      end

      image = cube_image[0]
      is_image_power_of_two = Math.power_of_two?(image.width) && Math.power_of_two?(image.height)
      gl_format = GL_MITTSU_PARAMS[@texture.format]
      gl_type = GL_MITTSU_PARAMS[@texture.type]

      set_parameters(GL_TEXTURE_CUBE_MAP, is_image_power_of_two)

      6.times do |i|
        if !is_compressed
          if is_data_texture
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl_format, cube_image[i].width, cube_image[i].height, 0, gl_format, gl_type, cube_image[i].data)
          else
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl_format, cube_image[i].width, cube_image[i].height, 0, gl_format, gl_type, cube_image[i].data)
          end
        else
          mipmaps = cube_image[i].mipmaps

          mipmaps.each_with_index do |mipmap, j|
            if @texture.format != RGBAFormat && @texture.format != RGBFormat
              if @renderer.compressed_texture_formats.include?(gl_format)
                glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, j, gl_format, mipmap.width, mipmap.height, 0, mipmap.data)
              else
                puts "WARNING: Mittsu::OpenGLCubeTexture: Attempt to load unsupported compressed texture format in #set"
              end
            else
              glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, j, gl_format, mipmap.width, mipmap.height, 0, gl_format, gl_type, mipmap.data)
            end
          end
        end
      end

      if @texture.generate_mipmaps && is_image_power_of_two
        glGenerateMipmap(GL_TEXTURE_CUBE_MAP)
      end

      @texture.needs_update = false

      @texture.on_update.call if @texture.on_update
    else
      glActiveTexture(GL_TEXTURE0 + slot)
      glBindTexture(GL_TEXTURE_CUBE_MAP, @texture.image[:_opengl_texture_cube])
    end
  end
end