Class: RBGLox::Texture

Inherits:
Resource show all
Includes:
Gl, Glfw
Defined in:
lib/rbglox/texture.rb

Overview

Texture Class

Instance Attribute Summary collapse

Attributes inherited from Resource

#id

Instance Method Summary collapse

Constructor Details

#initialize(filename, mipmaps = false) ⇒ Texture

Constructor



9
10
11
12
13
14
15
# File 'lib/rbglox/texture.rb', line 9

def initialize(filename, mipmaps=false)
  @filename = filename
  @mipmaps = mipmaps
  @id = glGenTextures(1)[0]

  reload
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/rbglox/texture.rb', line 6

def filename
  @filename
end

#mipmapsObject

Returns the value of attribute mipmaps.



6
7
8
# File 'lib/rbglox/texture.rb', line 6

def mipmaps
  @mipmaps
end

Instance Method Details

#bind(unit = 0) ⇒ Object

Bind the Texture



18
19
20
21
# File 'lib/rbglox/texture.rb', line 18

def bind(unit=0)
  glActiveTexture(GL_TEXTURE0 + unit)
  glBindTexture GL_TEXTURE_2D, id
end

#freeObject

Free the Texture



49
50
51
# File 'lib/rbglox/texture.rb', line 49

def free
  glDeleteTextures [id]
end

#release(unit = 0) ⇒ Object

Release the Texture



24
25
26
27
# File 'lib/rbglox/texture.rb', line 24

def release(unit=0)
  glActiveTexture(GL_TEXTURE0 + unit)
  glBindTexture GL_TEXTURE_2D, 0
end

#reload(unit = 0) ⇒ Object

Reload the Texture



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbglox/texture.rb', line 30

def reload(unit=0)
  bind unit

  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
  glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR

  if mipmaps
    glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST
    glfwLoadTexture2D filename, GLFW_BUILD_MIPMAPS_BIT
  else
    glfwLoadTexture2D filename, 0
  end

  release unit
end