@@ -232,6 +232,11 @@ bool ModelGLTF::LoadFromFile(const std::string& filePath, IDirect3DDevice8* devi
232232 const int bonesIdx = map_contains (prim.attributes , std::string (" JOINTS_0" )) ? prim.attributes .at (" JOINTS_0" ) : -1 ;
233233 const int weightsIdx = map_contains (prim.attributes , std::string (" WEIGHTS_0" )) ? prim.attributes .at (" WEIGHTS_0" ) : -1 ;
234234
235+ if (uvIdx == -1 ) {
236+ section.numVertices = 0 ;
237+ continue ;
238+ }
239+
235240 const tinygltf::Accessor& posAcc = model.accessors [posIdx];
236241 const tinygltf::Accessor& normAcc = model.accessors [normIdx];
237242 const tinygltf::Accessor* colorAcc = (colorIdx != -1 ) ? &model.accessors [colorIdx] : nullptr ;
@@ -243,7 +248,7 @@ bool ModelGLTF::LoadFromFile(const std::string& filePath, IDirect3DDevice8* devi
243248 assert (posAcc.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT && posAcc.type == TINYGLTF_TYPE_VEC3);
244249 assert (normAcc.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT && normAcc.type == TINYGLTF_TYPE_VEC3);
245250 if (colorAcc) {
246- assert ((colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_FLOAT || colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT || colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) && colorAcc->type == TINYGLTF_TYPE_VEC4);
251+ assert ((colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_FLOAT || colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT || colorAcc->componentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) && ( colorAcc->type == TINYGLTF_TYPE_VEC4 || colorAcc-> type == TINYGLTF_TYPE_VEC3) );
247252 }
248253 assert (uvAcc.componentType == TINYGLTF_COMPONENT_TYPE_FLOAT && uvAcc.type == TINYGLTF_TYPE_VEC2);
249254
@@ -377,9 +382,9 @@ bool ModelGLTF::LoadFromFile(const std::string& filePath, IDirect3DDevice8* devi
377382 mTextures .resize (model.images .size ());
378383 for (size_t i = 0 , n = model.images .size (); i < n; ++i) {
379384 const tinygltf::Image& img = model.images [i];
380- IUnknownPtr<IDirect3DTexture8>& texture = mTextures [i];
385+ Texture& tex = mTextures [i];
381386
382- HRESULT hr = GfxCreateTextureFromFileInMem (device, (void *)img.image .data (), img.image .size (), texture.ReleaseAndGetAddressOf ());
387+ HRESULT hr = GfxCreateTextureFromFileInMem (device, (void *)img.image .data (), img.image .size (), tex. texture .ReleaseAndGetAddressOf (), &tex. transparent );
383388 if (FAILED (hr)) {
384389 return false ;
385390 }
@@ -482,7 +487,7 @@ void ModelGLTF::Update(const float deltaInSeconds, const D3DXMATRIX& globalXForm
482487 }
483488}
484489
485- HRESULT ModelGLTF::Draw (IDirect3DDevice8* device) {
490+ HRESULT ModelGLTF::Draw (IDirect3DDevice8* device, BOOL enableTransparency ) {
486491 const DWORD vertexSize = (mVertexType == VertexType::PosNormalTexcoord) ? sizeof (Vertex_PNT) : sizeof (Vertex_PNCT);
487492
488493 HRESULT hr = S_OK;
@@ -496,17 +501,42 @@ HRESULT ModelGLTF::Draw(IDirect3DDevice8* device) {
496501 const uint8_t * vertices = mXFormedVertices .data ();
497502 const uint16_t * indices = mIndices .data ();
498503
499- uint32_t lastTextureIdx = ~0u ;
500- for (const ModelGLTF::Mesh& mesh : mMeshes ) {
501- for (const ModelGLTF::Section& section : mesh.sections ) {
502- if (section.textureIdx != lastTextureIdx) {
503- device->SetTexture (0 , mTextures [section.textureIdx ].GetPtr ());
504- lastTextureIdx = section.textureIdx ;
505- }
504+ const size_t numPasses = enableTransparency ? 2 : 1 ;
505+ for (size_t pass = 0 ; pass < numPasses; ++pass) {
506+ uint32_t lastTextureIdx = ~0u ;
507+
508+ for (const ModelGLTF::Mesh& mesh : mMeshes ) {
509+ for (const ModelGLTF::Section& section : mesh.sections ) {
510+ if (!section.numVertices ) {
511+ continue ;
512+ }
513+
514+ const Texture& tex = mTextures [section.textureIdx ];
515+ if (enableTransparency) {
516+ if (pass == 0 && tex.transparent ) {
517+ continue ;
518+ } else if (pass == 1 && !tex.transparent ) {
519+ continue ;
520+ }
521+ }
522+
523+ if (section.textureIdx != lastTextureIdx) {
524+ device->SetTexture (0 , mTextures [section.textureIdx ].texture .GetPtr ());
525+ lastTextureIdx = section.textureIdx ;
526+ }
527+
528+ if (mUploadToGPU ) {
529+ device->SetIndices (mIB .GetPtr (), section.vbOffset );
530+ device->SetStreamSource (0 , mVB .GetPtr (), vertexSize);
506531
507- hr = device->DrawIndexedPrimitiveUP (D3DPT_TRIANGLELIST, 0 , section.numVertices , section.numIndices / 3u , indices + section.ibOffset , D3DFMT_INDEX16, vertices + section.vbOffset * vertexSize, vertexSize);
508- if (FAILED (hr)) {
509- return hr;
532+ hr = device->DrawIndexedPrimitive (D3DPT_TRIANGLELIST, 0 , section.numVertices , section.ibOffset , section.numIndices / 3u );
533+ } else {
534+ hr = device->DrawIndexedPrimitiveUP (D3DPT_TRIANGLELIST, 0 , section.numVertices , section.numIndices / 3u , indices + section.ibOffset , D3DFMT_INDEX16, vertices + section.vbOffset * vertexSize, vertexSize);
535+ }
536+
537+ if (FAILED (hr)) {
538+ return hr;
539+ }
510540 }
511541 }
512542 }
@@ -557,7 +587,7 @@ size_t ModelGLTF::GetNumTextures() const {
557587}
558588
559589IDirect3DTexture8* ModelGLTF::GetTexture (const size_t idx) const {
560- return mTextures [idx].GetPtr ();
590+ return mTextures [idx].texture . GetPtr ();
561591}
562592
563593const ModelGLTF::SceneNode* ModelGLTF::GetRootNode () const {
0 commit comments