[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系统中, ...
随机推荐
- git统计报告
2016年10月30日--2016年11月9日
- PTF 安装及简单测试 Packet Testing Framework
PTF PTF is a Python based dataplane test framework. It is based on unittest, which is included in th ...
- Sublime Text初识
一 基础安装1. 安装Package Control使用快捷键ctrl+`,调出输出窗口, 然后输入官网提供的代码, 获取代码地址:https://packagecontrol.io/installa ...
- JiaThis WordPress分享插件安装全攻略
WordPress 是一个功能非常强大的博客系统,插件众多,易于扩充功能.安装和使用都非常方便.目前 WordPress 已经成为主流的 Blog 搭建平台.按照下面的方法,只需简单几步,就可轻松地将 ...
- IMP-00009: 导出文件异常结束
今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到" IMP-00009: 导出文件异常结束" 错误,google一下,发现可能有如下原因导致 imp的数据太大,没有 ...
- reduce()
Professional.JavaScript.for.Web.Developers.3rd.Edition.Jan.2012 var value = [1,2,3,4,5]; var sum = v ...
- Apache Kafka源码分析 - PartitionStateMachine
startup 在onControllerFailover中被调用, initializePartitionState private def initializePartitionState() { ...
- 批处理快速创建wifi
为什么要用cmd这种古老的东西创建wifi呢,电脑管家.360安全卫士都有这种插件,一键开启关闭,多方便啊! 开始用的也是电脑管家的免费wifi插件,但是我越来越不能忍它极慢的启动关闭过程,每一次看着 ...
- 【转】C# 解析JSON格式数据
http://blog.csdn.net/coolszy/article/details/8606803 JSON简介 JSON(全称为JavaScript ObjectNotation) 是一种轻量 ...
- base64coder调用
base64coder 可以查看官网: http://www.source-code.biz/base64coder/java/ 我所涉及到的 base64coder调用: 某天,因需要修改Pr ...