游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3175|回复: 6

我的第一个D3D程序,什么也看不见,郁闷!

[复制链接]

11

主题

20

帖子

20

积分

注册会员

Rank: 2

积分
20
发表于 2004-11-13 16:52:00 | 显示全部楼层 |阅读模式
从本站下载的D3D8教程中找了第二课,稍微改了改,居然那个美丽的三角形就看不见了,哪位大侠帮我瞧瞧哪儿不对,先谢谢了.


#include <d3d8.h>
#include <d3dx8.h>

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

struct CUSTOMVERTEX
{
    float position[4];                // The transformed position for the vertex.
    DWORD colour;                        // The vertex colour.
    float normal[3];
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_NORMAL)

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

D3DXVECTOR3 getNormal(D3DXVECTOR3 vVertex1,D3DXVECTOR3 vVertex2,D3DXVECTOR3 vVertex3)
{
        //        val = &faces;
        //        D3DXVECTOR3 vVertex1(val->vertex[0].position[0],val->vertex[0].position[1],val->vertex[0].position[2]);
        //        D3DXVECTOR3 vVertex2(val->vertex[1].position[0],val->vertex[1].position[1],val->vertex[1].position[2]);
        //        D3DXVECTOR3 vVertex3(val->vertex[2].position[0],val->vertex[2].position[1],val->vertex[2].position[2]);

                D3DXVECTOR3 vNormal;
                D3DXVECTOR3 v1;
                D3DXVECTOR3 v2;

                D3DXVec3Subtract(&v1, &vVertex2, &vVertex1);
                D3DXVec3Subtract(&v2, &vVertex3, &vVertex1);

                D3DXVec3Cross(&vNormal, &v1, &v2);

                D3DXVec3Normalize(&vNormal, &vNormal);
                return vNormal;
}


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;
    }
   

D3DLIGHT8 d3dLight;

//Initialize the light structure.
ZeroMemory(&d3dLight, sizeof(D3DLIGHT8));
//Set up a white point light at (0, 0, -10).
d3dLight.Type = D3DLIGHT_POINT;

d3dLight.Diffuse.r = 1.0f;
d3dLight.Diffuse.g = 1.0f;
d3dLight.Diffuse.b = 1.0f;

d3dLight.Ambient.r = 0.0f;
d3dLight.Ambient.g = 0.0f;
d3dLight.Ambient.b = 0.0f;

d3dLight.Specular.r = 0.0f;
d3dLight.Specular.g = 0.0f;
d3dLight.Specular.b = 0.0f;

d3dLight.Position.x = 0.0f;
d3dLight.Position.y = 0.0f;
d3dLight.Position.z = -10.0f;

d3dLight.Attenuation0 = 1.0f;
d3dLight.Attenuation1 = 0.0f;
d3dLight.Attenuation2 = 0.0f;
d3dLight.Range = 100.0f;

//Assign the point light to our device in poisition (index) 0
g_pD3DDevice->SetLight(0, &d3dLight);
g_pD3DDevice->LightEnable(0, TRUE);

//Turn on lighting
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);

//Set ambient light level
g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(32, 32, 32));

    return S_OK;
}

HRESULT InitialiseVertexBuffer()
{
        VOID* pVertices;
       
        //Store each point of the triangle together with it's colour
        D3DXVECTOR3 pos0(250.0f, 100.0f, 0.5f);
        D3DXVECTOR3 pos1(400.0f, 350.0f, 0.5f);
        D3DXVECTOR3 pos2(100.0f, 350.0f, 0.5f);
        D3DXVECTOR3 normal = getNormal(pos0,pos1,pos2);

        CUSTOMVERTEX cvVertices[3];
        cvVertices[0].position[0] = pos0.x;
        cvVertices[0].position[1] = pos0.y;
        cvVertices[0].position[2] = pos0.z;
        cvVertices[0].position[3] = 1.0;
    cvVertices[0].normal[0] = normal.x;
        cvVertices[0].normal[1] = normal.y;
        cvVertices[0].normal[2] = normal.z;

        cvVertices[1].position[0] = pos1.x;
        cvVertices[1].position[1] = pos1.y;
        cvVertices[1].position[2] = pos1.z;
        cvVertices[1].position[3] = 1.0;
    cvVertices[1].normal[0] = normal.x;
        cvVertices[1].normal[1] = normal.y;
        cvVertices[1].normal[2] = normal.z;

        cvVertices[2].position[0] = pos2.x;
        cvVertices[2].position[1] = pos2.y;
        cvVertices[2].position[2] = pos2.z;
        cvVertices[2].position[3] = 1.0;
    cvVertices[2].normal[0] = normal.x;
        cvVertices[2].normal[1] = normal.y;
        cvVertices[2].normal[2] = normal.z;

        cvVertices[0].colour = D3DCOLOR_XRGB(255, 0, 0);
        cvVertices[1].colour = D3DCOLOR_XRGB(0, 255, 0);
        cvVertices[2].colour = D3DCOLOR_XRGB(0, 0, 255);


        D3DCOLORVALUE rgbaDiffuse  = {1.0, 1.0, 1.0, 0.0,};
        D3DCOLORVALUE rgbaAmbient  = {1.0, 1.0, 1.0, 0.0,};
        D3DCOLORVALUE rgbaSpecular = {0.0, 0.0, 0.0, 0.0,};
        D3DCOLORVALUE rgbaEmissive = {0.0, 0.0, 0.0, 0.0,};
       
    m_matMaterial.Diffuse = rgbaDiffuse;
        m_matMaterial.Ambient = rgbaAmbient;
        m_matMaterial.Specular = rgbaSpecular;
        m_matMaterial.Emissive = rgbaEmissive;
        /*
    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->SetMaterial(&m_matMaterial);
        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;
}

89

主题

4036

帖子

4132

积分

论坛元老

Rank: 8Rank: 8

积分
4132
发表于 2004-11-13 17:30:00 | 显示全部楼层

Re:我的第一个D3D程序,什么也看不见,郁闷!

太长.没时间看.

9

主题

151

帖子

153

积分

注册会员

Rank: 2

积分
153
发表于 2004-11-13 17:34:00 | 显示全部楼层

Re:我的第一个D3D程序,什么也看不见,郁闷!

D3D代码头疼

63

主题

871

帖子

891

积分

高级会员

Rank: 4

积分
891
QQ
发表于 2004-11-13 18:53:00 | 显示全部楼层

Re: 我的第一个D3D程序,什么也看不见,郁闷!

引用Help的一句话:

D3DFVF_NORMAL
Vertex format includes a vertex normal vector. This flag cannot be used with the D3DFVF_XYZRHW flag.

顶点包括法线矢量,不能与D3DFVF_XYZRHW标志一起使用

[em2] [em2] [em2] [em2]

63

主题

871

帖子

891

积分

高级会员

Rank: 4

积分
891
QQ
发表于 2004-11-13 18:54:00 | 显示全部楼层

Re:我的第一个D3D程序,什么也看不见,郁闷!

我实在是太无聊了

13

主题

978

帖子

978

积分

高级会员

Rank: 4

积分
978
发表于 2004-11-13 20:33:00 | 显示全部楼层

Re:我的第一个D3D程序,什么也看不见,郁闷!

拜托!能不能自己研究研究?!今上午我也遇见了这个问题……研究了半天……你的代码太长谁有耐心看?我研究这个问题时只改动了2个block中的代码……至于把全文发上来吗??
另:我用dx9,数据结构差别太大,没法帮你改了…………

4

主题

140

帖子

140

积分

注册会员

Rank: 2

积分
140
发表于 2004-11-14 11:25:00 | 显示全部楼层

Re:我的第一个D3D程序,什么也看不见,郁闷!

你改乐啥地方?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-23 07:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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