游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2459|回复: 3

在directx 的 DX Project 2 中如何画两个三角形?

[复制链接]

2

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2005-5-9 12:57:00 | 显示全部楼层 |阅读模式
想了好久 也许是自己太菜了 就是不知道怎么改才能在例子中画出两个三角形,
请求知道的人指点一下 小弟在此谢过! [em7]

2

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2005-5-9 13:13:00 | 显示全部楼层

Re:在directx 的 DX Project 2 中如何画两个三角形?

附上DX Project 2源码:
#include <d3d8.h>

LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices


struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex.
    DWORD colour; // The vertex colour.
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}

HRESULT InitialiseD3D(HWND hWnd)
{
    //First of all, create the main D3D object. If it is created successfully we
    //should get a pointer to an IDirect3D8 interface.
    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
    if(g_pD3D == NULL)
    {
        return E_FAIL;
    }

    //Get the current display mode
    D3DDISPLAYMODE d3ddm;
    if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    {
        return E_FAIL;
    }

    //Create a structure to hold the settings for our device
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    //Fill the structure.
    //We want our program to be windowed, and set the back buffer to a format
    //that matches our current display mode
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
    d3dpp.BackBufferFormat = d3ddm.Format;

    //Create a Direct3D device.
    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp,
                                   &g_pD3DDevice)))
    {
        return E_FAIL;
    }
   
    return S_OK;
}

HRESULT InitialiseVertexBuffer()
{
    VOID* pVertices;
   
    //Store each point of the triangle together with it's colour
    CUSTOMVERTEX cvVertices[] =
    {
        //Vertex 1 - Red (250, 100)
        {250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
   
        //Vertex 2 - Green (400, 350)
        {400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
   
        //Vertex 3 - Blue (100, 350)
        {100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},   
    };

    //Create the vertex buffer from our device
    if(FAILED(g_pD3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX),
                                               0, D3DFVF_CUSTOMVERTEX,
                                               D3DPOOL_DEFAULT, &g_pVertexBuffer)))
    {
        return E_FAIL;
    }

    //Get a pointer to the vertex buffer vertices and lock the vertex buffer
    if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
    {
        return E_FAIL;
    }

    //Copy our stored vertices values into the vertex buffer
    memcpy(pVertices, cvVertices, sizeof(cvVertices));

    //Unlock the vertex buffer
    g_pVertexBuffer->Unlock();

    return S_OK;
}

void Render()
{
    if(g_pD3DDevice == NULL)
    {
        return;
    }

    //Clear the backbuffer to black
    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
   
    //Begin the scene
    g_pD3DDevice->BeginScene();
   

    //Rendering our triangle
    g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
    g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
    g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);


    //End the scene
    g_pD3DDevice->EndScene();
   
    //Filp the back and front buffers so that whatever has been rendered on the
    //back buffer will now be visible on screen (front buffer).
    g_pD3DDevice-&gtresent(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
    SafeRelease(g_pVertexBuffer);
    SafeRelease(g_pD3DDevice);
    SafeRelease(g_pD3D);
}

void GameLoop()
{
    //Enter the game loop
    MSG msg;
    BOOL fMessage;

    PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
   
    while(msg.message != WM_QUIT)
    {
        fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

        if(fMessage)
        {
            //Process message
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            //No message to process, so render the current scene
            Render();
        }

    }
}

//The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        break;
        case WM_KEYUP:
            switch (wParam)
            {
                case VK_ESCAPE:
                    //User has pressed the escape key, so quit
                    DestroyWindow(hWnd);
                    return 0;
                break;
            }
        break;

    }

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

//Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
    //Register the window class
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
                     GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                     "DX Project 2", NULL};
    RegisterClassEx(&wc);

    //Create the application's window
    HWND hWnd = CreateWindow("DX Project 2", "www.andypike.com: Tutorial 2",
                              WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL);

    //Initialize Direct3D
    if(SUCCEEDED(InitialiseD3D(hWnd)))
    {
        //Show our window
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);

        //Initialize Vertex Buffer
        if(SUCCEEDED(InitialiseVertexBuffer()))
        {
            //Start game running: Enter the game loop
            GameLoop();
        }
    }
   
    CleanUp();

    UnregisterClass("DX Project 2", wc.hInstance);
   
    return 0;
}

0

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2005-5-10 21:30:00 | 显示全部楼层

Re:在directx 的 DX Project 2 中如何画两个三角形?

唉~~一个三角型都这么长……

1

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2005-6-19 23:33:00 | 显示全部楼层

Re:在directx 的 DX Project 2 中如何画两个三角形?

一个三角形三个点,两个三角形就六个点啊.  cvVertices结构修改成下
    CUSTOMVERTEX cvVertices[] =
    {
        //Vertex 1.1 - Red (150, 100)
        {150.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
   
        //Vertex 1.2 - Green (300, 350)
        {300.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
   
        //Vertex 1.3 - Blue (0, 350)
        {0.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},   

        //Vertex 2.1 - Red (350, 100)
        {350.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
   
        //Vertex 2.2 - Green (500, 350)
        { 500.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
   
        //Vertex 2.3 - Blue (200, 350)
        {200.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},   

    };

然后修改 Render()中的g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);  为
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);  //最后一个参数2代表为两个三角形

重新编译就ok了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-26 02:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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