SOL9 Sample: Direct3DX10TorusMesh

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2015/11/10

#define COMMONCONTROLS_V6

#include <sol/TickCounter.h>

#include <sol/direct3d10/DirectX3D10MainView.h>
#include <sol/direct3d10/DirectX3D10View.h>

#include <sol/direct3d10/Direct3D10RenderTargetView.h>
#include <sol/direct3d10/Direct3D10DepthStencilView.h>

#include <sol/direct3d10/Direct3D10Texture2D.h>
#include <sol/direct3d10/Direct3DX10Font.h>
#include <sol/direct3d10/D3D10Texture2DDesc.h>
#include <sol/direct3d10/D3D10DepthStencilViewDesc.h>
#include <sol/direct3d10/Direct3DX10Mesh.h>

#include <sol/direct3d10/D3DXVertex.h>
#include <sol/direct3d10/Direct3D10EffectModel.h>
#include <sol/direct3d10/Direct3D10EffectTechnique.h>
#include <sol/direct3d10/Direct3D10EffectPass.h>

#include <sol/direct3d10/Direct3D10EffectMatrixVariable.h>
#include <sol/direct3d10/Direct3D10RasterizerState.h>

#include <sol/direct3d10/Direct3D10InputLayout.h>

#include <sol/direct3d10/Direct3DX10Torus.h>

#include "resource.h"

namespace SOL {
  
class MainView :public DirectX3D10MainView {
  
private:
  //////////////////////////////////////////////
  //Inner class starts
  class SimpleView : public DirectX3D10View {
  private: 
    Direct3D10RenderTargetView*  renderTargetView;
    Direct3D10DepthStencilView*  depthStencilView;

    Direct3D10RasterizerState*   rasterizerState;

    Direct3D10EffectModel*       model;
    
    Direct3D10EffectTechnique*   technique;

    Direct3DX10Torus*            mesh;
    
    Direct3D10InputLayout*       inputLayout;

    D3DXMATRIX          worldMatrix;
    D3DXMATRIX          viewMatrix;
    D3DXMATRIX          projectionMatrix;

    StringT<TCHAR>      directory;

    float               angle;

  public:
    void deleteViews()
    {
      Direct3D10Device* d3d10device = getD3D10Device();
    
      d3d10device ->setOMRenderTargets(0, NULL, NULL);
 
      if (renderTargetView) {
        delete renderTargetView;
        renderTargetView = NULL;
      }

      if (depthStencilView) {
        delete depthStencilView;
        depthStencilView = NULL;
      }
    }

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

      try {
        Direct3D10Device*   d3d10Device  = getD3D10Device();
        DirectXGISwapChain* swapChain = getSwapChain();

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

        //1 Create an instance of Direct3D10RenderTargetView
        Direct3D10Texture2D renderTargetViewTexture(*swapChain); ; 

        renderTargetView = new Direct3D10RenderTargetView(*d3d10Device, renderTargetViewTexture, NULL);

        //2 Create a temporary depthDesc(D3D10Texture2DDesc).
        D3D10Texture2DDesc 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(D3D10_USAGE_DEFAULT);
        depthDesc.bindFlags(D3D10_BIND_DEPTH_STENCIL);

        //3 Create a temporary depthStencilTexture(Direct3D10Texture2D) from texture2DDesc.
        Direct3D10Texture2D depthStencilTexture(*d3d10Device, depthDesc); 

        //4 Create a temporary depthStencilViewDesc(D3D10DepthStencilViewDesc) 
        D3D10DepthStencilViewDesc depthStencilViewDesc(DXGI_FORMAT_D32_FLOAT, D3D10_DSV_DIMENSION_TEXTURE2D);

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

        ID3D10RenderTargetView* targetView   = *renderTargetView; 
        //6 Set renderTargetView and depthStencilView to id3d10Device
        d3d10Device ->setOMRenderTargets(1, &targetView, *depthStencilView);
        
      } catch (Exception& ex) {
        ex.display();
      }
    }

    void createEffect()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
        TCHAR fullpath[MAX_PATH];
        _stprintf_s(fullpath, SizeOf(fullpath), _T("%s\\..\\fx\\%s"), (const TCHAR*)directory, _T("shapeEffect.fx"));

        Args args;
        args.set(XmNtechnique, "render");
        args.set(XmNview,      "View");
        args.set(XmNworld,     "World");
        args.set(XmNprojection,"Projection");
        
        model = new Direct3D10EffectModel(*d3d10Device,  
                  fullpath, 
                  args);

      } catch (Exception& ex) {
        ex.display();
      }
    }


    const D3D10_INPUT_ELEMENT_DESC* getInputElementDesc(size_t& count)
    {
      static const D3D10_INPUT_ELEMENT_DESC desc[] = {  
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0,  0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, sizeof(D3DXVECTOR3), D3D10_INPUT_PER_VERTEX_DATA, 0 }  
      };
      count = SizeOf(desc);
      return desc;
    }
    
    void createInputLayout()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
        Direct3D10EffectTechnique* technique = model -> getTechnique();
        
        Direct3D10EffectPass effectPass =  technique->getPassByIndex(0);

        D3D10_PASS_DESC passDesc;
        effectPass.getDesc(passDesc);
        size_t count = 0;
        const D3D10_INPUT_ELEMENT_DESC* inputElementDesc = getInputElementDesc(count);

        inputLayout = new Direct3D10InputLayout(
              *d3d10Device, 
              inputElementDesc, 
              count, 
              passDesc.pIAInputSignature,
              passDesc.IAInputSignatureSize);

        d3d10Device->setIAInputLayout(*inputLayout);

      } catch (Exception& ex) {
        ex.display();
      }
    }

    void createMesh()
    {
      
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();

        float fInnerRadius = 0.2f;
        float fOuterRadius = 1.0f;
        UINT uSides  = 15;
        UINT uRings  = 20; 
        mesh = new Direct3DX10Torus(*d3d10Device, 
                    fInnerRadius, fOuterRadius, uSides,
                                uRings);
        
      } catch (Exception& ex) {
        ex.display();
      }
    }

    void createRasterizerState()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
        D3D10_RASTERIZER_DESC desc;
        memset(&desc, 0, sizeof(desc));
        desc.CullMode               = D3D10_CULL_FRONT;//D3D10_CULL_NONE;
        desc.FillMode               =   D3D10_FILL_WIREFRAME;//D3D10_FILL_SOLID;
        desc.FrontCounterClockwise  = true;
        desc.DepthBias              = false;
        desc.DepthClipEnable        = true;
        desc.ScissorEnable          = false;
        desc.MultisampleEnable      = false;
        desc.AntialiasedLineEnable  = true;

        rasterizerState = new Direct3D10RasterizerState (*d3d10Device, &desc);

        d3d10Device->setRSState(*rasterizerState);

      } catch (Exception& ex) {
        ex.display();
      }
    }

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

      try {
        D3DXVECTOR3 eye( 0.0f, 2.0f,-3.0f );
        D3DXVECTOR3 at ( 0.0f, 0.0f, 0.0f );
        D3DXVECTOR3 up ( 0.0f, 1.0f, 0.0f );
        
        D3DXMatrixLookAtLH(&viewMatrix,  &eye, &at, &up);
        
        D3DXMatrixPerspectiveFovLH(&projectionMatrix, 
                  (float)D3DX_PI * 0.3f, (float)width/(float)height, 0.1f, 100.0f);

        model -> setView(viewMatrix);
        model -> setProjection(projectionMatrix);
       
      } catch (Exception& ex) {
        ex.display();
      }
    }

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

        createInputLayout();

        createMesh();
        
        createRasterizerState();  

        setShaderMatrices();

        // Create rendarTargetView and depthStencilView.
        createViews();

        
      } catch (Exception& ex) {
        ex.display();
      }
    }

    virtual void display()
    {      
      try {
        Direct3D10Device*  d3d10Device = getD3D10Device();
        DirectXGISwapChain* swapChain = getSwapChain();
        if (renderTargetView && depthStencilView) {
          renderTargetView -> clear( D3DXCOLOR(0.0, 0.0, 0.0, 0.0));
          depthStencilView -> clear();
          angle += 0.1f;
          D3DXMatrixRotationY(&worldMatrix, angle);
          model -> setWorld(worldMatrix);
          Direct3D10EffectTechnique* technique = model -> getTechnique();
 
          //Draw the sphere mesh.
          mesh -> draw(*technique);

        }
        swapChain -> present();

      } catch (Exception& ex) {
        ex.display();
      }
    }

  public:
    // Constructor
    SimpleView(DirectX3D10MainView* parent, const TCHAR* name, Args& args)
   :DirectX3D10View(parent, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) ),
    renderTargetView(NULL),
    depthStencilView(NULL),
    
    rasterizerState(NULL),
    model(NULL),

    mesh(NULL),
    inputLayout(NULL),
   
    directory(_T("")),
    angle(0.0f)
    {
      directory = (const TCHAR*)args.get(XmNapplicationDirectory);
      D3DXMatrixIdentity(&worldMatrix);
      D3DXMatrixIdentity(&projectionMatrix);
      D3DXMatrixIdentity(&viewMatrix);

    }

    ~SimpleView()
    {
      delete rasterizerState;
      delete model;

      delete mesh;
      delete inputLayout;
      
      delete renderTargetView;
      delete depthStencilView;
    }
    
  private:
    void resize(int width, int height)
    {
      Direct3D10Device*   device = getD3D10Device();
      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) {
        ex.display();
      }
    }  
  };
  //////////////////////////////////////////////
  // Inner class ends.
  
private:
  SimpleView* view;

public:
  // Constructor
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectX3D10MainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN|WS_CLIPSIBLINGS) ),
  view(NULL)
  {    
    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()
  {
    delete view;
  }

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

//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  const TCHAR* appClass = appName(argv[0]); 

  TCHAR directory[MAX_PATH];
  appDirectory(argv[0], directory, SizeOf(directory));
  
  try {
    Application applet(appClass, argc, argv);

    Args args;
    args.set(XmNapplicationDirectory, directory);
    args.set(XmNwidth,  640);
    args.set(XmNheight, 480);
    MainView mainv(applet, appClass, args);
    
    mainv.realize();

    applet.run();
  } catch (Exception& ex) {
    ex.display();
  }
}


Last modified: 27 Nov 2015

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