|
|
|
@@ -132,6 +132,253 @@ davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElemen
|
|
|
|
|
return davinci::VET_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedIntegerType(davinci::Scene& scene,
|
|
|
|
|
davinci::DeviceBufferAccessorPtr inpuptBufferAccesser)
|
|
|
|
|
{
|
|
|
|
|
size_t elementCounts = inpuptBufferAccesser->getCount();
|
|
|
|
|
davinci::ComponentType inputComponentType = inpuptBufferAccesser->getComponentType();
|
|
|
|
|
if (inputComponentType != davinci::ComponentType::CT_Uint8 &&
|
|
|
|
|
inputComponentType != davinci::ComponentType::CT_Uint16 &&
|
|
|
|
|
inputComponentType != davinci::ComponentType::CT_Uint32)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
davinci::DataType dataType = inpuptBufferAccesser->getDataType();
|
|
|
|
|
davinci::DeviceBufferPtr newBuffer = scene.newDeviceBuffer();
|
|
|
|
|
size_t bufferSize = elementCounts * davinci::MathUtil::ComponentSize(davinci::ComponentType::CT_Float32) * davinci::MathUtil::DataTypeSize(dataType);
|
|
|
|
|
newBuffer->init(bufferSize);
|
|
|
|
|
|
|
|
|
|
if (dataType == davinci::DataType::DT_Scalar)
|
|
|
|
|
{
|
|
|
|
|
inpuptBufferAccesser->walk([newBuffer, inputComponentType](const uint8_t* ptr, size_t index)
|
|
|
|
|
{
|
|
|
|
|
float32_t* target = (float32_t*)(newBuffer->ptr(index * sizeof(float32_t)));
|
|
|
|
|
if (inputComponentType == davinci::ComponentType::CT_Uint8)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint8_t*)ptr)[0] / (float32_t)0xFF;
|
|
|
|
|
}
|
|
|
|
|
else if (inputComponentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint16_t*)ptr)[0] / (float32_t)0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint32_t*)ptr)[0] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else if (dataType == davinci::DataType::DT_Vec2)
|
|
|
|
|
{
|
|
|
|
|
inpuptBufferAccesser->walk(
|
|
|
|
|
[newBuffer, inputComponentType](const uint8_t* ptr, size_t index)
|
|
|
|
|
{
|
|
|
|
|
float32_t* target = (float32_t*)(newBuffer->ptr(index * sizeof(float32_t) * 2));
|
|
|
|
|
if (inputComponentType == davinci::ComponentType::CT_Uint8)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint8_t*)ptr)[0] / (float32_t)0xFF;
|
|
|
|
|
target[1] = ((const uint8_t*)ptr)[1] / (float32_t)0xFF;
|
|
|
|
|
}
|
|
|
|
|
else if (inputComponentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint16_t*)ptr)[0] / (float32_t)0xFFFF;
|
|
|
|
|
target[1] = ((const uint16_t*)ptr)[1] / (float32_t)0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint32_t*)ptr)[0] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[1] = ((const uint32_t*)ptr)[1] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else if (dataType == davinci::DataType::DT_Vec3)
|
|
|
|
|
{
|
|
|
|
|
inpuptBufferAccesser->walk(
|
|
|
|
|
[newBuffer, inputComponentType](const uint8_t* ptr, size_t index)
|
|
|
|
|
{
|
|
|
|
|
float32_t* target = (float32_t*)(newBuffer->ptr(index * sizeof(float32_t) * 3));
|
|
|
|
|
if (inputComponentType == davinci::ComponentType::CT_Uint8)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint8_t*)ptr)[0] / (float32_t)0xFF;
|
|
|
|
|
target[1] = ((const uint8_t*)ptr)[1] / (float32_t)0xFF;
|
|
|
|
|
target[2] = ((const uint8_t*)ptr)[2] / (float32_t)0xFF;
|
|
|
|
|
}
|
|
|
|
|
else if (inputComponentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint16_t*)ptr)[0] / (float32_t)0xFFFF;
|
|
|
|
|
target[1] = ((const uint16_t*)ptr)[1] / (float32_t)0xFFFF;
|
|
|
|
|
target[2] = ((const uint16_t*)ptr)[2] / (float32_t)0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint32_t*)ptr)[0] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[1] = ((const uint32_t*)ptr)[1] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[2] = ((const uint32_t*)ptr)[2] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else if (dataType == davinci::DataType::DT_Vec4)
|
|
|
|
|
{
|
|
|
|
|
inpuptBufferAccesser->walk(
|
|
|
|
|
[newBuffer, inputComponentType](const uint8_t* ptr, size_t index)
|
|
|
|
|
{
|
|
|
|
|
float32_t* target = (float32_t*)(newBuffer->ptr(index * sizeof(float32_t) * 4));
|
|
|
|
|
if (inputComponentType == davinci::ComponentType::CT_Uint8)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint8_t*)ptr)[0] / (float32_t)0xFF;
|
|
|
|
|
target[1] = ((const uint8_t*)ptr)[1] / (float32_t)0xFF;
|
|
|
|
|
target[2] = ((const uint8_t*)ptr)[2] / (float32_t)0xFF;
|
|
|
|
|
target[3] = ((const uint8_t*)ptr)[3] / (float32_t)0xFF;
|
|
|
|
|
}
|
|
|
|
|
else if (inputComponentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint16_t*)ptr)[0] / (float32_t)0xFFFF;
|
|
|
|
|
target[1] = ((const uint16_t*)ptr)[1] / (float32_t)0xFFFF;
|
|
|
|
|
target[2] = ((const uint16_t*)ptr)[2] / (float32_t)0xFFFF;
|
|
|
|
|
target[3] = ((const uint16_t*)ptr)[3] / (float32_t)0xFFFF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
target[0] = ((const uint32_t*)ptr)[0] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[1] = ((const uint32_t*)ptr)[1] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[2] = ((const uint32_t*)ptr)[2] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
target[3] = ((const uint32_t*)ptr)[3] / (float32_t)0xFFFFFFFF;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
davinci::DeviceBufferViewPtr newBufferViewer = scene.newDeviceBufferView(newBuffer, 0, bufferSize, 0);
|
|
|
|
|
return scene.newDeviceBufferAccessor(newBufferViewer, 0, elementCounts, davinci::ComponentType::CT_Float32, dataType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccessor(
|
|
|
|
|
davinci::Scene& scene, davinci::VertexElementType vetType, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser)
|
|
|
|
|
{
|
|
|
|
|
if (!inpuptBufferAccesser) return nullptr;
|
|
|
|
|
davinci::ComponentType componentType = inpuptBufferAccesser->getComponentType();
|
|
|
|
|
davinci::DataType dataType = inpuptBufferAccesser->getDataType();
|
|
|
|
|
|
|
|
|
|
switch (vetType)
|
|
|
|
|
{
|
|
|
|
|
case davinci::VET_POSITION:
|
|
|
|
|
case davinci::VET_NORMAL:
|
|
|
|
|
case davinci::VET_BINORMAL:
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Float32 && dataType == davinci::DataType::DT_Vec3)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case davinci::VET_TANGENT:
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Float32 && dataType == davinci::DataType::DT_Vec4)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case davinci::VET_TEXCOORD_0:
|
|
|
|
|
case davinci::VET_TEXCOORD_1:
|
|
|
|
|
case davinci::VET_TEXCOORD_2:
|
|
|
|
|
case davinci::VET_TEXCOORD_3:
|
|
|
|
|
case davinci::VET_TEXCOORD_4:
|
|
|
|
|
case davinci::VET_TEXCOORD_5:
|
|
|
|
|
case davinci::VET_TEXCOORD_6:
|
|
|
|
|
case davinci::VET_TEXCOORD_7:
|
|
|
|
|
if (dataType == davinci::DataType::DT_Vec2)
|
|
|
|
|
{
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Float32)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case davinci::VET_COLOR_0:
|
|
|
|
|
case davinci::VET_COLOR_1:
|
|
|
|
|
case davinci::VET_COLOR_2:
|
|
|
|
|
case davinci::VET_COLOR_3:
|
|
|
|
|
case davinci::VET_COLOR_4:
|
|
|
|
|
case davinci::VET_COLOR_5:
|
|
|
|
|
case davinci::VET_COLOR_6:
|
|
|
|
|
case davinci::VET_COLOR_7:
|
|
|
|
|
if (dataType == davinci::DataType::DT_Vec3 || dataType == davinci::DataType::DT_Vec4)
|
|
|
|
|
{
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Float32)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case davinci::VET_JOINTS_0:
|
|
|
|
|
case davinci::VET_JOINTS_1:
|
|
|
|
|
case davinci::VET_JOINTS_2:
|
|
|
|
|
case davinci::VET_JOINTS_3:
|
|
|
|
|
case davinci::VET_JOINTS_4:
|
|
|
|
|
case davinci::VET_JOINTS_5:
|
|
|
|
|
case davinci::VET_JOINTS_6:
|
|
|
|
|
case davinci::VET_JOINTS_7:
|
|
|
|
|
if (dataType == davinci::DataType::DT_Vec4)
|
|
|
|
|
{
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case davinci::VET_WEIGHTS_0:
|
|
|
|
|
case davinci::VET_WEIGHTS_1:
|
|
|
|
|
case davinci::VET_WEIGHTS_2:
|
|
|
|
|
case davinci::VET_WEIGHTS_3:
|
|
|
|
|
case davinci::VET_WEIGHTS_4:
|
|
|
|
|
case davinci::VET_WEIGHTS_5:
|
|
|
|
|
case davinci::VET_WEIGHTS_6:
|
|
|
|
|
case davinci::VET_WEIGHTS_7:
|
|
|
|
|
if (dataType == davinci::DataType::DT_Vec4)
|
|
|
|
|
{
|
|
|
|
|
if (componentType == davinci::ComponentType::CT_Float32)
|
|
|
|
|
{
|
|
|
|
|
return inpuptBufferAccesser;
|
|
|
|
|
}
|
|
|
|
|
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _isSupportedIndicesType(const davinci::DeviceBufferAccessorConstPtr accesserBuffer)
|
|
|
|
|
{
|
|
|
|
|
if (!accesserBuffer) return false;
|
|
|
|
|
davinci::ComponentType componentType = accesserBuffer->getComponentType();
|
|
|
|
|
davinci::DataType dataType = accesserBuffer->getDataType();
|
|
|
|
|
|
|
|
|
|
return componentType == davinci::ComponentType::CT_Uint16 && dataType == davinci::DataType::DT_Scalar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _loadDeviceBuffer(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < gltfModel.buffers.size(); i++)
|
|
|
|
@@ -225,6 +472,13 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//convert to supported primitive buffer(float32)
|
|
|
|
|
deviceBufferAccessor = _createSupportedPrimitiveAttributeBufferAccessor(scene, vetype, deviceBufferAccessor);
|
|
|
|
|
if (deviceBufferAccessor == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primitive->attributes.insert(std::make_pair(vetype, deviceBufferAccessor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -233,6 +487,12 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
bool isSupportedType = _isSupportedIndicesType(indicesBufferAccessor);
|
|
|
|
|
if (!isSupportedType)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primitive->indices = indicesBufferAccessor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -609,30 +869,9 @@ bool export_mesh_to_obj(const davinci::Scene& scene)
|
|
|
|
|
davinci::DeviceBufferAccessorConstPtr indexBufferAccessor = primitive->indices;
|
|
|
|
|
indexBufferAccessor->walk3([=](const uint8_t* ptr0, const uint8_t* ptr1, const uint8_t* ptr2, size_t index) {
|
|
|
|
|
|
|
|
|
|
uint32_t indice0, indice1, indice2;
|
|
|
|
|
if (indexBufferAccessor->getComponentType() == davinci::CT_Uint16)
|
|
|
|
|
{
|
|
|
|
|
indice0 = *((uint16_t*)ptr0) + 1;
|
|
|
|
|
indice1 = *((uint16_t*)ptr1) + 1;
|
|
|
|
|
indice2 = *((uint16_t*)ptr2) + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (indexBufferAccessor->getComponentType() == davinci::CT_Uint32)
|
|
|
|
|
{
|
|
|
|
|
indice0 = *((uint32_t*)ptr0) + 1;
|
|
|
|
|
indice1 = *((uint32_t*)ptr1) + 1;
|
|
|
|
|
indice2 = *((uint32_t*)ptr2) + 1;
|
|
|
|
|
}
|
|
|
|
|
else if (indexBufferAccessor->getComponentType() == davinci::CT_Uint8)
|
|
|
|
|
{
|
|
|
|
|
indice0 = *((uint8_t*)ptr0) + 1;
|
|
|
|
|
indice1 = *((uint8_t*)ptr1) + 1;
|
|
|
|
|
indice2 = *((uint8_t*)ptr2) + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//error
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
uint32_t indice0 = *((uint16_t*)ptr0) + 1;
|
|
|
|
|
uint32_t indice1 = *((uint16_t*)ptr1) + 1;
|
|
|
|
|
uint32_t indice2 = *((uint16_t*)ptr2) + 1;
|
|
|
|
|
|
|
|
|
|
if (bHasTex0)
|
|
|
|
|
{
|
|
|
|
|