DirectX 10 - 整个对象闪烁,然后只绘制点
DirectX 10 - Full object flashes, then draws only points
我正在使用 C# 和 SharpDX 库对我正在处理的项目进行一些渲染。但是,我只能让对象在第一次通过时完全绘制,随后的每次通过只会绘制点。我使用 FillMode.Solid 或 FillMode.Wireframe 都没有关系。我也禁用了剔除。如果我围绕物体旋转相机,我仍然只能看到点。我的文件中有显示问题和要点的图像。这几天看了这段代码,完全没思路了,也许新手能看懂。
另外,好像只画了第一个三角形,没有画第二个。
图片如下:
这是我的代码:
网格初始化
rCom.mesh = new Components.Mesh3D(this.render.Device,
new Components.VertexStructures.Color[] {
new Components.VertexStructures.Color(
new SharpDX.Vector3(-1.0f, -1.0f, 0.0f), new SharpDX.Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(-1.0f, 1.0f, 0.0f), new SharpDX.Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(1.0f, 1.0f, 0.0f), new SharpDX.Vector4(0.0f, 0.0f, 1.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(1.0f, -1.0f, 0.0f), new SharpDX.Vector4(1.0f, 1.0f, 1.0f, 1.0f))
},
new[] {
0, 1, 2, 0, 2, 3
}
);
顶点结构 - 颜色
public static class VertexStructures
{
...
public struct Color
{
public SharpDX.Vector3 pos;
public SharpDX.Vector4 col;
public static int sizeInBytes
{ get { return Vector3.SizeInBytes + Vector4.SizeInBytes; } }
public Color(SharpDX.Vector3 pos, SharpDX.Vector4 col)
{ this.pos = pos; this.col = col; }
}
...
}
网格Class
public class Mesh3D
{
private D3D10.Device device;
public D3D10.VertexBufferBinding vertexBuffer;
public D3D10.Buffer indexBuffer;
public int numberOfVertices;
public int numberOfIndices;
public static D3D10.Buffer CreateBuffer<T>(D3D10.Device device, BindFlags bindFlags, params T[] items)
where T : struct
{
var len = Utilities.SizeOf(items);
var stream = new DataStream(len, true, true);
foreach (var item in items)
stream.Write(item);
stream.Position = 0;
var buffer = new D3D10.Buffer(device, stream, len, ResourceUsage.Default,
bindFlags, CpuAccessFlags.None, ResourceOptionFlags.None);
return buffer;
}
...
public Mesh3D(D3D10.Device device, VertexStructures.Color[] vertices, int[] indices = null)
{
this.numberOfVertices = vertices.Length;
this.numberOfIndices = indices.Length;
this.vertexBuffer = new VertexBufferBinding(
CreateBuffer<VertexStructures.Color>(device, BindFlags.VertexBuffer, vertices),
VertexStructures.Color.sizeInBytes, 0);
if (indices != null)
this.indexBuffer = CreateBuffer<int>(device, BindFlags.IndexBuffer, indices);
}
...
}
渲染更新代码
public override void Update(double timeDelta = 0.0f)
{
// Clear our backbuffer with the rainbow color
d3d10Device.ClearRenderTargetView(this.renderTargetView, (Color4)SharpDX.Color.CornflowerBlue);
float time = (float)(timeDelta / 1000.0f); // time in milliseconds?
// Do actual drawing here
foreach (RenderComponent com in this._components)
{
// Get any required components
PositionComponent pos = com.entity.GetComponent<PositionComponent>();
// Set up required buffers
var inputAssembler = this.d3d10Device.InputAssembler;
inputAssembler.SetVertexBuffers(0, com.mesh.vertexBuffer);
if (com.mesh.indexBuffer != null)
inputAssembler.SetIndexBuffer(com.mesh.indexBuffer, Format.R32_UInt, 0);
// Set up effect variables
// These matrices should always be defined in the shader, even if they're not used
com.shader.shader.GetVariableByIndex(0).AsMatrix().SetMatrix(this.camera.viewMatrix);
com.shader.shader.GetVariableByIndex(1).AsMatrix().SetMatrix(this.camera.projectionMatrix);
com.shader.shader.GetVariableByIndex(2).AsMatrix().SetMatrix(pos.rotationXMatrix);
com.shader.shader.GetVariableByIndex(3).AsMatrix().SetMatrix(pos.rotationYMatrix);
com.shader.shader.GetVariableByIndex(4).AsMatrix().SetMatrix(pos.rotationZMatrix);
com.shader.shader.GetVariableByIndex(5).AsMatrix().SetMatrix(pos.scalingMatrix);
com.shader.shader.GetVariableByIndex(6).AsMatrix().SetMatrix(pos.translationLocalMatrix);
com.shader.shader.GetVariableByIndex(7).AsMatrix().SetMatrix(pos.translationWorldMatrix);
foreach (var shaderVars in com.shader.vars)
{
// Eventually, we'll use this to set all the required variables needed
}
// Run through each technique, pass, draw
int i = 0, j = 0;
foreach (var techniqueContainer in com.shader.inputLayouts)
{
var technique = com.shader.shader.GetTechniqueByIndex(i);
foreach (var passContainer in techniqueContainer)
{
var pass = technique.GetPassByIndex(j);
inputAssembler.InputLayout = passContainer;
pass.Apply();
this.d3d10Device.Draw(com.mesh.numberOfVertices, 0);
j += 1;
}
i += 1;
}
}
// Present our drawn scene waiting for one vertical sync
this.swapChain.Present(1, PresentFlags.None);
}
最后,我的着色器文件
matrix View;
matrix Projection;
matrix rotationXMatrix;
matrix rotationYMatrix;
matrix rotationZMatrix;
matrix scalingMatrix;
matrix translationLocalMatrix;
matrix translationWorldMatrix;
struct VS_IN
{
float4 pos : POSITION;
float4 col : COLOR;
};
struct PS_IN
{
float4 pos : SV_POSITION;
float4 col : COLOR;
};
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
input.pos = mul(input.pos, rotationXMatrix);
input.pos = mul(input.pos, rotationYMatrix);
input.pos = mul(input.pos, rotationZMatrix);
input.pos = mul(input.pos, scalingMatrix);
input.pos = mul(input.pos, translationLocalMatrix);
input.pos = mul(input.pos, translationWorldMatrix);
input.pos = mul(input.pos, View);
input.pos = mul(input.pos, Projection);
output.pos = input.pos;
output.col = float4(1.0f, 1.0f, 1.0f, 1.0f);//input.col;
return output;
}
float4 PS( PS_IN input ) : SV_Target
{
return input.col;
}
technique10 Render
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}
如果需要任何进一步的信息或代码,请告诉我。我真的希望有人能帮我解决这个问题,我想不通。
在上面的代码中,您似乎没有设置拓扑(三角形、直线、点)。例如参见 [=13=].
在您的 Update()
方法中,您可能想要添加
inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
出于性能原因,您可能应该将其放在 foreach
之前,因为它不会改变。
你没有在 update()
舞台上用三角形块组织你的点。
我正在使用 C# 和 SharpDX 库对我正在处理的项目进行一些渲染。但是,我只能让对象在第一次通过时完全绘制,随后的每次通过只会绘制点。我使用 FillMode.Solid 或 FillMode.Wireframe 都没有关系。我也禁用了剔除。如果我围绕物体旋转相机,我仍然只能看到点。我的文件中有显示问题和要点的图像。这几天看了这段代码,完全没思路了,也许新手能看懂。
另外,好像只画了第一个三角形,没有画第二个。
图片如下:
这是我的代码:
网格初始化
rCom.mesh = new Components.Mesh3D(this.render.Device,
new Components.VertexStructures.Color[] {
new Components.VertexStructures.Color(
new SharpDX.Vector3(-1.0f, -1.0f, 0.0f), new SharpDX.Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(-1.0f, 1.0f, 0.0f), new SharpDX.Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(1.0f, 1.0f, 0.0f), new SharpDX.Vector4(0.0f, 0.0f, 1.0f, 1.0f)),
new Components.VertexStructures.Color(
new SharpDX.Vector3(1.0f, -1.0f, 0.0f), new SharpDX.Vector4(1.0f, 1.0f, 1.0f, 1.0f))
},
new[] {
0, 1, 2, 0, 2, 3
}
);
顶点结构 - 颜色
public static class VertexStructures
{
...
public struct Color
{
public SharpDX.Vector3 pos;
public SharpDX.Vector4 col;
public static int sizeInBytes
{ get { return Vector3.SizeInBytes + Vector4.SizeInBytes; } }
public Color(SharpDX.Vector3 pos, SharpDX.Vector4 col)
{ this.pos = pos; this.col = col; }
}
...
}
网格Class
public class Mesh3D
{
private D3D10.Device device;
public D3D10.VertexBufferBinding vertexBuffer;
public D3D10.Buffer indexBuffer;
public int numberOfVertices;
public int numberOfIndices;
public static D3D10.Buffer CreateBuffer<T>(D3D10.Device device, BindFlags bindFlags, params T[] items)
where T : struct
{
var len = Utilities.SizeOf(items);
var stream = new DataStream(len, true, true);
foreach (var item in items)
stream.Write(item);
stream.Position = 0;
var buffer = new D3D10.Buffer(device, stream, len, ResourceUsage.Default,
bindFlags, CpuAccessFlags.None, ResourceOptionFlags.None);
return buffer;
}
...
public Mesh3D(D3D10.Device device, VertexStructures.Color[] vertices, int[] indices = null)
{
this.numberOfVertices = vertices.Length;
this.numberOfIndices = indices.Length;
this.vertexBuffer = new VertexBufferBinding(
CreateBuffer<VertexStructures.Color>(device, BindFlags.VertexBuffer, vertices),
VertexStructures.Color.sizeInBytes, 0);
if (indices != null)
this.indexBuffer = CreateBuffer<int>(device, BindFlags.IndexBuffer, indices);
}
...
}
渲染更新代码
public override void Update(double timeDelta = 0.0f)
{
// Clear our backbuffer with the rainbow color
d3d10Device.ClearRenderTargetView(this.renderTargetView, (Color4)SharpDX.Color.CornflowerBlue);
float time = (float)(timeDelta / 1000.0f); // time in milliseconds?
// Do actual drawing here
foreach (RenderComponent com in this._components)
{
// Get any required components
PositionComponent pos = com.entity.GetComponent<PositionComponent>();
// Set up required buffers
var inputAssembler = this.d3d10Device.InputAssembler;
inputAssembler.SetVertexBuffers(0, com.mesh.vertexBuffer);
if (com.mesh.indexBuffer != null)
inputAssembler.SetIndexBuffer(com.mesh.indexBuffer, Format.R32_UInt, 0);
// Set up effect variables
// These matrices should always be defined in the shader, even if they're not used
com.shader.shader.GetVariableByIndex(0).AsMatrix().SetMatrix(this.camera.viewMatrix);
com.shader.shader.GetVariableByIndex(1).AsMatrix().SetMatrix(this.camera.projectionMatrix);
com.shader.shader.GetVariableByIndex(2).AsMatrix().SetMatrix(pos.rotationXMatrix);
com.shader.shader.GetVariableByIndex(3).AsMatrix().SetMatrix(pos.rotationYMatrix);
com.shader.shader.GetVariableByIndex(4).AsMatrix().SetMatrix(pos.rotationZMatrix);
com.shader.shader.GetVariableByIndex(5).AsMatrix().SetMatrix(pos.scalingMatrix);
com.shader.shader.GetVariableByIndex(6).AsMatrix().SetMatrix(pos.translationLocalMatrix);
com.shader.shader.GetVariableByIndex(7).AsMatrix().SetMatrix(pos.translationWorldMatrix);
foreach (var shaderVars in com.shader.vars)
{
// Eventually, we'll use this to set all the required variables needed
}
// Run through each technique, pass, draw
int i = 0, j = 0;
foreach (var techniqueContainer in com.shader.inputLayouts)
{
var technique = com.shader.shader.GetTechniqueByIndex(i);
foreach (var passContainer in techniqueContainer)
{
var pass = technique.GetPassByIndex(j);
inputAssembler.InputLayout = passContainer;
pass.Apply();
this.d3d10Device.Draw(com.mesh.numberOfVertices, 0);
j += 1;
}
i += 1;
}
}
// Present our drawn scene waiting for one vertical sync
this.swapChain.Present(1, PresentFlags.None);
}
最后,我的着色器文件
matrix View;
matrix Projection;
matrix rotationXMatrix;
matrix rotationYMatrix;
matrix rotationZMatrix;
matrix scalingMatrix;
matrix translationLocalMatrix;
matrix translationWorldMatrix;
struct VS_IN
{
float4 pos : POSITION;
float4 col : COLOR;
};
struct PS_IN
{
float4 pos : SV_POSITION;
float4 col : COLOR;
};
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
input.pos = mul(input.pos, rotationXMatrix);
input.pos = mul(input.pos, rotationYMatrix);
input.pos = mul(input.pos, rotationZMatrix);
input.pos = mul(input.pos, scalingMatrix);
input.pos = mul(input.pos, translationLocalMatrix);
input.pos = mul(input.pos, translationWorldMatrix);
input.pos = mul(input.pos, View);
input.pos = mul(input.pos, Projection);
output.pos = input.pos;
output.col = float4(1.0f, 1.0f, 1.0f, 1.0f);//input.col;
return output;
}
float4 PS( PS_IN input ) : SV_Target
{
return input.col;
}
technique10 Render
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}
如果需要任何进一步的信息或代码,请告诉我。我真的希望有人能帮我解决这个问题,我想不通。
在上面的代码中,您似乎没有设置拓扑(三角形、直线、点)。例如参见 [=13=].
在您的 Update()
方法中,您可能想要添加
inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
出于性能原因,您可能应该将其放在 foreach
之前,因为它不会改变。
你没有在 update()
舞台上用三角形块组织你的点。