aboutsummaryrefslogtreecommitdiff
path: root/tools/ArtistTools/source/BlastPlugin/SampleBase/renderer/PrimitiveRenderMesh.cpp
blob: 06c761655dcf705fe8f7573e164e30392d8ddbc1 (plain) (blame)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2008-2015, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/


#include "PrimitiveRenderMesh.h"
#include "Renderer.h"


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//											Base Mesh internal class
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PrimitiveRenderMesh::PrimitiveRenderMesh(const float v[], UINT numVertices)
{
	std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
	layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });

	initialize(v, numVertices, sizeof(float) * 6, layout, nullptr, 0);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//													Box Mesh
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const float boxVertices[] =
{
		-1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f,
		 1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f, 
		 1.0f,  1.0f, -1.0f,  0.0f,  0.0f, -1.0f, 
		 1.0f,  1.0f, -1.0f,  0.0f,  0.0f, -1.0f, 
		-1.0f,  1.0f, -1.0f,  0.0f,  0.0f, -1.0f, 
		-1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f, 

		-1.0f, -1.0f,  1.0f,  0.0f,  0.0f, 1.0f,
		 1.0f,  1.0f,  1.0f,  0.0f,  0.0f, 1.0f,
		 1.0f, -1.0f,  1.0f,  0.0f,  0.0f, 1.0f,
		 1.0f,  1.0f,  1.0f,  0.0f,  0.0f, 1.0f,
		-1.0f, -1.0f,  1.0f,  0.0f,  0.0f, 1.0f,
		-1.0f,  1.0f,  1.0f,  0.0f,  0.0f, 1.0f,

		-1.0f,  1.0f,  1.0f, -1.0f,  0.0f,  0.0f,
		-1.0f, -1.0f, -1.0f, -1.0f,  0.0f,  0.0f,
		-1.0f,  1.0f, -1.0f, -1.0f,  0.0f,  0.0f,
		-1.0f, -1.0f, -1.0f, -1.0f,  0.0f,  0.0f,
		-1.0f,  1.0f,  1.0f, -1.0f,  0.0f,  0.0f,
		-1.0f, -1.0f,  1.0f, -1.0f,  0.0f,  0.0f,

		 1.0f,  1.0f,  1.0f,  1.0f,  0.0f,  0.0f,
		 1.0f,  1.0f, -1.0f,  1.0f,  0.0f,  0.0f,
		 1.0f, -1.0f, -1.0f,  1.0f,  0.0f,  0.0f,
		 1.0f, -1.0f, -1.0f,  1.0f,  0.0f,  0.0f,
		 1.0f, -1.0f,  1.0f,  1.0f,  0.0f,  0.0f,
		 1.0f,  1.0f,  1.0f,  1.0f,  0.0f,  0.0f,

		-1.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f,
		 1.0f, -1.0f,  1.0f,  0.0f, -1.0f,  0.0f,
		 1.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f,
		 1.0f, -1.0f,  1.0f,  0.0f, -1.0f,  0.0f,
		-1.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f,
		-1.0f, -1.0f,  1.0f,  0.0f, -1.0f,  0.0f,

		-1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f,
		 1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f,
		 1.0f,  1.0f,  1.0f,  0.0f,  1.0f,  0.0f,
		 1.0f,  1.0f,  1.0f,  0.0f,  1.0f,  0.0f,
		-1.0f,  1.0f,  1.0f,  0.0f,  1.0f,  0.0f,
		-1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f
};

BoxRenderMesh::BoxRenderMesh() : PrimitiveRenderMesh(boxVertices, sizeof(boxVertices) / (6 * sizeof(boxVertices[0]))) {}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//													Plane Mesh
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const float planeSize = 1.0f; // we use scaling instead
const float planeTilesCount = 1000.0f;

const float planeVertices[] =
{
	0,  planeSize,  planeSize, 1.0f, 0.0f, 0.0f,  planeTilesCount,  planeTilesCount,
	0,  planeSize, -planeSize, 1.0f, 0.0f, 0.0f,  planeTilesCount, -planeTilesCount,
	0, -planeSize, -planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount, -planeTilesCount,
	0, -planeSize, -planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount, -planeTilesCount,
	0, -planeSize,  planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount,  planeTilesCount,
	0,  planeSize,  planeSize, 1.0f, 0.0f, 0.0f,  planeTilesCount,  planeTilesCount
};

PlaneRenderMesh::PlaneRenderMesh()
{
	std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
	layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 });

	initialize(planeVertices, sizeof(planeVertices) / (8 * sizeof(planeVertices[0])), sizeof(float) * 8, layout, nullptr, 0);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//													Sphere Mesh
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


const uint32_t g_numSlices = 8;  // along lines of longitude
const uint32_t g_numStacks = 16; // along lines of latitude

const uint32_t g_numSphereVertices = (g_numSlices * 2 + 1)*(g_numStacks + 1);
const uint32_t g_numSphereIndices = g_numSlices * 2 * g_numStacks * 6;

const uint32_t g_numConeVertices = (g_numSlices * 2 + 1) * 2;
const uint32_t g_numConeIndices = g_numSlices * 2 * 6;

PxVec3 g_spherePositions[g_numSphereVertices];
uint16_t g_sphereIndices[g_numSphereIndices];

void generateSphereMesh(uint16_t slices, uint16_t stacks, PxVec3* positions, uint16_t* indices)
{
	const PxF32 thetaStep = PxPi / stacks;
	const PxF32 phiStep = PxTwoPi / (slices * 2);

	PxF32 theta = 0.0f;

	// generate vertices
	for (uint16_t y = 0; y <= stacks; ++y)
	{
		PxF32 phi = 0.0f;

		PxF32 cosTheta = PxCos(theta);
		PxF32 sinTheta = PxSin(theta);

		for (uint16_t x = 0; x <= slices * 2; ++x)
		{
			PxF32 cosPhi = PxCos(phi);
			PxF32 sinPhi = PxSin(phi);

			PxVec3 p(cosPhi*sinTheta, cosTheta, sinPhi*sinTheta);

			// write vertex
			*(positions++) = p;

			phi += phiStep;
		}

		theta += thetaStep;
	}

	const uint16_t numRingQuads = 2 * slices;
	const uint16_t numRingVerts = 2 * slices + 1;

	// add faces
	for (uint16_t y = 0; y < stacks; ++y)
	{
		for (uint16_t i = 0; i < numRingQuads; ++i)
		{
			// add a quad
			*(indices++) = (y + 0)*numRingVerts + i;
			*(indices++) = (y + 1)*numRingVerts + i;
			*(indices++) = (y + 1)*numRingVerts + i + 1;

			*(indices++) = (y + 1)*numRingVerts + i + 1;
			*(indices++) = (y + 0)*numRingVerts + i + 1;
			*(indices++) = (y + 0)*numRingVerts + i;
		}
	}
}


struct SphereVertex
{
	PxVec3 position;
	PxVec3 normal;
};

SphereRenderMesh::SphereRenderMesh()
{
	generateSphereMesh(g_numSlices, g_numStacks, g_spherePositions, g_sphereIndices);

	std::vector<SphereVertex> vertices;
	for (uint32_t i = 0; i < g_numSphereVertices; i++)
	{
		vertices.push_back({ g_spherePositions[i], g_spherePositions[i] });
	}

	std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
	layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });

	initialize(vertices.data(), (uint32_t)vertices.size(), sizeof(SphereVertex), layout, g_sphereIndices, g_numSphereIndices);
}


SphereRenderMesh::~SphereRenderMesh()
{
}

// Add By Lixu Begin
const float coneVertices[] =
{
	0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f,//top
	-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f,//left bottom
	-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,//left top

	0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,//top
	1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,//right top
	1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,//right bottom

	0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f,//top
	-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,//left top
	1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,//right top

	0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,//top
	1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,//right bottom
	-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,//left bottom

	-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,//left top
	1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,//right bottom
	1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,//right top

	1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,//right bottom
	-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,//left top
	-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,//left bottom
};

ConeRenderMesh::ConeRenderMesh() : PrimitiveRenderMesh(coneVertices, sizeof(coneVertices) / (6 * sizeof(coneVertices[0]))) {}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//											FBX Mesh class
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <AnimUtil.h>
#include <PxVec2.h>
#include <vector>

struct FBXVertex
{
	PxVec3 position;
	PxVec3 normal;
	PxVec2 uv;
};

FBXRenderMesh::FBXRenderMesh(MeshDesc* pMeshData)
{
	std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
	layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
	layout.push_back({ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 });

	std::vector<FBXVertex> vertices;
	std::vector<uint16_t>  indices;

	for (uint32_t i = 0; i < pMeshData->m_NumVertices; ++i)
	{
		atcore_float3 pos = pMeshData->m_pVertices[i];
		atcore_float3 vertexNormal = pMeshData->m_pVertexNormals[i];
		atcore_float2 texcoord = pMeshData->m_pTexCoords[i];

		FBXVertex newVertex;
		newVertex.position = physx::PxVec3(pos.x, pos.y, pos.z);
		newVertex.normal = physx::PxVec3(vertexNormal.x, vertexNormal.y, vertexNormal.z);
		newVertex.uv = physx::PxVec2(texcoord.x, texcoord.y);

		vertices.push_back(newVertex);
	}

	for (uint32_t i = 0; i < pMeshData->m_NumTriangles; ++i)
	{
		indices.push_back(pMeshData->m_pIndices[i * 3 + 0]);
		indices.push_back(pMeshData->m_pIndices[i * 3 + 1]);
		indices.push_back(pMeshData->m_pIndices[i * 3 + 2]);
	}

	initialize(vertices.data(), (uint32_t)vertices.size(), sizeof(FBXVertex), layout, indices.data(), (uint32_t)indices.size());
}
// Add By Lixu End