定义网格顶点和索引缓冲,绘制了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个分离的三角形的更多相关文章

  1. 小练手:用HTML5 Canvas绘制谢尔宾斯基三角形

    文章首发于我的知乎专栏,原地址:https://zhuanlan.zhihu.com/p/26606208 以前看到过一个问题:谢尔宾斯基三角形能用编程写出来么?该怎么写? - 知乎,在回答里,各方大 ...

  2. Elasticsearch7.X ILM索引生命周期管理(冷热分离)

    Elasticsearch7.X ILM索引生命周期管理(冷热分离) 一.“索引生命周期管理”概述 Elasticsearch索引生命周期管理指:Elasticsearch从设置.创建.打开.关闭.删 ...

  3. opengl入门篇二: 索引缓冲对象EBO

    在绘制图形的过程中,顶点可能会重复.比如两个三角形组成了四边形,那么,必然有两个点是重复的.因此采用索引的方式,四个点即可描述四边形. // 四个顶点 GLfloat vertices[] = { / ...

  4. Python使用递归绘制谢尔宾斯基三角形

    谢尔宾斯基三角形使用了三路递归算法,从一个大三角形开始,通过连接每一个边的中点,将大三角型分为四个三角形,然后忽略中间的三角形,依次对其余三个三角形执行上述操作. 运行效果: 源代码: 1 impor ...

  5. 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记5——Direct3D中的顶点缓存和索引缓存

    第12章 Direct3D绘制基础 1. 顶点缓存 计算机所描绘的3D图形是通过多边形网格来构成的,网网格勾勒出轮廓,然后在网格轮廓的表面上贴上相应的图片,这样就构成了一个3D模型.三角形网格是构建物 ...

  6. Direct3D11学习:(九)绘制基本几何体

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct3D中很多复杂的几何效果都是由基本的几何体组合而成的,这篇文章中,我们来学习集中常见的基本几何体的绘 ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...

  8. DirectX11 学习笔记10 - 用文件存储顶点布局

    这节须要把顶点布局写在文件中面,为了方便.由于一大串很抽象的坐标放在CPP和程序混在一起很的不方便. 以下全为c++知识,读取文件中面的特定格式的数据: Vertex Count: 36 Data: ...

  9. 【Stage3D学习笔记续】山寨Starling(九):上下文丢失处理方法

    Stage3D在运行中是存在随时会丢失上下文的尴尬情况. 渲染内容丢失的问题本身就说明是因为丢失了Context3D对象.出现此问题的原因很多,通常还不是因为Stage3D应用.比如在win7系统中, ...

随机推荐

  1. PAT天梯赛练习题 L3-011. 直捣黄龙(多关键字SPFA+DFS)

    L3-011. 直捣黄龙 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题是一部战争大片 —— 你需要从己方大本营出发,一路 ...

  2. Go项目的目录结构说明

    一.项目目录结构 GoPath    /bin    /pkg    /src project_1      project_2 ...... project_n GoPath : 相当于donet下 ...

  3. Memcached 笔记与总结(4)memcache 扩展的使用

    在 wamp 环境下进行测试:WAMPSERVER 2.2(Windows 7 + Apache 2.2.21 + PHP 5.3.10 + memcache 3.0.8 + Memcached 1. ...

  4. Web 在线文件管理器学习笔记与总结(19)上传文件

    dir.func.php 中添加方法: /* 上传文件 */ function uploadFile($fileInfo,$path,$allowExt = array('jpg','jpeg','p ...

  5. PHP+jQuery 注册模块的改进之一:验证码存入SESSION

    /* ******* Date:2014-09-28 ******* Author:小dee ******* Blog:http://www.cnblogs.com/dee0912/*/ 对上一篇博文 ...

  6. PHP常用正则表达式汇总 [复制链接]

    PHP常用正则表达式汇总 [复制链接] 上一主题下一主题   离线我是小猪头   法师     发帖 539 加关注 发消息 只看楼主 倒序阅读 使用道具楼主  发表于: 2011-06-22 更多 ...

  7. 【转载】C编译过程概述

    gcc:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html#_Toc311642844 gdb:http://www.cn ...

  8. laravel 取sql语句

    \DB::connection()->enableQueryLog(); some sql action... $query = \DB::getQueryLog(); $lastQuery = ...

  9. Windows与Linux共享文件夹互相访问

    [原文]  首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...

  10. Android 的上下文菜单: Context Menu,registerForContextMenu(getListView())

    概述: Android 的上下文菜单类似于 PC 上的右键菜单.当为一个视图注册了上下文菜单之后,长按(2 秒左右)这个视图对象就会弹出一个浮动菜单,即上下文菜单.任何视图都可以注册上下文菜单,不过, ...