Advertisement Jump to content
Adam Jachocki

DirectxTK and basic concept of drawing models

Recommended Posts

Hi, I am completely newbie. I've been reading about DirectX for 2, 3 months now and the only thing I am able to do is to draw a simple model on a scene (with lighting).

I started to do things using DirectXTK. But I think I don't understand some basic concepts, so maybe someone would be so kind and will explain them for me.

So I tried to create basic scene with anchor and some models. By anchor I mean just x, y, z axes.

So I did something like that (I am using basic examples from here:

    Vector3 originVector(0.f, 0.f, 0.f);
    m_batch->DrawLine(VertexPositionColor(originVector, Colors::Red), VertexPositionColor(Vector3::UnitX, Colors::Red));
    m_batch->DrawLine(VertexPositionColor(originVector, Colors::Yellow), VertexPositionColor(Vector3::UnitY, Colors::Yellow));
    m_batch->DrawLine(VertexPositionColor(originVector, Colors::Green), VertexPositionColor(Vector3::UnitZ, Colors::Green));

Batch is just primitive batch(with VertexPositionColor) that I use to drawing axes.

This code gives me beautiful colorful axes.

But now I wanted to add a model. Simple sphere. So I did (before drawing axes):

m_shape->Draw(m_world, m_view, m_proj, Colors::Red);

(m_world is identity matrix)
And then first problem arouse. My axes now all have red color. Actually they get the color that my sphere has. Why is that?

Next, I wanted to move the sphere somewhere else, so I did:

Matrix m1 = Matrix::CreateTranslation(Vector3(-5.f, -2.f, -5.f));
m_shape->Draw(m1, m_view, m_proj, Colors::Red);

And what happened? My axes moved with the sphere. Now they are not drawn in place that I wanted, but they "come from inside" the sphere. Why is that?

Share this post

Link to post
Share on other sites

I haven't used DirectXTK, but maybe you're calling m_shape->Draw() inbetween m_batch->Begin() and m_batch->End(). I could be wrong, but I don't think its meant to be used like that. That's the only reason I can think of for the color changing.

As for the axes translation problem, I think you are changing the world matrix which I guess is effecting the other thing being drawn after it, I'm not sure how the Draw function works. In regular DirectX and OpenGL you would create a shader, and pass the same matrices as you do in m_shape->Draw(), and that would effect the entire draw call. Any changes to the matrices would effect the next draw calls, which I think might be happening with the axes. You might need to call something like SetWorld(identityMatrix) before rendering the axes, I don't know what exactly is suppose to do that, since I don't even know if m_batch has a function like that.

Share this post

Link to post
Share on other sites
On 12/1/2017 at 4:31 PM, Adam Jachocki said:

m_shape->Draw(m_world, m_view, m_proj, Colors::Red);

What is the type of shape? Is this part of DirectXTK or is this something you wrote yourself, since i do not see to find a member method with that signature.

If this method does not use the DirectXTK's PrimitiveBatch internally you may not call it between PrimitiveBatch::Begin() and PrimitiveBatch::End() like @Yxjmir pointed out. The PrimitiveBatch (and SpriteBatch) modify the pipeline. This is already the case when calling PrimitiveBatch::Begin(). So you probably override something you do not want in both cases.

Start your .exe with, take a screenshot and notice the sequence of pipeline calls. Here, you will probably notice some wrong states (buffers, shaders, whatever) of your m_shape->Draw interferring with your line drawing.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!