Browse Source

fix world model iteration bug

Kolja Strohm 2 years ago
parent
commit
d67c918b0f
6 changed files with 50 additions and 143 deletions
  1. 10 15
      DX11GraphicsApi.cpp
  2. 2 6
      DX12GraphicsApi.cpp
  3. 2 8
      Model3DCollection.h
  4. 20 20
      UIPixelShader.h
  5. 14 71
      Welt3D.cpp
  6. 2 23
      Welt3D.h

+ 10 - 15
DX11GraphicsApi.cpp

@@ -606,8 +606,7 @@ void DirectX11::renderObject( Model3D* zObj )
     if( !zObj->zModelData() )
         return;
     int curId = zObj->zModelData()->getId();
-    zObj->zModelData()->zDXVertexBuffer()->copieren();
-    zObj->zModelData()->zDXIndexBuffer()->copieren();
+    zObj->zModelData()->updateGPUMemory();
     Mat4< float > trans = Mat4< float >::identity();
     int anz = zObj->errechneMatrizen( trans, matrixBuffer );
     if( vertexShader )
@@ -643,6 +642,13 @@ void DirectX11::renderObject( Model3D* zObj )
         if( pixelShader )
             pixelShader->füllConstBuffer( (char*)&e, 3, sizeof( TexturEffect ) );
     }
+    DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
+    if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 2 )
+        f = DXGI_FORMAT_R16_UINT;
+    if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 1 )
+        f = DXGI_FORMAT_R8_UINT;
+    d3d11Context->IASetIndexBuffer( ((DX11Buffer*)zObj->zModelData()->zDXIndexBuffer())->zBuffer(), f, 0 );
+    d3d11Context->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
     for( auto i = zObj->zModelData()->getPolygons(); i; i++ )
     {
         if( zObj->needRenderPolygon( ind ) )
@@ -650,13 +656,6 @@ void DirectX11::renderObject( Model3D* zObj )
             Textur* t = zTextur->zPolygonTextur( ind );
             if( t && t->brauchtUpdate() )
                 t->updateTextur();
-            DXGI_FORMAT f = DXGI_FORMAT_R32_UINT;
-            if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 2 )
-                f = DXGI_FORMAT_R16_UINT;
-            if( zObj->zModelData()->zDXIndexBuffer()->getElementLength() == 1 )
-                f = DXGI_FORMAT_R8_UINT;
-            d3d11Context->IASetIndexBuffer( ((DX11Buffer*)zObj->zModelData()->zDXIndexBuffer())->zBuffer(), f, 0 );
-            d3d11Context->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
             if( t )
             {
                 ID3D11ShaderResourceView* v[ 3 ];
@@ -752,11 +751,8 @@ void DirectX11::renderKamera( Kam3D* zKamera )
     w->copyLight( diffuseLights, pointLights );
     int maxDist = 0;
     int minDist = 0x7FFFFFFF;
-    Model3DIterator* iterator = w->getIterator();
     Array<Model3D*> alphaModels;
-    while( iterator->hasNext() )
-    {
-        Model3D* obj = iterator->getNext();
+    w->forAll( [this, &minDist, &maxDist, &alphaModels]( Model3D* obj ) {
         float dist;
         if( isInFrustrum( obj->getPos(), obj->getRadius(), &dist ) )
         {
@@ -769,8 +765,7 @@ void DirectX11::renderKamera( Kam3D* zKamera )
             else
                 renderObject( obj );
         }
-    }
-    iterator->release();
+    } );
     maxDist++;
     if( alphaModels.getEintragAnzahl() )
     {

+ 2 - 6
DX12GraphicsApi.cpp

@@ -916,14 +916,10 @@ void DirectX12::renderKamera( Kam3D* zKamera )
     if( pixelShader )
         pixelShader->füllConstBuffer( (char*)&kamPos, 2, sizeof( float ) * 3 );
     Welt3D* w = zKamera->zWelt();
-    Model3DIterator* iterator = w->getIterator();
-    while( iterator->hasNext() )
-    {
-        Model3D* obj = iterator->getNext();
+    w->forAll( [this]( Model3D* obj ) {
         if( isInFrustrum( obj->getPos(), obj->getRadius() ) )
             renderObject( obj );
-    }
-    iterator->release();
+    } );
 }
 
 void DirectX12::presentFrame()

+ 2 - 8
Model3DCollection.h

@@ -1,20 +1,14 @@
 #pragma once
 #include "ReferenceCounter.h"
+#include <functional>
 
 namespace Framework
 {
     class Model3D;
 
-    class Model3DIterator : public virtual ReferenceCounter
-    {
-    public:
-        virtual Model3D* getNext() = 0;
-        virtual bool hasNext() = 0;
-    };
-
     class Model3DCollection : public virtual ReferenceCounter
     {
     public:
-        virtual Model3DIterator* getIterator() = 0;
+        virtual void forAll( std::function<void( Model3D* )> f ) = 0;
     };
 }

+ 20 - 20
UIPixelShader.h

@@ -353,10 +353,10 @@ ret
 
 const BYTE UIPixelShader[] =
 {
-     68,  88,  66,  67,  33,   9, 
-     50, 196, 128, 118, 184,  11, 
-    231, 152, 220,  66, 100, 205, 
-    167, 177,   1,   0,   0,   0, 
+     68,  88,  66,  67, 156, 195, 
+    156, 236, 219, 228,  33, 184, 
+    111, 245,  63,  35,  72,   0, 
+    211,  34,   1,   0,   0,   0, 
      64, 134,   0,   0,   6,   0, 
       0,   0,  56,   0,   0,   0, 
     192,   6,   0,   0,  80,   7, 
@@ -1815,10 +1815,10 @@ const BYTE UIPixelShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0, 148,  46,  49,   1, 
-     96,  37, 219,  97,   1,   0, 
-      0,   0, 247, 227, 107, 170, 
-     34, 189,  14,  74, 165, 108, 
-     30, 162, 109,  29, 166,  49, 
+    202, 235, 225,  97,   1,   0, 
+      0,   0, 101,  13, 141, 190, 
+    165,  83, 124,  78, 186, 239, 
+    212,   8, 168,  21, 140, 208, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   1,   0,   0,   0, 
       1,   0,   0,   0,   0,   0, 
@@ -1990,9 +1990,9 @@ const BYTE UIPixelShader[] =
     242,  56,   1,   0,  43, 236, 
       3,   0,  28,  19,   2,   0, 
      65,  36,   1,   0, 236, 179, 
-      1,   0, 111, 129,   0,   0, 
+      1,   0, 156,  25,   2,   0, 
     125,  10,   2,   0, 125, 181, 
-      2,   0,  37, 171,   1,   0, 
+      2,   0, 227,  23,   0,   0, 
     193,  33,   3,   0,  65, 185, 
       2,   0,   9, 241,   2,   0, 
     146, 230,   3,   0, 125, 218, 
@@ -3010,8 +3010,8 @@ const BYTE UIPixelShader[] =
     120, 116, 117, 114, 101,  50, 
      68,  32, 115, 104,  97, 100, 
      27, 226,  48,   1, 128,   0, 
-      0,   0, 134,  51,   5,  92, 
-    132,   5, 216,   1,   1,   0, 
+      0,   0,  12, 211, 200,  38, 
+    142,   9, 216,   1,   1,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
@@ -4253,14 +4253,14 @@ const BYTE UIPixelShader[] =
       0,   0,  23,   0,   1,   0, 
       5,  16,   0,   0,  14,   0, 
      23,  21,   0,  16,   0,   0, 
-      3,   2, 112, 215,   0,   0, 
+      3,   2, 240, 182,   0,   0, 
     242, 241,  10,   0,  24,  21, 
       8,  16,   0,   0,   1,   0, 
       1,   0,  10,   0,  24,  21, 
       9,  16,   0,   0,   1,   0, 
       0,   2,  14,   0,  23,  21, 
       0,   0,   0,   0,  10,   2, 
-    112, 215,   0,   0, 242, 241, 
+    240, 182,   0,   0, 242, 241, 
      10,   0,  24,  21,  11,  16, 
       0,   0,   1,   0,   1,   0, 
      10,   0,  24,  21,  12,  16, 
@@ -5484,11 +5484,11 @@ const BYTE UIPixelShader[] =
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0, 148,  46, 
-     49,   1,  96,  37, 219,  97, 
-      1,   0,   0,   0, 247, 227, 
-    107, 170,  34, 189,  14,  74, 
-    165, 108,  30, 162, 109,  29, 
-    166,  49, 128,   0,   0,   0, 
+     49,   1, 202, 235, 225,  97, 
+      1,   0,   0,   0, 101,  13, 
+    141, 190, 165,  83, 124,  78, 
+    186, 239, 212,   8, 168,  21, 
+    140, 208, 128,   0,   0,   0, 
      47,  76, 105, 110, 107,  73, 
     110, 102, 111,   0,  47, 110, 
      97, 109, 101, 115,   0,  47, 
@@ -5588,7 +5588,7 @@ const BYTE UIPixelShader[] =
       2,   0,   9,   0, 204,   8, 
       0,   0,   0,   0,   0,   0, 
     164,  14,   0,   0,   1,   0, 
-    220, 226,   0,   0,   0,   0, 
+      0,   0,   0,   0,   0,   0, 
       0,   0,   0,   0,   0,   0, 
       0,   0,  84, 101, 120, 116, 
     117, 114, 101,  80, 105, 120, 

+ 14 - 71
Welt3D.cpp

@@ -8,63 +8,6 @@
 
 using namespace Framework;
 
-Welt3DIterator::Welt3DIterator( Welt3D* zWelt )
-    : ReferenceCounter(),
-    modelIterator( zWelt->members->begin() ),
-    collectionIterator( zWelt->modelCollections.begin() )
-{
-    this->zWelt = zWelt;
-    currentCollectionIterator = collectionIterator.hasNext() ? collectionIterator.next()->getIterator() : 0;
-    nextModel = 0;
-    calculateNext();
-}
-
-Welt3DIterator::~Welt3DIterator()
-{
-    if( currentCollectionIterator )
-        currentCollectionIterator->release();
-    this->zWelt->unlock();
-}
-
-void Welt3DIterator::calculateNext()
-{
-    if( modelIterator )
-    {
-        nextModel = modelIterator;
-        ++modelIterator;
-    }
-    else if( currentCollectionIterator && currentCollectionIterator->hasNext() )
-    {
-        nextModel = currentCollectionIterator->getNext();
-    }
-    else
-    {
-        nextModel = 0;
-        while( collectionIterator.hasNext() && !nextModel )
-        {
-            if( currentCollectionIterator )
-                currentCollectionIterator->release();
-            currentCollectionIterator = collectionIterator.next()->getIterator();
-            if( currentCollectionIterator->hasNext() )
-                nextModel = currentCollectionIterator->getNext();
-        }
-    }
-}
-
-Model3D* Welt3DIterator::getNext()
-{
-    Model3D* tmp = nextModel;
-    if( nextModel )
-        calculateNext();
-    return tmp;
-}
-
-bool Welt3DIterator::hasNext()
-{
-    return nextModel != 0;
-}
-
-
 
 // Inhalt der Welt3D Klasse aus Welt3D.h
 // Konstructor
@@ -225,8 +168,9 @@ void Welt3D::doMausEreignis( MausEreignis3D& me )
 bool Welt3D::tick( double tickval )
 {
     cs.lock();
-    for( auto m : *members )
+    forAll( [this, &tickval]( Model3D* m ) {
         rend |= m->tick( tickval );
+    } );
     cs.unlock();
     bool tmp = rend;
     rend = 0;
@@ -240,29 +184,28 @@ bool Welt3D::tick( double tickval )
 int Welt3D::traceRay( Vec3< float >& point, Vec3< float >& dir )
 {
     float min = INFINITY;
-    int minId = -1;
-    int index = 0;
     int pId = 0;
-    for( auto m : *members )
-    {
+    Model3D* nearest = 0;
+    forAll( [this, &point, &dir, &pId, &min, &nearest]( Model3D* m ) {
         float tmp = m->traceRay( point, dir, min, pId );
         if( min > tmp && tmp >= 0 )
         {
             min = tmp;
-            minId = index;
+            nearest = m;
         }
-        index++;
-    }
-    if( minId >= 0 )
-        return members->z( minId )->traceRay( point, dir, pId, this );
+    } );
+    if( nearest )
+        return nearest->traceRay( point, dir, pId, this );
     return 0xFF000000;
 }
 
-// Gibt einen Iterator zurück, mit dem alle Members aufgezählt werden können
-Model3DIterator* Welt3D::getIterator()
+//! führt eine funktion auf jedem Model aus
+void Framework::Welt3D::forAll( std::function<void( Model3D* )> f )
 {
-    lock();
-    return new Welt3DIterator( this );
+    for( auto m : *members )
+        f( m );
+    for( auto c : modelCollections )
+        c->forAll( f );
 }
 
 int Framework::Welt3D::getPointLightCount() const

+ 2 - 23
Welt3D.h

@@ -15,25 +15,6 @@ namespace Framework
     class DXBuffer;
     class Welt3D;
 
-    class Welt3DIterator : public Model3DIterator
-    {
-    private:
-        Iterator<Model3D*> modelIterator;
-        Iterator<Model3DCollection*> collectionIterator;
-        Model3DIterator* currentCollectionIterator;
-        Model3D* nextModel;
-        Welt3D* zWelt;
-
-        Welt3DIterator( Welt3D* zWelt );
-        ~Welt3DIterator();
-        void calculateNext();
-    public:
-        Model3D* getNext() override;
-        bool hasNext() override;
-
-        friend Welt3D;
-    };
-
     //! Speichert alle 3D Zeichnungen einer Szene ab
     class Welt3D : public Model3DCollection
     {
@@ -82,8 +63,8 @@ namespace Framework
         //! \param dir Die Richtung des Strahls
         //! \return Die Farbe des Strahls
         DLLEXPORT virtual int traceRay( Vec3< float >& point, Vec3< float >& dir );
-        //! Gibt einen Iterator zurück, mit dem alle Members aufgezählt werden können
-        DLLEXPORT Model3DIterator* getIterator() override;
+        //! führt eine funktion auf jedem Model aus
+        DLLEXPORT virtual void forAll( std::function<void( Model3D* )> f ) override;
         //! Gibt die Anzahl an Punkt Lichtquellen zurück
         DLLEXPORT int getPointLightCount() const;
         //! Gibt die Anzahl an Richtungs Lichtquellen zurück
@@ -104,7 +85,5 @@ namespace Framework
         //! Gibt die Referenz auf eine Punkt Lichtquelle zurück
         //! \param index Der Index der Lichtquelle
         DLLEXPORT PointLight& getPointLight( int index ) const;
-
-        friend Welt3DIterator;
     };
 }