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:
@@ -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
|
||||
{
|
||||
|
||||
@@ -7,7 +7,8 @@ struct b3ImportMeshData
|
||||
{
|
||||
struct GLInstanceGraphicsShape* m_gfxShape;
|
||||
|
||||
unsigned char* m_textureImage;//in 3 component 8-bit RGB data
|
||||
unsigned char* m_textureImage1;//in 3 component 8-bit RGB data
|
||||
bool m_isCached;
|
||||
int m_textureWidth;
|
||||
int m_textureHeight;
|
||||
};
|
||||
|
||||
@@ -878,12 +878,13 @@ void BulletURDFImporter::convertURDFToVisualShapeInternal(const UrdfVisual* visu
|
||||
if (b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(visual->m_geometry.m_meshFileName, meshData))
|
||||
{
|
||||
|
||||
if (meshData.m_textureImage)
|
||||
if (meshData.m_textureImage1)
|
||||
{
|
||||
BulletURDFTexture texData;
|
||||
texData.m_width = meshData.m_textureWidth;
|
||||
texData.m_height = meshData.m_textureHeight;
|
||||
texData.textureData = meshData.m_textureImage;
|
||||
texData.textureData1 = meshData.m_textureImage1;
|
||||
texData.m_isCached = meshData.m_isCached;
|
||||
texturesOut.push_back(texData);
|
||||
}
|
||||
glmesh = meshData.m_gfxShape;
|
||||
@@ -1137,7 +1138,7 @@ int BulletURDFImporter::convertLinkVisualShapes(int linkIndex, const char* pathP
|
||||
if (textures.size())
|
||||
{
|
||||
|
||||
textureIndex = m_data->m_guiHelper->registerTexture(textures[0].textureData,textures[0].m_width,textures[0].m_height);
|
||||
textureIndex = m_data->m_guiHelper->registerTexture(textures[0].textureData1,textures[0].m_width,textures[0].m_height);
|
||||
}
|
||||
{
|
||||
B3_PROFILE("registerGraphicsShape");
|
||||
@@ -1151,7 +1152,10 @@ int BulletURDFImporter::convertLinkVisualShapes(int linkIndex, const char* pathP
|
||||
for (int i=0;i<textures.size();i++)
|
||||
{
|
||||
B3_PROFILE("free textureData");
|
||||
free( textures[i].textureData);
|
||||
if (!textures[i].m_isCached)
|
||||
{
|
||||
free( textures[i].textureData1);
|
||||
}
|
||||
}
|
||||
return graphicsIndex;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ struct BulletURDFTexture
|
||||
{
|
||||
int m_width;
|
||||
int m_height;
|
||||
unsigned char* textureData;
|
||||
unsigned char* textureData1;
|
||||
bool m_isCached;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user