[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系统中, ...
随机推荐
- CSS 实现背景图尺寸不随浏览器缩放而变化
<!-- Author:博客园小dee --> 一些网站的首页背景图尺寸不随浏览器缩放而变化,例如百度个人版的首页,缩放后背景图的尺寸并不改变: 再比如花瓣网( http://www.hu ...
- sql语句什么时候用双引号或者单引号
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如果数据是字符型,必须 ...
- directX基础学习系列7 网格(自己创建)
D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...
- 1st-code-review summary
每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...
- 表单序列化 js
function serliaze(form) { //序列化表单 var obj = {}; $.each(form.serializeArray(), function (index) { if ...
- 搭建C语言开发环境
大学的时候有数据结构这门课,但...终究还是得学.电脑是win8的,根据网上的教程倒是能安装成功vc6.0并且能够打开新建工程,但是一编译运行就提示兼容性问题. 首先安装C语言编译器.下载MinGw ...
- 【转】Unity利用WWW http传输Json数据
http://blog.csdn.net/h570768995/article/details/50386935 首先去下载LitJson.dll,放在Plugins 目录下: LitJson可以从下 ...
- svn截图
一.合并一个范围的版本 此类型应用最为广泛,主要是把分支中的修改合并到主干上来.在主干上点击右键选择合并,然后选择合并类型:合并一个范围的版本.合并的源URL填写的是要合并的分支的URL,待合 ...
- 三种查看SqlServer中数据物理pge页的方法
1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...
- node express 学习1
参考链接https://cnodejs.org/topic/55ece31004e2cdb230671c50 express-session connect-nongo mongoose 1.安装mo ...