Skip to content

Commit

Permalink
Don't change blending modes unless required by the material (TrenchBr…
Browse files Browse the repository at this point in the history
  • Loading branch information
kduske authored Feb 6, 2020
1 parent 036e43d commit 5c04ce8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
27 changes: 16 additions & 11 deletions common/src/Assets/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace TrenchBroom {
m_format(format),
m_type(type),
m_culling(TextureCulling::CullDefault),
m_blendFunc{false, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_blendFunc{TextureBlendFunc::Enable::UseDefault, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_textureId(0) {
assert(m_width > 0);
assert(m_height > 0);
Expand All @@ -57,7 +57,7 @@ namespace TrenchBroom {
m_format(format),
m_type(type),
m_culling(TextureCulling::CullDefault),
m_blendFunc{false, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_blendFunc{TextureBlendFunc::Enable::UseDefault, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_textureId(0),
m_buffers(std::move(buffers)) {
assert(m_width > 0);
Expand All @@ -83,7 +83,7 @@ namespace TrenchBroom {
m_format(format),
m_type(type),
m_culling(TextureCulling::CullDefault),
m_blendFunc{false, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_blendFunc{TextureBlendFunc::Enable::UseDefault, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
m_textureId(0) {}

Texture::~Texture() {
Expand Down Expand Up @@ -146,13 +146,13 @@ namespace TrenchBroom {
}

void Texture::setBlendFunc(GLenum srcFactor, GLenum destFactor) {
m_blendFunc.enable = true;
m_blendFunc.enable = TextureBlendFunc::Enable::UseFactors;
m_blendFunc.srcFactor = srcFactor;
m_blendFunc.destFactor = destFactor;
}

void Texture::disableBlend() {
m_blendFunc.enable = false;
m_blendFunc.enable = TextureBlendFunc::Enable::DisableBlend;
}

size_t Texture::usageCount() const {
Expand Down Expand Up @@ -269,18 +269,23 @@ namespace TrenchBroom {
}


glAssert(glPushAttrib(GL_COLOR_BUFFER_BIT));
if (m_blendFunc.enable) {
glAssert(glBlendFunc(m_blendFunc.srcFactor, m_blendFunc.destFactor));
} else {
glAssert(glDisable(GL_BLEND));
if (m_blendFunc.enable != TextureBlendFunc::Enable::UseDefault) {
glAssert(glPushAttrib(GL_COLOR_BUFFER_BIT));
if (m_blendFunc.enable == TextureBlendFunc::Enable::UseFactors) {
glAssert(glBlendFunc(m_blendFunc.srcFactor, m_blendFunc.destFactor));
} else {
assert(m_blendFunc.enable == TextureBlendFunc::Enable::DisableBlend);
glAssert(glDisable(GL_BLEND));
}
}
}
}

void Texture::deactivate() const {
if (isPrepared()) {
glAssert(glPopAttrib());
if (m_blendFunc.enable != TextureBlendFunc::Enable::UseDefault) {
glAssert(glPopAttrib());
}

switch (m_culling) {
case Assets::TextureCulling::CullNone:
Expand Down
17 changes: 16 additions & 1 deletion common/src/Assets/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,22 @@ namespace TrenchBroom {
};

struct TextureBlendFunc {
bool enable;
enum class Enable {
/**
* Don't change GL_BLEND and don't change the blend function.
*/
UseDefault,
/**
* Don't change GL_BLEND, but set the blend function.
*/
UseFactors,
/**
* Set GL_BLEND to off.
*/
DisableBlend
};

Enable enable;
GLenum srcFactor;
GLenum destFactor;
};
Expand Down

0 comments on commit 5c04ce8

Please sign in to comment.