SOL9 Sample: Direct3DX11MultipleLightedSpheresEffect

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * Direct3DX11MultipleLightedSpheresEffect.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2016/02/10
// This is based on Direct3DX10MultipleLightedSpheresEffect.cpp project.
// 2017/01/18 Updated display method to draw true spheres even if the internak window was resized. 
// 2017/01/28 Updated to use ModuleFileName class and caught macro.

#pragma warning(disable : 4005) 
#pragma warning(disable : 4838) 

#define COMMONCONTROLS_V6

#include <sol/TickCounter.h>

#include <sol/direct3d11/DirectX3D11MainView.h>
#include <sol/direct3d11/DirectX3D11View.h>

#include <sol/direct3d11/Direct3D11RenderTargetView.h>
#include <sol/direct3d11/Direct3D11DepthStencilView.h>

#include <sol/direct3d11/Direct3D11Texture2D.h>
#include <sol/direct3d11/D3D11Texture2DDesc.h>
#include <sol/direct3d11/D3D11DepthStencilViewDesc.h>

#include <sol/direct3d11/Direct3DX11EffectModel.h>
#include <sol/direct3d11/Direct3DX11EffectTechnique.h>
#include <sol/direct3d11/Direct3DX11EffectPass.h>

#include <sol/direct3d11/Direct3D11RasterizerState.h>
#include <sol/direct3d11/D3D11InputElements.h>
#include <sol/direct3d11/Direct3D11InputLayout.h>

#include <sol/direct3d11/Direct3DX11Sphere.h>

#include "resource.h"

namespace SOL {
  
class MainView :public DirectX3D11MainView {
  
private:
  //////////////////////////////////////////////
  //Inner class starts
  class SimpleView : public DirectX3D11View {
  private: 
    SmartPtr<Direct3D11RenderTargetView>  renderTargetView;
    SmartPtr<Direct3D11DepthStencilView>  depthStencilView;
    SmartPtr<Direct3D11RasterizerState>   rasterizerState;
    SmartPtr<Direct3DX11EffectModel>      model;
   
    SmartPtr<Direct3DX11Sphere>           sphere;
    SmartPtr<Direct3DX11Sphere>           lightSource;

    SmartPtr<Direct3D11InputLayout>       inputLayout;

    XMMatrix                     world;
    XMMatrix                     view;
    XMMatrix                     projection;

    StringT<TCHAR>               directory;
    float                        angle;

  public:
    void deleteViews()
    {
      Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
    
      d3d11ImmediateContext -> setOMRenderTargets(0, NULL, NULL);
      renderTargetView = NULL;
      depthStencilView = NULL;
    }

    virtual void createViews()
    {
      int width  = 0;
      int height = 0;
     
      validateClientSize(width, height);

      try {
        Direct3D11Device*   d3d11Device  = getD3D11Device();
        DirectXGISwapChain* swapChain = getSwapChain();
        Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();

        d3d11ImmediateContext ->setOMRenderTargets(0, NULL, NULL);

        //1 Create an instance of Direct3D11RenderTargetView
        Direct3D11Texture2D renderTargetViewTexture(*swapChain); ; 

        renderTargetView = new Direct3D11RenderTargetView(*d3d11Device, renderTargetViewTexture, NULL);

        //2 Create a temporary depthDesc(D3D11Texture2DDesc).
        D3D11Texture2DDesc depthDesc;
        depthDesc.width(width);
        depthDesc.height(height);
        depthDesc.mipLevels(1);
        depthDesc.arraySize(1);
        depthDesc.format(DXGI_FORMAT_D32_FLOAT);
        depthDesc.sampleDescCount(1);
        depthDesc.sampleDescQuality(0);
        depthDesc.usage(D3D11_USAGE_DEFAULT);
        depthDesc.bindFlags(D3D11_BIND_DEPTH_STENCIL);

        //3 Create a temporary depthStencilTexture(Direct3D11Texture2D) from texture2DDesc.
        Direct3D11Texture2D depthStencilTexture(*d3d11Device, depthDesc); 

        //4 Create a temporary depthStencilViewDesc(D3D11DepthStencilViewDesc) 
        D3D11DepthStencilViewDesc depthStencilViewDesc(DXGI_FORMAT_D32_FLOAT, D3D11_DSV_DIMENSION_TEXTURE2D);

        //5 Create an instance of Direct3DDepthStencilView from depthStencilTexture and depthStencilViewDesc
        depthStencilView = new Direct3D11DepthStencilView(*d3d11Device, depthStencilTexture, depthStencilViewDesc);

        ID3D11RenderTargetView* targetView   = *renderTargetView; 
        //6 Set renderTargetView and depthStencilView to id3d11ImmediateContext
        d3d11ImmediateContext ->setOMRenderTargets(1, &targetView, *depthStencilView);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }

    void createEffect()
    {
      try {
        Direct3D11Device* d3d11Device = getD3D11Device();
        TCHAR fullpath[MAX_PATH];
        _stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\fx\\%s"), 
                  (const TCHAR*)directory, _T("Tutorial06.fx"));
        
        Args args;
        args.set(XmNrenderTechnique,       "Render");
        args.set(XmNrenderLightTechnique,  "RenderLight");

        args.set(XmNview,                   "View");
        args.set(XmNworld,                  "World");
        args.set(XmNprojection,             "Projection");
        args.set(XmNlightDirection,         "LightDirection");
        args.set(XmNlightColor,             "LightColor" );
        args.set(XmNoutputColor,            "OutputColor" );
        model = new Direct3DX11EffectModel(*d3d11Device,  
                  fullpath, 
                  args);

      } catch (Exception& ex) {
        caught(ex);
      }
    }

    void createInputLayout()
    {
      try {
        Direct3D11Device* d3d11Device = getD3D11Device();
        Direct3DX11EffectTechnique* technique = model -> getRenderTechnique();
        Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
        
        Direct3DX11EffectPass effectPass =  technique->getPassByIndex(0);

        D3DX11_PASS_DESC passDesc;
        effectPass.getDesc(passDesc);
        size_t count = 0;
        const D3D11_INPUT_ELEMENT_DESC* inputElementDesc = D3D11InputElements::getPosNormalDesc(count);

        inputLayout = new Direct3D11InputLayout(
              *d3d11Device, 
              inputElementDesc, 
              count, 
              passDesc.pIAInputSignature,
              passDesc.IAInputSignatureSize);

        d3d11ImmediateContext->setIAInputLayout(*inputLayout);

      } catch (Exception& ex) {
        caught(ex);
      }
    }

    void createSpheres()
    {
      try {
        Direct3D11Device* d3d11Device = getD3D11Device();
        Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();

        float fRadius = 1.0f;
        UINT  uSlices = 40;
        UINT  uStacks = 40;
      
        sphere = new Direct3DX11Sphere(d3d11Device, d3d11ImmediateContext,
           fRadius, uSlices, uStacks);
        
        fRadius = 1.0f;
        lightSource = new Direct3DX11Sphere(d3d11Device, d3d11ImmediateContext,
           fRadius, uSlices, uStacks);

      } catch (Exception& ex) {
        caught(ex);
      }
    }

    void createRasterizerState()
    {
      try {
        Direct3D11Device* d3d11Device = getD3D11Device();
        Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();

        D3D11_RASTERIZER_DESC desc;
        memset(&desc, 0, sizeof(desc));
        desc.CullMode               = D3D11_CULL_NONE;
        desc.FillMode               = D3D11_FILL_SOLID;
        desc.FrontCounterClockwise  = true;
        desc.DepthBias              = false;
        desc.DepthClipEnable        = true;
        desc.ScissorEnable          = false;
        desc.MultisampleEnable      = false;
        desc.AntialiasedLineEnable  = true;

        rasterizerState = new Direct3D11RasterizerState (*d3d11Device, &desc);

        d3d11ImmediateContext->setRSState(*rasterizerState);

      } catch (Exception& ex) {
        caught(ex);
      }
    }

    void setShaderMatrices()
    {
      int width = 0;
      int height = 0;
      getClientSize(width, height);

      try {

        XMVECTOR eye = XMVectorSet(  0.0f,  2.0f, -12.0f,  0.0f );
        XMVECTOR at  = XMVectorSet(  0.0f,  0.0f,  0.0f,  0.0f );
        XMVECTOR up  = XMVectorSet(  0.0f,  1.0f,  0.0f,  0.0f );

        view = XMMatrix::lookAtLH(eye, at, up);

        projection = XMMatrix::perspectiveFovLH( XM_PIDIV2*0.3f, width / (FLOAT)height, 0.01f, 100.0f );

        model -> setView(view);
        model -> setProjection(projection);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }

    virtual void initialize()
    {
      try {
        createEffect();

        createInputLayout();

        createSpheres();
        
        createRasterizerState();  

        setShaderMatrices();

        createViews();

        postResizeRequest();

      } catch (Exception& ex) {
        caught(ex);
      }
    }

    //This method is based on the following samples,
    //C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\Direct3D11-work\Tutorials\Tutorial06
    virtual void display()
    {     
      try {
        
        Direct3D11Device*  d3d11Device = getD3D11Device();
        DirectXGISwapChain* swapChain = getSwapChain();
        Direct3D11ImmediateContext* d3d11ImmediateContext = getD3D11ImmediateContext();
        
        if (renderTargetView != NULL && 
            depthStencilView != NULL && 
            model != NULL &&
            sphere != NULL &&
            lightSource != NULL) {
 
          XMFLOAT4 lightDirections[2] ={
            XMFLOAT4( -0.6f, 0.6f, -0.0f, 1.0f ),
            XMFLOAT4(  0.0f, 0.0f, -1.0f, 1.0f ),
          };
          XMFLOAT4 lightColors[2] = {
            XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f),
            XMFLOAT4(1.0f, 0.5f, 0.0f, 1.0f)
          };
          //2017/01/18
          setShaderMatrices();
              
          d3d11ImmediateContext -> clear(*renderTargetView, XMColor(0.0f, 0.1f, 0.2f, 0.0f));
          d3d11ImmediateContext -> clear(*depthStencilView);

          XMMatrix rotate   = XMMatrix::rotationY( -2.0f * angle );
          XMVector lightDir = lightDirections[1];

          lightDirections[1] =lightDir.d3Transform(rotate);
              
          XMFLOAT3 origin(0, 0, 0);
          world = XMMatrix::rotationY(angle);

          XMMatrix temp = XMMatrix::translation( origin.x, 0, origin.z);
          world *= temp;
              
          model -> setWorld(world );
          model -> setView( view);
          model -> setProjection( projection );
                            
          Direct3DX11EffectTechnique* render = model -> getRenderTechnique();
          D3DX11_TECHNIQUE_DESC techDesc;
          render -> getDesc(&techDesc);
          
          model -> getLightDirection() -> setFloatVectorArray( ( float* )lightDirections, 0, 2 );
          model -> getLightColor()     -> setFloatVectorArray( ( float* )lightColors, 0, 2 );
          
          //Draw the sphere.
          for( UINT i = 0; i < techDesc.Passes; i++ ) {
            render -> getPassByIndex(i) -> Apply(0, *d3d11ImmediateContext);
            sphere -> drawIndexed();
          }
            
         //Draw the sphere lightSource.
          for( int m = 0; m < 2; m++ ) {
            XMFLOAT3 lightPos = XMFLOAT3(lightDirections[m].x * 3.5f,
                              lightDirections[m].y * 3.5f,
                              lightDirections[m].z * 3.5f);
                     
            XMMatrix light = XMMatrix::translation(lightPos.x, lightPos.y,  lightPos.z );
            XMMatrix scale = XMMatrix::scaling(0.2f, 0.2f, 0.2f );
            light = scale * light;
            model -> setWorld(light);
            model -> getOutputColor() -> setFloatVector( ( float* )&lightColors[m] );
         
            Direct3DX11EffectTechnique* renderLight = model -> getRenderLightTechnique();
            D3DX11_TECHNIQUE_DESC renderTechDesc;
            renderLight -> getDesc(&renderTechDesc);

            //lightSource -> draw(*renderLight);
            for( UINT i = 0; i < renderTechDesc.Passes; i++ ) {
              renderLight -> getPassByIndex(i) -> Apply(0, *d3d11ImmediateContext);
              lightSource -> drawIndexed();
            }
          }
        }
        
        swapChain -> present();

      } catch (Exception& ex) {
        caught(ex);
      }
    }
        
  public:
    virtual void render()
    {
      angle += 0.01f;
      display();
    }

  public:
    // Constructor
    SimpleView(DirectX3D11MainView* parent, const TCHAR* name, Args& args)
   :DirectX3D11View(parent, name, args ),
    directory(_T("")),
    angle(0.0f)
    {
      directory = (const TCHAR*)args.get(XmNapplicationDirectory);

      world = XMMatrixIdentity();
      projection = XMMatrixIdentity(); 
      view = XMMatrixIdentity();
    }

    ~SimpleView()
    {
    }
    
  private:
    void resize(int width, int height)
    {
      Direct3D11Device*   device = getD3D11Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      if (device           == NULL && 
          swapChain        == NULL && 
          renderTargetView == NULL &&
          depthStencilView == NULL) {
        return ;
      }
    
      try {
        // 1 Delete existing rendarTargetView and depthStencilView.
        deleteViews();

        // 2 ResizeBuffers swapChain(IDXGISwapChain)
        swapChain -> resizeBuffers(width, height); 
     
        // 3 Recreate rendarTargetView and depthStencilView.
        createViews();
      
        // 4 SetViewPort
        setViewPort(width, height);

      } catch (Exception& ex) {
        caught(ex);
      }
    }

  };
  //////////////////////////////////////////////
  // Inner class ends.
  
private:
  SmartPtr<SimpleView> view;
  TickCounter tickCounter;

public:
  // Constructor
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectX3D11MainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN|WS_CLIPSIBLINGS) )
  {    
    DWORD interval = (DWORD)args.get(XmNrenderingInterval);
    tickCounter.setInterval(interval);

    const TCHAR* directory = (const TCHAR*)args.get(XmNapplicationDirectory);
    // 1 Create a view of SimpleView.
    Args ar;
    int width  = 0;
    int height = 0;
    getClientSize(width, height);

    ar.set(XmNwidth, width);
    ar.set(XmNheight,height);
    ar.set(XmNapplicationDirectory, directory);
    ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
    view = new SimpleView(this, _T(""), ar);

    // 2 Post a resize request to this MainView.
    postResizeRequest();
  }
  
public:
  ~MainView()
  {
  }

  virtual void render()
  {
    if (view != nullptr) {
      if (tickCounter.timeout()) {
        view -> render();
        view -> update();
      }
    }
  }

private:
  void resize(int width, int height)
  {
    if (view != nullptr) {
      view -> reshape(0, 0, width, height);
    }
  }
};
  
}

//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* directory = module.getDirectory();
  const TCHAR* appClass =  module.getAppName(); 
  
  try {
    Application applet(appClass, argc, argv);

    Args args;
    args.set(XmNapplicationDirectory, directory);
    args.set(XmNwidth,  640);
    args.set(XmNheight, 480);
    args.set(XmNrenderingInterval, 10);  //10 millisec

    MainView mainv(applet, appClass, args);
    mainv.realize();

    applet.run(&mainv);

  } catch (Exception& ex) {
    caught(ex);
  }
}


Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.