SOL9 Sample: Direct3D10TexturedCubeBuffer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2017/01/19 Updated display to setShaderMatrices.
// 2017/01/28 Updated to use ModuleFileName class and caught macro.

// The used vertices and indiceis data are taken from
//C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\Direct3D10-work\Tutorials\Tutorial08
#pragma warning(disable : 4005) 
#pragma warning(disable : 4838) 

#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/Direct3D10Buffer.h>

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

#include <sol/direct3d10/Direct3D10InputLayout.h>
#include <sol/direct3d10/Direct3D10ShaderResourceView.h>
#include <sol/direct3d10/D3DXSimpleVertex.h>

#include "resource.h"

namespace SOL {
  
class MainView :public DirectX3D10MainView {
  
private:
  //////////////////////////////////////////////
  //Inner class starts
  class SimpleView : public DirectX3D10View {
  private: 
    SmartPtr<Direct3D10RenderTargetView>   renderTargetView;
    SmartPtr<Direct3D10DepthStencilView>   depthStencilView;
    SmartPtr<Direct3D10EffectModel>        model;
    SmartPtr<Direct3D10Buffer>             vertexBuffer;
    SmartPtr<Direct3D10Buffer>             indexBuffer;
    int                                    vertexCounter;

    SmartPtr<Direct3D10InputLayout>        inputLayout;
    SmartPtr<Direct3D10ShaderResourceView> shaderResourceView;

    D3DXMATRIX          worldMatrix;
    D3DXMATRIX          viewMatrix;
    D3DXMATRIX          projectionMatrix;

    StringT<TCHAR>      directory;

    float               angle;

  public:
    void deleteViews()
    {
      Direct3D10Device* d3d10device = getD3D10Device();
    
      d3d10device ->setOMRenderTargets(0, NULL, NULL);
      renderTargetView = 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);
        
        ID3D10RenderTargetView* targets[1];
        targets[0] = *renderTargetView; 

        //2 Set renderTargetView and depthStencilView to id3d10Device
        
        d3d10Device ->setOMRenderTargets(1, targets, NULL);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }

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

        Args args;
        args.set(XmNtechnique,      "Render" );
        args.set(XmNworld,          "World" );
        args.set(XmNview,           "View" );
        args.set(XmNprojection,     "Projection" );
        args.set(XmNshaderTexture,  "TextureDiffuse" );    //ID3D10EffectShaderResourceVariable

        model = new Direct3D10EffectModel(*d3d10Device,  
                  fullpath, 
                args);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }


    const D3D10_INPUT_ELEMENT_DESC* getInputElementDesc(size_t& count)
    {
      static D3D10_INPUT_ELEMENT_DESC desc[] ={
        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
      };
      count = CountOf(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) {
        caught(ex);
      }
    }

    void createBuffer()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
            
        D3DXPosTexCoordVertex vertices[] = {
        { D3DXVECTOR3( -1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

        { D3DXVECTOR3( -1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3( -1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

        { D3DXVECTOR3(  1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

        { D3DXVECTOR3( -1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f, -1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f,  1.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

        { D3DXVECTOR3( -1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f, -1.0f,  1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
        { D3DXVECTOR3(  1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
        { D3DXVECTOR3( -1.0f,  1.0f,  1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
        };
        
        DWORD indices[] = {
           3, 1, 0,   2, 1, 3,
           6, 4, 5,   7, 4, 6,

          11, 9, 8,  10, 9,11,
          14,12,13,  15,12,14,

          19,17,16,  18,17,19,
          22,20,21,  23,20,22
        };

        size_t count = 0;
        const D3D10_INPUT_ELEMENT_DESC* inputElementDesc = getInputElementDesc(count);
        D3D10_BUFFER_DESC bufferDesc;
        memset(&bufferDesc, 0, sizeof(bufferDesc));
        bufferDesc.Usage = D3D10_USAGE_DEFAULT;
        bufferDesc.ByteWidth = sizeof(vertices);
        bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    
        D3D10_SUBRESOURCE_DATA subResource;
        memset(&subResource, 0, sizeof(subResource));
        subResource.pSysMem = vertices;
        //Create a vertex buffer for the vertex from the above parameters.
        vertexBuffer = new Direct3D10Buffer(*d3d10Device, &bufferDesc, &subResource);

        ID3D10Buffer* buffers[1];
        buffers[0] = *vertexBuffer;
        UINT stride = sizeof(D3DXPosTexCoordVertex);
        UINT offset = 0;
        //set the vertex buffers.
        d3d10Device->setIAVertexBuffers( 0, 1, buffers, &stride, &offset );

        D3D10_BUFFER_DESC indexBufferDesc;
        memset(&indexBufferDesc, 0, sizeof(indexBufferDesc));
        indexBufferDesc.Usage     = D3D10_USAGE_DEFAULT;
        indexBufferDesc.ByteWidth = sizeof(indices); 
        indexBufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;

        vertexCounter = CountOf(indices);

        D3D10_SUBRESOURCE_DATA indexData;
        memset(&indexData, 0, sizeof(indexData));
        indexData.pSysMem = indices;

        //Create an indexBuffer from above parameters.
        indexBuffer= new Direct3D10Buffer(*d3d10Device, &indexBufferDesc, &indexData);

        d3d10Device -> setIAIndexBuffer(*indexBuffer, DXGI_FORMAT_R32_UINT, 0 );

        //Set a primitiveTopology
        d3d10Device -> setIAPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST );


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

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

      try {
        D3DXVECTOR3 eye( 0.0f, 0.0f,-5.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.2f, (float)width/(float)height, 0.1f, 100.0f);

        model -> setView(viewMatrix);
        model -> setProjection(projectionMatrix);

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

   void createShaderResourceView()
    {
      try {
        Direct3D10Device* d3d10Device = getD3D10Device();
        TCHAR fullpath[MAX_PATH];
        _stprintf_s(fullpath, CountOf(fullpath), _T("%s\\..\\image\\%s"), 
                    (const TCHAR*)directory, 
                    _T("Nioh.png"));
     
       shaderResourceView = new Direct3D10ShaderResourceView(
            *d3d10Device,
            fullpath,
            NULL,   //D3D10_SHADER_RESOURCE_VIEW_DESC *desc
            NULL);  //D3DX10_IMAGE_LOAD_INFO* loadInfo
        
        model -> getShaderTexture() ->setResource(*shaderResourceView );

      } catch(Exception& ex) {
        caught(ex); 
      }
    }
    
    virtual void initialize()
    {
      try {
        createEffect();

        createInputLayout();
        
        createBuffer();

        createShaderResourceView();
        
        setShaderMatrices();

        createViews();

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

    virtual void display()
    {
      try {
        Direct3D10Device*  d3d10Device = getD3D10Device();
        DirectXGISwapChain* swapChain = getSwapChain();
        if (renderTargetView ) {
          renderTargetView -> clear(D3DXCOLOR(0.0, 0.0, 0.0, 0.0));
          //2017/01/19
          setShaderMatrices();
          
          D3DXMATRIX temp;
          D3DXVECTOR3 origin(0, 0, 0);
          D3DXMatrixRotationY(&worldMatrix, angle);
          D3DXMatrixTranslation( &temp, origin.x, 0, origin.z);
          worldMatrix *= temp;
          model -> setWorld(worldMatrix);

          Direct3D10EffectTechnique* technique = model -> getTechnique();

          d3d10Device -> drawIndexed(*technique, vertexCounter);

        }
        swapChain -> present();

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

  public:
    // Constructor
    SimpleView(DirectX3D10MainView* parent, const TCHAR* name, Args& args)
   :DirectX3D10View(parent, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) ),
    vertexCounter(0),
    directory(_T("")),
    angle(0.0f)
    {
      directory = (const TCHAR*)args.get(XmNapplicationDirectory);
      D3DXMatrixIdentity(&worldMatrix);
      D3DXMatrixIdentity(&projectionMatrix);
      D3DXMatrixIdentity(&viewMatrix);

    }

    ~SimpleView()
    {
    }
    
  private:
    void resize(int width, int height)
    {
      Direct3D10Device*   device = getD3D10Device();
      DirectXGISwapChain* swapChain = getSwapChain();
      if (device           == NULL && 
          swapChain        == NULL && 
          renderTargetView == 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)
  :DirectX3D10MainView(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();
      }
    }
  }

private:
  void resize(int width, int height)
  {
    if (view != nullptr) {
      view -> reshape(0, 0, width, height);
      view -> postResizeRequest(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.