diff options
Diffstat (limited to 'advtrains_signals_ks/models/advtrains_signals_ks_sign_zs3_smr60.obj')
0 files changed, 0 insertions, 0 deletions
![]() |
index : advtrains.git | |
Advtrains mod for minetest. | orwell |
aboutsummaryrefslogtreecommitdiff |
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "tile.h"
#include <ICameraSceneNode.h>
#include "util/string.h"
#include "util/container.h"
#include "util/thread.h"
#include "filesys.h"
#include "settings.h"
#include "mesh.h"
#include "gamedef.h"
#include "util/strfnd.h"
#include "imagefilters.h"
#include "guiscalingfilter.h"
#include "renderingengine.h"
#ifdef __ANDROID__
#include <GLES/gl.h>
#endif
/*
A cache from texture name to texture path
*/
MutexedMap<std::string, std::string> g_texturename_to_path_cache;
/*
Replaces the filename extension.
eg:
std::string image = "a/image.png"
replace_ext(image, "jpg")
-> image = "a/image.jpg"
Returns true on success.
*/
static bool replace_ext(std::string &path, const char *ext)
{
if (ext == NULL)
return false;
// Find place of last dot, fail if \ or / found.
s32 last_dot_i = -1;
for (s32 i=path.size()-1; i>=0; i--)
{
if (path[i] == '.')
{
last_dot_i = i;
break;
}
if (path[i] == '\\' || path[i] == '/')
break;
}
// If not found, return an empty string
if (last_dot_i == -1)
return false;
// Else make the new path
path = path.substr(0, last_dot_i+1) + ext;
return true;
}
/*
Find out the full path of an image by trying different filename
extensions.
If failed, return "".
*/
std::string getImagePath(std::string path)
{
// A NULL-ended list of possible image extensions
const char *extensions[] = {
"png", "jpg", "bmp", "tga",
"pcx", "ppm", "psd", "wal", "rgb",
NULL
};
// If there is no extension, add one
if (removeStringEnd(path, extensions).empty())
path = path + ".png";
// Check paths until something is found to exist
const char **ext = extensions;
do{
bool r = replace_ext(path, *ext);
if (!r)
return "";
if (fs::PathExists(path))
return path;
}
while((++ext) != NULL);
return "";
}
/*
Gets the path to a texture by first checking if the texture exists
in texture_path and if not, using the data path.
Checks all supported extensions by replacing the original extension.
If not found, returns "".
Utilizes a thread-safe cache.
*/
std::string getTexturePath(const std::string &filename)
{
std::string fullpath;
/*
Check from cache
*/
bool incache = g_texturename_to_path_cache.get(filename, &fullpath);
if (incache)
return fullpath;
/*
Check from texture_path
*/
const std::string &texture_path = g_settings->get("texture_path");
if (!texture_path.empty()) {
std::string testpath = texture_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
}
/*
Check from default data directory
*/
if (fullpath.empty())
{
std::string base_path = porting::path_share + DIR_DELIM + "textures"
+ DIR_DELIM + "base" + DIR_DELIM + "pack";
std::string testpath = base_path + DIR_DELIM + filename;
// Check all filename extensions. Returns "" if not found.
fullpath = getImagePath(testpath);
}
// Add to cache (also an empty result is cached)
g_texturename_to_path_cache.set(filename, fullpath);
// Finally return it
return fullpath;
}
void clearTextureNameCache()
{
g_texturename_to_path_cache.clear();
}
/*
Stores internal information about a texture.
*/
struct TextureInfo
{
std::string name;
video::ITexture *texture;
TextureInfo(
const std::string &name_,
video::ITexture *texture_=NULL
):
name(name_),
texture(texture_)
{
}
};
/*
SourceImageCache: A cache used for storing source images.
*/
class SourceImageCache
{
public:
~SourceImageCache() {
for (auto &m_image : m_images) {
m_image.second->drop();
}
m_images.clear();
}
void insert(const std::string &name, video::IImage *img, bool prefer_local)
{
assert(img); // Pre-condition
// Remove old image
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end()){
if (n->second)
n->second->drop();
}
video::IImage* toadd = img;
bool need_to_grab = true;
// Try to use local texture instead if asked to
if (prefer_local){
std::string path = getTexturePath(name);
if (!path.empty()) {
video::IImage *img2 = RenderingEngine::get_video_driver()->
createImageFromFile(path.c_str());
if (img2){
toadd = img2;
need_to_grab = false;
}
}
}
if (need_to_grab)
toadd->grab();
m_images[name] = toadd;
}
video::IImage* get(const std::string &name)
{
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end())
return n->second;
return NULL;
}
// Primarily fetches from cache, secondarily tries to read from filesystem
video::IImage *getOrLoad(const std::string &name)
{
std::map<std::string, video::IImage*>::iterator n;
n = m_images.find(name);
if (n != m_images.end()){
n->second->grab(); // Grab for caller
return n->second;
}
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
std::string path = getTexturePath(name);
if (path.empty()) {
infostream<<"SourceImageCache::getOrLoad(): No path found for \""
<<name<<"\""<<std::endl;
return NULL;
}
infostream<<"SourceImageCache::getOrLoad(): Loading path \""<<path
<<"\""<<std::endl;
video::IImage *img = driver->createImageFromFile(path.c_str());
if (img){
m_images[name] = img;
img->grab(); // Grab for caller
}
return img;
}
private:
std::map<std::string, video::IImage*> m_images;
};
/*
TextureSource
*/
class TextureSource : public IWritableTextureSource
{
public:
TextureSource();
virtual ~TextureSource();
/*
Example case:
Now, assume a texture with the id 1 exists, and has the name
"stone.png^mineral1".
Then a random thread calls getTextureId for a texture called
"stone.png^mineral1^crack0".
...Now, WTF should happen? Well:
- getTextureId strips off stuff recursively from the end until
the remaining part is found, or nothing is left when
something is stripped out
But it is slow to search for textures by names and modify them
like that?
- ContentFeatures is made to contain ids for the basic plain
textures
- Crack textures can be slow by themselves, but the framework
must be fast.
Example case #2:
- Assume a texture with the id 1 exists, and has the name
"stone.png^mineral_coal.png".
- Now getNodeTile() stumbles upon a node which uses
texture id 1, and determines that MATERIAL_FLAG_CRACK
must be applied to the tile
- MapBlockMesh::animate() finds the MATERIAL_FLAG_CRACK and
has received the current crack level 0 from the client. It
finds out the name of the texture with getTextureName(1),
appends "^crack0" to it and gets a new texture id with
getTextureId("stone.png^mineral_coal.png^crack0").
*/
/*
Gets a texture id from cache or
- if main thread, generates the texture, adds to cache and returns id.
- if other thread, adds to request queue and waits for main thread.
The id 0 points to a NULL texture. It is returned in case of error.
*/
u32 getTextureId(const std::string &name);
// Finds out the name of a cached texture.
std::string getTextureName(u32 id);
/*
If texture specified by the name pointed by the id doesn't
exist, create it, then return the cached texture.
Can be called from any thread. If called from some other thread
and not found in cache, the call is queued to the main thread
for processing.
*/
video::ITexture* getTexture(u32 id);
video::ITexture* getTexture(const std::string &name, u32 *id = NULL);
/*
Get a texture specifically intended for mesh
application, i.e. not HUD, compositing, or other 2D
use. This texture may be a different size and may
have had additional filters applied.
*/
video::ITexture* getTextureForMesh(const std::string &name, u32 *id);
virtual Palette* getPalette(const std::string &name);
bool isKnownSourceImage(const std::string &name)
{
bool is_known = false;
bool cache_found = m_source_image_existence.get(name, &is_known);
if (cache_found)
return is_known;
// Not found in cache; find out if a local file exists
is_known = (!getTexturePath(name).empty());
m_source_image_existence.set(name, is_known);
return is_known;
}
// Processes queued texture requests from other threads.
// Shall be called from the main thread.
void processQueue();
// Insert an image into the cache without touching the filesystem.
// Shall be called from the main thread.
void insertSourceImage(const std::string &name, video::IImage *img);
// Rebuild images and textures from the current set of source images
// Shall be called from the main thread.
void rebuildImagesAndTextures();
// Render a mesh to a texture.
// Returns NULL if render-to-texture failed.
// Shall be called from the main thread.
video::ITexture* generateTextureFromMesh(
const TextureFromMeshParams ¶ms);
video::ITexture* getNormalTexture(const std::string &name);
video::SColor getTextureAverageColor(const std::string &name);
video::ITexture *getShaderFlagsTexture(bool normamap_present);
private:
// The id of the thread that is allowed to use irrlicht directly
std::thread::id m_main_thread;
// Cache of source images
// This should be only accessed from the main thread
SourceImageCache m_sourcecache;
// Generate a texture
u32 generateTexture(const std::string &name);
// Generate image based on a string like "stone.png" or "[crack:1:0".
// if baseimg is NULL, it is created. Otherwise stuff is made on it.
bool generateImagePart(std::string part_of_name, video::IImage *& baseimg);
/*! Generates an image from a full string like
* "stone.png^mineral_coal.png^[crack:1:0".
* Shall be called from the main thread.
* The returned Image should be dropped.
*/
video::IImage* generateImage(const std::string &name);
// Thread-safe cache of what source images are known (true = known)
MutexedMap<std::string, bool> m_source_image_existence;
// A texture id is index in this array.
// The first position contains a NULL texture.
std::vector<TextureInfo> m_textureinfo_cache;
// Maps a texture name to an index in the former.
std::map<std::string, u32> m_name_to_id;
// The two former containers are behind this mutex
std::mutex m_textureinfo_cache_mutex;
// Queued texture fetches (to be processed by the main thread)
RequestQueue<std::string, u32, u8, u8> m_get_texture_queue;
// Textures that have been overwritten with other ones
// but can't be deleted because the ITexture* might still be used
std::vector<video::ITexture*> m_texture_trash;
// Maps image file names to loaded palettes.
std::unordered_map<std::string, Palette> m_palettes;
// Cached settings needed for making textures from meshes
bool m_setting_trilinear_filter;
bool m_setting_bilinear_filter;
bool m_setting_anisotropic_filter;
};
IWritableTextureSource *createTextureSource()
{
return new TextureSource();
}
TextureSource::TextureSource()
{
m_main_thread = std::this_thread::get_id();
// Add a NULL TextureInfo as the first index, named ""
m_textureinfo_cache.emplace_back("");
m_name_to_id[""] = 0;
// Cache some settings
// Note: Since this is only done once, the game must be restarted
// for these settings to take effect
m_setting_trilinear_filter = g_settings->getBool("trilinear_filter");
m_setting_bilinear_filter = g_settings->getBool("bilinear_filter");
m_setting_anisotropic_filter = g_settings->getBool("anisotropic_filter");
}
TextureSource::~TextureSource()
{
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
unsigned int textures_before = driver->getTextureCount();
for (const auto &iter : m_textureinfo_cache) {
//cleanup texture
if (iter.texture)
driver->removeTexture(iter.texture);
}
m_textureinfo_cache.clear();
for (auto t : m_texture_trash) {
//cleanup trashed texture
driver->removeTexture(t);
}
infostream << "~TextureSource() "<< textures_before << "/"
<< driver->getTextureCount() << std::endl;
}
u32 TextureSource::getTextureId(const std::string &name)
{
//infostream<<"getTextureId(): \""<<name<<"\""<<std::endl;
{
/*
See if texture already exists
*/
MutexAutoLock lock(m_textureinfo_cache_mutex);
std::map<std::string, u32>::iterator n;
n = m_name_to_id.find(name);
if (n != m_name_to_id.end())
{
return n->second;
}
}
/*
Get texture
*/
if (std::this_thread::get_id() == m_main_thread)
{
return generateTexture(name);
}
else
{
infostream<<"getTextureId(): Queued: name=\""<<name<<"\""<<std::endl;
// We're gonna ask the result to be put into here
static ResultQueue<std::string, u32, u8, u8> result_queue;
// Throw a request in
m_get_texture_queue.add(name, 0, 0, &result_queue);
/*infostream<<"Waiting for texture from main thread, name=\""
<<name<<"\""<<std::endl;*/
try
{
while(true) {
// Wait result for a second
GetResult<std::string, u32, u8, u8>
result = result_queue.pop_front(1000);
if (result.key == name) {
return result.item;
}
}
}
catch(ItemNotFoundException &e)
{
errorstream<<"Waiting for texture " << name << " timed out."<<std::endl;
return 0;
}
}
infostream<<"getTextureId(): Failed"<<std::endl;
return 0;
}
// Draw an image on top of an another one, using the alpha channel of the
// source image
static void blit_with_alpha(video::IImage *src, video::IImage *dst,
v2s32 src_pos, v2s32 dst_pos, v2u32 size);
// Like blit_with_alpha, but only modifies destination pixels that
// are fully opaque
static void blit_with_alpha_overlay(video::IImage *src, video::IImage *dst,
v2s32 src_pos, v2s32 dst_pos, v2u32 size);
// Apply a color to an image. Uses an int (0-255) to calculate the ratio.
// If the ratio is 255 or -1 and keep_alpha is true, then it multiples the
// color alpha with the destination alpha.
// Otherwise, any pixels that are not fully transparent get the color alpha.
static void apply_colorize(video::IImage *dst, v2u32 dst_pos, v2u32 size,
const video::SColor &color, int ratio, bool keep_alpha);
// paint a texture using the given color
static void apply_multiplication(video::IImage *dst, v2u32 dst_pos, v2u32 size,
const video::SColor &color);
// Apply a mask to an image
static void apply_mask(video::IImage *mask, video::IImage *dst,
v2s32 mask_pos, v2s32 dst_pos, v2u32 size);
// Draw or overlay a crack
static void draw_crack(video::IImage *crack, video::IImage *dst,
bool use_overlay, s32 frame_count, s32 progression,
video::IVideoDriver *driver);
// Brighten image
void brighten(video::IImage *image);
// Parse a transform name
u32 parseImageTransform(const std::string& s);
// Apply transform to image dimension
core::dimension2d<u32> imageTransformDimension(u32 transform, core::dimension2d<u32> dim);
// Apply transform to image data
void imageTransform(u32 transform, video::IImage *src, video::IImage *dst);
/*
This method generates all the textures
*/
u32 TextureSource::generateTexture(const std::string &name)
{
//infostream << "generateTexture(): name=\"" << name << "\"" << std::endl;
// Empty name means texture 0
if (name.empty()) {
infostream<<"generateTexture(): name is empty"<<std::endl;
return 0;
}
{
/*
See if texture already exists
*/
MutexAutoLock lock(m_textureinfo_cache_mutex);
std::map<std::string, u32>::iterator n;
n = m_name_to_id.find(name);
if (n != m_name_to_id.end()) {
return n->second;
}
}
/*
Calling only allowed from main thread
*/
if (std::this_thread::get_id() != m_main_thread) {
errorstream<<"TextureSource::generateTexture() "
"called not from main thread"<<std::endl;
return 0;
}
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
sanity_check(driver);
video::IImage *img = generateImage(name);
video::ITexture *tex = NULL;
if (img != NULL) {
#ifdef __ANDROID__
img = Align2Npot2(img, driver);
#endif
// Create texture from resulting image
tex = driver->addTexture(name.c_str(), img);
guiScalingCache(io::path(name.c_str()), driver, img);
img->drop();
}
/*
Add texture to caches (add NULL textures too)
*/
MutexAutoLock lock(m_textureinfo_cache_mutex);
u32 id = m_textureinfo_cache.size();
TextureInfo ti(name, tex);
m_textureinfo_cache.push_back(ti);
m_name_to_id[name] = id;
return id;
}
std::string TextureSource::getTextureName(u32 id)
{
MutexAutoLock lock(m_textureinfo_cache_mutex);
if (id >= m_textureinfo_cache.size())
{
errorstream<<"TextureSource::getTextureName(): id="<<id
<<" >= m_textureinfo_cache.size()="
<<m_textureinfo_cache.size()<<std::endl;
return "";
}
return m_textureinfo_cache[id].name;
}
video::ITexture* TextureSource::getTexture(u32 id)
{
MutexAutoLock lock(m_textureinfo_cache_mutex);
if (id >= m_textureinfo_cache.size())
return NULL;
return m_textureinfo_cache[id].texture;
}
video::ITexture* TextureSource::getTexture(const std::string &name, u32 *id)
{
u32 actual_id = getTextureId(name);
if (id){
*id = actual_id;
}
return getTexture(actual_id);
}
video::ITexture* TextureSource::getTextureForMesh(const std::string &name, u32 *id)
{
return getTexture(name + "^[applyfiltersformesh", id);
}
Palette* TextureSource::getPalette(const std::string &name)
{
// Only the main thread may load images
sanity_check(std::this_thread::get_id() == m_main_thread);
if (name == "")
return NULL;
auto it = m_palettes.find(name);
if (it == m_palettes.end()) {
// Create palette
video::IImage *img = generateImage(name);
if (!img) {
warningstream << "TextureSource::getPalette(): palette \"" << name
<< "\" could not be loaded." << std::endl;
return NULL;
}
Palette new_palette;
u32 w = img->getDimension().Width;
u32 h = img->getDimension().Height;
// Real area of the image
u32 area = h * w;
if (area == 0)
return NULL;
if (area > 256) {
warningstream << "TextureSource::getPalette(): the specified"
<< " palette image \"" << name << "\" is larger than 256"
<< " pixels, using the first 256." << std::endl;
area = 256;
} else if (256 % area != 0)
warningstream << "TextureSource::getPalette(): the "
<< "specified palette image \"" << name << "\" does not "
<< "contain power of two pixels." << std::endl;
// We stretch the palette so it will fit 256 values
// This many param2 values will have the same color
u32 step = 256 / area;
// For each pixel in the image
for (u32 i = 0; i < area; i++) {
video::SColor c = img->getPixel(i % w, i / w);
// Fill in palette with 'step' colors
for (u32 j = 0; j < step; j++)
new_palette.push_back(c);
}
img->drop();
// Fill in remaining elements
while (new_palette.size() < 256)
new_palette.emplace_back(0xFFFFFFFF);
m_palettes[name] = new_palette;
it = m_palettes.find(name);
}
if (it != m_palettes.end())
return &((*it).second);
return NULL;
}
void TextureSource::processQueue()
{
/*
Fetch textures
*/
//NOTE this is only thread safe for ONE consumer thread!
if (!m_get_texture_queue.empty())
{
GetRequest<std::string, u32, u8, u8>
request = m_get_texture_queue.pop();
/*infostream<<"TextureSource::processQueue(): "
<<"got texture request with "
<<"name=\""<<request.key<<"\""
<<std::endl;*/
m_get_texture_queue.pushResult(request, generateTexture(request.key));
}
}
void TextureSource::insertSourceImage(const std::string &name, video::IImage *img)
{
//infostream<<"TextureSource::insertSourceImage(): name="<<name<<std::endl;
sanity_check(std::this_thread::get_id() == m_main_thread);
m_sourcecache.insert(name, img, true);
m_source_image_existence.set(name, true);
}
void TextureSource::rebuildImagesAndTextures()
{
MutexAutoLock lock(m_textureinfo_cache_mutex);
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
sanity_check(driver);
// Recreate textures
for (TextureInfo &ti : m_textureinfo_cache) {
video::IImage *img = generateImage(ti.name);
#ifdef __ANDROID__
img = Align2Npot2(img, driver);
#endif
// Create texture from resulting image
video::ITexture *t = NULL;
if (img) {
t = driver->addTexture(ti.name.c_str(), img);
guiScalingCache(io::path(ti.name.c_str()), driver, img);
img->drop();
}
video::ITexture *t_old = ti.texture;
// Replace texture
ti.texture = t;
if (t_old)
m_texture_trash.push_back(t_old);
}
}
video::ITexture* TextureSource::generateTextureFromMesh(
const TextureFromMeshParams ¶ms)
{
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
sanity_check(driver);
#ifdef __ANDROID__
const GLubyte* renderstr = glGetString(GL_RENDERER);
std::string renderer((char*) renderstr);
// use no render to texture hack
if (
(renderer.find("Adreno") != std::string::npos) ||
(renderer.find("Mali") != std::string::npos) ||
(renderer.find("Immersion") != std::string::npos) ||
(renderer.find("Tegra") != std::string::npos) ||
g_settings->getBool("inventory_image_hack")
) {
// Get a scene manager
scene::ISceneManager *smgr_main = m_device->getSceneManager();
sanity_check(smgr_main);
scene::ISceneManager *smgr = smgr_main->createNewSceneManager();
sanity_check(smgr);
const float scaling = 0.2;
scene::IMeshSceneNode* meshnode =
smgr->addMeshSceneNode(params.mesh, NULL,
-1, v3f(0,0,0), v3f(0,0,0),
v3f(1.0 * scaling,1.0 * scaling,1.0 * scaling), true);
meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
meshnode->setMaterialFlag(video::EMF_ANTI_ALIASING, true);
meshnode->setMaterialFlag(video::EMF_TRILINEAR_FILTER, m_setting_trilinear_filter);
meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, m_setting_bilinear_filter);
meshnode->setMaterialFlag(video::EMF_ANISOTROPIC_FILTER, m_setting_anisotropic_filter);
scene::ICameraSceneNode* camera = smgr->addCameraSceneNode(0,
params.camera_position, params.camera_lookat);
// second parameter of setProjectionMatrix (isOrthogonal) is ignored
camera->setProjectionMatrix(params.camera_projection_matrix, false);
smgr->setAmbientLight(params.ambient_light);
smgr->addLightSceneNode(0,
params.light_position,
params.light_color,
params.light_radius*scaling);
core::dimension2d<u32> screen = driver->getScreenSize();
// Render scene
driver->beginScene(true, true, video::SColor(0,0,0,0));
driver->clearZBuffer();
smgr->drawAll();
core::dimension2d<u32> partsize(screen.Width * scaling,screen.Height * scaling);
irr::video::IImage* rawImage =
driver->createImage(irr::video::ECF_A8R8G8B8, partsize);
u8* pixels = static_cast<u8*>(rawImage->lock());
if (!pixels)
{
rawImage->drop();
return NULL;
}
core::rect<s32> source(
screen.Width /2 - (screen.Width * (scaling / 2)),
screen.Height/2 - (screen.Height * (scaling / 2)),
screen.Width /2 + (screen.Width * (scaling / 2)),
screen.Height/2 + (screen.Height * (scaling / 2))
);
glReadPixels(source.UpperLeftCorner.X, source.UpperLeftCorner.Y,
partsize.Width, partsize.Height, GL_RGBA,
GL_UNSIGNED_BYTE, pixels);
driver->endScene();
// Drop scene manager
smgr->drop();
unsigned int pixelcount = partsize.Width*partsize.Height;
u8* runptr = pixels;
for (unsigned int i=0; i < pixelcount; i++) {
u8 B = *runptr;
u8 G = *(runptr+1);
u8 R = *(runptr+2);
u8 A = *(runptr+3);
//BGRA -> RGBA
*runptr = R;
runptr ++;
*runptr = G;
runptr ++;
*runptr = B;
runptr ++;
*runptr = A;
runptr ++;
}
video::IImage* inventory_image =
driver->createImage(irr::video::ECF_A8R8G8B8, params.dim);
rawImage->copyToScaling(inventory_image);
rawImage->drop();