XNA4RenderingPipeline using DrawIndexedPrimitives
XNA4 DrawIndexedPrimitives vs DrawUserIndexedPrimitives
GraphicsDevice.DrawIndexedPrimitives
– 그래픽카드에 vertex와 index 정보를 초기화하고, 그것을 호출하여 Draw하는 방식
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.drawindexedprimitives.aspx
void InitializePrimitive()
{
// 중간생략…
// Initialize our Vertex Declaration
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
}
);
// Initialize the vertex buffer, allocating memory for each vertex.
vertexBuffer = new VertexBuffer(graphics.GraphicsDevice,
vertexDeclaration, vertices.Count, BufferUsage.None);
// Set the vertex buffer data to the array of vertices.
vertexBuffer.SetData(vertices.ToArray());
// Create an index buffer, and copy our index data into it.
indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(ushort),
indices.Count, BufferUsage.None);
indexBuffer.SetData(indices.ToArray());
}
void DrawPrimitive()
{
// 중간생략…
// Set our vertex declaration, vertex buffer, and index buffer.
graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
graphics.GraphicsDevice.Indices = indexBuffer;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
int primitiveCount = indices.Count / 3;
graphics.GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList, 0, 0, vertices.Count, 0, primitiveCount);
}
}
GraphicsDevice.DrawUserIndexedPrimitives
– 매 프레임마다 GPU로 vertex와 index 정보를 보내어 Draw 하는 방식
– vertex와 index 정보가 계속해서 바뀌는 경우에 사용 (그러나, DynamicVertexBuffer가 이것보다 좀 더 빠르다고 함)
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.drawuserindexedprimitives.aspx
{
// 중간생략…
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
int primitiveCount = indices.Count / 3;
graphics.GraphicsDevice.DrawUserIndexedPrimitives(
PrimitiveType.TriangleList, vertices.ToArray(), 0, vertices.Count,
indices.ToArray(), 0, primitiveCount);
}
}
XNA shader multi-pass
http://stackoverflow.com/questions/6787733/how-do-multiple-pass-shaders-work-in-hlsl
Shader의 technique 안에 2-pass 또는 N-pass 는 이전 패스에서 나온 결과에 다음 패스를 더하는 방식 – 예를 들어, multiple light, fog, multi-pass texturing 등에서 사용
XNA4Primitives3DEffectFX
XNA4Primitives3DEffect using Effect
1. FlatShaded.fx – FlatShaded VS & PS
2. AmbientDiffuseSpecularVertexLighting.fx – AmbientVS & SimplePS
3. AmbientDiffuseSpecularVertexLighting.fx – AmbientDiffuseVS & SimplePS
4. AmbientDiffuseSpecularVertexLighting.fx – AmbientDiffuseSpecularVS & SimplePS
5. PerPixelLighting.fx – PerPixelDiffuseVS & DiffuseOnlyPS
– PerPixelDiffuseVS : Position, WorldNormal, WorldPosition
– DiffuseOnlyPS : ambient+diffuse (per pixel)
6. PerPixelLighting.fx – PerPixelDiffuseVS & PhongPS
– PerVertexDiffuseVS : Position, WorldNormal, WorldPosition, Color (ambient+diffuse per vertex)
– DiffuseOnlyPS : color(ambient+diffuse)를 받아서 specular(per pixel)를 추가 (ambient+diffuse+specular)
7. PerPixelLighting.fx – PerPixelDiffuseVS & DiffuseAndPhongPS
– PerPixelDiffuseVS : Position, WorldNormal, WorldPosition
– DiffuseAndPhongPS : ambient+diffuse+specular (per pixel)
XNA4Primitives3DEffect
XNA4Primitives3DBasicEffect
Shader
MSDN XNA Shader Serie: Vertex Lighting, Textures & Coilors, Per-Pixel Lighting
http://xbox.create.msdn.com/ko-KR/education/catalog/article/shader_primer
XNA 4 Custom Shader Effects Tutorial
http://www.andrejeworutzki.de/game-developement/xna-4-tutorial-custom-shader-effects/