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

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

 using System;
using System.Drawing;
using RGeos.SlimScene.Core;
using SlimDX;
using SlimDX.Direct3D9;
using CustomVertex;
using RGeos.AppScene.Renderable; namespace RGeos.SlimScene.Renderable
{
/// <summary>
/// 定义网格顶点和索引缓冲
/// </summary>
public class BoxMesh : RenderableObject
{
private CustomVertex.PositionColored[] vertices;//定义网格顶点
private short[] indices;//定义网格中三角形索引
private int modelVertCount = ;
private int modelFaceCount = ;
private VertexBuffer vertBuffer;
private IndexBuffer indBuffer; public BoxMesh(string name)
: base(name)
{
this.isSelectable = true;
}
private void ComputeVertexs()
{
vertices = new CustomVertex.PositionColored[]; Vector3 pt = new Vector3();
pt.X = ;
pt.Y = ;
pt.Z = ;
vertices[].Position = pt;
vertices[].Color = Color.Red.ToArgb(); Vector3 pt1 = new Vector3();
pt1.X = ;
pt1.Y = ;
pt1.Z = ;
vertices[].Position = pt1;
vertices[].Color = Color.Red.ToArgb(); Vector3 pt2 = new Vector3();
pt2.X = ;
pt2.Y = ;
pt2.Z = ;
vertices[].Position = pt2;
vertices[].Color = Color.Red.ToArgb(); Vector3 pt3 = new Vector3();
pt3.X = ;
pt3.Y = ;
pt3.Z = ;
vertices[].Position = pt3;
vertices[].Color = Color.Blue.ToArgb(); Vector3 pt4 = new Vector3();
pt4.X = ;
pt4.Y = ;
pt4.Z = ;
vertices[].Position = pt4;
vertices[].Color = Color.Blue.ToArgb(); Vector3 pt5 = new Vector3();
pt5.X = ;
pt5.Y = ;
pt5.Z = ;
vertices[].Position = pt5;
vertices[].Color = Color.Blue.ToArgb();
} /// <summary>
/// 计算索引
/// </summary>
private void ComputeIndices()
{
indices = new short[]; indices[] = ;
indices[] = ;
indices[] = ;
indices[] = ;
indices[] = ;
indices[] = ; } #region Renderable
/// <summary>
/// 初始化对象
/// </summary>
/// <param name="drawArgs">渲染参数</param>
public override void Initialize(DrawArgs drawArgs)
{
LoadTexturesAndMaterials(drawArgs);//导入贴图和材质
ComputeVertexs();//计算顶点
ComputeIndices();//计算索引 vertBuffer = BufferCreator.CreateVertexBuffer(drawArgs.Device, vertices);
indBuffer = BufferCreator.CreateIndexBuffer(drawArgs.Device, indices);
modelVertCount = vertices.Length;
modelFaceCount = indices.Length / ;
this.isInitialized = true;
} /// <summary>
/// 渲染对象
/// </summary>
/// <param name="drawArgs">渲染参数</param>
public override void Render(DrawArgs drawArgs)
{
if (!this.IsOn || !this.isInitialized) return;
//获取当前世界变换
Matrix world = drawArgs.Device.GetTransform(TransformState.World);
//获取当前顶点格式
VertexFormat format = drawArgs.Device.VertexFormat;
//获取当前的Z缓冲方式
int zEnable = drawArgs.Device.GetRenderState(RenderState.ZEnable);
//获取纹理状态
int colorOper = drawArgs.Device.GetTextureStageState(, TextureStage.ColorOperation); try
{
drawArgs.Device.SetTextureStageState(, TextureStage.ColorOperation, TextureOperation.Modulate);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorArg1, TextureArgument.Texture);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorArg2, TextureArgument.Diffuse);
drawArgs.Device.SetTextureStageState(, TextureStage.AlphaOperation, TextureOperation.Disable); //设置顶点格式
drawArgs.Device.VertexFormat = CustomVertex.PositionColored.Format;
//设置Z缓冲
drawArgs.Device.SetRenderState(RenderState.ZEnable, );
//设置纹理状态,此处使用纹理
//drawArgs.Device.SetTexture(0, texture);//设置贴图
drawArgs.Device.SetStreamSource(, vertBuffer, , PositionColored.SizeBytes);
drawArgs.Device.Indices = indBuffer;
drawArgs.Device.VertexFormat = PositionColored.Format;
drawArgs.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, , , modelVertCount, , modelFaceCount);
}
catch (Exception e)
{
Utility.Log.Write(e);
}
finally
{
drawArgs.Device.SetTransform(TransformState.World, world);
drawArgs.Device.VertexFormat = format;
drawArgs.Device.SetRenderState(RenderState.ZEnable, zEnable);
drawArgs.Device.SetTextureStageState(, TextureStage.ColorOperation, colorOper);
}
if (disposing)
{
Dispose();
disposing = false;
}
}
public bool disposing = false;
/// <summary>
/// 更新对象
/// </summary>
/// <param name="drawArgs">渲染参数</param>
public override void Update(DrawArgs drawArgs)
{
if (!this.isInitialized)
{
this.Initialize(drawArgs);
}
} /// <summary>
/// 执行选择操作
/// </summary>
/// <param name="X">点选X坐标</param>
/// <param name="Y">点选Y坐标</param>
/// <param name="drawArgs">渲染参数</param>
/// <returns>选择返回True,否则返回False</returns>
public bool PerformSelectionAction(int X, int Y, DrawArgs drawArgs)
{
return false;
} /// <summary>
/// 释放对象
/// </summary>
public override void Dispose()
{
this.isInitialized = false;
//base.Dispose();
}
#endregion private void LoadTexturesAndMaterials(DrawArgs drawArgs)//导入贴图和材质
{ } public override bool PerformSelectionAction(DrawArgs drawArgs)
{
bool flag = PerformSelectionAction(DrawArgs.LastMousePosition.X, DrawArgs.LastMousePosition.Y, drawArgs);
return flag;
} }
}

顶点缓冲创建方法:

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

CreateVertexBuffer

索引的:

  public static IndexBuffer CreateIndexBuffer(Device device, short[] indicesData)
{
IndexBuffer indexBuffer = new IndexBuffer(device, * indicesData.Length, Usage.WriteOnly, Pool.Default, true);
DataStream ds = indexBuffer.Lock(, * indicesData.Length, LockFlags.None);
ds.WriteRange(indicesData);
indexBuffer.Unlock();
ds.Dispose();
return indexBuffer;
}

CreateIndexBuffer

效果图:

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

上一篇:JavaFX与Jfoenix教程5


下一篇:sql索引扫盲