Skip to content

Commit

Permalink
GRAPHICS: Fix transparency blending
Browse files Browse the repository at this point in the history
Depth writing needs to be disabled for transparent materials.

The material now has a transparency hint member, ideally
read from the model. If the model does not provide a hint
the transparency is inferred from the textures and diffuse
color. Since Aurora textures compressed with DXT1 are always
opaque, we override the default Ogre claim that DXT1 textures
have an alpha channel.
  • Loading branch information
DrMcCoy committed Jan 29, 2014
1 parent 686b3b8 commit ec895ad
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/graphics/aurora/model_nwn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ void Model_NWN::readBinaryMesh(ParserContext &ctx) {
if (!render)
ctx.nodeEntity->dontRender = true;

bool transparencyHint;
transparencyHint = ctx.mdl->readUint32LE() == 1;
bool transparencyHint = ctx.mdl->readUint32LE() == 1;
ctx.material.transparency = transparencyHint ? kTransparencyHintTransparent : kTransparencyHintOpaque;

ctx.mdl->skip(4); // Unknown

Expand Down Expand Up @@ -978,6 +978,8 @@ void Model_NWN::loadASCIINode(ParserContext &ctx, Ogre::SceneNode *parent,
} else if (line[0] == "transparencyhint") {
bool transparencyHint;
line[1].parse(transparencyHint);

ctx.material.transparency = transparencyHint ? kTransparencyHintTransparent : kTransparencyHintOpaque;
} else if (line[0] == "danglymesh") {
line[1].parse(dangly);
} else if (line[0] == "constraints") {
Expand Down
25 changes: 22 additions & 3 deletions src/graphics/materialman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ void MaterialDeclaration::reset() {
writeColor = true;
writeDepth = true;

transparency = kTransparencyHintUnknown;

textures.clear();
}

Expand Down Expand Up @@ -157,16 +159,33 @@ void MaterialManager::create(const MaterialDeclaration &decl, Ogre::MaterialPtr
texState->setTexture(texture);
texState->setTextureAddressingMode(Ogre::TextureUnitState::TAM_WRAP);

if (!texture->hasAlpha())
// DXT1 textures used in Aurora games are always opaque
if (!texture->hasAlpha() || ((PixelFormat)texture->getFormat() == kPixelFormatDXT1))
transparent = false;

} else
texState->setBlank();
}

material->getTechnique(0)->getPass(0)->setSceneBlending(transparent ? Ogre::SBT_TRANSPARENT_ALPHA : Ogre::SBT_REPLACE);
// Even if the textures themselves aren't tranparent, the color might still be
if (decl.diffuse[3] != 1.0)
transparent = true;

// Figure out whether this material is transparent.
// If we don't get a hint from the declaration, try to infer
// it from the texture and color information gathered above.
TransparencyHint transparency = decl.transparency;
if (transparency == kTransparencyHintUnknown)
transparency = transparent ? kTransparencyHintTransparent : kTransparencyHintOpaque;

if (transparency == kTransparencyHintTransparent) {
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
} else {
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_REPLACE);
material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(decl.writeDepth);
}

material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(decl.writeDepth);
material->getTechnique(0)->getPass(0)->setColourWriteEnabled(decl.writeColor);

material->setReceiveShadows(decl.receiveShadows);
Expand Down
4 changes: 4 additions & 0 deletions src/graphics/materialman.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "common/singleton.h"
#include "common/ustring.h"

#include "graphics/types.h"

namespace Graphics {

struct MaterialDeclaration {
Expand All @@ -53,6 +55,8 @@ struct MaterialDeclaration {
bool writeColor;
bool writeDepth;

TransparencyHint transparency;

MaterialDeclaration();

void reset();
Expand Down
7 changes: 7 additions & 0 deletions src/graphics/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@

namespace Graphics {

/** Hinting at a materials transparency. */
enum TransparencyHint {
kTransparencyHintOpaque = 0, ///< This material is opaque.
kTransparencyHintTransparent = 1, ///< This material is transparent.
kTransparencyHintUnknown = 2 ///< Unknown whether the material is transparent.
};

enum PixelFormat {
kPixelFormatNone = Ogre::PF_UNKNOWN,

Expand Down

0 comments on commit ec895ad

Please sign in to comment.