游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2803|回复: 1

提个问题?为什么我用索引画规则三角形网 会显示不出来

[复制链接]

3

主题

11

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2010-8-28 10:38:00 | 显示全部楼层 |阅读模式
求解释,下面是代码:



  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Microsoft.DirectX;
  9. using Microsoft.DirectX.Direct3D;

  10. namespace test
  11. {
  12.     public partial class test : Form
  13.     {
  14.         private int xCount = 5, yCount = 4;//定义横向和纵向网格数目
  15.         private float cellHeight = 1f, cellWidth = 1.5f;//定义单元的宽度和长度
  16.         private VertexBuffer vertextBuffer;//定义顶点缓冲变量
  17.         private IndexBuffer indexBuffer;//定义索引缓冲变量
  18.         private int[] indices;//定义索引号变量

  19.         private CustomVertex.PositionColored[] vertices;//定义顶点向量
  20.         Device device = null;//定义设备对象
  21.         public test()
  22.         {
  23.             this.ClientSize = new Size(800,600);
  24.             this.Text = "摄像机变换";
  25.         }
  26.         //初始化D3D设备
  27.         public bool InitDirect3D()
  28.         {
  29.             try
  30.             {
  31.                 PresentParameters presentParameters = new PresentParameters();
  32.                 presentParameters.Windowed = true;//指定以windows窗体形式显示
  33.                 presentParameters.SwapEffect = SwapEffect.Discard;//当前屏幕绘制后 它将自动从内存中清除
  34.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);

  35.                 vertexDeclaration();//定义顶点
  36.                 IndicesDeclaration();//定义索引缓冲

  37.                 return true;
  38.             }
  39.             catch (Exception ee)
  40.             {
  41.                 MessageBox.Show(ee.ToString(), "Error");//处理异常
  42.                 return false;
  43.             }
  44.         }

  45.         //定义渲染函数
  46.         public void Render()
  47.         {
  48.             if (device == null)   //如果device为空则不渲染
  49.             {
  50.                 return;
  51.             }
  52.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  53.             device.BeginScene();

  54.             //要渲染的图形的代码
  55.             device.RenderState.CullMode = Cull.None;
  56.             device.RenderState.Lighting = false;

  57.             device.VertexFormat = CustomVertex.PositionColored.Format;//设置绘图设备的顶点类型为CustomVertex.PositionColored
  58.             device.SetStreamSource(0, vertextBuffer,0);//绑定顶点缓冲到绘图设备的数据流中
  59.             device.Indices = indexBuffer;//表示设置绘图设备的索引数据
  60.             device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
  61. (xCount + 1) * (yCount + 1), 0, indices.Length / 3);

  62.             device.EndScene();
  63.             device.Present();
  64.         }

  65.         //对顶点向量进行设置
  66.         public void vertexDeclaration()
  67.         {
  68.             string bitmapPath = @"C:\heightMap.BMP";
  69.             Bitmap bitmap = new Bitmap(bitmapPath);
  70.             xCount = (bitmap.Width - 1) / 2;
  71.             yCount = (bitmap.Height - 1) / 2;
  72.             cellWidth = bitmap.Width / xCount;
  73.             cellHeight = bitmap.Height / yCount;
  74.             vertextBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
  75.             (xCount + 1) * (yCount + 1), device,
  76.             Usage.Dynamic | Usage.WriteOnly,
  77.             CustomVertex.PositionColored.Format, Pool.Default);
  78.             vertices = new
  79.             CustomVertex.PositionColored[(xCount + 1) * (yCount + 1)];//定义顶点
  80.             for (int i = 0; i < yCount + 1; i++)
  81.             {
  82.                 for (int j = 0; j < xCount + 1; j++)
  83.                 {
  84.                     Color color = bitmap.GetPixel((int)(j * cellWidth), (int)(i *
  85.                     cellHeight));
  86.                     float height = float.Parse(color.R.ToString()) +
  87.                     float.Parse(color.G.ToString()) + float.Parse(color.B.ToString());
  88.                     height /= 10;
  89.                     vertices[j + i * (xCount + 1)].Position = new Vector3(j *
  90.                     cellWidth, height, i * cellHeight);
  91.                     vertices[j + i * (xCount + 1)].Color = Color.White.ToArgb();
  92.                 }
  93.             }
  94.             vertextBuffer.SetData(vertices, 0, LockFlags.None);
  95.         }

  96.         //对索引进行设置
  97.         private void IndicesDeclaration()//定义索引
  98.         {
  99.             indexBuffer = new IndexBuffer(typeof(int), 6 * xCount * yCount, device,
  100. Usage.WriteOnly, Pool.Default);
  101.             indices = new int[6 * xCount * yCount];
  102.             for (int i = 0; i < yCount; i++)
  103.             {
  104.                 for (int j = 0; j < xCount; j++)
  105.                 {
  106.                     indices[6 * (j + i * xCount)] = j + i * (xCount + 1);
  107.                     indices[6 * (j + i * xCount) + 1] = j + (i + 1) * (xCount + 1);
  108.                     indices[6 * (j + i * xCount) + 2] = j + i * (xCount + 1) + 1;
  109.                     indices[6 * (j + i * xCount) + 3] = j + i * (xCount + 1) + 1;
  110.                     indices[6 * (j + i * xCount) + 4] = j + (i + 1) * (xCount + 1);
  111.                     indices[6 * (j + i * xCount) + 5] = j + (i + 1) * (xCount + 1)
  112.                     + 1;
  113.                 }
  114.             }
  115.             indexBuffer.SetData(indices, 0, LockFlags.None);
  116.         }

  117.         
  118.         static void Main()
  119.         {
  120.             test testForm = new test();
  121.             if (testForm.InitDirect3D() == false)
  122.             {
  123.                 MessageBox.Show("无法启动DirectX3D!", "错误");
  124.                 return;
  125.             }
  126.             testForm.Show();

  127.             while (testForm.Created)
  128.             {
  129.                 testForm.Render();//保持device渲染,知道程序结束
  130.                 Application.DoEvents();//处理消息的函数
  131.             }
  132.         }
  133.     }
  134. }
复制代码


求有会的帮我调试一下错哪了。总是显示不出来 我很郁闷!

附件是我的工程

2

主题

29

帖子

29

积分

注册会员

Rank: 2

积分
29
发表于 2011-4-29 11:41:00 | 显示全部楼层

Re:提个问题?为什么我用索引画规则三角形网 会显示不

#include <d3d9.h>

//创建全局对象
LPDIRECT3D9  g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB =NULL;
int g_iType=1;//图元类型


//定点结构
struct CUSTOMVERTEX
{
        FLOAT x,y,z,rhw;
        DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

//初始化D3D
HRESULT Init3D(HWND hwnd)
{
        if(NULL==(g_pD3D=Direct3DCreate9(D3D_SDK_VERSION)))
                return E_FAIL;

        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory(&d3dpp,sizeof(d3dpp));
        d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

        if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice)))
        {
                return E_FAIL;
        }
        //设置剔除模式为不剔除任何面
        g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
        //设置图元填充模式为线框模式
        g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);

        return S_OK;
}

//创建并填充顶点缓冲区
HRESULT InitVB()
{

        CUSTOMVERTEX vertices[]={        { 50.0f,250.0f,0.5f,1.0f,0xffff0000, },        {150.0f, 50.0f,0.5f,1.0f,0xffff0000, },        {250.0f,250.0f,0.5f,1.0f,0xffff0000, },        {350.0f, 50.0f,0.5f,1.0f,0xffff0000, },        {450.0f,250.0f,0.5f,1.0f,0xffff0000, },        {550.0f, 50.0f,0.5f,1.0f,0xffff0000, }        };


        if(FAILED(g_pd3dDevice->CreateVertexBuffer(6*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL)))
                return E_FAIL;

        VOID *pVertices;
        if(FAILED(g_pVB->Lock(0,sizeof(vertices),(void**)&pVertices,0)))
                return E_FAIL;
        memcpy(pVertices,vertices,sizeof(vertices));
        g_pVB->Unlock();
    return S_OK;
}


//释放创建的对象
VOID Cleanup()
{
        if(g_pVB!=NULL)
                g_pVB->Release();
        if(g_pd3dDevice!=NULL)
                g_pd3dDevice->Release();
        if(g_pD3D!=NULL)
                g_pD3D->Release();
}


//渲染对象
VOID Render()
{
        //清空后台缓冲区
        g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(45,50,170),1.0f,0);
        if(SUCCEEDED(g_pd3dDevice->BeginScene() ))
        {
                //在后台缓冲区绘制图形
                g_pd3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
                g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);

        switch(g_iType)
                {
                case 1://三角条带
                        //g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,4);//指定顶点渲染的序列孤立的三角形
                        g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,4);//作为一个单一的顶点渲染折线
                        break;

                case 2://三角形列表
                        g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
                        break;

                case 3://线段条带
                        g_pd3dDevice->DrawPrimitive(D3DPT_LINESTRIP,0,5);
                        break;

                case 4://线段列表
                        g_pd3dDevice->DrawPrimitive(D3DPT_LINELIST,0,3);
                        break;

                case 5://点列表
                        g_pd3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,6);
                        break;

                case 6://三角扇形
                        g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);
                        break;

                default:
                        break;

                }
//                g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);//三角形
                g_pd3dDevice->EndScene();
        }
        g_pd3dDevice-&gtresent(NULL,NULL,NULL,NULL);
}


//消息处理
LRESULT WINAPI MsgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
        switch(msg)
        {
        case WM_DESTROY:
                Cleanup();
                PostQuitMessage(0);//该函数向系统表明有个线程有终止请求。通常用来响应WM_DESTROY消息
                return 0;

        case WM_KEYDOWN:
                switch(wParam)//设置渲染图元类型
                {
                case 49://1键
        case 50://2键
                case 51://3键
                case 52://4键
                case 53://5键
                case 54://6键
                        g_iType=(int)wParam-48;//ASCII码转换
                        break;
                }

        }
        return DefWindowProc(hwnd,msg,wParam,lParam);
}


int main(HINSTANCE hInst,HINSTANCE,LPSTR,INT)
{
        //注册窗口类
        WNDCLASSEX wc = {sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"ClassName",NULL};
       
        RegisterClassEx( &wc );
        //创建窗口
    HWND hwnd=CreateWindow("ClassName","基本图元",WS_OVERLAPPEDWINDOW,100,100,600,300,NULL,NULL,wc.hInstance,NULL);

        //初始化D3D
        if(SUCCEEDED(Init3D(hwnd)))
        {
                if(SUCCEEDED(InitVB()))
                {
                        //显示窗口
                        ShowWindow(hwnd,SW_SHOWDEFAULT);
                        UpdateWindow(hwnd);
                        //进入消息循环
                        MSG msg;
                        ZeroMemory(&msg,sizeof(msg));
                        while(msg.message!=WM_QUIT)
                        {
                                if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
                                {
                                        TranslateMessage(&msg);
                                        DispatchMessage(&msg);
                                }
                                else
                                {
                                        Render();//渲染图形
                                }
                        }
                }
        }
    UnregisterClass("ClassName",wc.hInstance);
        return 0;
}

希望对你有帮助  我不会看 不过我这有例子
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-8 06:37

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表