24小时热门版块排行榜    

CyRhmU.jpeg
查看: 753  |  回复: 0

断点~+++

金虫 (小有名气)

[求助] DX 画个矩形问题 求大神

//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: triangle.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a triangle in wireframe mode.  Demonstrates vertex buffers,
//       render states, and drawing commands.
//         
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "d3dUtility.h"

//
// Globals
//

IDirect3DDevice9* Device = 0;

const int Width  = 640;
const int Height = 480;

IDirect3DVertexBuffer9* Triangle = 0; // vertex buffer to store
                                      // our triangle data.

//
// Classes and Structures
//

struct Vertex
{
        Vertex(){}

        Vertex(float x, float y, float z)
        {
        _x = x;         _y = y;  _z = z;
        }

        float _x, _y, _z;

        static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ;

//
// Framework Functions
//
bool Setup()
{
        //
        // Create the vertex buffer.
        //

        Device->CreateVertexBuffer(
                3 * sizeof(Vertex), // size in bytes
                D3DUSAGE_WRITEONLY, // flags
                Vertex::FVF,        // vertex format
                D3DPOOL_MANAGED,    // managed memory pool
                &Triangle,          // return create vertex buffer
                0);                 // not used - set to 0

        //
        // Fill the buffers with the triangle data.
        //

        Vertex* vertices;

        Triangle->Lock(0, 0, (void**)&vertices, 0);   

        vertices[0] = Vertex(0.0f, 0.5f, 2.0f); //left  
        vertices[1] = Vertex(-1.0f, 1.0f, 2.0f); //
        vertices[2] = Vertex( 1.0f, 1.0f, 2.0f); //right
        vertices[3] = Vertex( 1.0f, 0.0f, 2.0f);
        vertices[4] = Vertex( -1.0f, 0.0f, 2.0f);
        vertices[5] = Vertex( -1.0f, 1.0f, 2.0f);
        Triangle->Unlock();

        //
        // Set the projection matrix.
        //

        D3DXMATRIX proj;
        D3DXMatrixPerspectiveFovLH(
                        &proj,                        // result
                        D3DX_PI * 0.5f,               // 90 - degrees
                        (float)Width / (float)Height, // aspect ratio
                        1.0f,                         // near plane
                        1000.0f);                     // far plane
        Device->SetTransform(D3DTS_PROJECTION, &proj);

        //
        // Set wireframe mode render state.
        //

        Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

        return true;
}
void Cleanup()
{
        d3d::Release<IDirect3DVertexBuffer9*>(Triangle);
}

bool Display(float timeDelta)
{
        if( Device )
        {
                Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
                Device->BeginScene();

                Device->SetStreamSource(0, Triangle, 0, sizeof(Vertex));
                Device->SetFVF(Vertex::FVF);

                // Draw one triangle.

               
                        Device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4);

                Device->EndScene();
                Device->Present(0, 0, 0, 0);
        }
        return true;
}


//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch( msg )
        {
        case WM_DESTROY:
                :ostQuitMessage(0);
                break;
               
        case WM_KEYDOWN:
                if( wParam == VK_ESCAPE )
                        :estroyWindow(hwnd);
                break;
        }
        return :efWindowProc(hwnd, msg, wParam, lParam);
}

//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
                                   HINSTANCE prevInstance,
                                   PSTR cmdLine,
                                   int showCmd)
{
        if(!d3d::InitD3D(hinstance,
                Width, Height, true, D3DDEVTYPE_HAL, &Device))
        {
                ::MessageBox(0, "InitD3D() - FAILED", 0, 0);
                return 0;
        }
               
        if(!Setup())
        {
                ::MessageBox(0, "Setup() - FAILED", 0, 0);
                return 0;
        }

        d3d::EnterMsgLoop( Display );

        Cleanup();

        Device->Release();

        return 0;
}

Device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,4); 使用D3DPT_TRIANGLEFAN 系统使用顶点v2、v3和v1画第一个三角形;v3,v4,v1画第二个三角形;v4,v5,v1画第三个三角形;等等。启用平面着色时,系统色调颜色从第一个顶点的三角形 但是运行的时候只有第一个三角形画出来了  应该是四个三角形组成一个矩形啊
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 断点~+++ 的主题更新
信息提示
请填处理意见