directx 9:c ++ 6的基本灯光?

时间:2015-02-27 07:16:59

标签: c++ directx

我有一个源代码: 这是初始化

//The Direct3d and device object
IDirect3D9      *g_pD3D = NULL;
IDirect3DDevice9    *g_pD3DDevice = NULL;

//the 3-D vertex format and descriptor
typedef struct
{
    FLOAT x, y, z;  //3-D coordinates
    FLOAT nx, ny, nz;   //Normals
    D3DCOLOR Diffuse;   //Colors

}sVertex;
#define VERTEXFVF (D3DFVF_XYZ |  D3DFVF_DIFFUSE | D3DFVF_NORMAL)

//Vertex buffer
IDirect3DVertexBuffer9 *g_pVB = NULL;
sVertex Verts[16] = 
        {
            {-100.f, 100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255,255,255,255)},
            { 100.f, 100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, -100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { 100.f, -100.0f, -100.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },

            { 100.f, 100.0f, -100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { 100.f, 100.0f, 100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { 100.f, -100.0f, -100.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { 100.f, -100.0f, 100.0f, 1.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },

            { 100.f, 100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, 100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { 100.f, -100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, -100.0f, 100.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },

            { -100.f, 100.0f, 100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, 100.0f, -100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, -100.0f, 100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) },
            { -100.f, -100.0f, -100.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 255, 255, 255) }
        };

这里是init函数

BOOL DoInit()
{
    //perform application initialization functions here
    //such as those that set up the graphics, sound, network, etc
    //Return a value of TRUE for success, FALSE otherwise.

    D3DPRESENT_PARAMETERS d3dpp;
    D3DDISPLAYMODE          d3ddm;
    D3DXMATRIX  matProj, matView;
    D3DLIGHT9 Light;
    BYTE *Ptr;
    sVertex Verts[16]; 

    //do a windowed mode initialization of Direct3D
    if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
        return FALSE;
    if (FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
        return FALSE;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp, &g_pD3DDevice)))
        return FALSE;

    //set the rendering states
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
    g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, TRUE);

    //create and set the view matrix
    D3DXMatrixLookAtLH(&matView,
        &D3DXVECTOR3(0.0f, 0.0f, -500.0f),
        &D3DXVECTOR3(0.0f, 0.0f, 0.0f),
        &D3DXVECTOR3(0.0f, 1.0f,0.0f));
    g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);

    //create the vertex buffer and set data
    g_pD3DDevice->CreateVertexBuffer(sizeof(sVertex)* 16, 0, VERTEXFVF, D3DPOOL_DEFAULT, &g_pVB, 0);
    g_pVB->Lock(0, 0, (VOID**)&Ptr, 0);
    memcpy(Ptr, Verts, sizeof(Verts));
    g_pVB->Unlock();

    //set light data, color, position, and range
    ZeroMemory(&Light, sizeof(Light));
    Light.Type = D3DLIGHT_POINT;
    Light.Diffuse.r = Light.Ambient.r = 0.5f;
    Light.Diffuse.g = Light.Ambient.g = 0.5f;
    Light.Diffuse.b = Light.Ambient.b = 0.0f;
    Light.Diffuse.a = Light.Ambient.a = 1.0f;
    Light.Range = 1000.0f;
    Light.Attenuation0 = 0.5f;
    Light.Position.x = 300.0f;
    Light.Position.y = 0.0f;
    Light.Position.z = -600.0f;

    //set and enable the light
    g_pD3DDevice->SetLight(0, &Light);
    g_pD3DDevice->LightEnable(0, TRUE);

    return TRUE;
}

这里是函数doFrame

BOOL DoFrame()
{
    //Perform per-frame processing, such as rendering.
    //Return TRUE on success, FALSE otherwise.

    D3DXMATRIX matWorld;

    //cleat device backbuffer
    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);

    //begin scene
    if (SUCCEEDED(g_pD3DDevice->BeginScene()))
    {
        //create and set the world transformation matrix
        //rotate object along Y-axis
        D3DXMatrixRotationY(&matWorld, (float)timeGetTime()/1000.0f);
        g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);

        //set the vertex stream and shader
        g_pD3DDevice->SetStreamSource(0, g_pVB, sizeof(sVertex), 0);
        //g_pD3DDevice->SetVertexShader(VERTEXFVF);
        g_pD3DDevice->SetFVF(VERTEXFVF);

        //Draw the vertex buffer
        for (short i = 0; i < 4; i++)
            g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, i * 4, 2);

        //end the scene
        g_pD3DDevice->EndScene();

    }

    //displa the scene
    g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
    return TRUE;
}

当我编译这段代码时,它只显示黑色背景..问题在哪里?

1 个答案:

答案 0 :(得分:0)

我有一个opengl的背景。我不太了解directx。如果我错了,请纠正我。但我认为你需要设置投影矩阵。我的意思是没有观看视锥,GPU不会渲染场景。

您似乎已经声明了投影矩阵,但您还没有为其设置值。 (像FOV一样的价值)。