pybullet: allow to replace existing text, to avoid flickering (remove/add)

allow texture caching (disable using the disable file caching)
This commit is contained in:
erwincoumans
2017-10-25 08:15:01 -07:00
parent e9415f5912
commit ed8de36ffa
17 changed files with 256 additions and 88 deletions

View File

@@ -8,14 +8,37 @@
#include "Bullet3Common/b3FileUtils.h"
#include "stb_image/stb_image.h"
#include "../ImportObjDemo/LoadMeshFromObj.h"
#include "Bullet3Common/b3HashMap.h"
struct CachedTextureResult
{
std::string m_textureName;
int m_width;
int m_height;
unsigned char* m_pixels;
CachedTextureResult()
:m_width(0),
m_height(0),
m_pixels(0)
{
}
};
static b3HashMap<b3HashString, CachedTextureResult> gCachedTextureResults;
bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string& fileName, b3ImportMeshData& meshData)
{
B3_PROFILE("loadAndRegisterMeshFromFileInternal");
meshData.m_gfxShape = 0;
meshData.m_textureImage = 0;
meshData.m_textureImage1 = 0;
meshData.m_textureHeight = 0;
meshData.m_textureWidth = 0;
meshData.m_isCached = false;
char relativeFileName[1024];
if (b3ResourcePath::findResourcePath(fileName.c_str(), relativeFileName, 1024))
@@ -37,7 +60,7 @@ bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string&
B3_PROFILE("Load Texture");
//int textureIndex = -1;
//try to load some texture
for (int i = 0; meshData.m_textureImage == 0 && i < shapes.size(); i++)
for (int i = 0; meshData.m_textureImage1 == 0 && i < shapes.size(); i++)
{
const tinyobj::shape_t& shape = shapes[i];
if (shape.material.diffuse_texname.length() > 0)
@@ -57,21 +80,51 @@ bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string&
char relativeFileName2[1024];
if (b3ResourcePath::findResourcePath(relativeFileName, relativeFileName2, 1024))
{
image = stbi_load(relativeFileName, &width, &height, &n, 3);
meshData.m_textureImage = image;
if (image)
if (b3IsFileCachingEnabled())
{
meshData.m_textureWidth = width;
meshData.m_textureHeight = height;
}
else
{
b3Warning("Unsupported texture image format [%s]\n", relativeFileName);
meshData.m_textureWidth = 0;
meshData.m_textureHeight = 0;
break;
CachedTextureResult* texture = gCachedTextureResults[relativeFileName];
if (texture)
{
image = texture->m_pixels;
width = texture->m_width;
height = texture->m_height;
meshData.m_textureWidth = width;
meshData.m_textureHeight = height;
meshData.m_textureImage1 = image;
meshData.m_isCached = true;
}
}
if (image==0)
{
image = stbi_load(relativeFileName, &width, &height, &n, 3);
meshData.m_textureImage1 = image;
if (image)
{
meshData.m_textureWidth = width;
meshData.m_textureHeight = height;
if (b3IsFileCachingEnabled())
{
CachedTextureResult result;
result.m_textureName = relativeFileName;
result.m_width = width;
result.m_height = height;
result.m_pixels = image;
meshData.m_isCached = true;
gCachedTextureResults.insert(relativeFileName,result);
}
}
else
{
b3Warning("Unsupported texture image format [%s]\n", relativeFileName);
break;
}
}
}
else
{