-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometryUtility.cxx
187 lines (151 loc) · 6.43 KB
/
GeometryUtility.cxx
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
/****************************************************************************************
Copyright (C) 2015 Autodesk, Inc.
All rights reserved.
Use of this software is subject to the terms of the Autodesk license agreement
provided at the time of installation or download, or which otherwise accompanies
this software in either electronic or hard copy form.
****************************************************************************************/
#include "GeometryUtility.h"
FbxNode * CreatePyramid(FbxScene * pScene, const char * pName, double pBottomWidth, double pHeight)
{
FbxMesh * lPyramid = FbxMesh::Create(pScene, pName);
// Calculate the vertices of the pyramid
const double lBottomWidthHalf = pBottomWidth / 2;
const FbxVector4 PyramidControlPointArray[] =
{
FbxVector4(0, pHeight, 0),
FbxVector4(lBottomWidthHalf, 0, lBottomWidthHalf),
FbxVector4(lBottomWidthHalf, 0, -lBottomWidthHalf),
FbxVector4(-lBottomWidthHalf, 0, -lBottomWidthHalf),
FbxVector4(-lBottomWidthHalf, 0, lBottomWidthHalf)
};
// Initialize and set the control points of the mesh
const int lControlPointCount = sizeof(PyramidControlPointArray) / sizeof(FbxVector4);
lPyramid->InitControlPoints(lControlPointCount);
for (int lIndex = 0; lIndex < lControlPointCount; ++lIndex)
{
lPyramid->SetControlPointAt(PyramidControlPointArray[lIndex], lIndex);
}
// Set the control point indices of the bottom side of the pyramid
lPyramid->BeginPolygon();
lPyramid->AddPolygon(1);
lPyramid->AddPolygon(4);
lPyramid->AddPolygon(3);
lPyramid->AddPolygon(2);
lPyramid->EndPolygon();
// Set the control point indices of the front side of the pyramid
lPyramid->BeginPolygon();
lPyramid->AddPolygon(0);
lPyramid->AddPolygon(1);
lPyramid->AddPolygon(2);
lPyramid->EndPolygon();
// Set the control point indices of the left side of the pyramid
lPyramid->BeginPolygon();
lPyramid->AddPolygon(0);
lPyramid->AddPolygon(2);
lPyramid->AddPolygon(3);
lPyramid->EndPolygon();
// Set the control point indices of the back side of the pyramid
lPyramid->BeginPolygon();
lPyramid->AddPolygon(0);
lPyramid->AddPolygon(3);
lPyramid->AddPolygon(4);
lPyramid->EndPolygon();
// Set the control point indices of the right side of the pyramid
lPyramid->BeginPolygon();
lPyramid->AddPolygon(0);
lPyramid->AddPolygon(4);
lPyramid->AddPolygon(1);
lPyramid->EndPolygon();
// Attach the mesh to a node
FbxNode * lPyramidNode = FbxNode::Create(pScene, pName);
lPyramidNode->SetNodeAttribute(lPyramid);
// Set this node as a child of the root node
pScene->GetRootNode()->AddChild(lPyramidNode);
return lPyramidNode;
}
typedef double Vector4[4];
typedef double Vector2[2];
// Create a cube.
FbxNode* CreateCube(FbxScene* pScene, const char* pName, FbxDouble3& pLclTranslation)
{
// indices of the vertices per each polygon
static int vtxId[24] = {
0,1,2,3, // front face (Z+)
1,5,6,2, // right side (X+)
5,4,7,6, // back face (Z-)
4,0,3,7, // left side (X-)
0,4,5,1, // bottom face (Y-)
3,2,6,7 // top face (Y+)
};
// control points
static Vector4 lControlPoints[8] = {
{ -5.0, 0.0, 5.0, 1.0}, { 5.0, 0.0, 5.0, 1.0}, { 5.0,10.0, 5.0, 1.0}, { -5.0,10.0, 5.0, 1.0},
{ -5.0, 0.0, -5.0, 1.0}, { 5.0, 0.0, -5.0, 1.0}, { 5.0,10.0, -5.0, 1.0}, { -5.0,10.0, -5.0, 1.0}
};
// normals
static Vector4 lNormals[8] = {
{-0.577350258827209,-0.577350258827209, 0.577350258827209, 1.0},
{ 0.577350258827209,-0.577350258827209, 0.577350258827209, 1.0},
{ 0.577350258827209, 0.577350258827209, 0.577350258827209, 1.0},
{-0.577350258827209, 0.577350258827209, 0.577350258827209, 1.0},
{-0.577350258827209,-0.577350258827209,-0.577350258827209, 1.0},
{ 0.577350258827209,-0.577350258827209,-0.577350258827209, 1.0},
{ 0.577350258827209, 0.577350258827209,-0.577350258827209, 1.0},
{-0.577350258827209, 0.577350258827209,-0.577350258827209, 1.0}
};
// uvs
static Vector2 lUVs[14] = {
{ 0.0, 1.0},
{ 1.0, 0.0},
{ 0.0, 0.0},
{ 1.0, 1.0}
};
// indices of the uvs per each polygon
static int uvsId[24] = {
0,1,3,2,2,3,5,4,4,5,7,6,6,7,9,8,1,10,11,3,12,0,2,13
};
// create the main structure.
FbxMesh* lMesh = FbxMesh::Create(pScene,"");
// Create control points.
lMesh->InitControlPoints(8);
FbxVector4* vertex = lMesh->GetControlPoints();
memcpy((void*)vertex, (void*)lControlPoints, 8*sizeof(FbxVector4));
// create the materials.
/* Each polygon face will be assigned a unique material.
*/
FbxGeometryElementMaterial* lMaterialElement = lMesh->CreateElementMaterial();
lMaterialElement->SetMappingMode(FbxGeometryElement::eAllSame);
lMaterialElement->SetReferenceMode(FbxGeometryElement::eIndexToDirect);
lMaterialElement->GetIndexArray().Add(0);
// Create polygons later after FbxGeometryElementMaterial is created. Assign material indices.
int vId = 0;
for (int f=0; f<6; f++)
{
lMesh->BeginPolygon();
for (int v=0; v<4; v++)
lMesh->AddPolygon(vtxId[vId++]);
lMesh->EndPolygon();
}
// specify normals per control point.
FbxGeometryElementNormal* lNormalElement = lMesh->CreateElementNormal();
lNormalElement->SetMappingMode(FbxGeometryElement::eByControlPoint);
lNormalElement->SetReferenceMode(FbxGeometryElement::eDirect);
for (int n=0; n<8; n++)
lNormalElement->GetDirectArray().Add(FbxVector4(lNormals[n][0], lNormals[n][1], lNormals[n][2]));
// Create the node containing the mesh
FbxNode* lNode = FbxNode::Create(pScene,pName);
lNode->LclTranslation.Set(pLclTranslation);
lNode->SetNodeAttribute(lMesh);
lNode->SetShadingMode(FbxNode::eTextureShading);
// create UVset
FbxGeometryElementUV* lUVElement1 = lMesh->CreateElementUV("UVSet1");
FBX_ASSERT( lUVElement1 != NULL);
lUVElement1->SetMappingMode(FbxGeometryElement::eByPolygonVertex);
lUVElement1->SetReferenceMode(FbxGeometryElement::eIndexToDirect);
for (int i = 0; i <4; i++)
lUVElement1->GetDirectArray().Add(FbxVector2(lUVs[i][0], lUVs[i][1]));
for (int i = 0; i<24; i++)
lUVElement1->GetIndexArray().Add(uvsId[i%4]);
return lNode;
}