aboutsummaryrefslogtreecommitdiff
path: root/mayaPlug/shaveGeometryOverride.h
blob: 2499bd873ac87eeddbf7c0770a9780b803cfcdac (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
#ifndef shaveGeometryOverride_h
#define shaveGeometryOverride_h
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

#include <vector>

#include <maya/M3dView.h>
#include <maya/MColor.h>
#include <maya/MGeometryRequirements.h>
#include <maya/MGeometry.h>
#include <maya/MHWGeometry.h>
#include <maya/MMatrix.h>
#include <maya/MObject.h>
#include <maya/MPxGeometryOverride.h>
#include <maya/MShaderManager.h>
#include <maya/MTextureManager.h>
#include <maya/MUserData.h>
#include <maya/MVector.h>
#include <maya/MViewport2Renderer.h>

#include "shaveHairShape.h"

#if MAYA_API_VERSION < 20180000
class MDagPath;
#endif

class shaveInstanceDisplay;


class shaveGeometryOverride : public MHWRender::MPxGeometryOverride {
public:
	static MPxGeometryOverride* Creator(const MObject& obj)
	{
		return new shaveGeometryOverride(obj);
	}
	virtual ~shaveGeometryOverride();

	virtual MHWRender::DrawAPI supportedDrawAPIs() const;
	virtual void updateDG();
	
	virtual void updateRenderItems(
		const MDagPath& path,
		MHWRender::MRenderItemList& list);

	virtual void populateGeometry(
		const MHWRender::MGeometryRequirements& requirements,
		const MHWRender::MRenderItemList& renderItems,
		MHWRender::MGeometry& data
	);

#if MAYA_API_VERSION >= 201600
	virtual void updateSelectionGranularity(const MDagPath& path, MHWRender::MSelectionContext& selectCtx);
#endif
	virtual void cleanUp();

#if MAYA_API_VERSION >= 201600
	static MStatus registerComponentConverters();
	static MStatus deregisterComponentConverters();
#endif

	//	Custom user data to attach to render items
	//	for doing selection.
	//
	class SelectionData : public MUserData
	{
	public:
		SelectionData()
		: MUserData(false)
		{}

		virtual ~SelectionData()
		{}

		MString mShaveSelectType;

		//	Holds the encoded (guide, vert) value for each
		//	primitive used to draw guide verts.
		//
		std::vector<unsigned int> mPrimIdxToVertIdx;

		//	Holds the guide index for each primitive used to
		//	draw a guide.
		//
		std::vector<unsigned int> mPrimIdxToGuideIdx;
	};

	//	RenderItem names.
	//
	static const MString kHairShadedItemName;
	static const MString kHairShadedSelectedItemName;
	static const MString kHairWireItemName;
	static const MString kHairWireSelectedItemName;
	static const MString kInstanceShadedItemName;
	static const MString kInstanceWireItemName;
	static const MString kInstanceWireSelectedItemName;
	static const MString kGuidesItemName;
	static const MString kKnotsItemName;

protected:
	shaveGeometryOverride(const MObject& obj);

	bool disableRenderItem(
		MHWRender::MRenderItemList& list,
		const MString& itemName,
		MHWRender::MGeometry::Primitive primType,
		MHWRender::MGeometry::DrawMode drawMode
	);

	virtual void populateHairInstances(
		const MHWRender::MGeometryRequirements& requirements,
        const MHWRender::MRenderItemList& renderItems,
		MHWRender::MGeometry& data
		);


	//uses geometry shader
	virtual void populateHairLines(
		const MHWRender::MGeometryRequirements& requirements,
        const MHWRender::MRenderItemList& renderItems,
		MHWRender::MGeometry& data
		);


	//polygonal hair
	virtual void populateHairPolys(
		const MHWRender::MGeometryRequirements& requirements,
        const MHWRender::MRenderItemList& renderItems,
		MHWRender::MGeometry& data
		);


	//uses geometry shader
	virtual void populateHairGuides(
		const MHWRender::MGeometryRequirements& requirements,
        const MHWRender::MRenderItemList& renderItems,
		MHWRender::MGeometry& data
		);

	void cacheHairPolys();
	void cacheGuidePolys();
	void cacheKnotPolys();

	shaveHairShape* shave;
	MObject			shobj;

	struct Poly {
		MVector t[3];
		MVector v[3];
		MColor  c[3];
		MVector uv[3];
	};

	class PolyGroup {
	public:
		std::vector<Poly> p;
	};
	std::vector<PolyGroup> groups;
	unsigned int ingroups;

	struct GuidePoly {
		MVector v[3];
		bool active;
	};
	std::vector<GuidePoly> guidePolys;

	struct KnotPoly {
		MVector v[3];
		bool active;
	};
	std::vector<KnotPoly> knotPolys;

	MHWRender::MTexture* instex;

	SelectionData mSelectionData;

	//	We're only allowed to access the DG during updateDG(). Below
	//	are the DG-dependent data that we need to cache for use in subsequent
	//	methods.
	//
	unsigned int mNumHairsToDraw;
	const shaveHairShape::DisplayHairCache* mHairCache;
	M3dView::LightingMode mLightMode;
	int mHairDisplayMode;
	MColor mSpecularTint;
	MColor mSpecularTint2;
	float mGloss;
	float mSpecular;
	const shaveInstanceDisplay* mInstanceCache;
	MMatrix mWorldToCam;
	MVector mViewDir;
	MVector mUpDir;
	MVector mRightDir;
	float mTransparency;
	bool mDoTipFade;
	int mPasses;
	const shaveHairShape::Guides* mGuideCache;
	float mGuideExtent;
};

#endif