Class: Bestguigui::Render3D::Texture

Inherits:
Object
  • Object
show all
Includes:
OpenGL
Defined in:
lib/bestguigui/render_3d/texture.rb

Overview

Charge une image (chemin ou Gosu::Image déjà chargée) comme texture OpenGL indépendante — utilisé pour les tuiles de sol/murs, les matériaux de modèles .obj, et les sprites billboard.

floor = Bestguigui::Render3D::Texture.new("gfx/tileset.png")
floor.bind # avant un glBegin(GL_QUADS) qui échantillonne cette texture

tiles = Bestguigui::Render3D::Texture.load_tiles("gfx/tileset.png", 16, 16)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Texture

Returns a new instance of Texture.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bestguigui/render_3d/texture.rb', line 16

def initialize(source)
  gosu_image = source.is_a?(Gosu::Image) ? source : Gosu::Image.new(source, retro: true)
  pixels = gosu_image.to_blob

  name_buf = " " * 4
  glGenTextures(1, name_buf)
  @id = name_buf.unpack("L")[0]

  glBindTexture(GL_TEXTURE_2D, @id)
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gosu_image.width, gosu_image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

  @width = gosu_image.width
  @height = gosu_image.height
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



14
15
16
# File 'lib/bestguigui/render_3d/texture.rb', line 14

def height
  @height
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/bestguigui/render_3d/texture.rb', line 14

def id
  @id
end

#widthObject (readonly)

Returns the value of attribute width.



14
15
16
# File 'lib/bestguigui/render_3d/texture.rb', line 14

def width
  @width
end

Class Method Details

.load_tiles(filename, tile_width, tile_height, retro: true) ⇒ Object



38
39
40
# File 'lib/bestguigui/render_3d/texture.rb', line 38

def self.load_tiles(filename, tile_width, tile_height, retro: true)
  Gosu::Image.load_tiles(filename, tile_width, tile_height, retro: retro).map { |tile| new(tile) }
end

Instance Method Details

#bindObject



33
34
35
36
# File 'lib/bestguigui/render_3d/texture.rb', line 33

def bind
  glActiveTexture(GL_TEXTURE0)
  glBindTexture(GL_TEXTURE_2D, @id)
end