使用slimdx查看投影矩阵计算
View Projection matrix calculation using slimdx
我正在使用 slimdx 构建一个小型可视化工具,但是,我最近遇到了一个问题。当我用 mvp 矩阵变换三角形时,它消失了。
常量缓冲区已正确加载,因为我可以看到通过它们加载了正确的颜色。
如果我不在顶点着色器中对其进行变换,则会看到用作测试的三角形。
所以我想问题出在视图矩阵或投影矩阵中。
而且我不知道我是否应该转置它们..
vertices = new DataStream(12 * 3, true, true);
vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
vertices.Position = 0;
vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
dc3D.InputAssembler.InputLayout = baseShaders.GetInputLayout();
dc3D.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
dc3D.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
// set the shaders
dc3D.VertexShader.Set(baseShaders.GetVertexShader());
dc3D.PixelShader.Set(baseShaders.GetPixelShader());
cbufferData = new Buffer(device, new BufferDescription
{
Usage = ResourceUsage.Default,
SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)),
BindFlags = BindFlags.ConstantBuffer
});
dc3D.VertexShader.SetConstantBuffer(cbufferData, 0);
Vector3 eye = new Vector3(4, 4, 4);
Vector3 target = new Vector3(0, 0, 0);
Vector3 up = new Vector3(0, 1, 0);
Matrix.LookAtLH(ref eye, ref target, ref up, out cbuffer.view) ;
//for now width and height are hardcoded.
Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 1.0f, 100.0f, out cbuffer.projection);
cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
//Matrix.Transpose(cbuffer.view);
//Matrix.Transpose(cbuffer.projection);
// update constant buffers.
var data = new DataStream(System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)), true, true);
data.Write(cbuffer);
data.Position = 0;
dc3D.UpdateSubresource(new DataBox(0, 0, data), cbufferData, 0);
已经几个小时了,我没有找到任何解决方案。
哦,这是顶点着色器代码:
cbuffer ConstantBuffer : register(b0)
{
matrix view;
matrix projection;
float4 color;
}
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
VOut output;
output.position = mul(mul(position, view), projection);
output.color = color;
return output;
}
通过做一些小的改动,事实上转置矩阵,我设法让代码工作。
这是顶点着色器:
cbuffer ConstantBuffer : register(b0)
{
float4x4 mvp;
float4 color;
}
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
VOut output;
output.position = mul(position, mvp);
output.color = color;
return output;
}
代码更改如下:
Vector3 eye = new Vector3(1, 1, 1);
Vector3 target = new Vector3(0, 0, 0);
Vector3 up = new Vector3(0, 1, 0);
Matrix view = new Matrix();
Matrix projection = new Matrix();
view = Matrix.LookAtLH(eye, target, up) ;
projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 0.1f, 100.0f);
cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
cbuffer.mvp = Matrix.Multiply(view, projection);
Matrix.Transpose(ref cbuffer.mvp, out cbuffer.mvp);
我正在使用 slimdx 构建一个小型可视化工具,但是,我最近遇到了一个问题。当我用 mvp 矩阵变换三角形时,它消失了。
常量缓冲区已正确加载,因为我可以看到通过它们加载了正确的颜色。 如果我不在顶点着色器中对其进行变换,则会看到用作测试的三角形。
所以我想问题出在视图矩阵或投影矩阵中。 而且我不知道我是否应该转置它们..
vertices = new DataStream(12 * 3, true, true);
vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
vertices.Position = 0;
vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
dc3D.InputAssembler.InputLayout = baseShaders.GetInputLayout();
dc3D.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
dc3D.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
// set the shaders
dc3D.VertexShader.Set(baseShaders.GetVertexShader());
dc3D.PixelShader.Set(baseShaders.GetPixelShader());
cbufferData = new Buffer(device, new BufferDescription
{
Usage = ResourceUsage.Default,
SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)),
BindFlags = BindFlags.ConstantBuffer
});
dc3D.VertexShader.SetConstantBuffer(cbufferData, 0);
Vector3 eye = new Vector3(4, 4, 4);
Vector3 target = new Vector3(0, 0, 0);
Vector3 up = new Vector3(0, 1, 0);
Matrix.LookAtLH(ref eye, ref target, ref up, out cbuffer.view) ;
//for now width and height are hardcoded.
Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 1.0f, 100.0f, out cbuffer.projection);
cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
//Matrix.Transpose(cbuffer.view);
//Matrix.Transpose(cbuffer.projection);
// update constant buffers.
var data = new DataStream(System.Runtime.InteropServices.Marshal.SizeOf(typeof(BaseShaders.ConstantBuffer)), true, true);
data.Write(cbuffer);
data.Position = 0;
dc3D.UpdateSubresource(new DataBox(0, 0, data), cbufferData, 0);
已经几个小时了,我没有找到任何解决方案。
哦,这是顶点着色器代码:
cbuffer ConstantBuffer : register(b0)
{
matrix view;
matrix projection;
float4 color;
}
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
VOut output;
output.position = mul(mul(position, view), projection);
output.color = color;
return output;
}
通过做一些小的改动,事实上转置矩阵,我设法让代码工作。
这是顶点着色器:
cbuffer ConstantBuffer : register(b0)
{
float4x4 mvp;
float4 color;
}
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
VOut output;
output.position = mul(position, mvp);
output.color = color;
return output;
}
代码更改如下:
Vector3 eye = new Vector3(1, 1, 1);
Vector3 target = new Vector3(0, 0, 0);
Vector3 up = new Vector3(0, 1, 0);
Matrix view = new Matrix();
Matrix projection = new Matrix();
view = Matrix.LookAtLH(eye, target, up) ;
projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 617/643.0f, 0.1f, 100.0f);
cbuffer.color = new Vector4(0.1f, 1.0f, 1.0f, 1.0f);
cbuffer.mvp = Matrix.Multiply(view, projection);
Matrix.Transpose(ref cbuffer.mvp, out cbuffer.mvp);