增加材质和纹理
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "dvu_config.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "scene/dvc_scene.h"
|
||||
|
||||
DV_UTILS_BEGIN_NAMESPACE
|
||||
|
||||
bool DV_UTILS_API load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene);
|
||||
bool DV_UTILS_API load_scene_from_gltf(const char* szSceneFiles, davinci::Device& device, davinci::Scene& scene);
|
||||
|
||||
bool DV_UTILS_API export_mesh_to_obj(const davinci::Scene& scene);
|
||||
bool DV_UTILS_API export_mesh_to_obj(const davinci::Device& device, const davinci::Scene& scene);
|
||||
|
||||
DV_UTILS_END_NAMESPACE
|
||||
|
||||
@@ -17,8 +17,10 @@ bool dump_pixel_buffer(const char* filename, int32_t width, int32_t height,
|
||||
case davinci::PF_UINT8_RGBA:
|
||||
color_type = PNG_COLOR_TYPE_RGB_ALPHA; break;
|
||||
case davinci::PF_FLOAT32_RGB:
|
||||
case davinci::PF_UINT8_RGB:
|
||||
color_type = PNG_COLOR_TYPE_RGB; break;
|
||||
case davinci::PF_FLOAT32_R:
|
||||
case davinci::PF_FLOAT32_ALPHA:
|
||||
case davinci::PF_UINT8_ALPHA:
|
||||
color_type = PNG_COLOR_TYPE_GRAY; break;
|
||||
default:
|
||||
//not support yet!
|
||||
@@ -54,9 +56,14 @@ bool dump_pixel_buffer(const char* filename, int32_t width, int32_t height,
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
//integer pixelformat
|
||||
if (davinci::PF_UINT8_RGBA == pixelFormat) {
|
||||
for (int32_t y = 0; y < height; y++) {
|
||||
const uint32_t* p = (const uint32_t*)pixelData + (height - 1 - y) * width;
|
||||
if (davinci::PF_UINT8_RGBA == pixelFormat ||
|
||||
davinci::PF_UINT8_RGB == pixelFormat ||
|
||||
davinci::PF_UINT8_ALPHA == pixelFormat)
|
||||
{
|
||||
size_t pixelSize = davinci::MathUtil::PixelSize(pixelFormat);
|
||||
for (int32_t y = 0; y < height; y++)
|
||||
{
|
||||
const uint8_t* p = (const uint8_t*)pixelData + (height - 1 - y) * width * pixelSize;
|
||||
png_write_rows(png_ptr, (png_bytepp)&p, 1);
|
||||
}
|
||||
}
|
||||
@@ -165,7 +172,7 @@ bool dump_render_target(const char* baseFilename, const davinci::RenderTarget& r
|
||||
_snprintf(szFileName, 256, "%s_depth.png", baseFilename);
|
||||
dump_pixel_buffer(szFileName,
|
||||
renderTarget.getWidth(), renderTarget.getHeight(),
|
||||
&(depth_data[0]), davinci::PF_FLOAT32_R
|
||||
&(depth_data[0]), davinci::PF_FLOAT32_ALPHA
|
||||
);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -8,11 +8,18 @@
|
||||
#include "scene/component/dvc_mesh_component.h"
|
||||
#include "scene/component/dvc_camera_component.h"
|
||||
#include "scene/component/dvc_light_component.h"
|
||||
#include "device/dvc_device.h"
|
||||
#include "device/dvc_buffer_accessor.h"
|
||||
#include "asset/dvc_mesh.h"
|
||||
#include "asset/dvc_image.h"
|
||||
#include "asset/dvc_texture.h"
|
||||
#include "asset/dvc_material.h"
|
||||
|
||||
#include "math/dvc_vector3.h"
|
||||
#include "math/dvc_vector4.h"
|
||||
|
||||
#include "buffer/dvu_dump_buffer.h"
|
||||
|
||||
#include <tiny_gltf.h>
|
||||
|
||||
#include <string>
|
||||
@@ -94,6 +101,25 @@ davinci::PrimitiveType _getPrimitiveTypeFromGltf(int gltfModelType)
|
||||
}
|
||||
}
|
||||
|
||||
davinci::PixelFormat _getPixelFormatFromGltf(int32_t gltfComponent, int32_t gltfPixelType)
|
||||
{
|
||||
if (gltfPixelType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)
|
||||
{
|
||||
if (gltfComponent == 1) return davinci::PF_UINT8_ALPHA;
|
||||
else if (gltfComponent == 3) return davinci::PF_UINT8_RGB;
|
||||
else if (gltfComponent == 4) return davinci::PF_UINT8_RGBA;
|
||||
else return davinci::PF_UNKNOWN;
|
||||
}
|
||||
else if (gltfPixelType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT)
|
||||
{
|
||||
if (gltfComponent == 1) return davinci::PF_UINT16_ALPHA;
|
||||
else if (gltfComponent == 3) return davinci::PF_UINT16_RGB;
|
||||
else if (gltfComponent == 4) return davinci::PF_UINT16_RGBA;
|
||||
else return davinci::PF_UNKNOWN;
|
||||
}
|
||||
else return davinci::PF_UNKNOWN;
|
||||
}
|
||||
|
||||
//from "POSITION/NORMAL/..." to VET_***
|
||||
davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElementType)
|
||||
{
|
||||
@@ -132,7 +158,8 @@ davinci::VertexElementType _getElementTypeFromGltf(const std::string& gltfElemen
|
||||
return davinci::VET_UNKNOWN;
|
||||
}
|
||||
|
||||
davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedIntegerType(davinci::Scene& scene,
|
||||
davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedIntegerType(
|
||||
davinci::Device& device, davinci::Scene& scene,
|
||||
davinci::DeviceBufferAccessorPtr inpuptBufferAccesser)
|
||||
{
|
||||
size_t elementCounts = inpuptBufferAccesser->getCount();
|
||||
@@ -145,7 +172,7 @@ davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedInteg
|
||||
}
|
||||
|
||||
davinci::DataType dataType = inpuptBufferAccesser->getDataType();
|
||||
davinci::DeviceBufferPtr newBuffer = scene.newDeviceBuffer();
|
||||
davinci::DeviceBufferPtr newBuffer = device.newDeviceBuffer();
|
||||
size_t bufferSize = elementCounts * davinci::MathUtil::ComponentSize(davinci::ComponentType::CT_Float32) * davinci::MathUtil::DataTypeSize(dataType);
|
||||
newBuffer->init(bufferSize);
|
||||
|
||||
@@ -254,12 +281,13 @@ davinci::DeviceBufferAccessorPtr _createFloat32BufferAccessorFromNormalizedInteg
|
||||
}
|
||||
);
|
||||
}
|
||||
davinci::DeviceBufferViewPtr newBufferViewer = scene.newDeviceBufferView(newBuffer, 0, bufferSize, 0);
|
||||
return scene.newDeviceBufferAccessor(newBufferViewer, 0, elementCounts, davinci::ComponentType::CT_Float32, dataType);
|
||||
davinci::DeviceBufferViewPtr newBufferViewer = device.newDeviceBufferView(newBuffer, 0, bufferSize, 0);
|
||||
return device.newDeviceBufferAccessor(newBufferViewer, 0, elementCounts, davinci::ComponentType::CT_Float32, dataType);
|
||||
}
|
||||
|
||||
davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccessor(
|
||||
davinci::Scene& scene, davinci::VertexElementType vetType, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser)
|
||||
davinci::Device& device, davinci::Scene& scene,
|
||||
davinci::VertexElementType vetType, davinci::DeviceBufferAccessorPtr inpuptBufferAccesser)
|
||||
{
|
||||
if (!inpuptBufferAccesser) return nullptr;
|
||||
davinci::ComponentType componentType = inpuptBufferAccesser->getComponentType();
|
||||
@@ -299,7 +327,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso
|
||||
}
|
||||
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
||||
{
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -320,7 +348,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso
|
||||
}
|
||||
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
||||
{
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -358,7 +386,7 @@ davinci::DeviceBufferAccessorPtr _createSupportedPrimitiveAttributeBufferAccesso
|
||||
}
|
||||
else if (componentType == davinci::ComponentType::CT_Uint8 || componentType == davinci::ComponentType::CT_Uint16)
|
||||
{
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(scene, inpuptBufferAccesser);
|
||||
return _createFloat32BufferAccessorFromNormalizedIntegerType(device, scene, inpuptBufferAccesser);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -379,47 +407,53 @@ bool _isSupportedIndicesType(const davinci::DeviceBufferAccessorConstPtr accesse
|
||||
return componentType == davinci::ComponentType::CT_Uint16 && dataType == davinci::DataType::DT_Scalar;
|
||||
}
|
||||
|
||||
bool _loadDeviceBuffer(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
bool _loadDeviceBuffer(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.buffers.size(); i++)
|
||||
{
|
||||
const tinygltf::Buffer& gltfBuffer = gltfModel.buffers[i];
|
||||
tinygltf::Buffer& gltfBuffer = gltfModel.buffers[i];
|
||||
|
||||
davinci::DeviceBufferPtr deviceBufferPtr = scene.newDeviceBuffer();
|
||||
deviceBufferPtr->init(gltfBuffer.data.size());
|
||||
davinci::DeviceBufferPtr deviceBufferPtr = device.newDeviceBuffer(gltfBuffer.data.size());
|
||||
|
||||
//fill data
|
||||
memcpy(deviceBufferPtr->ptr(0), gltfBuffer.data.data(), gltfBuffer.data.size());
|
||||
|
||||
//set davinci id
|
||||
gltfBuffer.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)deviceBufferPtr->getID())));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _loadBufferView(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
bool _loadBufferView(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.bufferViews.size(); i++)
|
||||
{
|
||||
const tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[i];
|
||||
int32_t bufferIndex = gltfBufferView.buffer;
|
||||
davinci::DeviceBufferConstPtr deviceBufferPtr = scene.getDeviceBuffer(bufferIndex);
|
||||
tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[i];
|
||||
tinygltf::Buffer& gltfBuffer = gltfModel.buffers[gltfBufferView.buffer];
|
||||
int32_t bufferID = gltfBuffer.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
|
||||
davinci::DeviceBufferConstPtr deviceBufferPtr = device.getDeviceBuffer(bufferID);
|
||||
if (deviceBufferPtr == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
davinci::DeviceBufferViewPtr bufferViewPtr = scene.newDeviceBufferView(
|
||||
davinci::DeviceBufferViewPtr bufferViewPtr = device.newDeviceBufferView(
|
||||
deviceBufferPtr, gltfBufferView.byteOffset, gltfBufferView.byteLength, gltfBufferView.byteStride);
|
||||
gltfBufferView.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)bufferViewPtr->getID())));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _loadBufferAccessor(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
bool _loadBufferAccessor(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.accessors.size(); i++)
|
||||
{
|
||||
const tinygltf::Accessor& gltfAccessor = gltfModel.accessors[i];
|
||||
tinygltf::Accessor& gltfAccessor = gltfModel.accessors[i];
|
||||
tinygltf::BufferView& gltfBufferView = gltfModel.bufferViews[gltfAccessor.bufferView];
|
||||
int32_t bufferViewID = gltfBufferView.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
|
||||
int32_t bufferViewIndex = gltfAccessor.bufferView;
|
||||
davinci::DeviceBufferViewPtr deviceBufferView = scene.getDeviceBufferView(bufferViewIndex);
|
||||
davinci::DeviceBufferViewPtr deviceBufferView = device.getDeviceBufferView(bufferViewID);
|
||||
if (deviceBufferView==nullptr)
|
||||
{
|
||||
return false;
|
||||
@@ -437,20 +471,22 @@ bool _loadBufferAccessor(const tinygltf::Model& gltfModel, davinci::Scene& scene
|
||||
return false;
|
||||
}
|
||||
|
||||
davinci::DeviceBufferAccessorPtr bufferAccessor = scene.newDeviceBufferAccessor(
|
||||
davinci::DeviceBufferAccessorPtr bufferAccessor = device.newDeviceBufferAccessor(
|
||||
deviceBufferView, gltfAccessor.byteOffset, gltfAccessor.count, componentType, dataType);
|
||||
gltfAccessor.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)bufferAccessor->getID())));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
bool _loadMesh(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i=0; i<gltfModel.meshes.size(); i++)
|
||||
{
|
||||
const tinygltf::Mesh& gltfMesh = gltfModel.meshes[i];
|
||||
tinygltf::Mesh& gltfMesh = gltfModel.meshes[i];
|
||||
|
||||
davinci::MeshPtr mesh = std::make_shared<davinci::Mesh>(gltfMesh.name);
|
||||
davinci::MeshPtr mesh = device.newMeshAsset(gltfMesh.name);
|
||||
gltfMesh.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)mesh->getID())));
|
||||
|
||||
for (size_t j=0; j<gltfMesh.primitives.size(); j++)
|
||||
{
|
||||
@@ -459,21 +495,28 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
davinci::Mesh::PrimitivePtr primitive = mesh->newPrimitive();
|
||||
primitive->type = _getPrimitiveTypeFromGltf(gltfPrimitive.mode);
|
||||
|
||||
//apply attributes
|
||||
std::map<std::string, int>::const_iterator it(gltfPrimitive.attributes.begin());
|
||||
std::map<std::string, int>::const_iterator itEnd(gltfPrimitive.attributes.end());
|
||||
for (; it != itEnd; it++)
|
||||
{
|
||||
davinci::VertexElementType vetype = _getElementTypeFromGltf(it->first);
|
||||
int32_t gltfAccessIndex = it->second;
|
||||
if (gltfAccessIndex < 0 || gltfAccessIndex >= gltfModel.accessors.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
tinygltf::Accessor& gltfAccessor = gltfModel.accessors[gltfAccessIndex];
|
||||
int32_t accessorID = gltfAccessor.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
|
||||
int32_t gltfAccessor = it->second;
|
||||
davinci::DeviceBufferAccessorPtr deviceBufferAccessor = scene.getDeviceBufferAccessor(gltfAccessor);
|
||||
davinci::DeviceBufferAccessorPtr deviceBufferAccessor = device.getDeviceBufferAccessor(accessorID);
|
||||
if (deviceBufferAccessor ==nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//convert to supported primitive buffer(float32)
|
||||
deviceBufferAccessor = _createSupportedPrimitiveAttributeBufferAccessor(scene, vetype, deviceBufferAccessor);
|
||||
deviceBufferAccessor = _createSupportedPrimitiveAttributeBufferAccessor(device, scene, vetype, deviceBufferAccessor);
|
||||
if (deviceBufferAccessor == nullptr)
|
||||
{
|
||||
return false;
|
||||
@@ -482,7 +525,14 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
primitive->attributes.insert(std::make_pair(vetype, deviceBufferAccessor));
|
||||
}
|
||||
|
||||
davinci::DeviceBufferAccessorPtr indicesBufferAccessor = scene.getDeviceBufferAccessor(gltfPrimitive.indices);
|
||||
//apply indices
|
||||
if (gltfPrimitive.indices < 0 || gltfPrimitive.indices >= gltfModel.accessors.size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
tinygltf::Accessor& gltfIndicesAccessor = gltfModel.accessors[gltfPrimitive.indices];
|
||||
int32_t accessorID = gltfIndicesAccessor.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
davinci::DeviceBufferAccessorPtr indicesBufferAccessor = device.getDeviceBufferAccessor(accessorID);
|
||||
if (indicesBufferAccessor == nullptr)
|
||||
{
|
||||
return false;
|
||||
@@ -494,9 +544,113 @@ bool _loadMesh(const tinygltf::Model& gltfModel, davinci::Scene& scene)
|
||||
}
|
||||
|
||||
primitive->indices = indicesBufferAccessor;
|
||||
|
||||
//apply material
|
||||
if (gltfPrimitive.material >= 0 && gltfPrimitive.material < gltfModel.materials.size())
|
||||
{
|
||||
tinygltf::Material& gltfMaterial = gltfModel.materials[gltfPrimitive.material];
|
||||
int32_t materialID = gltfMaterial.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
davinci::MaterialPtr materialPtr = device.getMaterial(materialID);
|
||||
if (materialPtr == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
primitive->material = materialPtr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _loadTexture(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.images.size(); i++)
|
||||
{
|
||||
tinygltf::Image& gltfImage = gltfModel.images[i];
|
||||
|
||||
davinci::ImagePtr imagePtr = device.newImageAsset();
|
||||
imagePtr->initWithPixelData(device, gltfImage.width, gltfImage.height,
|
||||
_getPixelFormatFromGltf(gltfImage.component, gltfImage.pixel_type), gltfImage.image.data(), gltfImage.image.size());
|
||||
|
||||
gltfImage.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)imagePtr->getID())));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < gltfModel.textures.size(); i++)
|
||||
{
|
||||
tinygltf::Texture& gltfTexture = gltfModel.textures[i];
|
||||
if (gltfTexture.source < 0 || gltfTexture.source >= (int)gltfModel.images.size())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
tinygltf::Image& gltfImage = gltfModel.images[gltfTexture.source];
|
||||
if (gltfImage.width <= 0 || gltfImage.height <= 0 || gltfImage.component <= 0 || gltfImage.component > 4)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
scene.pushNewMeshAsset(mesh);
|
||||
int32_t imageID = gltfImage.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
davinci::ImagePtr imagePtr = device.getImage(imageID);
|
||||
|
||||
davinci::TexturePtr texturePtr = device.newTextureAsset();
|
||||
texturePtr->setImage(imagePtr);
|
||||
|
||||
gltfTexture.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)texturePtr->getID())));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
davinci::TexturePtr _getTexturePtr(tinygltf::Model& gltfModel, davinci::Device& device, int gltfTextureIndex)
|
||||
{
|
||||
if (gltfTextureIndex < 0 || gltfTextureIndex >= gltfModel.textures.size()) return nullptr;
|
||||
tinygltf::Texture& gltfTexture = gltfModel.textures[gltfTextureIndex];
|
||||
|
||||
int32_t textureID = gltfTexture.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
return device.getTexture(textureID);
|
||||
}
|
||||
|
||||
davinci::fVector4 _getVector4FromDoubleArray(const std::vector<double>& doubleArray)
|
||||
{
|
||||
if (doubleArray.size() != 4) return davinci::fVector4::ONE;
|
||||
return davinci::fVector4((float32_t)doubleArray[0], (float32_t)doubleArray[1], (float32_t)doubleArray[2], (float32_t)doubleArray[3]);
|
||||
}
|
||||
|
||||
bool _loadMaterial(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
for (size_t i = 0; i < gltfModel.materials.size(); i++)
|
||||
{
|
||||
tinygltf::Material& gltfMaterial = gltfModel.materials[i];
|
||||
|
||||
//create new material
|
||||
davinci::MaterialPtr materialPtr = device.newMaterialAsset();
|
||||
|
||||
//apply matellic roughness info
|
||||
const tinygltf::PbrMetallicRoughness& pbrMetllicRoughness = gltfMaterial.pbrMetallicRoughness;
|
||||
materialPtr->setBaseColorTexture(
|
||||
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.baseColorTexture.index),
|
||||
pbrMetllicRoughness.baseColorTexture.texCoord,
|
||||
_getVector4FromDoubleArray(pbrMetllicRoughness.baseColorFactor)
|
||||
);
|
||||
materialPtr->setMetallicRoughnessTexture(
|
||||
_getTexturePtr(gltfModel, device, pbrMetllicRoughness.metallicRoughnessTexture.index),
|
||||
pbrMetllicRoughness.metallicRoughnessTexture.texCoord,
|
||||
pbrMetllicRoughness.metallicFactor,
|
||||
pbrMetllicRoughness.roughnessFactor
|
||||
);
|
||||
|
||||
//set normal texture info
|
||||
materialPtr->setNormalTexture(
|
||||
_getTexturePtr(gltfModel, device, gltfMaterial.normalTexture.index),
|
||||
gltfMaterial.normalTexture.texCoord,
|
||||
gltfMaterial.normalTexture.scale
|
||||
);
|
||||
|
||||
//load shader
|
||||
if (!(materialPtr->loadShader()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
gltfMaterial.extensions.insert(std::make_pair("DAVINCI_ID", tinygltf::Value((int32_t)materialPtr->getID())));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -558,8 +712,7 @@ bool _loadLightExternsion(const tinygltf::Model& gltfModel, std::vector<davinci:
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, const std::vector<davinci::LightComponent::LightDefine>& lightsArray)
|
||||
bool _loadSceneNodes(tinygltf::Model& gltfModel, davinci::Device& device, davinci::Scene& scene, const std::vector<davinci::LightComponent::LightDefine>& lightsArray)
|
||||
{
|
||||
std::vector<std::pair<bool, davinci::SceneNodePtr>> sceneNodeArray;
|
||||
|
||||
@@ -599,7 +752,10 @@ bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, co
|
||||
//add mesh component
|
||||
if (gltfNode.mesh != -1)
|
||||
{
|
||||
davinci::MeshConstPtr meshPtr = scene.getMesh(gltfNode.mesh);
|
||||
tinygltf::Mesh& gltfMesh = gltfModel.meshes[gltfNode.mesh];
|
||||
int32_t meshID = gltfMesh.extensions["DAVINCI_ID"].GetNumberAsInt();
|
||||
|
||||
davinci::MeshConstPtr meshPtr = device.getMesh(meshID);
|
||||
if (meshPtr)
|
||||
{
|
||||
davinci::MeshComponentPtr meshComponet = std::make_shared<davinci::MeshComponent>(sceneNode);
|
||||
@@ -707,7 +863,7 @@ bool _loadSceneNodes(const tinygltf::Model& gltfModel, davinci::Scene& scene, co
|
||||
return true;
|
||||
}
|
||||
|
||||
bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene)
|
||||
bool load_scene_from_gltf(const char* szSceneFiles, davinci::Device& device, davinci::Scene& scene)
|
||||
{
|
||||
std::filesystem::path sceneFilePath = std::filesystem::path(szSceneFiles);
|
||||
|
||||
@@ -718,7 +874,10 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene)
|
||||
}
|
||||
|
||||
tinygltf::Model gltfModel;
|
||||
|
||||
tinygltf::TinyGLTF gltf_ctx;
|
||||
gltf_ctx.SetPreserveImageChannels(true);
|
||||
|
||||
std::string err;
|
||||
std::string warn;
|
||||
bool ret = false;
|
||||
@@ -752,25 +911,37 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene)
|
||||
//dump to davinci scene
|
||||
|
||||
//load device buffer
|
||||
if (!_loadDeviceBuffer(gltfModel, scene))
|
||||
if (!_loadDeviceBuffer(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//load buffer view
|
||||
if (!_loadBufferView(gltfModel, scene))
|
||||
if (!_loadBufferView(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//load buffer accessor
|
||||
if (!_loadBufferAccessor(gltfModel, scene))
|
||||
if (!_loadBufferAccessor(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//load texture
|
||||
if (!_loadTexture(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//load material
|
||||
if (!_loadMaterial(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//load mesh
|
||||
if (!_loadMesh(gltfModel, scene))
|
||||
if (!_loadMesh(gltfModel, device, scene))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -783,7 +954,7 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene)
|
||||
}
|
||||
|
||||
//load scene nodes
|
||||
if (!_loadSceneNodes(gltfModel, scene, lightDefine))
|
||||
if (!_loadSceneNodes(gltfModel, device, scene, lightDefine))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -799,13 +970,13 @@ bool load_scene_from_gltf(const char* szSceneFiles, davinci::Scene& scene)
|
||||
}
|
||||
|
||||
|
||||
bool export_mesh_to_obj(const davinci::Scene& scene)
|
||||
bool export_mesh_to_obj(const davinci::Device& device, const davinci::Scene& scene)
|
||||
{
|
||||
|
||||
int32_t meshCounts = scene.getMeshCounts();
|
||||
#if 0
|
||||
int32_t meshCounts = device.getMeshCounts();
|
||||
for (int32_t meshIndex = 0; meshIndex < meshCounts; meshIndex++)
|
||||
{
|
||||
davinci::MeshConstPtr mesh = scene.getMesh(meshIndex);
|
||||
davinci::MeshConstPtr mesh = device.getMesh(meshIndex);
|
||||
|
||||
const std::string& meshName = mesh->getName();
|
||||
if (meshName.empty()) {
|
||||
@@ -918,6 +1089,7 @@ bool export_mesh_to_obj(const davinci::Scene& scene)
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user