XSIのシーンからデータを抽出する1
随分と前回の更新から間が空いてしまいました…
今回からはシーンのデータにアクセスし描画に必要なデータの抽出する方法です。
最初に頂点データとポリゴンのインデックスを取得します。
頂点データを得るためにはまずPolygonMeshクラスを取得しなくてはなりません。
PolygonMeshクラスを取得する方法は以下のような流れになります。
1.Application::GetActiveSceneRoot()でルートのModelクラスを取得します。
2.Model::GetPrimitives()を呼び出して、Primitiveの配列を取得します。
3.Primitive::GetGeometry()でGeometoryクラスを取得し、Geometory::IsA(siPolygonMeshID)を呼び出し、ポリゴンメッシュクラスかどうか確かめる。
4.Primitive::GetGeometry()で取得できたのがPolygonMeshクラスであることが確認されたら、OK。
5.すべてのPrimitiveを調べてもPolygonMeshクラスを得られなかったら、Model::GetChildren()を呼び出して、子のオブジェクトを次々と検索していきます。
ソースコードはこんな感じになります。
void SerchPolygonMesh( const X3DObject& obj ) { CRefArray primitives = obj.GetPrimitives(); for( LONG i = 0 ; i < primitives.GetCount() ; ++i ){ Primitive primitive( primitives[ i ] ); Geometry geometry = primitive.GetGeometry(); if( geometry.IsA( siPolygonMeshID ) ){ PolygonMesh mesh( geometry ); // ポリゴンメッシュ } } CRefArray children = obj.GetChildren(); for( LONG i = 0 ; i < children.GetCount() ; ++i ){ SerchPolygonMesh( children[ i ], model_data_set ); } } { Application app; Project project = app.GetActiveProject(); Scene scene = project.GetActiveScene(); Model root = scene.GetRoot(); SerchPolygonMesh( root ); }
PolygonMeshクラスを取得出来たら、次のようにして頂点データを取得します。
{ CVertexRefArray vertices = mesh.GetVertices(); // 頂点データ for( LONG i = 0 ; i < vertices.GetCount() ; ++i ){ Vertex vertex = vertices[ i ]; bool is_normal_valid; MATH::CVector3 p = vertex.GetPosition(); // 頂点座標 MATH::CVector3 n = vertex.GetNormal( is_normal_valid ); // 法線 } }
ちなみに上記のis_normal_validがfalseの時は法線が無効なので、注意が必要です。
頂点データだけ取得しても、面を構成するためのデータが無くては描画できません。
以下のようにして面情報を取得します。
{ CPolygonFaceRefArray polygons = mesh.GetPolygons(); // 面データ for( LONG i = 0 ; i < polygons.GetCount() ; ++i ){ PolygonFace polygon = polygons[ i ]; CLongArray triangle_indices = polygon.GetTriangleSubIndexArray(); CLongArray vertex_indices = polygon.GetVertices().GetIndexArray(); for( LONG j = 0 ; j < ( triangle_indices.GetCount() / 3 ) ; ++j ){ LONG index0 = vertex_indices[ triangle_indices[ ( j * 3 ) + 0 ] ]; LONG index1 = vertex_indices[ triangle_indices[ ( j * 3 ) + 1 ] ]; LONG index2 = vertex_indices[ triangle_indices[ ( j * 3 ) + 2 ] ]; } } }
上記の index0, 1, 2 が vertices のインデックスになります。
vertices[ index0 ], vertices[ index1 ], vertices[ index2 ]
を並べることで1つの三角形ポリゴンになります。
これで描画に必要な最低限のデータを取得出来ました。
このデータを元に以前のプロジェクトに組み込んでみたのが以下の画像です。
どこかで見たような物が表示されました。
ソースコードはもう少し先に進んでからまとめて公開したいと思います。
次はマテリアルとUVデータの取得です。
コメントする
トラックバックする
トラックバック用URL: