forked from sideeffects/HoudiniEngineForMaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputMesh.C
202 lines (170 loc) · 6.47 KB
/
InputMesh.C
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
#include "InputMesh.h"
#include <maya/MFnMesh.h>
#include <maya/MDataBlock.h>
#include <maya/MFloatArray.h>
#include <maya/MFloatPointArray.h>
#include <maya/MFloatVectorArray.h>
#include <maya/MIntArray.h>
#include <maya/MMatrix.h>
#include "util.h"
InputMesh::InputMesh(int assetId, int inputIdx) :
Input(assetId, inputIdx),
myInputAssetId(0)
{
HAPI_CreateGeoInput(myAssetId, myInputIdx, &myInputInfo);
}
InputMesh::~InputMesh()
{
}
Input::AssetInputType
InputMesh::assetInputType() const
{
return Input::AssetInputType_Mesh;
}
void
InputMesh::setInputTransform(MDataHandle &dataHandle)
{
MMatrix transformMatrix = dataHandle.asMatrix();
float matrix[16];
transformMatrix.get(reinterpret_cast<float(*)[4]>(matrix));
HAPI_TransformEuler transformEuler;
HAPI_ConvertMatrixToEuler(matrix, HAPI_SRT, HAPI_XYZ, &transformEuler);
HAPI_SetObjectTransform(myInputAssetId, myInputInfo.objectId, transformEuler);
}
void
InputMesh::setInputGeo(
MDataBlock &dataBlock,
const MPlug &plug
)
{
MDataHandle dataHandle = dataBlock.inputValue(plug);
// extract mesh data from Maya
MObject meshObj = dataHandle.asMesh();
MFnMesh meshFn(meshObj);
// get face data
MIntArray faceCounts;
MIntArray vertexList;
meshFn.getVertices(faceCounts, vertexList);
Util::reverseWindingOrder(vertexList, faceCounts);
// set up part info
HAPI_PartInfo partInfo;
HAPI_PartInfo_Init(&partInfo);
partInfo.id = 0;
partInfo.faceCount = faceCounts.length();
partInfo.vertexCount = vertexList.length();
partInfo.pointCount = meshFn.numVertices();
// copy data to arrays
int * vl = new int[partInfo.vertexCount];
int * fc = new int[partInfo.faceCount];
vertexList.get(vl);
faceCounts.get(fc);
// Set the data
HAPI_SetPartInfo(myInputAssetId, myInputInfo.objectId,
myInputInfo.geoId, &partInfo);
HAPI_SetFaceCounts(myInputAssetId, myInputInfo.objectId,
myInputInfo.geoId, fc, 0, partInfo.faceCount);
HAPI_SetVertexList(myInputAssetId, myInputInfo.objectId,
myInputInfo.geoId, vl, 0, partInfo.vertexCount);
// Set position attributes.
HAPI_AttributeInfo pos_attr_info;
pos_attr_info.exists = true;
pos_attr_info.owner = HAPI_ATTROWNER_POINT;
pos_attr_info.storage = HAPI_STORAGETYPE_FLOAT;
pos_attr_info.count = meshFn.numVertices();
pos_attr_info.tupleSize = 3;
HAPI_AddAttribute(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "P", &pos_attr_info);
HAPI_SetAttributeFloatData(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "P", &pos_attr_info,
meshFn.getRawPoints(NULL), 0, meshFn.numVertices());
// normals
{
// get normal IDs
MIntArray normalCounts;
MIntArray normalIds;
meshFn.getNormalIds(normalCounts, normalIds);
if(normalIds.length())
{
// reverse winding order
Util::reverseWindingOrder(normalIds, faceCounts);
// get normal values
const float* rawNormals = meshFn.getRawNormals(NULL);
// build the per-vertex normals
std::vector<float> vertexNormals;
vertexNormals.reserve(normalIds.length() * 3);
for(unsigned int i = 0; i < normalIds.length(); ++i)
{
vertexNormals.push_back(rawNormals[normalIds[i] * 3 + 0]);
vertexNormals.push_back(rawNormals[normalIds[i] * 3 + 1]);
vertexNormals.push_back(rawNormals[normalIds[i] * 3 + 2]);
}
// add and set it to HAPI
HAPI_AttributeInfo attributeInfo;
attributeInfo.exists = true;
attributeInfo.owner = HAPI_ATTROWNER_VERTEX;
attributeInfo.storage = HAPI_STORAGETYPE_FLOAT;
attributeInfo.count = normalIds.length();
attributeInfo.tupleSize = 3;
HAPI_AddAttribute(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "N", &attributeInfo);
HAPI_SetAttributeFloatData(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "N", &attributeInfo,
&vertexNormals.front(), 0, normalIds.length());
}
}
// UVs
{
// get UV IDs
MIntArray uvCounts;
MIntArray uvIds;
meshFn.getAssignedUVs(uvCounts, uvIds);
// if there's UVs
if(uvIds.length())
{
// reverse winding order
Util::reverseWindingOrder(uvIds, uvCounts);
// get UV values
MFloatArray uArray;
MFloatArray vArray;
meshFn.getUVs(uArray, vArray);
// build the per-vertex UVs
std::vector<float> vertexUVs;
vertexUVs.reserve(vertexList.length() * 3);
unsigned int uvIdIndex = 0;
for(unsigned int i = 0; i < uvCounts.length(); ++i)
{
if(uvCounts[i] == faceCounts[i])
{
// has UVs assigned
for(int j = 0; j < uvCounts[i]; ++j)
{
vertexUVs.push_back(uArray[uvIds[uvIdIndex]]);
vertexUVs.push_back(vArray[uvIds[uvIdIndex]]);
vertexUVs.push_back(0);
uvIdIndex++;
}
}
else
{
// no UVs assigned
for(int j = 0; j < faceCounts[i]; ++j)
{
vertexUVs.push_back(0);
vertexUVs.push_back(0);
vertexUVs.push_back(0);
}
}
}
// add and set it to HAPI
HAPI_AttributeInfo attributeInfo;
attributeInfo.exists = true;
attributeInfo.owner = HAPI_ATTROWNER_VERTEX;
attributeInfo.storage = HAPI_STORAGETYPE_FLOAT;
attributeInfo.count = vertexList.length();
attributeInfo.tupleSize = 3;
HAPI_AddAttribute(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "uv", &attributeInfo);
HAPI_SetAttributeFloatData(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId, "uv", &attributeInfo,
&vertexUVs.front(), 0, vertexList.length());
}
}
// Commit it
HAPI_CommitGeo(myInputAssetId, myInputInfo.objectId, myInputInfo.geoId);
delete[] vl;
delete[] fc;
}