[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个分离的三角形的更多相关文章
- 小练手:用HTML5 Canvas绘制谢尔宾斯基三角形
文章首发于我的知乎专栏,原地址:https://zhuanlan.zhihu.com/p/26606208 以前看到过一个问题:谢尔宾斯基三角形能用编程写出来么?该怎么写? - 知乎,在回答里,各方大 ...
- Elasticsearch7.X ILM索引生命周期管理(冷热分离)
Elasticsearch7.X ILM索引生命周期管理(冷热分离) 一.“索引生命周期管理”概述 Elasticsearch索引生命周期管理指:Elasticsearch从设置.创建.打开.关闭.删 ...
- opengl入门篇二: 索引缓冲对象EBO
在绘制图形的过程中,顶点可能会重复.比如两个三角形组成了四边形,那么,必然有两个点是重复的.因此采用索引的方式,四个点即可描述四边形. // 四个顶点 GLfloat vertices[] = { / ...
- Python使用递归绘制谢尔宾斯基三角形
谢尔宾斯基三角形使用了三路递归算法,从一个大三角形开始,通过连接每一个边的中点,将大三角型分为四个三角形,然后忽略中间的三角形,依次对其余三个三角形执行上述操作. 运行效果: 源代码: 1 impor ...
- 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记5——Direct3D中的顶点缓存和索引缓存
第12章 Direct3D绘制基础 1. 顶点缓存 计算机所描绘的3D图形是通过多边形网格来构成的,网网格勾勒出轮廓,然后在网格轮廓的表面上贴上相应的图片,这样就构成了一个3D模型.三角形网格是构建物 ...
- Direct3D11学习:(九)绘制基本几何体
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct3D中很多复杂的几何效果都是由基本的几何体组合而成的,这篇文章中,我们来学习集中常见的基本几何体的绘 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...
- DirectX11 学习笔记10 - 用文件存储顶点布局
这节须要把顶点布局写在文件中面,为了方便.由于一大串很抽象的坐标放在CPP和程序混在一起很的不方便. 以下全为c++知识,读取文件中面的特定格式的数据: Vertex Count: 36 Data: ...
- 【Stage3D学习笔记续】山寨Starling(九):上下文丢失处理方法
Stage3D在运行中是存在随时会丢失上下文的尴尬情况. 渲染内容丢失的问题本身就说明是因为丢失了Context3D对象.出现此问题的原因很多,通常还不是因为Stage3D应用.比如在win7系统中, ...
随机推荐
- 修改编码格式MySQL
修改字符集的方法,就是使用mysql的命令 mysql> SET character_set_client = utf8 ; mysql> SET character_set_connec ...
- PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
PHP 魔术方法的使用 ① __get/__set:将对象的属性进行接管 当访问一个不存在的对象属性时: index.php <?php define('BASEDIR',__DIR__); / ...
- PHP+jQuery 长文章分页类 ( 支持 url / ajax 分页方式 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8 **** ...
- 五一结束,北戴河,yy,差一点,不太敢
collectionView Cell 设置颜色,蓝色,但是其他cell颜色也蓝色了,因为只写了if 没写else if (indexPath.item == 0) { cell.background ...
- 3D 生物打印血管成功植入恒河猴体内
3D 生物打印血管成功植入恒河猴体内
- 7添加一个“X”到HTML:转到XHTML
XHTML中的X代表extensible,是以XML为基础的另一种说法.XML表示可扩展的标记语言. XML是一种可以用来开发新的标记语言的语言,而HTML只是一门标记语言. HTML转化为XHTML ...
- 批处理快速创建wifi
为什么要用cmd这种古老的东西创建wifi呢,电脑管家.360安全卫士都有这种插件,一键开启关闭,多方便啊! 开始用的也是电脑管家的免费wifi插件,但是我越来越不能忍它极慢的启动关闭过程,每一次看着 ...
- 【转】unity开发android游戏(一)搭建Unity安卓开发环境
http://blog.csdn.net/chenggong2dm/article/details/20654075 1,下载安装Java的JDK: http://www.oracle.com/tec ...
- yii多数据库
Yii中同时连接多个数据库 Published by 荒野无灯 on 2011-07-09 02:12:45 under PHP/Yii Tags:yii,database 14162 views 0 ...
- Google 开源项目风格指南
Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...