Class: GL::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/opengl-aux/texture.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = nil, name = nil) {|_self| ... } ⇒ Texture

Returns a new instance of Texture

Yields:

  • (_self)

Yield Parameters:

  • _self (GL::Texture)

    the object that the method was called on


74
75
76
77
78
# File 'lib/opengl-aux/texture.rb', line 74

def initialize(target = nil, name = nil)
  @name = name || 0
  @target = target
  yield self if block_given?
end

Class Attribute Details

.__texture_target_getters__Object

Returns the value of attribute texture_target_getters


18
19
20
# File 'lib/opengl-aux/texture.rb', line 18

def __texture_target_getters__
  @__texture_target_getters__
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


71
72
73
# File 'lib/opengl-aux/texture.rb', line 71

def name
  @name
end

#targetObject

Returns the value of attribute target


72
73
74
# File 'lib/opengl-aux/texture.rb', line 72

def target
  @target
end

Class Method Details

.current_binding(target) ⇒ Object


24
25
26
# File 'lib/opengl-aux/texture.rb', line 24

def current_binding(target)
  GL.glGetInteger(__texture_target_getters__[target])
end

.current_unitObject


20
21
22
# File 'lib/opengl-aux/texture.rb', line 20

def current_unit
  GL.glGetInteger(GL::GL_ACTIVE_TEXTURE)
end

.preserve_binding(target) ⇒ Object

Implied preserve_unit


38
39
40
41
42
43
44
45
46
47
# File 'lib/opengl-aux/texture.rb', line 38

def preserve_binding(target)
  preserve_unit do
    binding = current_binding(target)
    begin
      yield
    ensure
      GL.glBindTexture(target, binding)
    end
  end
end

.preserve_unitObject


28
29
30
31
32
33
34
35
# File 'lib/opengl-aux/texture.rb', line 28

def preserve_unit
  unit = current_unit
  begin
    yield
  ensure
    GL.glActiveTexture(unit)
  end
end

Instance Method Details

#bind(target = nil) ⇒ Object

Raises:

  • (ArgumentError)

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/opengl-aux/texture.rb', line 91

def bind(target = nil)
  # You may think this is insane, but I assure you, it isn't.
  target ||= (@target || GL::GL_TEXTURE_2D)
  @target ||= target
  raise ArgumentError, "No target given and no previous target" unless target

  if block_given?
    self.class.preserve_binding(target) do
      bind(target)
      yield self
    end
  else
    if @name == 0
      @name ||= GL.glGenTextures(1)
      if @name == 0
        raise GL::GLCreateError, "Unable to allocate texture object"
      end
    end

    GL.glBindTexture(target, @name)
    self
  end
end

#deleteObject


84
85
86
87
88
89
# File 'lib/opengl-aux/texture.rb', line 84

def delete
  if @name != 0
    GL.glDeleteTextures(@name)
    @name = 0
  end
end

#is_texture?Boolean


80
81
82
# File 'lib/opengl-aux/texture.rb', line 80

def is_texture?
  GL::glIsTexture(@name) != GL::GL_FALSE
end