Browse Source

Unterstützung von Richtungs und Punktlichtquellen in Welt3d unter DirectX11 hinzugefügt

Kolja Strohm 4 years ago
parent
commit
129129156f
3 changed files with 46 additions and 29 deletions
  1. 4 29
      DX11GraphicsApi.cpp
  2. 28 0
      Welt3D.cpp
  3. 14 0
      Welt3D.h

+ 4 - 29
DX11GraphicsApi.cpp

@@ -381,7 +381,7 @@ void DirectX11::initialize( WFenster *fenster, Vec2<int> backBufferSize, bool fu
     pixelShader->erstelleConstBuffer( sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model
     pixelShader->erstelleConstBuffer( sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model
     // TODO: Remove Following Test Code
-    int lc[] = { 1, 0 };
+    int lc[] = { 0, 0 };
     pixelShader->füllConstBuffer( (char *)lc, 2, sizeof( int ) * 2 );
 
     // Create a texture sampler state description.
@@ -475,36 +475,8 @@ void DirectX11::initialize( WFenster *fenster, Vec2<int> backBufferSize, bool fu
     vertexBuffer = new DX11Buffer( sizeof( Vertex3D ), d3d11Device, d3d11Context, D3D11_BIND_VERTEX_BUFFER );
     indexBuffer = new DX11Buffer( sizeof( int ), d3d11Device, d3d11Context, D3D11_BIND_INDEX_BUFFER );
 
-    DiffuseLight dl[ 1 ];
-    dl[ 0 ].direction = Vec3< float >( 50.f, 50.f, -500.f ).normalize();
-    dl[ 0 ].color = Vec3<float>( 0.5f, 0.5f, 0.5f );
     diffuseLights = new DX11StructuredBuffer( sizeof( DiffuseLight ), d3d11Device, d3d11Context );
-    diffuseLights->setData( dl );
-    diffuseLights->setLength( sizeof( dl ) );
-    diffuseLights->copieren();
-    PointLight pl[ 6 ];
-    pl[ 0 ].position = Vec3< float >( 0, 130, 0 );
-    pl[ 0 ].color = Vec3< float >( 1.f, 1.f, 0.f );
-    pl[ 0 ].radius = 100;
-    pl[ 1 ].position = Vec3< float >( 150, 130, 0 );
-    pl[ 1 ].color = Vec3< float >( 0.f, 1.f, 0.f );
-    pl[ 1 ].radius = 100;
-    pl[ 2 ].position = Vec3< float >( 150, 130, 150 );
-    pl[ 2 ].color = Vec3< float >( 0.f, 0.f, 1.f );
-    pl[ 2 ].radius = 100;
-    pl[ 3 ].position = Vec3< float >( -150, 130, 0 );
-    pl[ 3 ].color = Vec3< float >( 1.f, 0.f, 1.f );
-    pl[ 3 ].radius = 100;
-    pl[ 4 ].position = Vec3< float >( 0, 130, 150 );
-    pl[ 4 ].color = Vec3< float >( 0.f, 1.f, 1.f );
-    pl[ 4 ].radius = 100;
-    pl[ 5 ].position = Vec3< float >( -150, 130, 150 );
-    pl[ 5 ].color = Vec3< float >( 1.f, 0.f, 0.f );
-    pl[ 5 ].radius = 100;
     pointLights = new DX11StructuredBuffer( sizeof( PointLight ), d3d11Device, d3d11Context );
-    pointLights->setData( pl );
-    pointLights->setLength( sizeof( pl ) * 6 );
-    pointLights->copieren();
 }
 
 void DirectX11::update()
@@ -746,6 +718,9 @@ void DirectX11::renderKamera( Kam3D *zKamera )
         pixelShader->füllConstBuffer( (char *)& kamPos, 0, sizeof( float ) * 3 );
     Welt3D *w = zKamera->zWelt();
     w->lock();
+    int lc[] = { w->getDiffuseLightCount(), w->getPointLightCount() };
+    pixelShader->füllConstBuffer( (char *)lc, 2, sizeof( int ) * 2 );
+    w->copyLight( diffuseLights, pointLights );
     int alphaAnzahl = 0;
     int maxDist = 0;
     int minDist = 0x7FFFFFFF;

+ 28 - 0
Welt3D.cpp

@@ -2,6 +2,8 @@
 #include "Zeichnung3D.h"
 #include "MausEreignis.h"
 #include "Model3D.h"
+#include "GraphicsApi.h"
+#include "DXBuffer.h"
 
 using namespace Framework;
 
@@ -10,6 +12,10 @@ using namespace Framework;
 Welt3D::Welt3D()
 {
     members = new RCArray< Model3D >();
+    pointLightCount = 0;
+    diffuseLightCount = 0;
+    pointLights = 0;
+    diffuseLights = 0;
     ref = 1;
     rend = 0;
 }
@@ -18,6 +24,8 @@ Welt3D::Welt3D()
 Welt3D::~Welt3D()
 {
     members->release();
+    delete[] pointLights;
+    delete[] diffuseLights;
 }
 
 // Blockiert den zugriff auf das Objekt und wartet gegebenfalls auf den Zugriff
@@ -156,6 +164,26 @@ Iterator< Model3D * > Welt3D::getMembers()
     return members->getIterator();
 }
 
+int Framework::Welt3D::getPointLightCount() const
+{
+    return pointLightCount;
+}
+
+int Framework::Welt3D::getDiffuseLightCount() const
+{
+    return diffuseLightCount;
+}
+
+void Framework::Welt3D::copyLight( DXBuffer *zDiffuse, DXBuffer *zPoints ) const
+{
+    zDiffuse->setData( diffuseLights );
+    zDiffuse->setLength( diffuseLightCount * sizeof( DiffuseLight ) );
+    zDiffuse->copieren();
+    zPoints->setData( pointLights );
+    zPoints->setLength( pointLightCount * sizeof( PointLight ) );
+    zPoints->copieren();
+}
+
 // Erhöht den Reference Counting Zähler.
 //  return: this.
 Welt3D *Welt3D::getThis()

+ 14 - 0
Welt3D.h

@@ -10,11 +10,17 @@ namespace Framework
     class Render3D; // Render3D.h
     struct MausEreignis3D; // MausEreignis.h
     class Model3D;
+    struct DiffuseLight;
+    struct PointLight;
 
     // Speichert alle 3D Zeichnungen einer Szene ab
     class Welt3D
     {
     private:
+        DiffuseLight *diffuseLights;
+        int diffuseLightCount;
+        PointLight *pointLights;
+        int pointLightCount;
         RCArray< Model3D > *members;
         bool rend;
         Critical cs;
@@ -49,6 +55,14 @@ namespace Framework
         __declspec( dllexport ) virtual int traceRay( Vec3< float > &point, Vec3< float > &dir );
         // Gibt einen Iterator zurück, mit dem alle Members aufgezählt werden können
         __declspec( dllexport ) Iterator< Model3D * > getMembers();
+        // Gibt die Anzahl an Punkt Lichtquellen zurück
+        __declspec( dllexport ) int getPointLightCount() const;
+        // Gibt die Anzahl an Richtungs Lichtquellen zurück
+        __declspec( dllexport ) int getDiffuseLightCount() const;
+        // Kopiert alle Lichtquellen in die Buffer
+        //  zDiffuse: der Buffer für die rechtungs Lichtquellen
+        //  zPoints: der Buffer für die Punkt Lichtquellen
+        __declspec( dllexport ) void copyLight( DXBuffer *zDiffuse, DXBuffer *zPoints ) const;
         // Erhöht den Reference Counting Zähler.
         //  return: this.
         __declspec( dllexport ) Welt3D *getThis();