Skip to content

Commit

Permalink
GRAPHICS: Fix creation of solid color materials
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Mar 21, 2014
1 parent 66b01c5 commit 65d0fd0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/graphics/aurora/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Ogre::Entity *Model::createEntity(const VertexDeclaration &vertexDecl, const Ogr
entity->getUserObjectBindings().setUserAny("renderable", Ogre::Any((Renderable *) this));

// Assign the material to the entity
entity->setMaterial(material.isNull() ? MaterialMan.getInvisible() : material);
entity->setMaterial(material.isNull() ? MaterialMan.getSolidColor(0.0, 0.0, 0.0, 0.0) : material);

return entity;
}
Expand Down
33 changes: 18 additions & 15 deletions src/graphics/materialman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "common/ustring.h"
#include "common/error.h"
#include "common/util.h"
#include "common/threads.h"

#include "graphics/textureman.h"
Expand Down Expand Up @@ -108,32 +109,34 @@ Ogre::MaterialPtr MaterialManager::get(const MaterialDeclaration &decl) {
return material;
}

Ogre::MaterialPtr MaterialManager::getInvisible() {
Ogre::MaterialPtr MaterialManager::getSolidColor(float r, float g, float b, float a) {
MaterialDeclaration decl;

decl.receiveShadows = false;
decl.writeColor = false;
decl.writeDepth = false;
decl.diffuse[0] = r; decl.diffuse[1] = g; decl.diffuse[2] = b; decl.diffuse[3] = a;

return get(decl);
}

Ogre::MaterialPtr MaterialManager::getBlack() {
MaterialDeclaration decl;

decl.ambient [0] = 0.0; decl.ambient [1] = 0.0; decl.ambient [2] = 0.0;
decl.diffuse [0] = 0.0; decl.diffuse [1] = 0.0; decl.diffuse [2] = 0.0; decl.diffuse [3] = 1.0;
decl.specular [0] = 0.0; decl.specular [1] = 0.0; decl.specular [2] = 0.0; decl.specular[3] = 1.0;
decl.selfIllum[0] = 0.0; decl.selfIllum[1] = 0.0; decl.selfIllum[2] = 0.0;

decl.shininess = 0.0;
void MaterialManager::createSolidColor(const MaterialDeclaration &decl, Ogre::MaterialPtr material) {
Ogre::TextureUnitState *texState = material->getTechnique(0)->getPass(0)->createTextureUnitState();

decl.receiveShadows = false;
const Ogre::ColourValue color(decl.diffuse[0], decl.diffuse[1], decl.diffuse[2]);
const float alpha = decl.diffuse[3];

return get(decl);
texState->setColourOperationEx(Ogre::LBX_SOURCE1, Ogre::LBS_MANUAL, Ogre::LBS_MANUAL, color, color, 1.0);
if (alpha != 1.0) {
texState->setAlphaOperation(Ogre::LBX_SOURCE1, Ogre::LBS_MANUAL, Ogre::LBS_MANUAL, alpha, alpha, 1.0);
material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
material->getTechnique(0)->getPass(0)->setDepthWriteEnabled(false);
}
}

void MaterialManager::create(const MaterialDeclaration &decl, Ogre::MaterialPtr material) {
if (decl.textures.empty()) {
createSolidColor(decl, material);
return;
}

material->getTechnique(0)->getPass(0)->setAmbient(decl.ambient[0], decl.ambient[1], decl.ambient[2]);
material->getTechnique(0)->getPass(0)->setDiffuse(decl.diffuse[0], decl.diffuse[1], decl.diffuse[2], decl.diffuse[3]);
material->getTechnique(0)->getPass(0)->setSpecular(decl.specular[0], decl.specular[1], decl.specular[2], decl.specular[3]);
Expand Down
7 changes: 3 additions & 4 deletions src/graphics/materialman.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ class MaterialManager : public Common::Singleton<MaterialManager> {
/** Get/Load a more complex material. */
Ogre::MaterialPtr get(const MaterialDeclaration &decl);

/** Get a default invisible material. */
Ogre::MaterialPtr getInvisible();
/** Get a default black material. */
Ogre::MaterialPtr getBlack();
/** Get a default material with a solid color. */
Ogre::MaterialPtr getSolidColor(float r, float g, float b, float a = 1.0);

private:
void create(const MaterialDeclaration &decl, Ogre::MaterialPtr material);
void createSolidColor(const MaterialDeclaration &decl, Ogre::MaterialPtr material);

Common::UString canonicalName(const MaterialDeclaration &decl);
};
Expand Down
29 changes: 0 additions & 29 deletions src/graphics/textureman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,6 @@ Ogre::TexturePtr TextureManager::get(const Common::UString &name) {
return texture;
}

Ogre::TexturePtr TextureManager::getInvisible() {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<Ogre::TexturePtr> functor(boost::bind(&TextureManager::getInvisible, this));

return RequestMan.callInMainThread(functor);
}

Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName("xoreos/invisible");
if (!texture.isNull())
return texture;

return createInvisible();
}

ImageDecoder *TextureManager::createImage(const Common::UString &name) {
// Get the image resource
::Aurora::FileType type;
Expand Down Expand Up @@ -353,21 +339,6 @@ void TextureManager::convert(Ogre::TexturePtr &texture, const ImageDecoder &imag
}
}

Ogre::TexturePtr TextureManager::createInvisible() {
Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().createManual("xoreos/invisible", "General",
Ogre::TEX_TYPE_2D, 1, 1, 1, Ogre::PF_BYTE_BGRA, Ogre::TU_STATIC_WRITE_ONLY);

Ogre::HardwarePixelBufferSharedPtr buffer = texture->getBuffer(0, 0);
buffer->lock(Ogre::HardwareBuffer::HBL_WRITE_ONLY);
const Ogre::PixelBox &pb = buffer->getCurrentLock();

memset(pb.data, 0, Ogre::PixelUtil::getMemorySize(pb.getWidth(), pb.getHeight(), pb.getDepth(), pb.format));

buffer->unlock();

return texture;
}

bool TextureManager::dumpTGA(const Common::UString &name, const Common::UString &fileName) {
ImageDecoder *image = 0;

Expand Down
4 changes: 0 additions & 4 deletions src/graphics/textureman.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ class TextureManager : public Common::Singleton<TextureManager> {
/** Get/Load a texture. */
Ogre::TexturePtr get(const Common::UString &name);

Ogre::TexturePtr getInvisible();

/** Get the properties of a texture. */
const TextureProperties &getProperties(const Common::UString &name);
/** Get the properties of a texture. */
Expand All @@ -101,8 +99,6 @@ class TextureManager : public Common::Singleton<TextureManager> {
Ogre::TexturePtr create(const Common::UString &name);
Ogre::TexturePtr create(const Common::UString &name, const ImageDecoder &image);

Ogre::TexturePtr createInvisible();

ImageDecoder *createImage(const Common::UString &name);

void convert(Ogre::TexturePtr &texture, const ImageDecoder &image, int mipMaps);
Expand Down

0 comments on commit 65d0fd0

Please sign in to comment.