Vertex Geometry Builder
説明Description
CanderaThe :Candera::VertexGeometryBuilderクラスは、 class helps effectively build Candera::VertexGeometryオブジェクトを効果的に構築するのに役立ちます。 objects.
章Chapters
ソリューション例Example Solution
- VertexGeometryBuilderSolution in the folder cgi_studio_player/content/Tutorials/03_SceneGraphAndNodes
フォルダー内のVertexGeometryBuilderSolution 基本的な頂点ジオメトリTheビルダー操作は、ウィジェットbasic vertex geometry builder operations are exemplified in the widget VertexGeometryBuilderWidgetで例示されています。基本的に、ウィジェットが行うことは、ウィジェット.プロパティ「モデルWhatタイプ」に設定された値に応じて、シーン内で連続してレンダリングされるthe4widgetつのdoes, basically, is to generate the vertex buffer for the four 3Dモデルの頂点バッファーを生成することです。models which are rendered successively in the scene depending on the value set for the widget property "Model Type".
手続き型ジオメトリProcedural Geometry
例:Example: Wireframe Cube with LineList を使用したワイヤーフレーム キューブ
First example shows how to generate a wireframe cube using a 最初の例は、 Candera::LineListオブジェクトを使用してワイヤーフレーム キューブを生成する方法を示しています。object.
The vertex geometry will be generated procedurally using a 頂点ジオメトリは、 Candera::VertexGeometryBuilderオブジェクト object (例:e.g. m_builder )を使用して手続き的に生成されます。.
頂点要素の形式:Vertex 位置と色Element Format: Position and Color
最初に、頂点にどのような情報を設定するかを指定する必要があります。もちろん、位置に関する情報が必要です。オプションで、視覚効果を向上させるために色情報を追加することもできます。At the beginning we have to specify what kind of information will be set in the vertices. Of course we need information about the position and, optionally, for an improved visual effect, we could also add color information.
// Set the data type to be used: position & color
m_builder.SetVertexElementFormat(VertexGeometry::Position, 0, VertexGeometry::Float32_3);
m_builder.SetVertexElementFormat(VertexGeometry::Color, 0, VertexGeometry::Float32_4);
頂点データ:Vertex 位置Data: Position
頂点の位置情報は配列The position information for the vertices is obtained from the array c_cubeDataから取得されます。この配列には、実際には点 which, in fact, stores the coordinates of the points A とand GG. の座標が格納されています。残りの頂点の座標は、この点に対して相対的に取得されますThe coordinates for the rest of the vertices are obtained relatively to this points (たとえば、e.g. F(X A , Y G , Z G ) またはor D(X G , Y A , Z A )

// Coordinates for the points A & G
static const Float c_cubeData[2][3] = {
{-1.0F, -1.0F, -1.0F },
{1.0F, 1.0F, 1.0F }
};
VertexGeometryBuilder: 頂点要素の設定Set Vertex Elements
LineListBecause オブジェクトのthe LineType プロパティがproperty of the LineList object will be set to LineType::LinesLines, に設定されるため、頂点バッファーには立方体の各ラインの頂点のペアが含まれている必要があります:the vertex buffer will have to contain a pair of vertices for each line of the cube: 2 頂点/ラインvertices/line * 12 ラインlines = 24 頂点。vertices.
// Set the position & color information for each vertex
// Line segment AD
m_builder.SetVertexElement(VertexGeometry::Position, 0, c_cubeData[0][0], c_cubeData[0][1], c_cubeData[0][2]); // A
m_builder.SetVertexElement(VertexGeometry::Color, 0, 1.F, 0.F, 1.F, 1.F); // magenta
// Increment the vertex cursor
m_builder.IncrementVertexCursor();
m_builder.SetVertexElement(VertexGeometry::Position, 0, c_cubeData[1][0], c_cubeData[0][1], c_cubeData[0][2]); // D
m_builder.SetVertexElement(VertexGeometry::Color, 0, 1.F, 1.F, 1.F, 1.F); // white
m_builder.IncrementVertexCursor();
// Line segment AB
m_builder.SetVertexElement(VertexGeometry::Position, 0, c_cubeData[0][0], c_cubeData[0][1], c_cubeData[0][2]); // A
m_builder.SetVertexElement(VertexGeometry::Color, 0, 1.F, 0.F, 1.F, 1.F); // magenta
m_builder.IncrementVertexCursor();
m_builder.SetVertexElement(VertexGeometry::Position, 0, c_cubeData[0][0], c_cubeData[0][1], c_cubeData[1][2]); // B
m_builder.SetVertexElement(VertexGeometry::Color, 0, 1.F, 1.F, 0.F, 1.F); // yellow
// ...
頂点バッファを作成Create Vertex Buffer
Create a Candera::VertexBufferオブジェクトを作成し、 object and attach to it the vertex geometry obtained from the Candera::VertexGeometryBuilderから取得した頂点ジオメトリをアタッチします。:
VertexGeometry* vertexGeom = m_builder.GetVertexGeometry();
SharedPointer<VertexBuffer> vertexBuffer = VertexBuffer::Create();
static_cast<void>(vertexBuffer->SetVertexGeometry(vertexGeom, VertexBuffer::VertexGeometryDisposer::Dispose));
vertexBuffer->SetPrimitiveType(VertexBuffer::Lines);
Create LineList using the Vertex Buffer
Attach the vertex buffer to LineList を作成する
頂点バッファーを LineList オブジェクトにアタッチします。object:
// Create and configure the line list object
m_lineList = LineList::Create();
m_lineList->SetAppearance(Appearance::Create());
m_lineList->GetAppearance()->SetMaterial(Material::Create());
m_lineList->GetAppearance()->GetMaterial()->SetEmissive(Color(1.0F, 1.0F, 0.0F));
m_lineList->GetAppearance()->SetShader(m_shaderLightMaterial);
m_lineList->SetLineType(LineList::Lines);
m_lineList->SetWidth(1.0);
m_lineList->SetIntersectionTestEnabled(false);
m_lineList->SetVertexBuffer(vertexBuffer);
m_lineList->SetScopeMask(ScopeMask(false));
static_cast<void>(m_lineList->SetScopeEnabled(1, true));
m_lineList->SetRenderingEnabled(true);
m_lineList->SetName("Wireframe");
static_cast<void>(m_lineList->Upload());
ワイヤーフレームWireframe キューブの結果Cube Result
下の画像は、結果のワイヤフレームThe キューブを示しています。image below shows the resulting wireframe cube:

-
For an easier manipulation of the widget in SceneComposer
でウィジェットを簡単に操作するために、すべてのシェーダーがソースallファイルにハードコードされthe shaders are hardcoded in the source file (see cgi_studio_player\src\LegacyWidgets\Tutorial\3D\VertexGeometryBuilderWidget.cpp)を参照)、初期化時にウィジェットによって作成されます。and then created by the widget at the initialization time.または、アプリケーションがアセットAlternativelyライブラリを使用する場合は、obtain shaders from Candera::AssetProvider (例:e.g. GetAssetProvider()->GetShader("..."))からシェーダーを取得します。in case the application uses an asset library.
インデックス付きジオメトリIndexed Geometry
例:Example: 1Solid つの三角形ストリップからのソリッドCube キューブfrom one Triangle Strip
The next example will show how to create a solid cube from one triangle strip using a 次の例では、 Candera::Meshオブジェクトを使用して、1 つの三角形ストリップからソリッドobject. キューブを作成する方法を示します。この場合、立方体は、特別な方法で順序付けする必要があるIn this case the cube is determined by only 14 個の頂点のみによって決定されます。vertices which have to be ordered in a special way.
次の図は、展開された立方体と、頂点の順序付けの考えられる解決策を示しています。The figure below presents a cube unfolded and a possible solution of ordering the vertices:

インデックス付き頂点シーケンスIndexed Vertex Sequence
頂点シーケンスはThe vertex sequence is stored in the c_index_order配列に格納され、順序がエンコードされます。 array, encoding the order:
- G->C->F->B->A->C->D->H->A->E->F->H->G->C
.
各配列要素は、頂点バッファー内のオフセットを表します。Each array element represents an offset it the vertex buffer.
// The vertex sequence - triangle strip
static const UInt16 c_index_order[] = { 12, 11, 7, 3, 0, 11, 1, 17, 0, 5, 7, 17, 12, 11 };
前の例で使用された頂点ジオメトリThe ビルダーvertex オブジェクトには、位置と色の情報が設定されたgeometry builder object used in the previous example has already 24 個の頂点が既に含まれています。この場合、必要な頂点はvertices set with position and color information. Because only 14 vertices out of 24 個のうちare 14needed 個だけであり、それらのシーケンスも異なる必要があるため、インデックスin バッファーが追加されます。this case and, also, because their sequence has to be different, an index buffer is added:
// Change the order of the vertices using indexes
for (Int index = 0; index < 14; index++) {
// Define the value of the index
m_builder.SetIndexElement(c_index_order[index]);
// Increment the index cursor
m_builder.IncrementIndexCursor();
}
Create TriangleStrip 頂点バッファーの作成Vertex Buffer
ビルダーからのジオメトリを使用して頂点バッファーを作成します。Create the vertex buffer using the geometry from the builder:
VertexGeometry* vertexGeom = m_builder.GetVertexGeometry();
SharedPointer<VertexBuffer> vertexBuffer = VertexBuffer::Create();
static_cast<void>(vertexBuffer->SetVertexGeometry(vertexGeom, VertexBuffer::VertexGeometryDisposer::Dispose));
vertexBuffer->SetPrimitiveType(VertexBuffer::TriangleStrip);
頂点バッファーを使用してメッシュを作成するCreate Mesh using the Vertex Buffer
メッシュを構成し、頂点バッファーをアタッチします。Configure mesh and attach the vertex buffer:
m_mesh = Mesh::Create(); m_mesh->SetVertexBuffer(vertexBuffer); m_mesh->SetAppearance(appearance); m_mesh->SetRenderingEnabled(false); m_mesh->SetName("SolidNonTextured"); static_cast<void>(m_mesh->Upload());
ソリッドSolid キューブの結果Cube Result
以下の画像は、結果として得られる立体立方体を示しています。The image below shows the resulting solid cube:

頂点要素の削除と追加Remove & Add Vertex Elements
例:Example: テクスチャTextured キューブCube
この例では、頂点ジオメトリIn ビルダthis オブジェクトを使用して、テクスチャ付き立方体の頂点ジオメトリを生成します。example the vertex geometry builder object will be used to generate the vertex geometry for a textured cube.
メッシュにテクスチャを追加するには、その頂点にAdding texture to a mesh requires that its vertices should contain specific information like 法線normalや and テクスチャ座標texture coordinateなどの特定の情報が含まれている必要があります。また、. Also, the 色color情報は不要になったため、削除できます。 information is no longer needed so it can be removed:
// Vertex Elements Manipulation
// Remove color data associated to the vertices
m_builder.RemoveVertexElement(VertexGeometry::Color,0);
// Add data type to be used: texture coordinate & normal
m_builder.SetVertexElementFormat(VertexGeometry::TextureCoordinate, 0, VertexGeometry::Float32_2);
m_builder.SetVertexElementFormat(VertexGeometry::Normal, 0, VertexGeometry::Float32_3);
頂点データ:Vertex 法線とテクスチャ座標Data: Normals and Texture Coordinates
頂点の新しいデータ型を追加したら、対応する情報を追加します。まず、頂点の法線を追加します。After adding the new data types for the vertices it is time to add the corresponding information. First, add normals for vertices:
// Move the cursor to the first vertex
m_builder.SetVertexCursor();
for (UInt32 i = 0; i < m_builder.GetVertexCount(); i++ )
{
m_builder.SetVertexElement(VertexGeometry::Normal, 0, 0.0F, 0.0F, 1.0F);
m_builder.IncrementVertexCursor();
}
In order to map the UV テクスチャ座標値を正しくマッピングするには、面ごとにtexture coordinate values correctly, the cube faces should be drawn using 4 つの頂点を使用して立方体面を描画する必要があるため、この場合はvertices per face, so, in this case, all 24 個の頂点すべてが使用されます。もちろん、頂点の順序を調整する必要があります。vertices will be used. Of course, the vertex order has to be adjusted.
新しい頂点の順序は、The new vertex order is stored in the c_indexDataTexturedCube配列に格納されます。 array.
// Vertex order - textured cube
static const UInt16 c_indexDataTexturedCube[24] = {
1,17,0,5,
2,6,3,7,
9,8,11,12,
13,14,19,21,
15,23,16,22,
4,10,20,18
};
次に、頂点を並べ替えます。Now, reorder the vertices:
// Overwrite the index buffer
// Move the cursor to the first index
m_builder.SetIndexCursor();
for (UInt32 i = 0; i < m_builder.GetVertexCount(); i++ )
{
// Define the value of the index
m_builder.SetIndexElement(c_indexDataTexturedCube[i]);
m_builder.IncrementIndexCursor();
}
立方体の各面の頂点は、以下に示すようにテクスチャ座標情報で設定する必要があります。The vertices of each face of the cube should be set with texture coordinate information as shown below:

頂点のテクスチャ座標を設定します。Set the texture coordinates for vertices:
Int idx = 0;
for (Int nb_of_faces = 0; nb_of_faces<6; nb_of_faces++) {
for (Int u=0; u<=1; u++) {
for (Int v=0; v<=1; v++) {
//Set the position of the vertex cursor
m_builder.SetVertexCursor(c_indexDataTexturedCube[idx]);
m_builder.SetVertexElement(VertexGeometry::TextureCoordinate, 0, static_cast<Float>(u), static_cast<Float>(v));
idx++;
}
}
}
The vertex buffer is created and configured in the same way as in the 頂点バッファーは、前の例previous exampleと同じ方法で作成および構成されます。.
メッシュのセットアップも似ていますが、The mesh setup is also similar but, unlike the ソリッドsolid キューブcubeとは異なり、テクスチャ, キューブの外観はテクスチャとテクスチャthe シェーダで設定する必要があります。textured cube appearance has to be set with a texture and a texture shader.
テクスチャTextured キューブの結果Cube Result
最終結果は次のとおりです。Here is the final result:

範囲データの使用法Range Data Usage
例:Example: 範囲データを使用したソリッドSolid キューブCube using Range Data
This example will show how to create a solid cube as in the この例では、second 2 番目の例exampleと同様にソリッド キューブを作成する方法を示しますが、範囲データを使用します。but using range data.
最初に、頂点ジオメトリFirst, ビルダーからのすべての既存データが削除されます。all existing data from the vertex geometry builder is removed.
// Clear all data stored by the builder
m_builder.Clear();
頂点データは、2The つのバッファーvertex data is taken from the two buffers c_positionDataおよび and c_colorDataから取得されます。.
static const Float c_positionData[8][3] = {
{ -1.0F, 1.0F, 1.0F},
{ 1.0F, 1.0F, 1.0F},
{ -1.0F, -1.0F, 1.0F},
{ 1.0F, -1.0F, 1.0F},
{ -1.0F, 1.0F, -1.0F},
{ 1.0F, 1.0F, -1.0F},
{ -1.0F, -1.0F, -1.0F},
{ 1.0F, -1.0F, -1.0F}
};
static const Float c_colorData[8][4] = {
{ 0.0F, 0.0F, 1.0F, 1.0F},
{ 0.0F, 1.0F, 0.0F, 1.0F},
{ 0.0F, 1.0F, 1.0F, 1.0F},
{ 1.0F, 0.0F, 0.0F, 1.0F},
{ 1.0F, 0.0F, 1.0F, 1.0F},
{ 1.0F, 1.0F, 0.0F, 1.0F},
{ 1.0F, 1.0F, 1.0F, 1.0F},
{ 1.0F, 0.5F, 1.0F, 1.0F}
};
頂点シーケンスは、配列The vertex sequence is taken from the array c_indexDataから取得されます。.
// Range data index
static const UInt16 c_indexData[] = {3, 2, 1, 0, 4, 2, 6, 3, 7, 1, 5, 4, 7, 6 };
メソッドUse the methods Candera::VertexGeometryBuilder::SetVertexElementRangeおよび and Candera::VertexGeometryBuilder::SetIndexElementRange を使用して、必要な情報を追加します。to add the required information.
// Add position, color and an index from range data
// Splice values to the position buffer starting with the first vertex
static_cast<void>(m_builder.SetVertexElementRange(VertexGeometry::Position, 0, 0, VertexGeometry::Float32_3,
8, 3*sizeof(Float), c_positionData ));
// Splice values to the color buffer starting with the first vertex
static_cast<void>(m_builder.SetVertexElementRange(VertexGeometry::Color, 0, 0, VertexGeometry::Float32_4,
8, 4*sizeof(Float), c_colorData ));
// Splice values to the index buffer starting with the first index
static_cast<void>(m_builder.SetIndexElementRange(0, sizeof(c_indexData)/sizeof(UInt16),c_indexData));
頂点The バッファーの構成vertex buffer configurationと and the メッシュのセットアップmesh setupは、2 番目の例のものと同じです。are identical with the ones from the second example.
最終結果は次のとおりです。Here is the final result:

ジオメトリの連結Concatenate Geometries
VertexGeometryBuilder::SpliceGeometry
場合によっては、2In つの頂点ジオメトリを連結すると便利な場合があります。これは、以下に示すように、some cases it might be useful to concatenate two vertex geometries. This can be done by using the Candera::VertexGeometryBuilder::SpliceGeometryメソッドを使用して実行できます。 method as shown below:
m_builder.SpliceGeometry(0, 0, vertexGeometry1);
m_builder.SpliceGeometry(vertexGeometry1->GetVertexCount(), vertexGeometry1->GetIndexCount(), vertexGeometry2);
vertexGeometry1cat2 = builder.GetGeometry();