八达网

标题: 看了2小时D3D编程终于全部搞懂了下一步我将重写星际2 [打印本页]

作者: woodangel    时间: 2012-11-30 22:35
标题: 看了2小时D3D编程终于全部搞懂了下一步我将重写星际2
没想到3D编程那么简单
作者: MakubeX    时间: 2012-11-30 22:38
要把屌激素引入兵种相克吗
作者: 教主威武    时间: 2012-11-30 22:38
  8折,买9颗好评送一颗
作者: 黄宏章    时间: 2012-11-30 22:39
提示: 作者被禁止或删除 内容自动屏蔽
作者: PEPSIcola    时间: 2012-11-30 22:47

作者: EZ企鹅    时间: 2012-11-30 22:54
带淡臭气味的程序确实是一种创新阿!
作者: 空幻    时间: 2012-12-1 09:08
这标题太威武了
吹草稿不打牛皮了都
作者: woodangel    时间: 2012-12-1 11:47
D3D比 DX DRAW 还简单,DX DRAW还要设置一大堆参数,还要建几个表面,动画的时候还要后台表面翻转到前台,使得不闪烁,D3D设置就少了很多连翻转都不用,比较符合人脑思维
作者: woodangel    时间: 2012-12-1 11:49
D3D实际上就是在3维空间生成三角形,任何建模都是三角形拼接成的,然后设置视角,设置环境光源就OK了。
作者: SimoN    时间: 2012-12-1 11:58

作者: [T.H]Ukyo    时间: 2012-12-1 11:58
请问看的哪本书。
作者: KoMoS    时间: 2012-12-1 12:01
[T.H]Ukyo 发表于 2012-12-1 11:58
请问看的哪本书。

<<程序猿的自我修养>>
作者: [T.H]Ukyo    时间: 2012-12-1 12:04
KoMoS 发表于 2012-12-1 12:01

熊大的吹牛逼流程吧
作者: woodangel    时间: 2012-12-1 12:22
D3D真的很弱智,看起来很高深,弄懂了就觉得不过如此,就是设置顶点,设置世界环境,设置视角,就设3个东西是最基本的,剩下的纹理,光源,重力系统都是一个函数的问题。还有一个调入建模也是一个函数搞定,真不知道你们在怂些什么?被大堆术语吓萌了?还是据说要什么矩阵,线性代数基础,吓得不敢去学?
作者: woodangel    时间: 2012-12-1 12:27
捕获.PNG
我的第一个D3D程序绘制一个运动的3D 三角形
作者: woodangel    时间: 2012-12-1 12:28
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;          //开发directx需要包含的两个命名空间
using Microsoft.DirectX.Direct3D;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
      
        private Device device = null;

      
        VertexBuffer vertexBuffer = null;

      
        PresentParameters presentParameters = new PresentParameters();

        
        bool pause = false;
      
     
        Random rn = new Random();
        public Form1()
        {
            InitializeComponent();
            InitializeGraphics();

        }
        public bool InitializeGraphics()
        {
            try
            {
                //设置屏幕显示模式为窗口模式
                presentParameters.Windowed = true;
               // presentParameters.FullScreenRefreshRateInHz = 60;
                //设置如何从后台缓冲区复制到前台缓冲区(SwapEffect.Discard表示缓冲区在显示后立即被舍弃,这样可以节省开销)
                presentParameters.SwapEffect = SwapEffect.Discard;
                //创建一个设备
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);
                //为设备释放订阅倳件处理
               // device.DeviceReset += new System.EventHandler(this.OnResetDevice);

                this.OnCreateDevice(device, null);
                this.OnResetDevice(device, null);
                pause = false;
                return true;
            }

            catch (DirectXException)
            {
                return false;
            }
        }
        public void OnCreateDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;

            //创建顶点缓冲,有个顶点
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 18, dev, 0, CustomVertex.PositionColored.Format, Pool.Default);
            //为创建顶点缓存订阅倳件处理
      //      vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);

            this.OnCreateVertexBuffer(vertexBuffer, null);
        }
        public void OnResetDevice(object sender, EventArgs e)
        {
            Device dev = (Device)sender;
            //关闭剔除模式,使我们能看见此四棱锥的前面和后面
            dev.RenderState.CullMode = Cull.None;
            // 关闭场景里的灯光,显示顶点自己的颜色
            dev.RenderState.Lighting = false;
        }
        public void OnCreateVertexBuffer(object sender, EventArgs e)
        {

            VertexBuffer vb = (VertexBuffer)sender;
            CustomVertex.PositionColored[] verts = (CustomVertex.PositionColored[])vb.Lock(0, 0);

            //四棱锥原始的个点
            Vector3 vertex1 = new Vector3(25, 0, 100);
            Vector3 vertex2 = new Vector3(0, 0, -25);
         //   Vector3 vertex3 = new Vector3(-25, 0, 0);
        //    Vector3 vertex4 = new Vector3(0, 0, 25);
            Vector3 vertex5 = new Vector3(0, 25, 0);

            //四棱锥中包含个三角形,所以要构造个点来绘制
            verts[0].Position = vertex1;
            verts[1].Position = vertex2;
            verts[2].Position = vertex5;
           /* verts[3].Position = vertex2;
            verts[4].Position = vertex3;
            verts[5].Position = vertex5;
            verts[6].Position = vertex3;
            verts[7].Position = vertex4;
            verts[8].Position = vertex5;
            verts[9].Position = vertex4;
            verts[10].Position = vertex1;
            verts[11].Position = vertex5;
            verts[12].Position = vertex2;
            verts[13].Position = vertex1;
            verts[14].Position = vertex3;
            verts[15].Position = vertex3;
            verts[16].Position = vertex1;
            verts[17].Position = vertex4;*/


            //给每个点赋予随机颜色
            for (int i = 0; i < 3; i++)
            {
                verts[i].Color = Color.FromArgb(SetColor(), SetColor(), SetColor()).ToArgb();

            }
            vb.Unlock();



        }
        public int SetColor()
        {
            int number = rn.Next(256);
            return number;
        }
        private void SetupCamera()
        {
            //设置世界矩阵,根据系统运行时间而变化
           // device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);
            //设置摄像机的位置,它在z轴上-50处,看着原点,y轴为正方向
            device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, -50f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));

            //设置摄像机的视界,角度为度,看的最近为,看的最远处为.不再这个视界中的影像都不会被显示
            device.Transform.Projection = Matrix.PerspectiveFovLH(((float)(float)Math.PI / 2), 1.0f, 10.0f, 200.0f);
        }

        public void Render()
        {
            if (device == null)
                return;

            if (pause)
                return;

            //背景设为绿色
            device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
            //开始场景
            device.BeginScene();
            // 设置世界,视野和投影矩阵
            SetupCamera();

            // 给设备指定顶点缓存
            device.SetStreamSource(0, vertexBuffer, 0);

            //设置设备的顶点格式
            device.VertexFormat = CustomVertex.PositionColored.Format;

            //绘制图形,使用的方法为三角形列表,个数为个
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 6);

            //结束场景
            device.EndScene();

            //更新场景
            device.Present();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Render();
            
        }

    }
}

作者: bruce_wayne    时间: 2012-12-1 12:29
伊万大师威武
作者: Ba^Sa^Ra    时间: 2012-12-1 12:36
碉堡了
作者: 鲽鲽鹣鹣    时间: 2012-12-1 12:38

作者: xnec    时间: 2012-12-1 12:40
........
作者: 飞翔的奶牛    时间: 2012-12-1 12:48
<<程序猿的自我修养>>
作者: 19560101    时间: 2012-12-1 13:55
真是碉堡了。。。。。。。
作者: Pentium9    时间: 2012-12-1 16:12
星际二写好再发帖好吗
作者: jocal    时间: 2012-12-1 16:50
&#128023;&#128023;&#128023;

作者: =pnz=wolf    时间: 2012-12-1 16:59
没有屁眼打回重写
作者: bdgfaa11    时间: 2012-12-1 17:02
提示: 作者被禁止或删除 内容自动屏蔽
作者: Hwady_3    时间: 2012-12-1 17:25
买9颗好评送一颗
作者: bwv1058    时间: 2012-12-1 22:17
http://www.cnblogs.com/top5/archive/2009/10/17/1584953.html
作者: 公子别这样    时间: 2012-12-1 22:23
打脸了。。。。
作者: lf426    时间: 2012-12-1 22:45
楼主你能达到我两年前的水平吗?
http://www.cppblog.com/lf426/

3D编程方面我在OpenGL方面研究了一下,不过没有发教程而已。
不过,你真的觉得线性代数很简单?




欢迎光临 八达网 (https://www.8-da.com/) Powered by Discuz! X2.5