Here is the code I use to render a .cmo (from .fbx) mesh :

void Render()

    Clear(Colors::LimeGreen, 1.0f, 0);

    //g_d3dDeviceContext->VSSetShader(g_d3d_GEOM_VertexShader, nullptr, 0);
    //g_d3dDeviceContext->VSSetConstantBuffers(0, 3, g_d3dConstantBuffers);

    g_d3dDeviceContext->RSSetViewports(1, &g_Viewport);

    //g_d3dDeviceContext->PSSetShader(g_d3d_GEOM_PixelShader, nullptr, 0);
    //g_d3dDeviceContext->PSSetShaderResources(0, 1, &g_SkyboxShaderRessourceView);

    g_d3dDeviceContext->OMSetRenderTargets(1, &g_d3dRenderTargetView, g_d3dDepthStencilView);
    g_d3dDeviceContext->OMSetDepthStencilState(g_d3dDepthStencilState, 1);

    // Draw ship
    std::unique_ptr<CommonStates> states(new CommonStates(g_d3dDevice));
    DGSLEffectFactory fx(g_d3dDevice);
    auto testMesh = Model::CreateFromCMO(g_d3dDevice, L"media/meshes/space-frigate/space_frigate_6.cmo", fx, false);
    testMesh->Draw(g_d3dDeviceContext, *states, g_WorldMatrix, g_ViewMatrix, g_ProjectionMatrix);


Test render

enter image description here

What puzzles me :

  1. How do I use my own vertex / pixel shaders instead of the auto-generated ones ? Notice I commented the line where I set the shaders and it works just as well...

  2. What is the CommonStates object and what is it used for ? It doesn't seem to do anything even when adding i.e. states->Wireframe();

  3. Why is an DGSLEffectFactory required and what is it used for ?

  4. Why does the Z-test fail in the 2nd image ?

All in all : should I write my own mesh loader instead to rely on custom vertex / pixel shaders ?


You shouldn't be loading the model every frame. Just load it once and render it over and over as needed. Also, you should create the DGSLEffectFactory and CommonStates once and reuse it.

  1. The design is for you to implement your own IEffectFactory and then you can return your own custom IEffect for custom shaders. You can also control the individual subset drawing instead of calling Model::Draw. See ModelMesh for details.

  2. The CommonStates object is a factory for 'stock' states. The Model drawing uses it to set the default states. You can use it directly. For example, to set a wireframe rendering mode: g_d3dDeviceContext->RSSetState(states->Wireframe()); It's a helper object so you don't end up creating lots of redundant states for common combinations.

  3. The IEffectFactory interface you pass to the Model loader is the abstraction for creating IEffect instances (i.e. shaders) and for loading textures. The default ones also handle 'material and texture sharing' so you don't create unique instances for every single model's material.

  4. I can't tell exactly what's wrong based on our snippet, but a common thing to have go wrong is right-handed vs. left-handed view coordinates vs. the winding of the model. You can try passing a 'true' at the end of the loader to see if that worked.

The general model of DirectX Tool Kit is to provide you a solid starting point. You can then replace portions as you want. That said, it's best to really understand how it works before moving on your own solution. I suggest you take the time to work through the DirectX Tool Kit tutorials for a comprehensive introduction to using Direct3D 11 and the library.

  • \$\begingroup\$ I'm still very confused at what an effect is. Is it a file ? What does it contain ? How do I integrate an effect into my project ? How do I link it inside my code ? What is it's purpose ? I don't see the pros too at using Model::CreateFromXXX / Draw() when forcing custom shaders (as you said is possible) because the main workload I see is shader code (stuff I need to learn like texture mapping / specular / what else) versus loading an .obj file I can do using a library like github.com/syoyo/tinyobjloader I found today. What is your opinion on this ? \$\endgroup\$ – PinkTurtle Sep 22 '15 at 18:58
  • \$\begingroup\$ In DirectX Tool Kit, a IEffect is just an abstract interface for setting up shaders. The library includes a selection of 'built-in effects' and the shader binaries are included directly into static library to avoid the need to deal with runtime loading of shader binary files. Again, you should read through the docs on the DirectX Tool Kit wiki and work through the tutorial to make sure you have the foundation to move to custom shaders. \$\endgroup\$ – Chuck Walbourn Sep 22 '15 at 21:18
  • \$\begingroup\$ You can of course write your own model loader and renderer with or without DirectX Tool Kit. It's just a convenience library that supports .cmo, .sdkmesh, and .vbo binary formats that can map those format's materials to either the built-in XNA Game Studio like BasicEffects or to the Visual Studio Starter Kit DGSL effects system. The problem you are having is more basic than 'writing your own shader', and that's why I recommend you work through the foundational material first to make sure you haven't missed something unrelated in your understanding. \$\endgroup\$ – Chuck Walbourn Sep 22 '15 at 21:20
  • \$\begingroup\$ I am already fairly advanced at writing my own shaders and compiling / mixing / using them (example grid.turtlespeak.net/upload/LMo6pu2bZ3pYC5QU/lMiQKOZTmLoMe9nF/… pure HLSL pixel shader raytracing). My main concern was : I need data computed in custom shaders for classic geometry as well (I.E. per-pixel distance from the PoV for polygon meshes) that I obviously can't have at using auto-generated shaders. The drawback from this is I have then to implement the stuff in said shaders like Phong etc... Loading meshes isn't really the issue : I need flexibility with shaders. \$\endgroup\$ – PinkTurtle Sep 23 '15 at 11:55
  • 1
    \$\begingroup\$ You can of course go with a custom model loader and renderer, but another approach would be to use DirectXTK template and tutorial to setup a simple render test of that model. If that works, then you can track back what you did differently in your app. \$\endgroup\$ – Chuck Walbourn Sep 24 '15 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.