aboutsummaryrefslogtreecommitdiff
path: root/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.cpp
blob: 492324a69baec2a0a95131fcd5db97c17a8844c8 (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
/*
* Copyright (c) 2008-2017, 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 "ClothRenderMesh.h"
#include "Renderer.h"

#include <NvClothExt/ClothFabricCooker.h>
#include "foundation/PxStrideIterator.h"

using namespace nv;
using namespace cloth;


template <typename T>
void gatherIndices(std::vector<uint16_t>& indices,
	const BoundedData& triangles, const BoundedData& quads)
{
	PxStrideIterator<const T> tIt, qIt;

	indices.reserve(triangles.count * 3 + quads.count * 6);

	tIt = PxMakeIterator(reinterpret_cast<const T*>(triangles.data), triangles.stride);
	for (PxU32 i = 0; i < triangles.count; ++i, ++tIt)
	{
		indices.push_back(static_cast<uint16_t>(tIt.ptr()[0]));
		indices.push_back(static_cast<uint16_t>(tIt.ptr()[1]));
		indices.push_back(static_cast<uint16_t>(tIt.ptr()[2]));
	}
	
	//Only do quads in case there wasn't triangle data provided
	// otherwise we risk to render triangles double
	if(indices.size() == 0)
	{
		qIt = PxMakeIterator(reinterpret_cast<const T*>(quads.data), quads.stride);
		for (PxU32 i = 0; i < quads.count; ++i, ++qIt)
		{
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[0]));
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[1]));
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[2]));
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[0]));
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[2]));
			indices.push_back(static_cast<uint16_t>(qIt.ptr()[3]));
		}
	}
}

ClothRenderMesh::ClothRenderMesh(const ClothMeshDesc& desc)
{
	uint32_t numVertices = desc.points.count;
	mVertices.resize(numVertices);

	PxStrideIterator<const PxVec3> pIt(reinterpret_cast<const PxVec3*>(desc.points.data), desc.points.stride);
	for (PxU32 i = 0; i < numVertices; ++i)
	{
		mVertices[i].position = *pIt++;
		mVertices[i].normal = PxVec3(0.f);
	}

	// build triangle indices
	if (desc.flags & MeshFlag::e16_BIT_INDICES)
		gatherIndices<PxU16>(mIndices, desc.triangles, desc.quads);
	else
		gatherIndices<PxU32>(mIndices, desc.triangles, desc.quads);

	for (PxU32 i = 0; i < mIndices.size(); i += 3)
	{
		auto p0 = mVertices[mIndices[i]].position;
		auto p1 = mVertices[mIndices[i + 1]].position;
		auto p2 = mVertices[mIndices[i + 2]].position;

		auto normal = ((p2 - p0).cross(p1 - p0)).getNormalized();

		mVertices[mIndices[i]].normal += normal;
		mVertices[mIndices[i + 1]].normal += normal;
		mVertices[mIndices[i + 2]].normal += normal;
	}

	for (PxU32 i = 0; i < numVertices; ++i)
		mVertices[i].normal.normalize();
	
	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(mVertices.data(), (uint32_t)mVertices.size(), sizeof(Vertex), layout, mIndices.data(), (uint32_t)mIndices.size() / 3);
}

ClothRenderMesh::ClothRenderMesh()
	: mIndexBuffer(nullptr)
{
}

ClothRenderMesh::ClothRenderMesh(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
	std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
	: mIndexBuffer(nullptr)
{
	initialize(vertices, numVertices, vertexSize, inputDesc, faces, numFaces);
}

void ClothRenderMesh::initialize(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
	std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
{
	mDevice = GetDeviceManager()->GetDevice();

	mInputDesc = inputDesc;
	mNumVertices = numVertices;
	mVertexSize = vertexSize;
	mNumFaces = numFaces;

	// VB
	{
		D3D11_SUBRESOURCE_DATA vertexBufferData;

		ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
		vertexBufferData.pSysMem = vertices;

		D3D11_BUFFER_DESC bufferDesc;

		memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
		bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		bufferDesc.ByteWidth = vertexSize * numVertices;
		bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
		bufferDesc.MiscFlags = 0;
		bufferDesc.Usage = D3D11_USAGE_DYNAMIC;

		V(mDevice->CreateBuffer(&bufferDesc, &vertexBufferData, &mVertexBuffer));
	}

	// IB
	if (faces != nullptr)
	{
		D3D11_SUBRESOURCE_DATA indexBufferData;

		ZeroMemory(&indexBufferData, sizeof(indexBufferData));
		indexBufferData.pSysMem = faces;

		D3D11_BUFFER_DESC bufferDesc;

		memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
		bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
		bufferDesc.ByteWidth = sizeof(uint16_t) * numFaces*3;
		bufferDesc.CPUAccessFlags = 0;
		bufferDesc.MiscFlags = 0;
		bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;

		V(mDevice->CreateBuffer(&bufferDesc, &indexBufferData, &mIndexBuffer));
	}
	std::vector<uint32_t> offsets;
	offsets.push_back(0);
	setSubmeshOffsets(offsets);
}

ClothRenderMesh::~ClothRenderMesh()
{
	SAFE_RELEASE(mVertexBuffer);
	SAFE_RELEASE(mIndexBuffer);
}

void ClothRenderMesh::update(const PxVec3* positions, uint32_t numVertices)
{
	PxStrideIterator<const PxVec3> pIt(positions, sizeof(PxVec3));
	Vertex* vertices = mVertices.data();
	const uint16_t* indices = mIndices.data();
	for (PxU32 i = 0; i < numVertices; ++i)
	{
		vertices[i].position = *pIt++;
		vertices[i].normal = PxVec3(0.f);
	}

	const PxU32 numIndices = (PxU32)mIndices.size();
	for (PxU32 i = 0; i < numIndices; i += 3)
	{
		const auto p0 = vertices[indices[i]].position;
		const auto p1 = vertices[indices[i + 1]].position;
		const auto p2 = vertices[indices[i + 2]].position;

		const auto normal = ((p2 - p0).cross(p1 - p0)).getNormalized();

		vertices[indices[i]].normal += normal;
		vertices[indices[i + 1]].normal += normal;
		vertices[indices[i + 2]].normal += normal;
	}

	for (PxU32 i = 0; i < numVertices; ++i)
		vertices[i].normal.normalize();

	ID3D11DeviceContext* context;
	mDevice->GetImmediateContext(&context);

	{
		D3D11_MAPPED_SUBRESOURCE mappedResource;
		ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));

		V(context->Map(mVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mappedResource));
		memcpy(mappedResource.pData, mVertices.data(), sizeof(Vertex) * mVertices.size());

		context->Unmap(mVertexBuffer, 0);
	}
}

void ClothRenderMesh::render(ID3D11DeviceContext& context, int submesh) const
{
	context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	UINT strides[1] = { mVertexSize };
	UINT offsets[1] = { 0 };
	context.IASetVertexBuffers(0, 1, &mVertexBuffer, strides, offsets);

	context.IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0);

	int firstIndex = mSubmeshOffsets[submesh];
	int indexCount = mSubmeshOffsets[submesh+1] - firstIndex;

	if (mIndexBuffer)
		context.DrawIndexed(indexCount, firstIndex, 0);
	else
		context.Draw(mNumVertices, 0);
}