Skip to content

ModelMesh

Chuck Walbourn edited this page Sep 26, 2021 · 14 revisions

This class is part of the Model hierarchy. The primary purpose of this class is to be a shared container for a list of ModelMeshPart instances which are referenced by one or more instances of the Model class.

Note that Model uses a collection of std::shared_ptr instances to ModelMesh since meshes can be shared by multiple instances of Model.

Header

#include <Model.h>

Initialization

ModelMesh instances are typically created by a Model loader along with the ModelMeshPart instances that make up the mesh.

Type alias

ModelMesh::Collection is an alias for std::vector<std::shared_ptr<ModelMesh>>.

Simple drawing

Use the Model::Draw function which will call ModelMesh::Draw on all the meshes it contains. See Model for an example.

Advanced drawing

The ModelMesh::Draw method draws the mesh in two passes. In the first pass, all 'opaque' ModelMeshPart instances are drawn (i.e. ModelMeshPart::isAlpha is false), and in the second pass all 'alpha' instances are drawn (i.e. ModelMeshPart::isAlpha is true).

The Draw method assumes that the proper blend state, depth/stencil state, rasterizer state, and sampler state have been set up before being called. The ModelMesh::PrepareForRendering method can be used to set up standard defaults via CommonStates or it can be skipped in favor of custom state setting.

  • The opaque pass is drawn using Opaque blending, DepthDefault sorting, an optional wireframe mode or a winding mode based on ModelMesh::ccw.
  • The alpha pass is drawn using alpha blending, DepthRead sorting, an optional wireframe mode or a winding mode based on ModelMesh::ccw. The choice of alpha blending is based on ModelMesh::pmalpha to select between AlphaBlend and NonPremultiplied.

ModelMesh::Draw can be used to implement alternate 'scene graph' policies.

// Rather than draw each model's opaque and then alpha parts in turn, this version
// draws all the models' opaque parts first then all the alpha parts second which
// can be important for some complex scenes.

std::list<std::unique_ptr<Model>> models;

...

// Draw opaque parts
for(const auto& mit : models)
{
    auto model = mit.get();
    assert( model != nullptr );

    for(const auto& it : model->meshes)
    {
        auto mesh = it.get();
        assert( mesh != nullptr );

        mesh->PrepareForRendering( deviceContext, states, false );

        // Do model-level setCustomState work here

        mesh->Draw( deviceContext, world, view, projection, false );
    }
}

// Draw alpha parts (should really be done in back-to-front sorted order)
for(const auto& mit : models)
{
    auto model = mit.get();
    assert( model != nullptr );

    for(const auto& it : model->meshes)
    {
        auto mesh = it.get();
        assert( mesh != nullptr );

        mesh->PrepareForRendering( deviceContext, states, true );

        // Do model-level setCustomState work here

        mesh->Draw( deviceContext, world, view, projection, true );
    }
}

Metadata

In addition to the list of ModelMeshPart instances that make up the mesh, a ModelMesh also includes a name (a wide-character string) for tracking and application logic.

It includes a bool to indicate if the mesh should be rendered using counter-clockwise winding or clockwise winding (ccw), as well as a bool to indicate if the mesh should be rendered with premultiplied alpha blending or 'straight' alpha blending (pmalpha).

A ModelMesh also includes bounding information for culling & collision detection in the form of a BoundingSphere and a BoundingBox.

The choice of frame-of-reference for these bounding volumes is up to the Model loader, but is typically in 'local' coordinates.

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally