Comment every line of code to express your understanding of what this line of co
ID: 3841736 • Letter: C
Question
Comment every line of code to express your understanding of what this line of code is intended to do
#include"CubeDemo.h"
//#include<xnamath.h>
#include<DirectXMath.h>
#include"DDSTextureLoader.h"
using namespace DirectX;
struct VertexPos
{
XMFLOAT3 pos;
XMFLOAT2 tex0;
};
CubeDemo::CubeDemo( ) : solidColorVS_( 0 ), solidColorPS_( 0 ), inputLayout_( 0 ),
vertexBuffer_( 0 ), indexBuffer_( 0 ), colorMap_( 0 ), colorMapSampler_( 0 ),
viewCB_( 0 ), projCB_( 0 ), worldCB_( 0 )
{
}
CubeDemo::~CubeDemo( )
{
}
bool CubeDemo::LoadContent( )
{
ID3DBlob* vsBuffer = 0;
bool compileResult = CompileD3DShader( L"TextureMap.fx", "VS_Main", "vs_4_0", &vsBuffer );
if( compileResult == false )
{
//DXTRACE_MSG( "Error compiling the vertex shader!" );
return false;
}
HRESULT d3dResult;
d3dResult = d3dDevice_->CreateVertexShader( vsBuffer->GetBufferPointer( ),
vsBuffer->GetBufferSize( ), 0, &solidColorVS_ );
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Error creating the vertex shader!" );
if( vsBuffer )
vsBuffer->Release( );
return false;
}
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout );
d3dResult = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements,
vsBuffer->GetBufferPointer( ), vsBuffer->GetBufferSize( ), &inputLayout_ );
vsBuffer->Release( );
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Error creating the input layout!" );
return false;
}
ID3DBlob* psBuffer = 0;
compileResult = CompileD3DShader( L"TextureMap.fx", "PS_Main", "ps_4_0", &psBuffer );
if( compileResult == false )
{
//DXTRACE_MSG( "Error compiling pixel shader!" );
return false;
}
d3dResult = d3dDevice_->CreatePixelShader( psBuffer->GetBufferPointer( ),
psBuffer->GetBufferSize( ), 0, &solidColorPS_ );
psBuffer->Release( );
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Error creating pixel shader!" );
return false;
}
VertexPos vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
};
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory( &vertexDesc, sizeof( vertexDesc ) );
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof( VertexPos ) * 24;
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory( &resourceData, sizeof( resourceData ) );
resourceData.pSysMem = vertices;
d3dResult = d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ );
if( FAILED( d3dResult ) )
{
/*DXTRACE_MSG*/( "Failed to create vertex buffer!" );
return false;
}
WORD 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
};
D3D11_BUFFER_DESC indexDesc;
ZeroMemory( &indexDesc, sizeof( indexDesc ) );
indexDesc.Usage = D3D11_USAGE_DEFAULT;
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexDesc.ByteWidth = sizeof( WORD ) * 36;
indexDesc.CPUAccessFlags = 0;
resourceData.pSysMem = indices;
d3dResult = d3dDevice_->CreateBuffer( &indexDesc, &resourceData, &indexBuffer_ );
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Failed to create index buffer!" );
return false;
}
//d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
//"decal.dds", 0, 0, &colorMap_, 0 );
d3dResult = CreateDDSTextureFromFile(d3dDevice_, L"decal.dds", 0, &colorMap_);
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Failed to load the texture image!" );
return false;
}
D3D11_SAMPLER_DESC colorMapDesc;
ZeroMemory( &colorMapDesc, sizeof( colorMapDesc ) );
colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;
d3dResult = d3dDevice_->CreateSamplerState( &colorMapDesc, &colorMapSampler_ );
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Failed to create color map sampler state!" );
return false;
}
D3D11_BUFFER_DESC constDesc;
ZeroMemory( &constDesc, sizeof( constDesc ) );
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof( XMMATRIX );
constDesc.Usage = D3D11_USAGE_DEFAULT;
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &viewCB_ );
if( FAILED( d3dResult ) )
{
return false;
}
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &projCB_ );
if( FAILED( d3dResult ) )
{
return false;
}
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &worldCB_ );
if( FAILED( d3dResult ) )
{
return false;
}
viewMatrix_ = XMMatrixIdentity( );
projMatrix_ = XMMatrixPerspectiveFovLH( XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f );
viewMatrix_ = XMMatrixTranspose( viewMatrix_ );
projMatrix_ = XMMatrixTranspose( projMatrix_ );
return true;
}
void CubeDemo::UnloadContent( )
{
if( colorMapSampler_ ) colorMapSampler_->Release( );
if( colorMap_ ) colorMap_->Release( );
if( solidColorVS_ ) solidColorVS_->Release( );
if( solidColorPS_ ) solidColorPS_->Release( );
if( inputLayout_ ) inputLayout_->Release( );
if( vertexBuffer_ ) vertexBuffer_->Release( );
if( indexBuffer_ ) indexBuffer_->Release( );
if( viewCB_ ) viewCB_->Release( );
if( projCB_ ) projCB_->Release( );
if( worldCB_ ) worldCB_->Release( );
colorMapSampler_ = 0;
colorMap_ = 0;
solidColorVS_ = 0;
solidColorPS_ = 0;
inputLayout_ = 0;
vertexBuffer_ = 0;
indexBuffer_ = 0;
viewCB_ = 0;
projCB_ = 0;
worldCB_ = 0;
}
void CubeDemo::Update( float dt )
{
// Nothing to update
}
void CubeDemo::Render( )
{
if( d3dContext_ == 0 )
return;
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );
d3dContext_->ClearDepthStencilView( depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0 );
unsigned int stride = sizeof( VertexPos );
unsigned int offset = 0;
d3dContext_->IASetInputLayout( inputLayout_ );
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset );
d3dContext_->IASetIndexBuffer( indexBuffer_, DXGI_FORMAT_R16_UINT, 0 );
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
d3dContext_->VSSetShader( solidColorVS_, 0, 0 );
d3dContext_->PSSetShader( solidColorPS_, 0, 0 );
d3dContext_->PSSetShaderResources( 0, 1, &colorMap_ );
d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ );
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw( 0.0f, 0.7f, 0.7f );
XMMATRIX translationMat = XMMatrixTranslation( 0.0f, 0.0f, 6.0f );
XMMATRIX worldMat = rotationMat * translationMat;
worldMat = XMMatrixTranspose( worldMat );
d3dContext_->UpdateSubresource( worldCB_, 0, 0, &worldMat, 0, 0 );
d3dContext_->UpdateSubresource( viewCB_, 0, 0, &viewMatrix_, 0, 0 );
d3dContext_->UpdateSubresource( projCB_, 0, 0, &projMatrix_, 0, 0 );
d3dContext_->VSSetConstantBuffers( 0, 1, &worldCB_ );
d3dContext_->VSSetConstantBuffers( 1, 1, &viewCB_ );
d3dContext_->VSSetConstantBuffers( 2, 1, &projCB_ );
d3dContext_->DrawIndexed( 36, 0, 0 );
swapChain_->Present( 0, 0 );
}
Explanation / Answer
#include"CubeDemo.h" //including CubeDemo file
//#include<xnamath.h>
#include<DirectXMath.h> //including DirectXMath library
#include"DDSTextureLoader.h" //including DDSTextureLoader file
using namespace DirectX; //namespace to be used across program
struct VertexPos //creating a structure named VertexPos
{
XMFLOAT3 pos; // a field variable named pos of type XMFLOAT3
XMFLOAT2 tex0; // a field variable named pos of type XMFLOAT3
};
//Constructor of class CubeDemo
CubeDemo::CubeDemo( ) : solidColorVS_( 0 ), solidColorPS_( 0 ), inputLayout_( 0 ),
vertexBuffer_( 0 ), indexBuffer_( 0 ), colorMap_( 0 ), colorMapSampler_( 0 ),
viewCB_( 0 ), projCB_( 0 ), worldCB_( 0 )
{
}
//Destructor of class CubeDemo
CubeDemo::~CubeDemo( )
{
}
//a boolean function of class CubeDemo named Load Content
bool CubeDemo::LoadContent( )
{
ID3DBlob* vsBuffer = 0; // a pointer named vsBuffer of type ID3DBlob of inital value of 0
bool compileResult = CompileD3DShader( L"TextureMap.fx", "VS_Main", "vs_4_0", &vsBuffer ); // boolean type variable named compileResult which holds the value return by function CompileD3Shader( which takes four agruments out of which one is pointer reference)
if( compileResult == false ) //this if condition check if the comoileResult computed in above line is false or not if yes control goes inside if condition
{
//DXTRACE_MSG( "Error compiling the vertex shader!" );
return false; // and method return false from here
}
HRESULT d3dResult; //declaring a variable named d3dResult of type HRESULT
d3dResult = d3dDevice_->CreateVertexShader( vsBuffer->GetBufferPointer( ),
vsBuffer->GetBufferSize( ), 0, &solidColorVS_ ); // assigning value to d3dResult computed by calling CreateVertexShader of d3dDevice and this function takes four parameters
if( FAILED( d3dResult ) ) // if checks whether d3dResult is false or not
{
//DXTRACE_MSG( "Error creating the vertex shader!" );
if( vsBuffer ) // Now this if vsBuffer is true or not
vsBuffer->Release( ); // if vsBuffer is true this calls Release function
return false; //returns false
}
//Below describes the solidColorLayout array
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout ); // getting the size of array solidColorLayout in totalLayoutElements variable
d3dResult = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements,
vsBuffer->GetBufferPointer( ), vsBuffer->GetBufferSize( ), &inputLayout_ ); // Calls CreateInputLayout with 5 parameters (array, size, bufferpointer, buffersize and inputlayout) and stores the result in d3dResult varaible
vsBuffer->Release( ); //this calls Release function to release the buffer
if( FAILED( d3dResult ) ) // Checks the d3Result and if false method returns false
{
//DXTRACE_MSG( "Error creating the input layout!" );
return false;
}
ID3DBlob* psBuffer = 0; //defining a pointer psBuffer of type ID3DBlob
compileResult = CompileD3DShader( L"TextureMap.fx", "PS_Main", "ps_4_0", &psBuffer ); calls CompileD3DShader with four parameter and stores the result in compileResult
if( compileResult == false ) //checks whether compileResult is equal to false
{
//DXTRACE_MSG( "Error compiling pixel shader!" );
return false; //method return false
}
d3dResult = d3dDevice_->CreatePixelShader( psBuffer->GetBufferPointer( ),
psBuffer->GetBufferSize( ), 0, &solidColorPS_ ); //calls method CreatePixelShader with four parameters bufferpointer, buffersize, and pointer reference) and stores the result in d3dResult
psBuffer->Release( ); //calls Release function and release the buffer
if( FAILED( d3dResult ) ) //checks if d3dResult and if failed then method returns false
{
//DXTRACE_MSG( "Error creating pixel shader!" );
return false;
}
//an object array of type VertexPos
VertexPos vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, -1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, -1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, -1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, -1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT2( 0.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, -1.0f, 1.0f ), XMFLOAT2( 1.0f, 0.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 1.0f ), XMFLOAT2( 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 1.0f ), XMFLOAT2( 0.0f, 1.0f ) },
};
D3D11_BUFFER_DESC vertexDesc; //declare a variable of type D3D11_BUFFER_DESC
ZeroMemory( &vertexDesc, sizeof( vertexDesc ) );//calling function by the passing pointer defined above and its size.
vertexDesc.Usage = D3D11_USAGE_DEFAULT; //storing the default usage in vertexDesc.Usage
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; // storing the vertex buffer
vertexDesc.ByteWidth = sizeof( VertexPos ) * 24; // storing the bytewidth by multiplying size of VertexPos with 24
D3D11_SUBRESOURCE_DATA resourceData; // declaring a variable of type D3D11_SUBRESOURCE_DATA
ZeroMemory( &resourceData, sizeof( resourceData ) ); //calling a function with pointer resourceData and its size
resourceData.pSysMem = vertices; //storing vertices
d3dResult = d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ ); //calling function with reference of resourceData, vertexDesc and vertexBuffer defined above) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
/*DXTRACE_MSG*/( "Failed to create vertex buffer!" );
return false;
}
//an array indices of type WORD
WORD 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
};
D3D11_BUFFER_DESC indexDesc; //declaring a variable of type D3D11_BUFFER_DESC
ZeroMemory( &indexDesc, sizeof( indexDesc ) ); //calls the method with indexDesc variable and its size
indexDesc.Usage = D3D11_USAGE_DEFAULT; //stores the default value
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; //stores indexbuffer
indexDesc.ByteWidth = sizeof( WORD ) * 36; //stores the bytewidth with size of Word * 36
indexDesc.CPUAccessFlags = 0; //setting the value to 0
resourceData.pSysMem = indices; //storing indices
d3dResult = d3dDevice_->CreateBuffer( &indexDesc, &resourceData, &indexBuffer_ );calling function with reference of resourceData, indexDesc and indexBuffer_ defined above) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
//DXTRACE_MSG( "Failed to create index buffer!" );
return false;
}
//d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
//"decal.dds", 0, 0, &colorMap_, 0 );
d3dResult = CreateDDSTextureFromFile(d3dDevice_, L"decal.dds", 0, &colorMap_);
if( FAILED( d3dResult ) )
{
//DXTRACE_MSG( "Failed to load the texture image!" );
return false;
}
D3D11_SAMPLER_DESC colorMapDesc; //declares variable of type D3D11_SAMPLER_DESC
ZeroMemory( &colorMapDesc, sizeof( colorMapDesc ) ); // calls method with variable defined above its size.
colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; // storing address
colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; // storing address
colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; // storing address
colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; //comparision value
colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; //storing filter value
colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX; //storing max value
d3dResult = d3dDevice_->CreateSamplerState( &colorMapDesc, &colorMapSampler_ ); calling function with reference of colorMapDesc, colorMapSampler_ defined above) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
//DXTRACE_MSG( "Failed to create color map sampler state!" );
return false;
}
D3D11_BUFFER_DESC constDesc; //declaring variable of type D3D11_BUFFER_DESC
ZeroMemory( &constDesc, sizeof( constDesc ) ); //calling function with constDesc and its size
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; //storing constant buffer
constDesc.ByteWidth = sizeof( XMMATRIX ); //storing size of matrix
constDesc.Usage = D3D11_USAGE_DEFAULT; //storing default usage
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &viewCB_ ); calling function with reference of constDesc, viewCB_ defined above) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
return false;
}
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &projCB_ ); calling function with reference of constDesc, and reference to projCB_) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
return false;
}
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &worldCB_ ); calling function with reference of constDesc, and reference to worldCB_) and storing result in d3dResult
if( FAILED( d3dResult ) ) //checking the result and if fail then method returns false
{
return false;
}
viewMatrix_ = XMMatrixIdentity( ); //storing result of XMMatrixIdentity
projMatrix_ = XMMatrixPerspectiveFovLH( XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f ); //storing result of XMMatrixPerspectiveFovLH
viewMatrix_ = XMMatrixTranspose( viewMatrix_ ); //storing result of XMMatrixTranspose
projMatrix_ = XMMatrixTranspose( projMatrix_ ); //storing result of XMMatrixTranspose
return true;
}
//function to class CubeDemo which basically release buffers arrays created above
void CubeDemo::UnloadContent( )
{
if( colorMapSampler_ ) colorMapSampler_->Release( ); //check if colorMapSampler_ is present then release
if( colorMap_ ) colorMap_->Release( ); //check if colorMap_ is present then release
if( solidColorVS_ ) solidColorVS_->Release( ); //check if solidColorVS_ is present then release
if( solidColorPS_ ) solidColorPS_->Release( ); //check if solidColorVS_ is present then release
if( inputLayout_ ) inputLayout_->Release( ); //check if inputLayout_ is present then release
if( vertexBuffer_ ) vertexBuffer_->Release( ); //check if vertexBuffer_ is present then release
if( indexBuffer_ ) indexBuffer_->Release( ); //check if indexBuffer_ is present then release
if( viewCB_ ) viewCB_->Release( ); //check if viewCB_ is present then release
if( projCB_ ) projCB_->Release( ); //check if projCB_ is present then release
if( worldCB_ ) worldCB_->Release( ); //check if worldCB_ is present then release
//assigning 0 to all
colorMapSampler_ = 0;
colorMap_ = 0;
solidColorVS_ = 0;
solidColorPS_ = 0;
inputLayout_ = 0;
vertexBuffer_ = 0;
indexBuffer_ = 0;
viewCB_ = 0;
projCB_ = 0;
worldCB_ = 0;
}
//function of class CubeDemo to update value but kept empty here
void CubeDemo::Update( float dt )
{
// Nothing to update
}
//function of class CubeDemo
void CubeDemo::Render( )
{
if( d3dContext_ == 0 ) //checks the value of d3dContext if 0 just return
return;
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f }; //creating array of type float and size 4
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor ); // calling function with buffertarget and array created above
d3dContext_->ClearDepthStencilView( depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0 ); calling function with four parameters
unsigned int stride = sizeof( VertexPos ); //taking size of VertexPos structure
unsigned int offset = 0; //setting offset to 0
d3dContext_->IASetInputLayout( inputLayout_ ); //calling function with inputLayout_ as parameter
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset ); // calling function with vertexBuffer, offset and reference to stride
d3dContext_->IASetIndexBuffer( indexBuffer_, DXGI_FORMAT_R16_UINT, 0 ); //calling function indexBuffer
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
d3dContext_->VSSetShader( solidColorVS_, 0, 0 ); //calling function with three parameters
d3dContext_->PSSetShader( solidColorPS_, 0, 0 ); //calling function with three parameters
d3dContext_->PSSetShaderResources( 0, 1, &colorMap_ ); //calling function with three parameters
d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ ); //calling function with three parameters
XMMATRIX rotationMat = XMMatrixRotationRollPitchYaw( 0.0f, 0.7f, 0.7f ); //calling function with three parameters and storing result
XMMATRIX translationMat = XMMatrixTranslation( 0.0f, 0.0f, 6.0f ); //calling function with three parameters and storing result
XMMATRIX worldMat = rotationMat * translationMat; //storing product of rotationMat and translationMat (which are computed above)
worldMat = XMMatrixTranspose( worldMat ); //calling function with worldMat as parameter
d3dContext_->UpdateSubresource( worldCB_, 0, 0, &worldMat, 0, 0 ); //calling function to updateSubresource
d3dContext_->UpdateSubresource( viewCB_, 0, 0, &viewMatrix_, 0, 0 ); //calling function to updateSubresource
d3dContext_->UpdateSubresource( projCB_, 0, 0, &projMatrix_, 0, 0 ); //calling function to updateSubresource
d3dContext_->VSSetConstantBuffers( 0, 1, &worldCB_ ); //setting constant buffers
d3dContext_->VSSetConstantBuffers( 1, 1, &viewCB_ ); //setting constant buffers
d3dContext_->VSSetConstantBuffers( 2, 1, &projCB_ ); //setting constant buffers
d3dContext_->DrawIndexed( 36, 0, 0 ); //calling function with values to use to draw
swapChain_->Present( 0, 0 ); //calling function Present
}