游戏开发论坛

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

请教一下 DX渲染绘制图元的问题

[复制链接]

3

主题

24

帖子

294

积分

中级会员

Rank: 3Rank: 3

积分
294
发表于 2010-7-4 10:04:00 | 显示全部楼层 |阅读模式
    在下初学DX 在一本书上照着例子写了个绘制两条白线的程序,但是两条白线始终不出来,用DX渲染窗口背景颜色又没有问题,说明DX是渲染上的。有劳各位能不能帮我看看这个代码。到底哪里出了问题。有点长,不好意思。
#include<windows.h>
#include<d3d9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS L"UGPDX"
#define WINDOW_NAME L"Drawing Lines"

// Function Prototypes.....
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();

// Direct3D object and device
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

//Vertex buffer to hold the geometry
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

struct stD3DVertex
{
        float x, y, z;
        unsigned long color;
};

//Our custom FVF, which describes our custom vertex structure;
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        switch(msg)
        {
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
                break;
        case WM_KEYUP:
                if(wParam == VK_ESCAPE) PostQuitMessage(0);
                break;
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
        //Register the window class
        WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0,
                GetModuleHandle(NULL), NULL, NULL, NULL, NULL, WINDOW_CLASS, NULL};

        RegisterClassEx(&wc);
       
        //Create the application's window
        HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
                100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL);

        //Initialize Direct3D
        if(InitializeD3D(hWnd, false))
        {
                //Show the window
                ShowWindow(hWnd, SW_SHOWDEFAULT);
                UpdateWindow(hWnd);

                //Enter the message loopx
                MSG msg;
                ZeroMemory(&msg, sizeof(msg));

                while(msg.message != WM_QUIT)
                {
                       
                        if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                        {
                                TranslateMessage(&msg);
                                DispatchMessage(&msg);
                        }
                        else
                        {
                                RenderScene();
                        }
                }
        }

        //Release any and all resources.
        Shutdown();

        //Unregister our window.
        UnregisterClass(WINDOW_CLASS, wc.hInstance);
        return 0;
}

bool InitializeD3D(HWND hWnd, bool fullscreen)
{
        D3DDISPLAYMODE displayMode;

        //Create the D3D object.
        g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
        if(g_D3D == NULL) return false;

        // Get the desktop display mode.
        if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) return false;

        //Set up the structure used to create the D3DDevice.
        D3DPRESENT_PARAMETERS d3dpp;
        ZeroMemory(&d3dpp, sizeof(d3dpp));

        if(fullscreen)
        {
                d3dpp.Windowed = FALSE;
                d3dpp.BackBufferWidth = 640;
                d3dpp.BackBufferHeight = 480;
        }
        else
                d3dpp.Windowed = TRUE;
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = displayMode.Format;

        //Create the D3DDevice
        if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
                D3DDEVTYPE_HAL, hWnd,
                D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp,
                &g_D3DDevice))) return false;

        //Initialize any objects we will be display.
        if(!InitializeObjects()) return false;

        return true;
}


bool InitializeObjects()
{
        unsigned long col = D3DCOLOR_XRGB(255, 255, 255);

        //Fill in our structure to draw an object;
        //x, y, z, color.
        stD3DVertex objData[] =
        {
                { 420.0f, 150.0f, 0.5f, col,},
                { 420.0f, 350.0f, 0.5f, col,},
                { 220.0f, 150.0f, 0.5f, col,},
                { 220.0f, 350.0f, 0.5f, col,},
        };

        //Create the vertex buffer.
        if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
                D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return false;

        //Fill the vertex buffer.
        void *ptr;

        if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
                (void**)&ptr, 0))) return false;

        memcpy(ptr, objData, sizeof(objData));

        g_VertexBuffer->Unlock();

        return true;
}

void RenderScene()
{
        //Clear the backbuffer.
        g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

        //Begin the scene. Start rendering.
        g_D3DDevice->BeginScene();

           //Render object.
       g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
           g_D3DDevice->SetFVF(D3DFVF_VERTEX);
           g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2);

        //End the scene. Stop rendering.
        g_D3DDevice->EndScene();

        //Display the scene.
        g_D3DDevice-&gtresent(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
        if(g_D3DDevice != NULL) g_D3DDevice->Release();
        if(g_D3D != NULL) g_D3D->Release();
        if(g_VertexBuffer != NULL) g_VertexBuffer->Release();

        g_D3DDevice = NULL;
        g_D3D = NULL;
        g_VertexBuffer = NULL;
}

4

主题

81

帖子

81

积分

注册会员

Rank: 2

积分
81
发表于 2010-7-5 20:49:00 | 显示全部楼层

Re:请教一下 DX渲染绘制图元的问题

struct stD3DVertex
{
float x, y, z;
unsigned long color;
};

//Our custom FVF, which describes our custom vertex structure;
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

上面是你的顶点结构 这个顶点结构中的坐标是 未经变换的顶点坐标 所以应该是显示不出来的
要想让他现实必须加上摄像机

像你这样的做法 应该这样写
struct stD3DVertex
{
float x, y, z, rhw;
unsigned long color;
};

//Our custom FVF, which describes our custom vertex structure;
#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

加个rhw变成经过变换的顶点坐标然后将D3DFVF_XYZ 该为 D3DFVF_XYZRHW 为经过变换的顶点坐标
最后 将

stD3DVertex objData[] =
{
{ 420.0f, 150.0f, 0.5f, col,},
{ 420.0f, 350.0f, 0.5f, col,},
{ 220.0f, 150.0f, 0.5f, col,},
{ 220.0f, 350.0f, 0.5f, col,},
};

改为
stD3DVertex objData[] =
{
{ 420.0f, 150.0f, 0.5f, 1.0f, col,},
{ 420.0f, 350.0f, 0.5f, 1.0f, col,},
{ 220.0f, 150.0f, 0.5f, 1.0f, col,},
{ 220.0f, 350.0f, 0.5f, 1.0f, col,},
};
应该就可以了

29

主题

107

帖子

107

积分

注册会员

Rank: 2

积分
107
发表于 2010-7-10 12:51:00 | 显示全部楼层

Re:请教一下 DX渲染绘制图元的问题

你没关闭灯光吗

1

主题

266

帖子

280

积分

中级会员

Rank: 3Rank: 3

积分
280
发表于 2010-7-11 01:53:00 | 显示全部楼层

Re:请教一下 DX渲染绘制图元的问题

你给出的顶点坐标是当屏幕坐标用么?
如果是的话,那你的这个“D3DFVF_XYZ”就用错了,应该使用齐次坐标——“D3DFVF_XYZRHW”。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-8 10:48

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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