aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornumzero <numzer0@yandex.ru>2020-11-22 16:29:31 +0300
committerlhofhansl <larsh@apache.org>2020-11-26 12:49:10 -0800
commit3077afc0a2039cd4c8d64d9df62ed9b2ba6463dc (patch)
tree1e72b17196b7290632ebe7472029e72b53b049bd
parent89cc5bf53730d72744d45f92fc11dc9ab6c232c9 (diff)
downloadminetest-3077afc0a2039cd4c8d64d9df62ed9b2ba6463dc.tar.gz
minetest-3077afc0a2039cd4c8d64d9df62ed9b2ba6463dc.tar.bz2
minetest-3077afc0a2039cd4c8d64d9df62ed9b2ba6463dc.zip
Store stars in a single static mesh buffer
-rw-r--r--src/client/sky.cpp199
-rw-r--r--src/client/sky.h8
2 files changed, 92 insertions, 115 deletions
diff --git a/src/client/sky.cpp b/src/client/sky.cpp
index 2e0cbca86..29a0545ab 100644
--- a/src/client/sky.cpp
+++ b/src/client/sky.cpp
@@ -1,6 +1,7 @@
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2020 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -34,16 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "config.h"
using namespace irr::core;
-Sky::Sky(s32 id, ITextureSource *tsrc) :
- scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
- RenderingEngine::get_scene_manager(), id)
-{
- setAutomaticCulling(scene::EAC_OFF);
- m_box.MaxEdge.set(0, 0, 0);
- m_box.MinEdge.set(0, 0, 0);
-
- // Create material
-
+static video::SMaterial baseMaterial() {
video::SMaterial mat;
mat.Lighting = false;
#if ENABLE_GLES
@@ -56,14 +48,29 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
mat.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
mat.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
mat.BackfaceCulling = false;
+ return mat;
+};
+
+Sky::Sky(s32 id, ITextureSource *tsrc) :
+ scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
+ RenderingEngine::get_scene_manager(), id)
+{
+ setAutomaticCulling(scene::EAC_OFF);
+ m_box.MaxEdge.set(0, 0, 0);
+ m_box.MinEdge.set(0, 0, 0);
+
+ // Create materials
- m_materials[0] = mat;
+ m_materials[0] = baseMaterial();
+ m_materials[0].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ m_materials[0].Lighting = true;
+ m_materials[0].ColorMaterial = video::ECM_NONE;
- m_materials[1] = mat;
+ m_materials[1] = baseMaterial();
//m_materials[1].MaterialType = video::EMT_TRANSPARENT_VERTEX_ALPHA;
m_materials[1].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
- m_materials[2] = mat;
+ m_materials[2] = baseMaterial();
m_materials[2].setTexture(0, tsrc->getTextureForMesh("sunrisebg.png"));
m_materials[2].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
//m_materials[2].MaterialType = video::EMT_TRANSPARENT_ADD_COLOR;
@@ -80,7 +87,7 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
tsrc->getTexture(m_moon_params.tonemap) : NULL;
if (m_sun_texture) {
- m_materials[3] = mat;
+ m_materials[3] = baseMaterial();
m_materials[3].setTexture(0, m_sun_texture);
m_materials[3].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
// Disables texture filtering
@@ -92,7 +99,7 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
m_materials[3].Lighting = true;
}
if (m_moon_texture) {
- m_materials[4] = mat;
+ m_materials[4] = baseMaterial();
m_materials[4].setTexture(0, m_moon_texture);
m_materials[4].MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
// Disables texture filtering
@@ -105,7 +112,7 @@ Sky::Sky(s32 id, ITextureSource *tsrc) :
}
for (int i = 5; i < 11; i++) {
- m_materials[i] = mat;
+ m_materials[i] = baseMaterial();
m_materials[i].Lighting = true;
m_materials[i].MaterialType = video::EMT_SOLID;
}
@@ -673,13 +680,12 @@ void Sky::draw_moon(video::IVideoDriver *driver, float moonsize, const video::SC
c = video::SColor(255, 255, 255, 255);
draw_sky_body(vertices, -d, d, c);
place_sky_body(vertices, -90, wicked_time_of_day * 360 - 90);
- driver->drawIndexedTriangleFan(&vertices[0], 4, indices, 2);
+ driver->drawIndexedTriangleList(&vertices[0], 4, indices, 2);
}
}
void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
{
- driver->setMaterial(m_materials[1]);
// Tune values so that stars first appear just after the sun
// disappears over the horizon, and disappear just before the sun
// appears over the horizon.
@@ -687,87 +693,19 @@ void Sky::draw_stars(video::IVideoDriver * driver, float wicked_time_of_day)
// to time 4000.
float tod = wicked_time_of_day < 0.5f ? wicked_time_of_day : (1.0f - wicked_time_of_day);
- float starbrightness = clamp((0.25f - fabsf(tod)) * 20.0f, 0.0f, 1.0f);
-
- float f = starbrightness;
- float d = (0.006 / 2) * m_star_params.scale;
-
- video::SColor starcolor = m_star_params.starcolor;
- starcolor.setAlpha(f * m_star_params.starcolor.getAlpha());
-
- // Stars are only drawn when not fully transparent
- if (m_star_params.starcolor.getAlpha() < 1)
+ float starbrightness = (0.25f - fabsf(tod)) * 20.0f;
+ int alpha = clamp<int>(starbrightness * m_star_params.starcolor.getAlpha(), 0, 255);
+ if (!alpha) // Stars are only drawn when not fully transparent
return;
-#if ENABLE_GLES
- u16 *indices = new u16[m_star_params.count * 3];
- video::S3DVertex *vertices =
- new video::S3DVertex[m_star_params.count * 3];
- for (u32 i = 0; i < m_star_params.count; i++) {
- indices[i * 3 + 0] = i * 3 + 0;
- indices[i * 3 + 1] = i * 3 + 1;
- indices[i * 3 + 2] = i * 3 + 2;
- v3f r = m_stars[i];
- core::CMatrix4<f32> a;
- a.buildRotateFromTo(v3f(0, 1, 0), r);
- v3f p = v3f(-d, 1, -d);
- v3f p1 = v3f(d, 1, 0);
- v3f p2 = v3f(-d, 1, d);
- a.rotateVect(p);
- a.rotateVect(p1);
- a.rotateVect(p2);
- p.rotateXYBy(wicked_time_of_day * 360 - 90);
- p1.rotateXYBy(wicked_time_of_day * 360 - 90);
- p2.rotateXYBy(wicked_time_of_day * 360 - 90);
- vertices[i * 3 + 0].Pos = p;
- vertices[i * 3 + 0].Color = starcolor;
- vertices[i * 3 + 1].Pos = p1;
- vertices[i * 3 + 1].Color = starcolor;
- vertices[i * 3 + 2].Pos = p2;
- vertices[i * 3 + 2].Color = starcolor;
- }
- driver->drawIndexedTriangleList(vertices, m_star_params.count * 3,
- indices, m_star_params.count);
- delete[] indices;
- delete[] vertices;
-#else
- u16 *indices = new u16[m_star_params.count * 4];
- video::S3DVertex *vertices =
- new video::S3DVertex[m_star_params.count * 4];
- for (u32 i = 0; i < m_star_params.count; i++) {
- indices[i * 4 + 0] = i * 4 + 0;
- indices[i * 4 + 1] = i * 4 + 1;
- indices[i * 4 + 2] = i * 4 + 2;
- indices[i * 4 + 3] = i * 4 + 3;
- v3f r = m_stars[i];
- core::CMatrix4<f32> a;
- a.buildRotateFromTo(v3f(0, 1, 0), r);
- v3f p = v3f(-d, 1, -d);
- v3f p1 = v3f(d, 1, -d);
- v3f p2 = v3f(d, 1, d);
- v3f p3 = v3f(-d, 1, d);
- a.rotateVect(p);
- a.rotateVect(p1);
- a.rotateVect(p2);
- a.rotateVect(p3);
- p.rotateXYBy(wicked_time_of_day * 360 - 90);
- p1.rotateXYBy(wicked_time_of_day * 360 - 90);
- p2.rotateXYBy(wicked_time_of_day * 360 - 90);
- p3.rotateXYBy(wicked_time_of_day * 360 - 90);
- vertices[i * 4 + 0].Pos = p;
- vertices[i * 4 + 0].Color = starcolor;
- vertices[i * 4 + 1].Pos = p1;
- vertices[i * 4 + 1].Color = starcolor;
- vertices[i * 4 + 2].Pos = p2;
- vertices[i * 4 + 2].Color = starcolor;
- vertices[i * 4 + 3].Pos = p3;
- vertices[i * 4 + 3].Color = starcolor;
- }
- driver->drawVertexPrimitiveList(vertices, m_star_params.count * 4,
- indices, m_star_params.count, video::EVT_STANDARD,
- scene::EPT_QUADS, video::EIT_16BIT);
- delete[] indices;
- delete[] vertices;
-#endif
+
+ m_materials[0].DiffuseColor = video::SColor(alpha, 0, 0, 0);
+ m_materials[0].EmissiveColor = m_star_params.starcolor;
+ auto sky_rotation = core::matrix4().setRotationAxisRadians(2.0f * M_PI * (wicked_time_of_day - 0.25f), v3f(0.0f, 0.0f, 1.0f));
+ auto world_matrix = driver->getTransform(video::ETS_WORLD);
+ driver->setTransform(video::ETS_WORLD, world_matrix * sky_rotation);
+ driver->setMaterial(m_materials[0]);
+ driver->drawMeshBuffer(m_stars.get());
+ driver->setTransform(video::ETS_WORLD, world_matrix);
}
void Sky::draw_sky_body(std::array<video::S3DVertex, 4> &vertices, float pos_1, float pos_2, const video::SColor &c)
@@ -822,7 +760,7 @@ void Sky::setSunTexture(std::string sun_texture,
m_sun_texture = tsrc->getTextureForMesh(m_sun_params.texture);
if (m_sun_texture) {
- m_materials[3] = m_materials[0];
+ m_materials[3] = baseMaterial();
m_materials[3].setTexture(0, m_sun_texture);
m_materials[3].MaterialType = video::
EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -870,7 +808,7 @@ void Sky::setMoonTexture(std::string moon_texture,
m_moon_texture = tsrc->getTextureForMesh(m_moon_params.texture);
if (m_moon_texture) {
- m_materials[4] = m_materials[0];
+ m_materials[4] = baseMaterial();
m_materials[4].setTexture(0, m_moon_texture);
m_materials[4].MaterialType = video::
EMT_TRANSPARENT_ALPHA_CHANNEL;
@@ -892,19 +830,54 @@ void Sky::setStarCount(u16 star_count, bool force_update)
// Allow force updating star count at game init.
if (m_star_params.count != star_count || force_update) {
m_star_params.count = star_count;
- m_stars.clear();
- // Rebuild the stars surrounding the camera
- for (u16 i = 0; i < star_count; i++) {
- v3f star = v3f(
- myrand_range(-10000, 10000),
- myrand_range(-10000, 10000),
- myrand_range(-10000, 10000)
- );
-
- star.normalize();
- m_stars.emplace_back(star);
- }
+ updateStars();
+ }
+}
+
+void Sky::updateStars() {
+ m_stars.reset(new scene::SMeshBuffer());
+ // Stupid IrrLicht doesn’t allow non-indexed rendering, and indexed quad
+ // rendering is slow due to lack of hardware support. So as indices are
+ // 16-bit and there are 4 vertices per star... the limit is 2^16/4 = 0x4000.
+ // That should be well enough actually.
+ if (m_star_params.count > 0x4000) {
+ warningstream << "Requested " << m_star_params.count << " stars but " << 0x4000 << " is the max\n";
+ m_star_params.count = 0x4000;
+ }
+ m_stars->Vertices.reallocate(4 * m_star_params.count);
+ m_stars->Indices.reallocate(6 * m_star_params.count);
+
+ float d = (0.006 / 2) * m_star_params.scale;
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ v3f r = v3f(
+ myrand_range(-10000, 10000),
+ myrand_range(-10000, 10000),
+ myrand_range(-10000, 10000)
+ );
+ core::CMatrix4<f32> a;
+ a.buildRotateFromTo(v3f(0, 1, 0), r);
+ v3f p = v3f(-d, 1, -d);
+ v3f p1 = v3f(d, 1, -d);
+ v3f p2 = v3f(d, 1, d);
+ v3f p3 = v3f(-d, 1, d);
+ a.rotateVect(p);
+ a.rotateVect(p1);
+ a.rotateVect(p2);
+ a.rotateVect(p3);
+ m_stars->Vertices.push_back(video::S3DVertex(p, {}, {}, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p1, {}, {}, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p2, {}, {}, {}));
+ m_stars->Vertices.push_back(video::S3DVertex(p3, {}, {}, {}));
+ }
+ for (u16 i = 0; i < m_star_params.count; i++) {
+ m_stars->Indices.push_back(i * 4 + 0);
+ m_stars->Indices.push_back(i * 4 + 1);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 2);
+ m_stars->Indices.push_back(i * 4 + 3);
+ m_stars->Indices.push_back(i * 4 + 0);
}
+ m_stars->setHardwareMappingHint(scene::EHM_STATIC);
}
void Sky::setSkyColors(const SkyColor &sky_color)
@@ -936,7 +909,7 @@ void Sky::addTextureToSkybox(std::string texture, int material_id,
// Keep a list of texture names handy.
m_sky_params.textures.emplace_back(texture);
video::ITexture *result = tsrc->getTextureForMesh(texture);
- m_materials[material_id+5] = m_materials[0];
+ m_materials[material_id+5] = baseMaterial();
m_materials[material_id+5].setTexture(0, result);
m_materials[material_id+5].MaterialType = video::EMT_SOLID;
}
diff --git a/src/client/sky.h b/src/client/sky.h
index 3227e8f59..176545015 100644
--- a/src/client/sky.h
+++ b/src/client/sky.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <array>
#include "camera.h"
#include "irrlichttypes_extrabloated.h"
+#include "irr_ptr.h"
#include "skyparams.h"
#pragma once
@@ -77,7 +78,7 @@ public:
void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
void setStarCount(u16 star_count, bool force_update);
void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
- void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; }
+ void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }
@@ -178,13 +179,16 @@ private:
bool m_default_tint = true;
- std::vector<v3f> m_stars;
+ irr_ptr<scene::SMeshBuffer> m_stars;
video::ITexture *m_sun_texture;
video::ITexture *m_moon_texture;
video::ITexture *m_sun_tonemap;
video::ITexture *m_moon_tonemap;
+ void updateStars();
+ void updateStarsColor(video::SColor color);
+
void draw_sun(video::IVideoDriver *driver, float sunsize, const video::SColor &suncolor,
const video::SColor &suncolor2, float wicked_time_of_day);
void draw_moon(video::IVideoDriver *driver, float moonsize, const video::SColor &mooncolor,
pt">, 1,0), video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1), video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1), // z+ video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0), video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1), video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1), video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } f32 pixelsize_x = 1 / (f32) resolution_x; f32 pixelsize_y = 1 / (f32) resolution_y; for (int i = 0; i < resolution_x; ++i) { f32 pixelpos_x = i * pixelsize_x - 0.5; f32 x0 = pixelpos_x; f32 x1 = pixelpos_x + pixelsize_x; f32 tex0 = (i + 0.1) * pixelsize_x; f32 tex1 = (i + 0.9) * pixelsize_x; video::S3DVertex vertices[8] = { // x- video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1), video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1), video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0), video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0), // x+ video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1), video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0), video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0), video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } for (int i = 0; i < resolution_y; ++i) { f32 pixelpos_y = i * pixelsize_y - 0.5; f32 y0 = -pixelpos_y - pixelsize_y; f32 y1 = -pixelpos_y; f32 tex0 = (i + 0.1) * pixelsize_y; f32 tex1 = (i + 0.9) * pixelsize_y; video::S3DVertex vertices[8] = { // y- video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0), video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0), video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1), video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1), // y+ video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0), video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1), video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1), video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } // Create mesh object scene::SMesh *mesh = new scene::SMesh(); mesh->addMeshBuffer(buf); buf->drop(); scaleMesh(mesh, scale); // also recalculates bounding box return mesh; } /* Caches extrusion meshes so that only one of them per resolution is needed. Also caches one cube (for convenience). E.g. there is a single extrusion mesh that is used for all 16x16 px images, another for all 256x256 px images, and so on. WARNING: Not thread safe. This should not be a problem since rendering related classes (such as WieldMeshSceneNode) will be used from the rendering thread only. */ class ExtrusionMeshCache: public IReferenceCounted { public: // Constructor ExtrusionMeshCache() { for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION; resolution <= MAX_EXTRUSION_MESH_RESOLUTION; resolution *= 2) { m_extrusion_meshes[resolution] = createExtrusionMesh(resolution, resolution); } m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0)); } // Destructor virtual ~ExtrusionMeshCache() { for (std::map<int, scene::IMesh*>::iterator it = m_extrusion_meshes.begin(); it != m_extrusion_meshes.end(); ++it) { it->second->drop(); } m_cube->drop(); } // Get closest extrusion mesh for given image dimensions // Caller must drop the returned pointer scene::IMesh* create(core::dimension2d<u32> dim) { // handle non-power of two textures inefficiently without cache if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) { return createExtrusionMesh(dim.Width, dim.Height); } int maxdim = MYMAX(dim.Width, dim.Height); std::map<int, scene::IMesh*>::iterator it = m_extrusion_meshes.lower_bound(maxdim); if (it == m_extrusion_meshes.end()) { // no viable resolution found; use largest one it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION); sanity_check(it != m_extrusion_meshes.end()); } scene::IMesh *mesh = it->second; mesh->grab(); return mesh; } // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face // Caller must drop the returned pointer scene::IMesh* createCube() { m_cube->grab(); return m_cube; } private: std::map<int, scene::IMesh*> m_extrusion_meshes; scene::IMesh *m_cube; }; ExtrusionMeshCache *g_extrusion_mesh_cache = NULL; WieldMeshSceneNode::WieldMeshSceneNode( scene::ISceneNode *parent, scene::ISceneManager *mgr, s32 id, bool lighting ): scene::ISceneNode(parent, mgr, id), m_meshnode(NULL), m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF), m_lighting(lighting), m_bounding_box(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) { m_enable_shaders = g_settings->getBool("enable_shaders"); m_anisotropic_filter = g_settings->getBool("anisotropic_filter"); m_bilinear_filter = g_settings->getBool("bilinear_filter"); m_trilinear_filter = g_settings->getBool("trilinear_filter"); // If this is the first wield mesh scene node, create a cache // for extrusion meshes (and a cube mesh), otherwise reuse it if (g_extrusion_mesh_cache == NULL) g_extrusion_mesh_cache = new ExtrusionMeshCache(); else g_extrusion_mesh_cache->grab(); // Disable bounding box culling for this scene node // since we won't calculate the bounding box. setAutomaticCulling(scene::EAC_OFF); // Create the child scene node scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube(); m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1); m_meshnode->setReadOnlyMaterials(false); m_meshnode->setVisible(false); dummymesh->drop(); // m_meshnode grabbed it } WieldMeshSceneNode::~WieldMeshSceneNode() { sanity_check(g_extrusion_mesh_cache); if (g_extrusion_mesh_cache->drop()) g_extrusion_mesh_cache = NULL; } void WieldMeshSceneNode::setCube(const TileSpec tiles[6], v3f wield_scale, ITextureSource *tsrc) { scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube(); changeToMesh(cubemesh); cubemesh->drop(); m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR); // Customize materials for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) { assert(i < 6); video::SMaterial &material = m_meshnode->getMaterial(i); if (tiles[i].animation_frame_count == 1) { material.setTexture(0, tiles[i].texture); } else { FrameSpec animation_frame = tiles[i].frames[0]; material.setTexture(0, animation_frame.texture); } tiles[i].applyMaterialOptions(material); } } void WieldMeshSceneNode::setExtruded(const std::string &imagename, v3f wield_scale, ITextureSource *tsrc, u8 num_frames) { video::ITexture *texture = tsrc->getTexture(imagename); if (!texture) { changeToMesh(NULL); return; } core::dimension2d<u32> dim = texture->getSize(); // Detect animation texture and pull off top frame instead of using entire thing if (num_frames > 1) { u32 frame_height = dim.Height / num_frames; dim = core::dimension2d<u32>(dim.Width, frame_height); } scene::IMesh *mesh = g_extrusion_mesh_cache->create(dim); changeToMesh(mesh); mesh->drop(); m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED); // Customize material video::SMaterial &material = m_meshnode->getMaterial(0); material.setTexture(0, tsrc->getTextureForMesh(imagename)); material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; material.MaterialType = m_material_type; material.setFlag(video::EMF_BACK_FACE_CULLING, true); // Enable bi/trilinear filtering only for high resolution textures if (dim.Width > 32) { material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter); material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter); } else { material.setFlag(video::EMF_BILINEAR_FILTER, false); material.setFlag(video::EMF_TRILINEAR_FILTER, false); } material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter); // mipmaps cause "thin black line" artifacts #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2 material.setFlag(video::EMF_USE_MIP_MAPS, false); #endif if (m_enable_shaders) { material.setTexture(2, tsrc->getShaderFlagsTexture(false)); } } void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client) { ITextureSource *tsrc = client->getTextureSource(); IItemDefManager *idef = client->getItemDefManager(); IShaderSource *shdrsrc = client->getShaderSource(); INodeDefManager *ndef = client->getNodeDefManager(); const ItemDefinition &def = item.getDefinition(idef); const ContentFeatures &f = ndef->get(def.name); content_t id = ndef->getId(def.name); if (m_enable_shaders) { u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL); m_material_type = shdrsrc->getShaderInfo(shader_id).material; } m_colors.clear(); // If wield_image is defined, it overrides everything else if (def.wield_image != "") { setExtruded(def.wield_image, def.wield_scale, tsrc, 1); return; } // Handle nodes // See also CItemDefManager::createClientCached() else if (def.type == ITEM_NODE) { if (f.mesh_ptr[0]) { // e.g. mesh nodes and nodeboxes changeToMesh(f.mesh_ptr[0]); // mesh_ptr[0] is pre-scaled by BS * f->visual_scale m_meshnode->setScale( def.wield_scale * WIELD_SCALE_FACTOR / (BS * f.visual_scale)); } else if (f.drawtype == NDT_AIRLIKE) { changeToMesh(NULL); } else if (f.drawtype == NDT_PLANTLIKE) { setExtruded(tsrc->getTextureName(f.tiles[0].texture_id), def.wield_scale, tsrc, f.tiles[0].animation_frame_count); } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES) { setCube(f.tiles, def.wield_scale, tsrc); } else { MeshMakeData mesh_make_data(client, false); MapNode mesh_make_node(id, 255, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0)); changeToMesh(mapblock_mesh.getMesh()); translateMesh(m_meshnode->getMesh(), v3f(-BS, -BS, -BS)); m_meshnode->setScale( def.wield_scale * WIELD_SCALE_FACTOR / (BS * f.visual_scale)); } u32 material_count = m_meshnode->getMaterialCount(); if (material_count > 6) { errorstream << "WieldMeshSceneNode::setItem: Invalid material " "count " << material_count << ", truncating to 6" << std::endl; material_count = 6; } for (u32 i = 0; i < material_count; ++i) { const TileSpec *tile = &(f.tiles[i]); video::SMaterial &material = m_meshnode->getMaterial(i); material.setFlag(video::EMF_BACK_FACE_CULLING, true); material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter); material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter); bool animated = (tile->animation_frame_count > 1); if (animated) { FrameSpec animation_frame = tile->frames[0]; material.setTexture(0, animation_frame.texture); } else { material.setTexture(0, tile->texture); } m_colors.push_back(tile->color); material.MaterialType = m_material_type; if (m_enable_shaders) { if (tile->normal_texture) { if (animated) { FrameSpec animation_frame = tile->frames[0]; material.setTexture(1, animation_frame.normal_texture); } else { material.setTexture(1, tile->normal_texture); } } material.setTexture(2, tile->flags_texture); } } return; } else if (def.inventory_image != "") { setExtruded(def.inventory_image, def.wield_scale, tsrc, 1); return; } // no wield mesh found changeToMesh(NULL); } void WieldMeshSceneNode::setColor(video::SColor c) { assert(!m_lighting); scene::IMesh *mesh=m_meshnode->getMesh(); if (mesh == NULL) return; u8 red = c.getRed(); u8 green = c.getGreen(); u8 blue = c.getBlue(); u32 mc = mesh->getMeshBufferCount(); for (u32 j = 0; j < mc; j++) { video::SColor bc(0xFFFFFFFF); if (m_colors.size() > j) bc = m_colors[j]; video::SColor buffercolor(255, bc.getRed() * red / 255, bc.getGreen() * green / 255, bc.getBlue() * blue / 255); scene::IMeshBuffer *buf = mesh->getMeshBuffer(j); colorizeMeshBuffer(buf, &buffercolor); } } void WieldMeshSceneNode::render() { // note: if this method is changed to actually do something, // you probably should implement OnRegisterSceneNode as well } void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh) { if (mesh == NULL) { scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube(); m_meshnode->setVisible(false); m_meshnode->setMesh(dummymesh); dummymesh->drop(); // m_meshnode grabbed it } else { if (m_lighting) { m_meshnode->setMesh(mesh); } else { /* Lighting is disabled, this means the caller can (and probably will) call setColor later. We therefore need to clone the mesh so that setColor will only modify this scene node's mesh, not others'. */ scene::IMeshManipulator *meshmanip = SceneManager->getMeshManipulator(); scene::IMesh *new_mesh = meshmanip->createMeshCopy(mesh); m_meshnode->setMesh(new_mesh); new_mesh->drop(); // m_meshnode grabbed it } } m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting); // need to normalize normals when lighting is enabled (because of setScale()) m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting); m_meshnode->setVisible(true); } scene::IMesh *getItemMesh(Client *client, const ItemStack &item) { ITextureSource *tsrc = client->getTextureSource(); IItemDefManager *idef = client->getItemDefManager(); INodeDefManager *ndef = client->getNodeDefManager(); const ItemDefinition &def = item.getDefinition(idef); const ContentFeatures &f = ndef->get(def.name); content_t id = ndef->getId(def.name); if (!g_extrusion_mesh_cache) { g_extrusion_mesh_cache = new ExtrusionMeshCache(); } else { g_extrusion_mesh_cache->grab(); } scene::IMesh *mesh; // If inventory_image is defined, it overrides everything else if (def.inventory_image != "") { mesh = getExtrudedMesh(tsrc, def.inventory_image); return mesh; } 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)); } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES || f.drawtype == NDT_LIQUID || f.drawtype == NDT_FLOWINGLIQUID) { mesh = cloneMesh(g_extrusion_mesh_cache->createCube()); scaleMesh(mesh, v3f(1.2, 1.2, 1.2)); } else { MeshMakeData mesh_make_data(client, false); MapNode mesh_make_node(id, 255, 0); mesh_make_data.fillSingleNode(&mesh_make_node); MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0)); mesh = cloneMesh(mapblock_mesh.getMesh()); translateMesh(mesh, v3f(-BS, -BS, -BS)); scaleMesh(mesh, v3f(0.12, 0.12, 0.12)); u32 mc = mesh->getMeshBufferCount(); for (u32 i = 0; i < mc; ++i) { video::SMaterial &material1 = mesh->getMeshBuffer(i)->getMaterial(); video::SMaterial &material2 = mapblock_mesh.getMesh()->getMeshBuffer(i)->getMaterial(); material1.setTexture(0, material2.getTexture(0)); material1.setTexture(1, material2.getTexture(1)); material1.setTexture(2, material2.getTexture(2)); material1.setTexture(3, material2.getTexture(3)); material1.MaterialType = material2.MaterialType; } } u32 mc = mesh->getMeshBufferCount(); for (u32 i = 0; i < mc; ++i) { const TileSpec *tile = &(f.tiles[i]); scene::IMeshBuffer *buf = mesh->getMeshBuffer(i); colorizeMeshBuffer(buf, &tile->color); video::SMaterial &material = buf->getMaterial(); material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; material.setFlag(video::EMF_BILINEAR_FILTER, false); material.setFlag(video::EMF_TRILINEAR_FILTER, false); material.setFlag(video::EMF_BACK_FACE_CULLING, true); material.setFlag(video::EMF_LIGHTING, false); if (tile->animation_frame_count > 1) { FrameSpec animation_frame = tile->frames[0]; material.setTexture(0, animation_frame.texture); } else { material.setTexture(0, tile->texture); } } rotateMeshXZby(mesh, -45); rotateMeshYZby(mesh, -30); return mesh; } return NULL; } scene::IMesh * getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename) { video::ITexture *texture = tsrc->getTextureForMesh(imagename); if (!texture) { return NULL; } core::dimension2d<u32> dim = texture->getSize(); scene::IMesh *mesh = cloneMesh(g_extrusion_mesh_cache->create(dim)); // Customize material video::SMaterial &material = mesh->getMeshBuffer(0)->getMaterial(); material.setTexture(0, tsrc->getTexture(imagename)); material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE; material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE; material.setFlag(video::EMF_BILINEAR_FILTER, false); material.setFlag(video::EMF_TRILINEAR_FILTER, false); material.setFlag(video::EMF_BACK_FACE_CULLING, true); material.setFlag(video::EMF_LIGHTING, false); material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; scaleMesh(mesh, v3f(2.0, 2.0, 2.0)); return mesh; }