作为一个.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. Unity 如何高效的解析数据

    昨天和朋友聊天时,他遇到这么一个问题:现在有按照一定格式的数据,例如:#code==text 此处是注释100==确定101==取消key==value 这么个格式的,说白了就是怎样解析这些固定格式字 ...

  2. windows 2008 r2 安装TabsStudio

    windows 2008 r2 安装TabsStudio 办法如下: HKLM\SOFTWARE\Policies\Microsoft\Windows\Installer.如果没有这个项,则新建这个项 ...

  3. fireworks切图

    下载安装完成后打开软件 打开一张图片 首选参数的撤销次数改成999 按住空格键 鼠标会变成小手的形状 这时候可以拖拽图像 找到切片工具 记住缩放比例的快捷键 ctrl+空格 放大某个区域 切的时候按住 ...

  4. Blender之OBJ转json

    要想从 Blender 中导出 Three. 模型, 我们首先要将 Tbree.js 导出器添加到Blender 中. 你可以从www.blender.org 上下载 Blender,然后按照相应平台 ...

  5. Nginx伪静态配置和常用Rewrite伪静态规则集锦

    伪静态是一种可以把文件后缀改成任何可能的一种方法,如果我想把php文件伪静态成html文件,这种相当简单的,下面我来介绍nginx 伪静态配置方法 nginx里使用伪静态是直接在nginx.conf ...

  6. Emulator Error: Could not load OpenGLES emulation library: Could not load DLL!

    Copy the file below from SDK\tools\lib to SDK\tools. libEGL_translator.dlllibGLES_CM_translator.dlll ...

  7. laravel 使用 vue (gulp)

    1)首先要安装 gulp 看这里 http://www.cnblogs.com/tujia/p/6397779.html 2)编辑js 默认 laravel 里有一个 /resources/asset ...

  8. 【RF库Collections测试】Get From Dictionary

    Name:Get From DictionarySource:Collections <test library>Arguments:[ dictionary | key ]Returns ...

  9. openstack的glance、nova、cinder使用ceph做后端存储

    块设备与 OPENSTACK 通过 libvirt 你可以把 Ceph 块设备用于 OpenStack ,它配置了 QEMU 到 librbd 的接口. Ceph 把块设备映像条带化为对象并分布到集群 ...

  10. LeetCode——Power of Two

    Description: Given an integer, write a function to determine if it is a power of two. public class S ...