Skip to content

Commit

Permalink
Implement texture unit setter and getter
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Dec 4, 2021
1 parent b95b584 commit b3c3934
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ while LibGLFW.window_should_close(window).false?
context.clear(:color)

# bind textures on corresponding texture units
LibGL.active_texture(LibGL::TextureUnit::Texture0)
texture_1.bind(:texture_2d)
LibGL.active_texture(LibGL::TextureUnit::Texture1)
texture_2.bind(:texture_2d)
context.textures.unit = 0
texture_1.bind(texture_2d)
context.textures.unit = 1
texture_2.bind(texture_2d)

# render container
our_shader.use
Expand Down
6 changes: 6 additions & 0 deletions spec/gloop/textures_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ Spectator.describe Gloop::Textures do
expect(binding.target).to eq(Gloop::Texture::Target::Texture2D)
end
end

describe "#unit=" do
it "sets the active texture unit" do
expect { textures.unit = 5 }.to change(&.unit).to(5)
end
end
end

Spectator.describe Gloop::Context do
Expand Down
24 changes: 23 additions & 1 deletion src/gloop/textures.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "./contextual"
require "./parameters"
require "./texture/bind_target"
require "./texture/target"
require "./contextual"

module Gloop
# Reference to all texture binding targets for a context.
struct Textures
include Contextual
include Parameters

# Defines a method that returns a texture binding target for the specified target.
#
Expand Down Expand Up @@ -59,6 +61,26 @@ module Gloop
def [](target : Symbol) : Texture::BindTarget
self[Texture::Target.new(target)]
end

# Retrieves the active texture unit.
#
# - OpenGL function: `glGetIntegerv`
# - OpenGL enum: `GL_ACTIVE_TEXTURE`
# - OpenGL version: 2.0
@[GLFunction("glGetIntegerv", enum: "GL_ACTIVE_TEXTURE", version: "2.0")]
parameter(ActiveTexture, unit) do |value|
value - LibGL::TextureUnit::Texture0.value
end

# Sets the active texture unit.
#
# - OpenGL function: `glActiveTexture`
# - OpenGL version: 2.0
@[GLFunction("glActiveTexture", version: "2.0")]
def unit=(unit)
value = LibGL::TextureUnit::Texture0.value + unit
gl.active_texture(LibGL::TextureUnit.new(value))
end
end

struct Context
Expand Down

0 comments on commit b3c3934

Please sign in to comment.