作为一个.Net程序员学习3D开发好尴尬啊,因为不论是OpenGL还是Direct3D都是用C/C++开发的比较多。虽然有计划使用C++进行开发,但是平时还是C#使用的多。很少用C++做东西,如果仅仅是学习是很难有进步的,学习C++很长时间了至今仍感觉很肤浅,而且语言并不是编程的全部,真正的高手是不区分语言的,所以决定注重基本原理的学习,具体实现用C#也是可以的,这里选择Slimdx。

环境:

VS2010+C#

SlimDX SDK (January 2012).msi

d3d9.dll, D3DX9_43.dll

 using System;
using System.Drawing;
using System.Windows.Forms;
using SlimDX.Direct3D9;
using SlimDX;
using System.Runtime.InteropServices; namespace D3Ddemo01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Device device = null;
private void Form1_Load(object sender, EventArgs e)
{
this.ClientSize = new System.Drawing.Size(, );
this.Text = " 第1个D3D程序"; }
public bool InitializeDirect3D()
{
try
{
Direct3D direct3d = new Direct3D();
//direct3d.GetAdapterDisplayMode(1);
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true; //指定以Windows窗体形式显示
presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
device = new Device(direct3d, , DeviceType.Hardware, this.Handle,
CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
return true;
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error"); //处理异常
return false;
}
}
public void Render()
{
if (device == null) //如果device为空则不渲染
{
return;
}
Vector3 eye = new Vector3(,, -);
Vector3 at = new Vector3(, , );
Vector3 up = new Vector3(, , );
Matrix viewMatrix = Matrix.LookAtLH(eye, at, up); Matrix projection = Matrix.PerspectiveFovLH((float)Math.PI / ,
this.Width / this.Height, 1.0f, 50.0f);
device.SetTransform(TransformState.Projection, projection);
device.SetTransform(TransformState.View, viewMatrix);
device.SetRenderState(RenderState.FillMode, FillMode.Solid);
device.SetRenderState(RenderState.Lighting, false);
//深度偏移
//device.SetRenderState(RenderState.DepthBias, drawArgs.CurrentWorld.CurrentDepthBias - 0.00001f);//防止闪烁
device.SetRenderState(RenderState.ZEnable, true);
device.SetRenderState(RenderState.CullMode, Cull.None); //在此添加渲染图形代码
VertexBuffer Vertices = new VertexBuffer(device, * , Usage.WriteOnly, VertexFormat.None, Pool.Managed);
DataStream stream = Vertices.Lock(, , LockFlags.None);
Vertex[] vertexData = new Vertex[];
vertexData[].PositionRhw = new Vector4(0.0f, 1.0f, 10.0f, 1.0f);
vertexData[].Color = Color.Red.ToArgb();
vertexData[].PositionRhw = new Vector4(1.0f, 0.0f, 10.0f, 1.0f);
vertexData[].Color = Color.Blue.ToArgb();
vertexData[].PositionRhw = new Vector4(2.0f, 2.0f, 10.0f, 1.0f);
vertexData[].Color = Color.Green.ToArgb();
stream.WriteRange(vertexData);
Vertices.Unlock();
//PositionColored[] vertices = new PositionColored[3];//定义顶点
//vertices[0].Position = new Vector3(0f, 0f, 0f);
//vertices[0].Color = Color.Red.ToArgb();
//vertices[1].Position = new Vector3(5f, 10f, 0f);
//vertices[1].Color = Color.Green.ToArgb();
//vertices[2].Position = new Vector3(10f, 0f, 0f);
//vertices[2].Color = Color.Yellow.ToArgb();
//device.VertexFormat = VertexFormat.Position;
//device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, Vertices);
//device.RenderState.CullMode = Cull.None;
// device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, );
device.BeginScene();
device.SetStreamSource(, Vertices, , );
device.VertexFormat = VertexFormat.Position | VertexFormat.Diffuse;
device.DrawPrimitives(PrimitiveType.TriangleList, , );
device.EndScene();
device.Present();
}
}
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector4 PositionRhw;
public int Color;
}
}

Form1

调用代码:

  static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Form1 basicForm = new Form1(); //创建窗体对象
if (basicForm.InitializeDirect3D() == false) //检查Direct3D是否启动
{
MessageBox.Show("无法启动Direct3D!", "错误!");
return;
}
basicForm.Show(); //如果一切都初始化成功,则显示窗体
while (basicForm.Created) //设置一个循环用于实时更新渲染状态
{
basicForm.Render(); //保持device渲染,直到程序结束
Application.DoEvents(); //处理键盘鼠标等输入事件
}
}
}

结果:

[3D]1.绘制三角形的更多相关文章

  1. 从0开发3D引擎(九):实现最小的3D程序-“绘制三角形”

    目录 上一篇博文 运行测试截图 需求分析 目标 特性 头脑风暴 确定需求 总体设计 具体实现 新建Engine3D项目 实现上下文 实现_init 实现"获得WebGL上下文" 实 ...

  2. Android OpenGL 入门示例----绘制三角形和正方形

    Android上对OpenGl的支持是无缝的,所以才有众多3D效果如此逼真的游戏,在Camera的一些流程中也有用到GLSurfaceView的情况.本文记录OpenGL在Android上的入门级示例 ...

  3. 【Android 应用开发】OpenGL ES 2.0 -- 制作 3D 彩色旋转三角形 - 顶点着色器 片元着色器 使用详解

    最近开始关注OpenGL ES 2.0 这是真正意义上的理解的第一个3D程序 , 从零开始学习 . 案例下载地址 : http://download.csdn.net/detail/han120201 ...

  4. opengl绘制三角形

    顶点数组对象:Vertex Array Object,VAO 顶点缓冲对象:Vertex Buffer Object,VBO 索引缓冲对象:Element Buffer Object,EBO或Inde ...

  5. OpenGL ES 2.0 -- 制作 3D 彩色旋转三角形 - 顶点着色器 片元着色器 使用详解

    最近开始关注OpenGL ES 2.0 这是真正意义上的理解的第一个3D程序 , 从零开始学习 . 案例下载地址 : http://download.csdn.net/detail/han120201 ...

  6. Unity3D之Mesh(一)绘制三角形

    前言: Unity自带几种简单的模型,如cube等:一般情况下,其余模型有3D建模软件生成,以合适的文件格式导入unity中:而mesh(以我目前很粗浅的了解)的一般用途就是:对现有的模型进行变形,以 ...

  7. iOS OpenGL ES简单绘制三角形

    OpenGL 是用于2D/3D图形编程的一套基于C语言的统一接口. windows,Linux,Unix上均可兼容. OpenGL ES 是在OpenGL嵌入式设备上的版本, android/iOS ...

  8. SharpDX初学者教程第4部分:绘制三角形

    原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-4-drawing-a-triangle 现在我们有了一个Direct3 ...

  9. 1.opengl绘制三角形

    顶点数组对象:Vertex Array Object,VAO,用于存储顶点状态配置信息,每当界面刷新时,则通过VAO进行绘制. 顶点缓冲对象:Vertex Buffer Object,VBO,通过VB ...

随机推荐

  1. Codeforces Round #313 D. Equivalent Strings(DFS)

    D. Equivalent Strings time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. UVa 10450 - World Cup Noise

    题目:构造一个01串,使得当中的1不相邻,问长度为n的串有多少中. 分析:数学,递推数列. 设长度为n的串有n个.则有递推关系:f(n)= f(n-1)+ f(n-2): 长度为n的结束可能是0或者1 ...

  3. testNG框架提示:Cannot find class in classpath: NewTest

    selenium+Java的testNG运行时,报如下错误: org.testng.TestNGException: Cannot find class in classpath: NewTest a ...

  4. linux 安装 nodejs

    原文地址:https://nodejs.org/en/download/package-manager/#enterprise-linux-and-fedora 1)定位到nodejs的官方源(如果直 ...

  5. 【RF库Collections测试】Get From List

    Name:Get From ListSource:Collections <test library>Arguments:[ list_ | index ]Returns the valu ...

  6. python cx_oracle单个表中批量插入数据

  7. 3dsmax sendto mudbox失效解决方案

    查看C:\ProgramData\Autodesk\Synergy下的max.2014.1.64.syncfg和Mudbox.2014.0.Windows.64.syncfg两个文件, 找到 Exec ...

  8. 将display设置为inline-block之后产生间隙然后换行问题的解决方法

    在我们会用display的时候,inline-block肯定不陌生吧,我今天在做项目的时候,用了inline-block,使a标签可以自定义宽度,但是随之而来的问题就是换行的BUG,如下图 特地加了一 ...

  9. PyQt4布局管理——绝对定位方式

    PyQt4中的布局管理器 布局管理器是编程中重要的一部分.所谓布局管理器是指我们在窗口中安排部件位置的方法.布局管理器有两种工作方式:绝对定位方式(absolute positioning)和布局类别 ...

  10. final,finally,finalize的区别

    1.final用于声明属性.方法和类,分别表示属性不可变.方法不可覆盖,类不可继承: 2.finally是异常处理语句结构的一部分,表示总是执行代码块: 3.finalize是Object类的一个方法 ...