Skip to content

Commit

Permalink
Add color information to item meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
juhdanad authored and sofar committed Apr 9, 2017
1 parent 0e31c47 commit ee36b87
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/itemdef.cpp
Expand Up @@ -232,12 +232,12 @@ class CItemDefManager: public IWritableItemDefManager
struct ClientCached
{
video::ITexture *inventory_texture;
scene::IMesh *wield_mesh;
ItemMesh wield_mesh;
Palette *palette;

ClientCached():
inventory_texture(NULL),
wield_mesh(NULL),
wield_mesh(),
palette(NULL)
{}
};
Expand All @@ -260,8 +260,8 @@ class CItemDefManager: public IWritableItemDefManager
i = values.begin(); i != values.end(); ++i)
{
ClientCached *cc = *i;
if (cc->wield_mesh)
cc->wield_mesh->drop();
if (cc->wield_mesh.mesh)
cc->wield_mesh.mesh->drop();
delete cc;
}

Expand Down Expand Up @@ -345,8 +345,8 @@ class CItemDefManager: public IWritableItemDefManager
ItemStack item = ItemStack();
item.name = def.name;

scene::IMesh *mesh = getItemMesh(client, item);
cc->wield_mesh = mesh;
getItemMesh(client, item, &(cc->wield_mesh));

cc->palette = tsrc->getPalette(def.palette_image);

// Put in cache
Expand Down Expand Up @@ -401,15 +401,15 @@ class CItemDefManager: public IWritableItemDefManager
return cc->inventory_texture;
}
// Get item wield mesh
virtual scene::IMesh* getWieldMesh(const std::string &name,
virtual ItemMesh* getWieldMesh(const std::string &name,
Client *client) const
{
ClientCached *cc = getClientCached(name, client);
if(!cc)
return NULL;
return cc->wield_mesh;
return &(cc->wield_mesh);
}

// Get item palette
virtual Palette* getPalette(const std::string &name,
Client *client) const
Expand Down
5 changes: 3 additions & 2 deletions src/itemdef.h
Expand Up @@ -32,6 +32,7 @@ class Client;
struct ToolCapabilities;
#ifndef SERVER
#include "client/tile.h"
struct ItemMesh;
struct ItemStack;
#endif

Expand Down Expand Up @@ -116,7 +117,7 @@ class IItemDefManager
virtual video::ITexture* getInventoryTexture(const std::string &name,
Client *client) const=0;
// Get item wield mesh
virtual scene::IMesh* getWieldMesh(const std::string &name,
virtual ItemMesh* getWieldMesh(const std::string &name,
Client *client) const=0;
// Get item palette
virtual Palette* getPalette(const std::string &name,
Expand Down Expand Up @@ -149,7 +150,7 @@ class IWritableItemDefManager : public IItemDefManager
virtual video::ITexture* getInventoryTexture(const std::string &name,
Client *client) const=0;
// Get item wield mesh
virtual scene::IMesh* getWieldMesh(const std::string &name,
virtual ItemMesh* getWieldMesh(const std::string &name,
Client *client) const=0;
#endif

Expand Down
12 changes: 7 additions & 5 deletions src/wieldmesh.cpp
Expand Up @@ -455,7 +455,7 @@ void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
m_meshnode->setVisible(true);
}

scene::IMesh *getItemMesh(Client *client, const ItemStack &item)
void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
{
ITextureSource *tsrc = client->getTextureSource();
IItemDefManager *idef = client->getItemDefManager();
Expand All @@ -475,12 +475,13 @@ scene::IMesh *getItemMesh(Client *client, const ItemStack &item)
// If inventory_image is defined, it overrides everything else
if (def.inventory_image != "") {
mesh = getExtrudedMesh(tsrc, def.inventory_image);
return mesh;
result->mesh = mesh;
result->buffer_colors.push_back(
std::pair<bool, video::SColor>(false, video::SColor(0xFFFFFFFF)));
} else if (def.type == ITEM_NODE) {
if (f.mesh_ptr[0]) {
mesh = cloneMesh(f.mesh_ptr[0]);
scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
setMeshColor(mesh, video::SColor (255, 255, 255, 255));
} else if (f.drawtype == NDT_PLANTLIKE) {
mesh = getExtrudedMesh(tsrc,
tsrc->getTextureName(f.tiles[0].texture_id));
Expand Down Expand Up @@ -515,6 +516,8 @@ scene::IMesh *getItemMesh(Client *client, const ItemStack &item)
for (u32 i = 0; i < mc; ++i) {
const TileSpec *tile = &(f.tiles[i]);
scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
result->buffer_colors.push_back(
std::pair<bool, video::SColor>(tile->has_color, tile->color));
colorizeMeshBuffer(buf, &tile->color);
video::SMaterial &material = buf->getMaterial();
material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
Expand All @@ -532,9 +535,8 @@ scene::IMesh *getItemMesh(Client *client, const ItemStack &item)

rotateMeshXZby(mesh, -45);
rotateMeshYZby(mesh, -30);
return mesh;
result->mesh = mesh;
}
return NULL;
}

scene::IMesh * getExtrudedMesh(ITextureSource *tsrc,
Expand Down
17 changes: 16 additions & 1 deletion src/wieldmesh.h
Expand Up @@ -28,6 +28,21 @@ class Client;
class ITextureSource;
struct TileSpec;

struct ItemMesh {
scene::IMesh* mesh;
/*!
* Stores the color of each mesh buffer.
* If the boolean is true, the color is fixed, else
* palettes can modify it.
*/
std::vector<std::pair<bool, video::SColor> > buffer_colors;

ItemMesh():
mesh(NULL),
buffer_colors()
{}
};

/*
Wield item scene node, renders the wield mesh of some item
*/
Expand Down Expand Up @@ -79,7 +94,7 @@ class WieldMeshSceneNode : public scene::ISceneNode
aabb3f m_bounding_box;
};

scene::IMesh *getItemMesh(Client *client, const ItemStack &item);
void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result);

scene::IMesh *getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename);
#endif

0 comments on commit ee36b87

Please sign in to comment.