[Slimdx]顶点和索引缓冲,绘制了2个分离的三角形

定义网格顶点和索引缓冲,绘制了2个分离的三角形。

  1 using System;
  2 using System.Drawing;
  3 using RGeos.SlimScene.Core;
  4 using SlimDX;
  5 using SlimDX.Direct3D9;
  6 using CustomVertex;
  7 using RGeos.AppScene.Renderable;
  8 
  9 namespace RGeos.SlimScene.Renderable
 10 {
 11     /// <summary>
 12     /// 定义网格顶点和索引缓冲
 13     /// </summary>
 14     public class BoxMesh : RenderableObject
 15     {
 16         private CustomVertex.PositionColored[] vertices;//定义网格顶点
 17         private short[] indices;//定义网格中三角形索引
 18         private int modelVertCount = 0;
 19         private int modelFaceCount = 0;
 20         private VertexBuffer vertBuffer;
 21         private IndexBuffer indBuffer;
 22 
 23         public BoxMesh(string name)
 24             : base(name)
 25         {
 26             this.isSelectable = true;
 27         }
 28         private void ComputeVertexs()
 29         {
 30             vertices = new CustomVertex.PositionColored[6];
 31 
 32             Vector3 pt = new Vector3();
 33             pt.X = 0;
 34             pt.Y = 0;
 35             pt.Z = 0;
 36             vertices[0].Position = pt;
 37             vertices[0].Color = Color.Red.ToArgb();
 38 
 39             Vector3 pt1 = new Vector3();
 40             pt1.X = 0;
 41             pt1.Y = 10;
 42             pt1.Z = 0;
 43             vertices[1].Position = pt1;
 44             vertices[1].Color = Color.Red.ToArgb();
 45 
 46             Vector3 pt2 = new Vector3();
 47             pt2.X = 0;
 48             pt2.Y = 10;
 49             pt2.Z = 10;
 50             vertices[2].Position = pt2;
 51             vertices[2].Color = Color.Red.ToArgb();
 52 
 53             Vector3 pt3 = new Vector3();
 54             pt3.X = 0;
 55             pt3.Y = 20;
 56             pt3.Z = 20;
 57             vertices[3].Position = pt3;
 58             vertices[3].Color = Color.Blue.ToArgb();
 59 
 60             Vector3 pt4 = new Vector3();
 61             pt4.X = 0;
 62             pt4.Y = 20;
 63             pt4.Z = 0;
 64             vertices[4].Position = pt4;
 65             vertices[4].Color = Color.Blue.ToArgb();
 66 
 67             Vector3 pt5 = new Vector3();
 68             pt5.X = 20;
 69             pt5.Y = 0;
 70             pt5.Z = 0;
 71             vertices[5].Position = pt5;
 72             vertices[5].Color = Color.Blue.ToArgb();
 73         }
 74 
 75         /// <summary>
 76         /// 计算索引
 77         /// </summary>
 78         private void ComputeIndices()
 79         {
 80             indices = new short[6];
 81 
 82             indices[0] = 0;
 83             indices[1] = 1;
 84             indices[2] = 2;
 85             indices[3] = 3;
 86             indices[4] = 4;
 87             indices[5] = 5;
 88 
 89         }
 90 
 91         #region Renderable
 92         /// <summary>
 93         /// 初始化对象
 94         /// </summary>
 95         /// <param name="drawArgs">渲染参数</param>
 96         public override void Initialize(DrawArgs drawArgs)
 97         {
 98             LoadTexturesAndMaterials(drawArgs);//导入贴图和材质 
 99             ComputeVertexs();//计算顶点
100             ComputeIndices();//计算索引
101 
102             vertBuffer = BufferCreator.CreateVertexBuffer(drawArgs.Device, vertices);
103             indBuffer = BufferCreator.CreateIndexBuffer(drawArgs.Device, indices);
104             modelVertCount = vertices.Length;
105             modelFaceCount = indices.Length / 3;
106             this.isInitialized = true;
107         }
108 
109 
110         /// <summary>
111         /// 渲染对象
112         /// </summary>
113         /// <param name="drawArgs">渲染参数</param>
114         public override void Render(DrawArgs drawArgs)
115         {
116             if (!this.IsOn || !this.isInitialized) return;
117             //获取当前世界变换
118             Matrix world = drawArgs.Device.GetTransform(TransformState.World);
119             //获取当前顶点格式
120             VertexFormat format = drawArgs.Device.VertexFormat;
121             //获取当前的Z缓冲方式
122             int zEnable = drawArgs.Device.GetRenderState(RenderState.ZEnable);
123             //获取纹理状态
124             int colorOper = drawArgs.Device.GetTextureStageState(0, TextureStage.ColorOperation);
125 
126             try
127             {
128                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.Modulate);
129                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
130                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Diffuse);
131                 drawArgs.Device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Disable);
132 
133                 //设置顶点格式
134                 drawArgs.Device.VertexFormat = CustomVertex.PositionColored.Format;
135                 //设置Z缓冲
136                 drawArgs.Device.SetRenderState(RenderState.ZEnable, 1);
137                 //设置纹理状态,此处使用纹理
138                 //drawArgs.Device.SetTexture(0, texture);//设置贴图 
139                 drawArgs.Device.SetStreamSource(0, vertBuffer, 0, PositionColored.SizeBytes);
140                 drawArgs.Device.Indices = indBuffer;
141                 drawArgs.Device.VertexFormat = PositionColored.Format;
142                 drawArgs.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, modelVertCount, 0, modelFaceCount);
143             }
144             catch (Exception e)
145             {
146                 Utility.Log.Write(e);
147             }
148             finally
149             {
150                 drawArgs.Device.SetTransform(TransformState.World, world);
151                 drawArgs.Device.VertexFormat = format;
152                 drawArgs.Device.SetRenderState(RenderState.ZEnable, zEnable);
153                 drawArgs.Device.SetTextureStageState(0, TextureStage.ColorOperation, colorOper);
154             }
155             if (disposing)
156             {
157                 Dispose();
158                 disposing = false;
159             }
160         }
161         public bool disposing = false;
162         /// <summary>
163         /// 更新对象
164         /// </summary>
165         /// <param name="drawArgs">渲染参数</param>
166         public override void Update(DrawArgs drawArgs)
167         {
168             if (!this.isInitialized)
169             {
170                 this.Initialize(drawArgs);
171             }
172         }
173 
174         /// <summary>
175         /// 执行选择操作
176         /// </summary>
177         /// <param name="X">点选X坐标</param>
178         /// <param name="Y">点选Y坐标</param>
179         /// <param name="drawArgs">渲染参数</param>
180         /// <returns>选择返回True,否则返回False</returns>
181         public bool PerformSelectionAction(int X, int Y, DrawArgs drawArgs)
182         {
183             return false;
184         }
185 
186 
187         /// <summary>
188         /// 释放对象
189         /// </summary>
190         public override void Dispose()
191         {
192             this.isInitialized = false;
193             //base.Dispose();
194         }
195         #endregion
196 
197         private void LoadTexturesAndMaterials(DrawArgs drawArgs)//导入贴图和材质 
198         {
199 
200         }
201 
202 
203         public override bool PerformSelectionAction(DrawArgs drawArgs)
204         {
205             bool flag = PerformSelectionAction(DrawArgs.LastMousePosition.X, DrawArgs.LastMousePosition.Y, drawArgs);
206             return flag;
207         }
208 
209     }
210 }
View Code

顶点缓冲创建方法:

1  public static VertexBuffer CreateVertexBuffer(Device device, PositionColored[] vertices)
2         {
3             VertexBuffer vertexBuffer = new VertexBuffer(device, vertices.Length * CustomVertex.PositionColored.SizeBytes, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
4             DataStream vs = vertexBuffer.Lock(0, vertices.Length * CustomVertex.PositionColored.SizeBytes, LockFlags.None);
5             vs.WriteRange(vertices);
6             vertexBuffer.Unlock();
7             vs.Dispose();
8             return vertexBuffer;
9         }
CreateVertexBuffer

索引的:

1  public static IndexBuffer CreateIndexBuffer(Device device, short[] indicesData)
2         {
3             IndexBuffer indexBuffer = new IndexBuffer(device, 16 * indicesData.Length, Usage.WriteOnly, Pool.Default, true);
4             DataStream ds = indexBuffer.Lock(0, 16 * indicesData.Length, LockFlags.None);
5             ds.WriteRange(indicesData);
6             indexBuffer.Unlock();
7             ds.Dispose();
8             return indexBuffer;
9         }
CreateIndexBuffer

效果图:

原文地址:https://www.cnblogs.com/yhlx125/p/4399039.html