Здравствуйте! Никак не могу разобраться как выводить простой треугольник в DX11. Если использую шейдер, то экран пустой, а если по старинке вывожу, то все работает нормально.
Если так,
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
// device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 1);
int passes = effect.Begin(FX.None);
for (int i = 0; i < passes; i++)
{
effect.BeginPass(i);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 1);
effect.EndPass();
}
effect.End();
то не работает.
А если все закомментирую, а оставлю только 1.
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 1);
, то треугольник выводится. Подскажите пожалуйста как исправить.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Device device;
private float angle;
private VertexBuffer vb = null;
private CustomVertex.PositionColored[] verts;
private const int NumberItem = 12;
private bool NeedRecreate = false;
private static readonly int InitCount = System.Environment.TickCount;
private static readonly short[] indices = {
2,0,1,// Задняя грань: треугольник 2-0-1 (невидимая сторона - обход левый)
2,1,3,// Задняя грань: треугольник 2-1-3 (невидимая сторона - обход левый)
5,6,4,// Передняя грань: треугольник 5-6-4 (лицевая сторона - обход правый)
5,7,6,// Передняя грань: треугольник 5-7-6 (лицевая сторона - обход правый)
0,5,4,// Верхняя грань: треугольник 0-5-4 (лицевая сторона - обход правый)
0,2,5,// Верхняя грань: треугольник 0-2-5 (лицевая сторона - обход правый)
1,6,7,// Нижняя грань: треугольник 1-6-7 (невидимая сторона - обход левый)
1,7,3,// Нижняя грань: треугольник 1-7-3 (невидимая сторона - обход левый)
0,6,1,// Левая грань: треугольник 0-6-1 (невидимая сторона - обход левый)
0,4,6,// Левая грань: треугольник 0-4-6 (невидимая сторона - обход левый)
2,3,7,// Правая грань: треугольник 2-3-7 (лицевая сторона - обход правый)
2,7,5 // Правая грань: треугольник 2-7-5 (лицевая сторона - обход правый)
};
private IndexBuffer ib = null;
private Mesh mesh = null;
private Effect effect;
private Matrix worldMatrix;
private Matrix projMatrix;
private Matrix viewMatrix;
private VertexDeclaration decl;
public Form1()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public bool InitializeGraphics()
{
PresentParameters pp= new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;
pp.EnableAutoDepthStencil = true;
pp.AutoDepthStencilFormat = DepthFormat.D16;
bool canDoShaders = true;
Caps hardware = Manager.GetDeviceCaps(0, DeviceType.Hardware);
if (hardware.VertexShaderVersion >= new Version(1, 1))
{
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
if (hardware.DeviceCaps.SupportsHardwareTransformAndLight)
{
flags = CreateFlags.HardwareVertexProcessing;
}
if (hardware.DeviceCaps.SupportsPureDevice)
{
flags |= CreateFlags.PureDevice;
}
device = new Device(0, DeviceType.Hardware, this, flags, pp);
}
else
{
canDoShaders = false;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, pp);
}
vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionOnly.Format, Pool.Default);
vb.Created += new EventHandler(this.OnVbCreate);
OnVbCreate(vb, null);
ib = new IndexBuffer(typeof(short), indices.Length, device, Usage.WriteOnly, Pool.Default);
ib.Created += new EventHandler(ib_Created);
ib_Created(ib, null);
VertexElement[] elements = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0), VertexElement.VertexDeclarationEnd };
decl = new VertexDeclaration(device, elements);
String s = "";
StreamReader sr = new StreamReader("simple.fx");
String li = sr.ReadToEnd();
var en = Encoding.ASCII;
var b = en.GetBytes(li);
String q = en.GetString(b);
effect = Effect.FromString(device, q, null, null, ShaderFlags.None, null, out s);
effect.ValidateTechnique("Fill");
effect.Technique = "Fill";
return canDoShaders;
}
void ib_Created(object sender, EventArgs e)
{
IndexBuffer buffer = (IndexBuffer)sender;
buffer.SetData(indices, 0, LockFlags.None);
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4,this.Width/this.Height,1.0f,100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,10.0f),new Vector3(),new Vector3(0,1,0));
device.RenderState.Lighting = false;
// device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 0.2f), angle / ((float)Math.PI * 0.4f), angle / ((float)Math.PI * 0.6f)), angle / (float)Math.PI);//Matrix.RotationX(angle / (float)Math.PI);
device.RenderState.PointSize = 5.0f;
angle += 0.1f;
// device.RenderState.CullMode = Cull.None;
}
private void OnVbCreate(object sender, EventArgs e)
{
VertexBuffer buffer = (VertexBuffer)sender;
CustomVertex.PositionOnly[] verts = new
CustomVertex.PositionOnly[3];
// Задняя грань: треугольник 2-0-1 (невидимая сторона - обход левый)
verts[0] = new CustomVertex.PositionOnly(0.0F, 1.0F, 0.0F);
verts[1] = new CustomVertex.PositionOnly(-1.0F, -1.0F, 0.0F);
verts[2] = new CustomVertex.PositionOnly(1.0F, -1.0F, 0.0F);
buffer.SetData(verts, 0, LockFlags.None);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
device.Clear(ClearFlags.Target|ClearFlags.ZBuffer, Color.Aquamarine, 1.0f, 0);
SetupCamera();
device.Lights[0].Type = LightType.Point;
device.Lights[0].Position = new Vector3();
device.Lights[0].Diffuse = Color.Red;
device.Lights[0].Attenuation0 = 0.2f;
device.Lights[0].Range = 1000.0f;
device.Lights[0].Enabled = false;
device.BeginScene();
device.SetStreamSource(0, vb, 0);
device.Indices = ib;
// device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 1);
int passes = effect.Begin(FX.None);
for (int i = 0; i < passes; i++)
{
effect.BeginPass(i);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 1);
effect.EndPass();
}
effect.End();
int index = ((System.Environment.TickCount - InitCount) / 4000) % 6;
device.EndScene();
device.Present();
this.Invalidate();
}
}
}
//Шейдер
float4 MainVS( float4 Pos : POSITION ) : POSITION
{
return Pos;
}
float4 MainPS():COLOR
{
return float4(4.0, 1.0, 1.0, 1.0);
}
technique Fill
{
pass p0
{
VertexShader = compile vs_1_1 MainVS();
PixelShader = compile ps_1_1 MainPS();
}
}
|