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

[Slimdx]顶点和索引缓冲,绘制了2个分离的三角形定义网格顶点和索引缓冲 绘制了 2 个分离的三角形 1usingSystem 2usingSystem Drawing 3usingRGeos SlimScene Core 4usingSlimDX 5usingSlimDX Direct3D9 6usingCustom 7usingRGeos Ap

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

[Slimdx]顶点和索引缓冲,绘制了2个分离的三角形[Slimdx]顶点和索引缓冲,绘制了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 ///   12 /// 定义网格顶点和索引缓冲  13 ///   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 ///   76 /// 计算索引  77 ///   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 ///   93 /// 初始化对象  94 ///   95 /// 渲染参数  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 ///  111 /// 渲染对象 112 ///  113 /// 渲染参数 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 ///  163 /// 更新对象 164 ///  165 /// 渲染参数 166 public override void Update(DrawArgs drawArgs) 167  { 168 if (!this.isInitialized) 169  { 170 this.Initialize(drawArgs); 171  } 172  } 173 174 ///  175 /// 执行选择操作 176 ///  177 /// 点选X坐标 178 /// 点选Y坐标 179 /// 渲染参数 180 ///  
      选择返回True,否则返回False 181 public bool PerformSelectionAction(int X, int Y, DrawArgs drawArgs) 182  { 183 return false; 184  } 185 186 187 ///  188 /// 释放对象 189 ///  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

顶点缓冲创建方法:

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

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

索引的:

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

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

效果图:

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214476.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午4:10
下一篇 2026年3月18日 下午4:10


相关推荐

  • 基于java的项目开发过程_软件开发项目管理整个流程图

    基于java的项目开发过程_软件开发项目管理整个流程图完整项目开发过程原型的设计有产品经理负责。界面的美化有专门的美工负责。前端有专门的前端开发人员负责。研发:研发主要工作就是根据项目的需求文档设计系统架构、设计数据库、编写调试程序代码。对于普通的码农来说,主要的就是编写和调试程序。基于Java的项目开发:1、要想编写程序,需要一个能编写源代码的编辑工具。例如:Notepad++;2、要想测试程序,需要一个编译、执行

    2025年7月23日
    4
  • 移动硬盘写入数据报错“MS-DOS功能无效”,或移动硬盘内新建文件夹报错0x8000FFFF灾难性错误

    移动硬盘写入数据报错“MS-DOS功能无效”,或移动硬盘内新建文件夹报错0x8000FFFF灾难性错误问题已解决,文章有点长,请耐心观看,包含遇到的问题和解决问题地过程。楼主近来新买了机械个硬盘,闲鱼出品,必属精品,主要是用来存储一些很有纪念意义的文件和平时用不到的文件,估计以后用到这些文件的机会应该很少了,一般来说机械硬盘虽然比固态硬盘不禁摔,但是存储数据的时间更长,很适合我的需求。晚上一股脑把数据存进去的时候,2T的硬盘大概存了150G之后开始报错。先是放入文件夹时显示【MS-DOS功能无效】,无法写入,我把文件夹内单个文件写入硬盘,可行,但不可能把各种类型的文件直接放进硬盘。我尝试新建文

    2026年3月9日
    5
  • mysql中的trace工具

    mysql中的trace工具

    2021年8月4日
    119
  • 详解FPN网络

    详解FPN网络目录导读摘要简介相关工作 FPN 算法 FPN 应用于 RPNFPN 应用于 FastRCNN 对比实验 FPN 对 RPN 网络的影响 FPN 对 FastRCNN 网络的影响总结参考文献导读 FeaturePyram 这篇文章主要是用来解决 FasterRCNN 物体检测算法在处理多尺度变化问题时的不足 FasterRCNN 中无论是 RPN 网络还是 FastRCNN 网络 都是基于单个高层特征 conv4 这

    2026年3月20日
    2
  • 试述Hadoop的HDFS及其组成_hadoop命令和hdfs命令区别

    试述Hadoop的HDFS及其组成_hadoop命令和hdfs命令区别hdfs命令,hadoop基本常用命令

    2026年4月17日
    5
  • UE4选不中半透明物体_半透明物体有哪些

    UE4选不中半透明物体_半透明物体有哪些UE4开发时,会出现透明材质的物体显示不全的情况本文给出几种解决方式供各位参考

    2022年10月5日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号