SOL9 2.0 Class: Direct3DX11Torus

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree  SOL9 ClassList 

Source code

/******************************************************************************
 *
 * Copyright (c) 2016 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer.
 *  
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 *  Direct3DX11Torus.h
 *
 *****************************************************************************/

//2016/0/20
//This is based on MakeTorus function in the following code.
// C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\DXUT\Optional\DXUTShape.cpp


#pragma once
#include <math.h>
#include <sol/direct3d11/Direct3DX11Shape.h>


namespace SOL {

class Direct3DX11Torus : public Direct3DX11Shape {
public:
  Direct3DX11Torus(
        __in Direct3D11Device*        device, 
        __in Direct3D11DeviceContext* context,
        __in float fInnerRadius = 1.0f, 
        __in float fOuterRadius = 2.0f, 
        __in UINT uSides        = 8,
        __in UINT uRings        = 15)
  :Direct3DX11Shape(device,  context)
  {
    if (device == NULL) {
      throw IException("Invalid devie.");
    }
    if (context == NULL ) {
      throw IException("Invalid devieContex.t");
    }
        
    if (fInnerRadius < 0.0f || fOuterRadius < 0.0f || uSides < 3 || uRings < 3) {
      throw IException("Invalid parameter"); 
    }
    // Create a vertexBuffer and an indexBuffer
    createTorus(fInnerRadius, fOuterRadius, uSides, uRings);

  }

public:
  Direct3DX11Torus(
        __in Direct3D11Device*        device, 
        __in Direct3D11DeviceContext* context,
        __in Direct3D11Blob*          vertexShaderBlob,
        __in float fInnerRadius = 1.0f, 
        __in float fOuterRadius = 2.0f, 
        __in UINT uSides        = 8,
        __in UINT uRings        = 15)
  :Direct3DX11Shape(device,  context)
  {
    if (device == NULL) {
      throw IException("Invalid devie.");
    }
    if (context == NULL ) {
      throw IException("Invalid devieContex.t");
    }
    if (vertexShaderBlob == NULL) {
      throw IException("Invalid vertexShaderBlob");
    }     
        
    if (fInnerRadius < 0.0f || fOuterRadius < 0.0f || uSides < 3 || uRings < 3) {
      throw IException("Invalid parameter"); 
    }
    // Create a vertexBuffer and an indexBuffer
    createTorus(fInnerRadius, fOuterRadius, uSides, uRings);

    createInputLayout(vertexShaderBlob);
  }

  //The following createTorus is based on MakeToris function in the following code.
  //
  //C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Samples\C++\DXUT\Optional\DXUTShape.cpp
  //--------------------------------------------------------------------------------------
  // File: DXUTShapes.cpp
  //
  // Shape creation functions for DXUT
  //
  // Copyright (c) Microsoft Corporation. All rights reserved
  //--------------------------------------------------------------------------------------
  void createTorus( float fInnerRadius, float fOuterRadius, UINT uSides,
                                UINT uRings )
  { 
    UINT cFaces = 2 * uSides * uRings;
    UINT cVertices = uRings * uSides;
    
    D3D11PosNormalVertex* vertices = new D3D11PosNormalVertex[cVertices];

    UINT i, j;

    // Generate vertices
    D3D11PosNormalVertex* pVertex = vertices;

    for( i = 0; i < uRings; i++ )
    {
        float theta = ( float )i * 2.0f * (float)MATH_PI / ( float )uRings;
        float st = sinf(theta);
        float ct = cosf(theta);

        //sincosf( theta, &st, &ct );

        for( j = 0; j < uSides; j++ )
        {
            float phi = ( float )j * 2.0f * (float)MATH_PI / (float)uSides;
            float sp = sinf(phi);
            float cp = cosf(phi);

            //sincosf( phi, &sp, &cp );

            pVertex->pos.x = ct * ( fOuterRadius + fInnerRadius * cp );
            pVertex->pos.y = -st * ( fOuterRadius + fInnerRadius * cp );
            pVertex->pos.z = sp * fInnerRadius;

            pVertex->normal.x = ct * cp;
            pVertex->normal.y = -st * cp;
            pVertex->normal.z = sp;

            pVertex++;
        }
    }

    UINT* indices = new UINT[ cFaces * 3 ];

    // Generate indices
    UINT* pwFace = indices;

    for( i = 0; i < uRings - 1; i++ )
    {
        for( j = 0; j < uSides - 1; j++ )
        {

            // Tri 1 (Top-Left tri, CCW)
            pwFace[0] = ( WORD )( i * uSides + j );
            pwFace[1] = ( WORD )( i * uSides + j + 1 );
            pwFace[2] = ( WORD )( ( i + 1 ) * uSides + j );
            pwFace += 3;

            // Tri 2 (Bottom-Right tri, CCW)
            pwFace[0] = ( WORD )( ( i + 1 ) * uSides + j );
            pwFace[1] = ( WORD )( i * uSides + j + 1 );
            pwFace[2] = ( WORD )( ( i + 1 ) * uSides + j + 1 );
            pwFace += 3;
        }

        // Tri 1 (Top-Left tri, CCW)
        pwFace[0] = ( WORD )( i * uSides + j );
        pwFace[1] = ( WORD )( i * uSides );
        pwFace[2] = ( WORD )( ( i + 1 ) * uSides + j );
        pwFace += 3;

        // Tri 2 (Bottom-Right tri, CCW)
        pwFace[0] = ( WORD )( ( i + 1 ) * uSides + j );
        pwFace[1] = ( WORD )( i * uSides + 0 );
        pwFace[2] = ( WORD )( ( i + 1 ) * uSides + 0 );
        pwFace += 3;
    }


    // join the two ends of the tube
    for( j = 0; j < uSides - 1; j++ )
    {
        // Tri 1 (Top-Left tri, CCW)
        pwFace[0] = ( WORD )( i * uSides + j );
        pwFace[1] = ( WORD )( i * uSides + j + 1 );
        pwFace[2] = ( WORD )( j );
        pwFace += 3;

        // Tri 2 (Bottom-Right tri, CCW)
        pwFace[0] = ( WORD )( j );
        pwFace[1] = ( WORD )( i * uSides + j + 1 );
        pwFace[2] = ( WORD )( j + 1 );
        pwFace += 3;
    }

    // Tri 1 (Top-Left tri, CCW)
    pwFace[0] = ( WORD )( i * uSides + j );
    pwFace[1] = ( WORD )( i * uSides );
    pwFace[2] = ( WORD )( j );
    pwFace += 3;

    // Tri 2 (Bottom-Right tri, CCW)
    pwFace[0] = ( WORD )( j );
    pwFace[1] = ( WORD )( i * uSides );
    pwFace[2] = ( WORD )( 0 );
    pwFace += 3;
  
    createVertexBuffer(vertices, sizeof(D3D11PosNormalVertex) * cVertices, cVertices);
    createIndexBuffer(indices, sizeof(UINT) * cFaces * 3, cFaces * 3 );
    
    delete[] vertices;
    delete[] indices;
  }


  ~Direct3DX11Torus()
  {
  }
 
 
};

}


Last modified: 5 May 2019

Copyright (c) 2009-2019 Antillia.com ALL RIGHTS RESERVED.