diff --git a/trinity/Eve/SpaceObject/Children/EveChildMesh.cpp b/trinity/Eve/SpaceObject/Children/EveChildMesh.cpp index a9efa903e..c76586eb9 100644 --- a/trinity/Eve/SpaceObject/Children/EveChildMesh.cpp +++ b/trinity/Eve/SpaceObject/Children/EveChildMesh.cpp @@ -655,7 +655,8 @@ void EveChildMesh::GetBatches( ITriRenderBatchAccumulator* batches, TriBatchType { if( m_mesh ) { - m_mesh->GetBatches( batches, m_mesh->GetAreas( batchType ), perObjectData, min( m_currentInstanceScreenSize, m_currentScreenSize ) ); + const bool reverseWinding = Determinant( m_worldTransform ) < 0.0f; + m_mesh->GetBatches( batches, m_mesh->GetAreas( batchType ), perObjectData, min( m_currentInstanceScreenSize, m_currentScreenSize ), reverseWinding ); } if( m_activationStrength != 0.0 ) @@ -674,7 +675,8 @@ void EveChildMesh::GetShadowBatches( ITriRenderBatchAccumulator* batches, const // Fix asap if( m_display && m_mesh && m_hasUpdated ) { - m_mesh->GetBatches( batches, m_mesh->GetAreas( TRIBATCHTYPE_OPAQUE ), perObjectData, shadowPixelSize ); + const bool reverseWinding = Determinant( m_worldTransform ) < 0.0f; + m_mesh->GetBatches( batches, m_mesh->GetAreas( TRIBATCHTYPE_OPAQUE ), perObjectData, shadowPixelSize, reverseWinding ); } } diff --git a/trinity/Eve/SpaceObject/Children/EveChildParticleSystem.cpp b/trinity/Eve/SpaceObject/Children/EveChildParticleSystem.cpp index 1fe57bbf2..8134510ae 100644 --- a/trinity/Eve/SpaceObject/Children/EveChildParticleSystem.cpp +++ b/trinity/Eve/SpaceObject/Children/EveChildParticleSystem.cpp @@ -196,13 +196,6 @@ Tr2PerObjectData* EveChildParticleSystem::GetPerObjectData( ITriRenderBatchAccum void EveChildParticleSystem::UpdateSyncronous( const EveUpdateContext& updateContext, const EveChildUpdateParams& ) { - if( m_mesh ) - { - if( EntityComponents::ShouldReflect( m_reflectionMode ) ) - { - m_mesh->ReverseIndexBuffers(); - } - } } void EveChildParticleSystem::UpdateAsyncronous( const EveUpdateContext& updateContext, const EveChildUpdateParams& params ) diff --git a/trinity/Resources/TriGeometryRes.cpp b/trinity/Resources/TriGeometryRes.cpp index 1014150f9..dd60e078a 100644 --- a/trinity/Resources/TriGeometryRes.cpp +++ b/trinity/Resources/TriGeometryRes.cpp @@ -178,69 +178,6 @@ uint32_t GetPrimitiveCount( const TriGeometryResLodData& lod, uint32_t index, ui } -namespace -{ -#if WITH_GRANNY -ALResult ReverseIndexBuffer( TriGeometryResLodData& lod, granny_mesh& grannyMesh, Tr2RenderContext& renderContext ) -{ - uint32_t bytesPerIndex = 2; - auto indexCount = grannyMesh.PrimaryTopology->Index16Count; - if( indexCount == 0 ) - { - indexCount = grannyMesh.PrimaryTopology->IndexCount; - if( indexCount == 0 ) - { - return S_OK; - } - if( grannyMesh.PrimaryVertexData->VertexCount > 65535 ) - { - bytesPerIndex = 4; - } - } - - std::vector tempBuffer( indexCount * bytesPerIndex ); - GrannyCopyMeshIndices( &grannyMesh, bytesPerIndex, tempBuffer.data() ); - - if( bytesPerIndex == 2 ) - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } - else - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } - - CR_RETURN_HR( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod.m_reversedIndexAllocation ) ); - lod.m_reversedIndicesValid = true; - return S_OK; -} -#endif - -ALResult ReverseIndexBuffer( TriGeometryResLodData& lod, const void* ibData, const cmf::MeshLod& cmfMeshLod, Tr2RenderContext& renderContext ) -{ - uint32_t bytesPerIndex = cmfMeshLod.ib.stride; - auto indexCount = cmf::GetStreamElementCount( cmfMeshLod.ib ); - - std::vector tempBuffer( indexCount * bytesPerIndex ); - memcpy( tempBuffer.data(), ibData, indexCount * bytesPerIndex ); - - if( bytesPerIndex == 2 ) - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } - else - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } - - CR_RETURN_HR( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod.m_reversedIndexAllocation ) ); - lod.m_reversedIndicesValid = true; - return S_OK; -} - -} - - TriGeometryRes::TriGeometryRes( IRoot* lockobj ) : #if WITH_GRANNY m_pGrannyFile( NULL ), @@ -1862,76 +1799,6 @@ TriGeometryResMeshData::TriGeometryResMeshData() : { } -void TriGeometryRes::RequestReversedIndexBuffers() -{ - if( !m_isGood ) - { - return; - } - if( m_reversedIndexBuffersRequested ) - { - return; - } - m_reversedIndexBuffersRequested = true; - if( !m_isPrepared ) - { - return; - } - - if( IsUsingCMF() && m_sourceGranny ) - { - if( !m_sourceGranny->GetCMFData() ) - { - return; - } - - USE_MAIN_THREAD_RENDER_CONTEXT(); - - CCP_ASSERT_M( m_meshes.size() == m_sourceGranny->GetCMFData()->meshes.size(), "Amount of meshes should match!" ); - for( uint32_t i = 0; i < m_meshes.size(); i++ ) - { - auto& mesh = m_meshes[i]; - auto& cmfMesh = m_sourceGranny->GetCMFData()->meshes[i]; - - // this assertion assumes that gTriDev->GetMinimumModelLOD() has not changed since last call to SetupMeshes. Not sure if this holds true. - int minimumLOD = gTriDev->GetMinimumModelLOD() < 0 ? 0 : min( gTriDev->GetMinimumModelLOD(), (int)cmfMesh.lods.size() - 1 ); - CCP_ASSERT_M( mesh->m_lods.size() == cmfMesh.lods.size() - minimumLOD, "Amount of mesh lods should match!" ); - - for( const auto& lod : mesh->m_lods ) - { - auto& cmfLod = cmfMesh.lods[lod->m_originalLodIndex]; - ReverseIndexBuffer( *lod, m_sourceGranny->GetCMFViewData( cmfLod.ib ), cmfLod, renderContext ); - } - } - } -#if WITH_GRANNY - else if( !IsUsingCMF() && m_sourceGranny ) - { - granny_file* f = m_sourceGranny->GetGrannyFile(); - if( !f ) - { - return; - } - granny_file_info* gi = GrannyGetFileInfo( f ); - - USE_MAIN_THREAD_RENDER_CONTEXT(); - - for( auto& mesh : m_meshes ) - { - for( auto& lod : mesh->m_lods ) - { - auto grannyMesh = gi->Meshes[lod->m_grannyMeshIndex]; - ReverseIndexBuffer( *lod, *grannyMesh, renderContext ); - } - } - } -#endif - else - { - Reload(); - } -} - bool TriGeometryRes::RenderAreas( unsigned int meshIx, unsigned int areaIx, unsigned int areaCount, Tr2RenderContext& renderContext, bool reversed ) { return RenderAreas( std::numeric_limits::max(), meshIx, areaIx, areaCount, renderContext, reversed ); @@ -2087,27 +1954,24 @@ bool TriGeometryRes::CreateLodFromCMFMesh( Tr2CmfContents& cmfContents, const cm return false; } - if( m_reversedIndexBuffersRequested ) + std::vector tempBuffer( indexCount * bytesPerIndex ); + memcpy( tempBuffer.data(), pSrcIB, tempBuffer.size() ); + if( bytesPerIndex == 2 ) { - std::vector tempBuffer( indexCount * bytesPerIndex ); - memcpy( tempBuffer.data(), pSrcIB, tempBuffer.size() ); - if( bytesPerIndex == 2 ) - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + cmfMeshLod.ib.size ) ); - } - else - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + cmfMeshLod.ib.size ) ); - } + std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + cmfMeshLod.ib.size ) ); + } + else + { + std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + cmfMeshLod.ib.size ) ); + } - if( FAILED( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod->m_reversedIndexAllocation ) ) ) - { - g_sharedBuffer.Free( lod->m_vertexAllocation ); - g_sharedBuffer.Free( lod->m_indexAllocation ); - return false; - } - lod->m_reversedIndicesValid = true; + if( FAILED( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod->m_reversedIndexAllocation ) ) ) + { + g_sharedBuffer.Free( lod->m_vertexAllocation ); + g_sharedBuffer.Free( lod->m_indexAllocation ); + return false; } + lod->m_reversedIndicesValid = true; } lod->m_morphVertexDeclaration = -1; @@ -2170,10 +2034,9 @@ bool TriGeometryRes::CreateLodFromCMFMesh( Tr2CmfContents& cmfContents, const cm lod->m_allocationsValid = true; m_memoryUse += vbSize + ibSize; // Memory use is only approximate as a hint for the resource cache - if( m_reversedIndexBuffersRequested ) - { - m_memoryUse += ibSize; - } + + // Append reversed index buffer size also + m_memoryUse += ibSize; return true; } @@ -2242,25 +2105,22 @@ bool TriGeometryRes::CreateLodFromGrannyMesh( granny_mesh* grannyMesh, TriGeomet return false; } - if( m_reversedIndexBuffersRequested ) + if( bytesPerIndex == 2 ) { - if( bytesPerIndex == 2 ) - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } - else - { - std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); - } + std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); + } + else + { + std::reverse( reinterpret_cast( tempBuffer.data() ), reinterpret_cast( tempBuffer.data() + tempBuffer.size() ) ); + } - if( FAILED( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod->m_reversedIndexAllocation ) ) ) - { - g_sharedBuffer.Free( lod->m_vertexAllocation ); - g_sharedBuffer.Free( lod->m_indexAllocation ); - return false; - } - lod->m_reversedIndicesValid = true; + if( FAILED( g_sharedBuffer.Allocate( bytesPerIndex, indexCount, tempBuffer.data(), renderContext, lod->m_reversedIndexAllocation ) ) ) + { + g_sharedBuffer.Free( lod->m_vertexAllocation ); + g_sharedBuffer.Free( lod->m_indexAllocation ); + return false; } + lod->m_reversedIndicesValid = true; } lod->m_morphVertexDeclaration = -1; @@ -2333,10 +2193,9 @@ bool TriGeometryRes::CreateLodFromGrannyMesh( granny_mesh* grannyMesh, TriGeomet lod->m_allocationsValid = true; m_memoryUse += vbSize + ibSize; // Memory use is only approximate as a hint for the resource cache - if( m_reversedIndexBuffersRequested ) - { - m_memoryUse += ibSize; - } + + // Append reversed index buffer size also + m_memoryUse += ibSize; return true; } diff --git a/trinity/Resources/TriGeometryRes.h b/trinity/Resources/TriGeometryRes.h index 6516a7e56..ae7b45f6d 100644 --- a/trinity/Resources/TriGeometryRes.h +++ b/trinity/Resources/TriGeometryRes.h @@ -347,8 +347,6 @@ BLUE_CLASS( TriGeometryRes ) : typedef void ( *PerTriangleCallback )( void* context, const Vector3& p1, const Vector3& p2, const Vector3& p3 ); void ProcessMeshTriangles( int meshIx, PerTriangleCallback cb, void* cbContext ); - void RequestReversedIndexBuffers(); - void Reload(); // name for logging/debugging @@ -375,7 +373,6 @@ BLUE_CLASS( TriGeometryRes ) : int32_t m_forcedLodIndex = -1; bool m_forceLod = false; - bool m_reversedIndexBuffersRequested = false; private: // Provide the functions that do the actual work of loading and preparing. diff --git a/trinity/Tr2InstancedMesh.cpp b/trinity/Tr2InstancedMesh.cpp index 5c6475ca3..8a343f874 100644 --- a/trinity/Tr2InstancedMesh.cpp +++ b/trinity/Tr2InstancedMesh.cpp @@ -210,15 +210,6 @@ void Tr2InstancedMesh::SetInstanceGeometryRes( ITr2InstanceData* res ) // areas - Vector of mesh areas for which batches need to be added // data - Per-object data // -------------------------------------------------------------------------------------- -void Tr2InstancedMesh::GetBatches( ITriRenderBatchAccumulator* batches, - const Tr2MeshAreaVector* areas, - const Tr2PerObjectData* data, - float screenSize ) const -{ - GetBatches( batches, areas, data, screenSize, false ); -} - - void Tr2InstancedMesh::GetBatches( ITriRenderBatchAccumulator* batches, const Tr2MeshAreaVector* areas, const Tr2PerObjectData* data, diff --git a/trinity/Tr2InstancedMesh.h b/trinity/Tr2InstancedMesh.h index 9157df723..bca3d5411 100644 --- a/trinity/Tr2InstancedMesh.h +++ b/trinity/Tr2InstancedMesh.h @@ -85,14 +85,8 @@ BLUE_CLASS( Tr2InstancedMesh ) : void GetBatches( ITriRenderBatchAccumulator * batches, const Tr2MeshAreaVector* areas, const Tr2PerObjectData* data, - float screenSize = std::numeric_limits::max() ) const override; - - - void GetBatches( ITriRenderBatchAccumulator * batches, - const Tr2MeshAreaVector* areas, - const Tr2PerObjectData* data, - float screenSize, - bool reverseAreas ) const; + float screenSize = std::numeric_limits::max(), + bool reverseAreas = false ) const override; CcpMath::AxisAlignedBox GetBounds( const Matrix* boneTransforms = nullptr, const int32_t* meshBindingIndices = nullptr, size_t boneCount = 0, const Tr2MorphTargetAnimationData* morphTargets = nullptr, size_t morphTargetsCount = 0 ) const override; CcpMath::AxisAlignedBox GetAreaBounds( unsigned int areaIx, const Matrix* boneTransforms = nullptr ) const override; diff --git a/trinity/Tr2Mesh.cpp b/trinity/Tr2Mesh.cpp index 88f181f50..b203bc0f5 100644 --- a/trinity/Tr2Mesh.cpp +++ b/trinity/Tr2Mesh.cpp @@ -70,10 +70,6 @@ void Tr2Mesh::SetGeometryRes( TriGeometryRes* res ) if( m_geometryResource ) { m_geometryResource->AddNotifyTarget( this ); - if( HasReversedAreas() ) - { - m_geometryResource->RequestReversedIndexBuffers(); - } } } @@ -90,10 +86,6 @@ void Tr2Mesh::SetLowResGeometryRes( TriGeometryRes* res ) if( m_lowResGeometryResource ) { m_lowResGeometryResource->AddNotifyTarget( this ); - if( HasReversedAreas() ) - { - m_lowResGeometryResource->RequestReversedIndexBuffers(); - } } } @@ -232,18 +224,6 @@ bool Tr2Mesh::IsLoading() const return !m_loadFence.IsReached(); } -void Tr2Mesh::ReverseIndexBuffers() -{ - if( m_geometryResource ) - { - m_geometryResource->RequestReversedIndexBuffers(); - } - if( m_lowResGeometryResource ) - { - m_lowResGeometryResource->RequestReversedIndexBuffers(); - } -} - std::vector* Tr2Mesh::GetMorphTargetNames() const { if( !GetGeometryResource() ) diff --git a/trinity/Tr2Mesh.h b/trinity/Tr2Mesh.h index d0797b40a..c23e8a00f 100644 --- a/trinity/Tr2Mesh.h +++ b/trinity/Tr2Mesh.h @@ -56,7 +56,6 @@ BLUE_CLASS( Tr2Mesh ) : void SetGeometryRes( TriGeometryRes * res ); bool IsLoading() const override; - void ReverseIndexBuffers() override; ///////////////////////////////////////////////////////////////////////////////////// // INotify diff --git a/trinity/Tr2MeshArea.cpp b/trinity/Tr2MeshArea.cpp index d4b456bf5..9c8e4b731 100644 --- a/trinity/Tr2MeshArea.cpp +++ b/trinity/Tr2MeshArea.cpp @@ -138,12 +138,6 @@ bool Tr2MeshArea::GetReversed() const void Tr2MeshArea::SetReversed( bool reversed ) { m_reversed = reversed; - if( m_reversed ) - { - for_each( begin( m_ownerMeshes ), end( m_ownerMeshes ), []( auto mesh ) { - mesh->ReverseIndexBuffers(); - } ); - } } // ------------------------------------------------------------- diff --git a/trinity/Tr2MeshBase.cpp b/trinity/Tr2MeshBase.cpp index 247ffec7f..1db1a252e 100644 --- a/trinity/Tr2MeshBase.cpp +++ b/trinity/Tr2MeshBase.cpp @@ -86,10 +86,6 @@ void Tr2MeshBase::OnListModified( if( Tr2MeshAreaPtr area = BlueCastPtr( value ) ) { area->AddOwnerMesh( this ); - if( area->IsReversed() ) - { - ReverseIndexBuffers(); - } } break; case BELIST_REMOVED: @@ -106,10 +102,6 @@ void Tr2MeshBase::OnListModified( entity->AddOwnerMesh( this ); } } - if( HasReversedAreas() ) - { - ReverseIndexBuffers(); - } break; case BELIST_UNLOADSTART: for( ssize_t i = 0; i < list->GetSize(); ++i ) @@ -125,29 +117,6 @@ void Tr2MeshBase::OnListModified( } } -bool Tr2MeshBase::HasReversedAreas() const -{ - for( auto& list : m_areaLookupArray ) - { - for( auto& area : *list ) - { - if( area->IsReversed() ) - { - return true; - } - } - } - return false; -} - -void Tr2MeshBase::ReverseIndexBuffers() -{ - if( auto geometry = GetGeometryResource() ) - { - geometry->RequestReversedIndexBuffers(); - } -} - unsigned int Tr2MeshBase::FindJoint( const std::string* boneList, const int numBones, const char* name ) const { if( boneList && name ) @@ -369,7 +338,7 @@ bool Tr2MeshBase::GetDisplay() const return m_display; } -Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* area, const Tr2PerObjectData* data ) +Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* area, const Tr2PerObjectData* data, bool reverseWinding ) { Tr2RenderBatch batch; @@ -392,8 +361,9 @@ Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* are } auto& meshArea = lod->m_areas[std::max( 0, area->GetIndex() )]; + const bool reversed = area->GetReversed() != reverseWinding; - if( area->GetReversed() && !lod->m_reversedIndicesValid ) + if( reversed && !lod->m_reversedIndicesValid ) { return batch; } @@ -403,9 +373,9 @@ Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* are batch.SetPerObjectData( data ); - auto& indices = area->GetReversed() ? lod->m_reversedIndexAllocation : lod->m_indexAllocation; + auto& indices = reversed ? lod->m_reversedIndexAllocation : lod->m_indexAllocation; uint32_t startIndex; - if( area->GetReversed() ) + if( reversed ) { startIndex = indices.GetStartIndex() + lod->m_primitiveCount * 3 - meshArea.m_firstIndex - primCount * 3; } @@ -428,7 +398,8 @@ Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* are void Tr2MeshBase::GetBatches( ITriRenderBatchAccumulator* batches, const Tr2MeshAreaVector* areas, const Tr2PerObjectData* data, - float screenSize ) const + float screenSize, + bool reverseWinding ) const { if( !GetDisplay() ) { @@ -449,7 +420,7 @@ void Tr2MeshBase::GetBatches( ITriRenderBatchAccumulator* batches, for( auto& area : *areas ) { - if( auto batch = CreateGeometryBatch( lod, area, data ) ) + if( auto batch = CreateGeometryBatch( lod, area, data, reverseWinding ) ) { batch.SetPickingData( m_meshIndex, area->GetIndex() ); batches->Commit( batch ); diff --git a/trinity/Tr2MeshBase.h b/trinity/Tr2MeshBase.h index d7dd0e692..e665c62bf 100644 --- a/trinity/Tr2MeshBase.h +++ b/trinity/Tr2MeshBase.h @@ -33,7 +33,8 @@ BLUE_CLASS( Tr2MeshBase ) : virtual void GetBatches( ITriRenderBatchAccumulator * batches, const Tr2MeshAreaVector* areas, const Tr2PerObjectData* data, - float screenSize = std::numeric_limits::max() ) const; + float screenSize = std::numeric_limits::max(), + bool reverseWinding = false ) const; Tr2MeshAreaVector* GetAreas( TriBatchType areaType ); const Tr2MeshAreaVector* GetAreas( TriBatchType areaType ) const; @@ -99,9 +100,6 @@ BLUE_CLASS( Tr2MeshBase ) : void UseWithScreenSize( float screenSize, float worldRadius ) const; - virtual void ReverseIndexBuffers(); - bool HasReversedAreas() const; - protected: unsigned int FindJoint( const std::string* boneList, const int numBones, const char* name ) const; void CacheBounds(); @@ -146,7 +144,7 @@ TYPEDEF_BLUECLASS( Tr2MeshBase ); BLUE_DECLARE_VECTOR( Tr2MeshBase ); -Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* area, const Tr2PerObjectData* data ); +Tr2RenderBatch CreateGeometryBatch( TriGeometryResLodData* lod, Tr2MeshArea* area, const Tr2PerObjectData* data, bool reverseWinding = false ); #endif // Tr2MeshBase_h \ No newline at end of file