-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCylinderPosColorNorm.cpp
161 lines (135 loc) · 5.49 KB
/
CylinderPosColorNorm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "stdafx.h"
#include "CylinderPosColorNorm.h"
#include "VertexStructs.h"
#include "ContentManager.h"
#include "Logger.h"
#include <vector>
CylinderPosColorNorm::CylinderPosColorNorm(float radius, UINT radiusSteps, float height, DirectX::XMFLOAT4 color)
: m_Radius{radius}
, m_Height{height}
, m_RadiusSteps{radiusSteps}
, m_Color{color}
{
}
CylinderPosColorNorm::~CylinderPosColorNorm()
{
SafeRelease(m_pVertexBuffer);
SafeRelease(m_pIndexBuffer);
SafeRelease(m_pVertexLayout);
}
void CylinderPosColorNorm::Initialize()
{
//Effect
m_pEffect = ContentManager::GetInstance()->Load<ID3DX11Effect>(L"Resources/Effects/PosColNorm3D.fx");
m_pTechnique = m_pEffect->GetTechniqueByIndex(0);
m_pMatWorldViewProjVariable = m_pEffect->GetVariableByName("gWorldViewProj")->AsMatrix();
if(!m_pMatWorldViewProjVariable->IsValid())Logger::GetInstance()->LogWarning(L"CylinderPosColorNorm::Initialize() - WvpVariable is invalid!");
m_pMatWorldVariable = m_pEffect->GetVariableByName("gWorld")->AsMatrix();
if(!m_pMatWorldVariable->IsValid())Logger::GetInstance()->LogWarning(L"CylinderPosColorNorm::Initialize() - WorldVariable is invalid!");
//InputLayout
D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
UINT numElements = sizeof( vertexDesc ) / sizeof( vertexDesc[0] );
// Create the input layout
D3DX11_PASS_DESC passDesc;
m_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
auto hr = Locator::GetD3D11()->pDevice->CreateInputLayout(
vertexDesc,
numElements,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&m_pVertexLayout);
Logger::GetInstance()->LogHResult(hr, L"Failed to Create InputLayout");
//**CYLINDER**
//Vertices
vector<VertexPosColNorm> vertices;
float interval = DirectX::XM_2PI/m_RadiusSteps;
for(UINT step = 0; step < m_RadiusSteps; ++step)
{
float circleRadius = step * interval;
XMFLOAT3 pos = DirectX::XMFLOAT3(m_Radius * cos(circleRadius), m_Height/2.0f, m_Radius * sin(circleRadius));
XMVECTOR vecNorm = XMVectorSet(pos.x, 0, pos.z, 0);
vecNorm = XMVector3Normalize(vecNorm);
DirectX::XMFLOAT3 norm;
XMStoreFloat3(&norm, vecNorm);
vertices.push_back(VertexPosColNorm(pos , m_Color, DirectX::XMFLOAT3(0,1,0)));
vertices.push_back(VertexPosColNorm(pos , m_Color, norm));
pos.y *= -1;
vertices.push_back(VertexPosColNorm(pos , m_Color, norm));
vertices.push_back(VertexPosColNorm(pos , m_Color, DirectX::XMFLOAT3(0,-1,0)));
}
vertices.push_back(VertexPosColNorm(DirectX::XMFLOAT3(0,m_Height/2.0f,0) , m_Color, DirectX::XMFLOAT3(0,1,0)));
vertices.push_back(VertexPosColNorm(DirectX::XMFLOAT3(0,-m_Height/2.0f,0) , m_Color, DirectX::XMFLOAT3(0,-1,0)));
UINT numVertices = static_cast<UINT>(vertices.size());
vector<DWORD> indices;
for(UINT i = 0; i < numVertices-2; i+=4)
{
indices.push_back(numVertices-2);
indices.push_back(i);
indices.push_back((i+4)%(numVertices-2));
indices.push_back(i+1);
indices.push_back(i+2);
indices.push_back((i+6)%(numVertices-2));
indices.push_back((i+6)%(numVertices-2));
indices.push_back((i+5)%(numVertices-2));
indices.push_back(i+1);
indices.push_back(i+3);
indices.push_back(numVertices - 1);
indices.push_back((i+7)%(numVertices-2));
}
m_NumIndices = static_cast<UINT>(indices.size());
//Vertexbuffer
D3D11_BUFFER_DESC bd {};
D3D11_SUBRESOURCE_DATA initData {0};
bd.Usage = D3D11_USAGE_IMMUTABLE;
bd.ByteWidth = UINT(sizeof( VertexPosColNorm ) * numVertices);
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
initData.pSysMem = vertices.data();
hr = Locator::GetD3D11()->pDevice->CreateBuffer( &bd, &initData, &m_pVertexBuffer );
Logger::GetInstance()->LogHResult(hr, L"Failed to Create Vertexbuffer");
//Indexbuffer
bd.Usage = D3D11_USAGE_IMMUTABLE;
bd.ByteWidth = sizeof(DWORD) * m_NumIndices;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
initData.pSysMem = indices.data();
hr = Locator::GetD3D11()->pDevice->CreateBuffer( &bd, &initData, &m_pIndexBuffer );
Logger::GetInstance()->LogHResult(hr, L"Failed to Create Indexbuffer");
}
void CylinderPosColorNorm::Update(const SceneContext& sceneContext)
{
}
void CylinderPosColorNorm::Draw(const SceneContext& sceneContext)
{
DirectX::XMMATRIX world = XMLoadFloat4x4(&m_WorldMatrix);
DirectX::XMMATRIX viewProj = XMLoadFloat4x4(&sceneContext.GetCamera()->GetViewProjection());
DirectX::XMMATRIX wvp = XMMatrixMultiply(world, viewProj);
m_pMatWorldVariable->SetMatrix(reinterpret_cast<float*>(&world));
m_pMatWorldViewProjVariable->SetMatrix(reinterpret_cast<float*>(&wvp));
// Set vertex buffer
UINT stride = sizeof( VertexPosColNorm );
UINT offset = 0;
auto deviceContext = Locator::GetD3D11()->pDeviceContext;
deviceContext->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &stride, &offset );
// Set index buffer
deviceContext->IASetIndexBuffer(m_pIndexBuffer,DXGI_FORMAT_R32_UINT,0);
// Set the input layout
deviceContext->IASetInputLayout( m_pVertexLayout );
// Set primitive topology
deviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
// Render a torus
D3DX11_TECHNIQUE_DESC techDesc;
m_pTechnique->GetDesc(&techDesc);
for(UINT p=0; p< techDesc.Passes;++p)
{
m_pTechnique->GetPassByIndex(p)->Apply(0, deviceContext);
deviceContext->DrawIndexed( m_NumIndices, 0, 0);
}
}