1
\$\begingroup\$

im just confused: Im trying to render 2 cubes instead of one. So in my main class i have my WORLD matrix. Then if i render the mesh the camera is multiplying all matrices with the worldProjectionBuffer etc... But how do i add the MeshRenderer´s world matrices to the of the Camera? And how do i add them to the Buffer then? :/ Please help me!

Creating the Buffer:

worldViewProjectionBuffer = new SharpDX.Direct3D11.Buffer(device1, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

Setting the Buffer:

context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer);

The Camera is updated every Frame:

currentCamera.Update(context, worldViewProjectionBuffer);
OnFrameUpdate();
renderer.Draw(context);

The Update of the Camera is recreating the matrices, but how would i add 2 other world matrices to the camera?

internal void Update(DeviceContext context, SharpDX.Direct3D11.Buffer worldView)
        {
            viewProjection = Matrix.Multiply(view, projection);
            worldViewProjection = world * viewProjection;
            worldViewProjection.Transpose();
            context.UpdateSubresource(ref worldViewProjection, worldView);
        }

And the MeshRenderer has it own world matrix - but how do i add the vertices from the model to it?!

 public sealed class MeshRenderer : BaseRenderer, IDisposable, ICloneable
    {
        private Model mesh;
        private Material material;
        private Matrix world;
...

Thank in Advance,

Best Regards, Kerbo

\$\endgroup\$
0
\$\begingroup\$

So, to be short, there are a few ways. But you are almost there with your current approach.

Probably the best way to get over the line is to add an additional Matrix to your constant buffer, say call it "meshMatrix". This is the only one you will need to update each time you want to render a model.

In your Vertex buffer, just multiply your WVP matrix by meshmatrix. Each time, before you render the next model, update your constant buffer. Do not modify your WVP matrix after you have set it, its used to project the world (basically), after that, your mesh matrices allow you to place and orientate objects in that world.

On a side note: If you declare your constant buffer in your shader with row_major, you wont need to execute a "Transpose" call in your CPU.

Now, in your vertex shader, you should be multiplying each vertex by the mesh matrix and then the WVP matrix in your constant buffer.

Simple vertex shader code

    struct VertexIn
{
    float4 pos : POSITION;
    float4 uv : TEXCOORD0;  
};

struct VertexOut
{
    float4 pos : SV_POSITION;
    float4 uv : TEXCOORD0;

};

// Vertex Shader
VertexOut VSMain(VertexIn input)
{
    VertexOut output = (VertexOut) 0;
    input.pos.w = 1.0f;
    output.pos = mul(input.pos, meshmatrix);
    output.pos = mul(output.pos, wvpmatrix);

    output.uv = input.uv;

    return output;
}

Each draw call you make, it pass the vertex buffer through and you need to multiply out the vertices, they are not permanently modified in your vertex buffer.

For light reading to understand more on your rendering pipeline, I recommend a couple of microsoft pages that will come in handy. these discuss index buffer and probably what you want to know more about, that being "Instance buffers".

DrawIndexedInstanced - Draws a mesh using its vertex buffer, index buffer and positioned in the world using an instance. (There is a lot more prep to get this to work, but its worth it). Rendering Pipeline - Just to highlight that vertex buffers may not be the only place you may end up using matrices.

Post more questions on this forum if you have follow ups.

\$\endgroup\$
2
  • \$\begingroup\$ Thank u i got i working now!!! Its amazing :D Have a nice Day! \$\endgroup\$ – Kerbo Games Feb 26 '18 at 16:08
  • \$\begingroup\$ I'm glad. Once you learn that part of the pipeline you will do alot! \$\endgroup\$ – ErnieDingo Feb 26 '18 at 19:29

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.