简体   繁体   English

DirectX 9:C ++ 6上的基本照明?

[英]directx 9 : basic lighting on c++ 6?

i have a source code : here's the initialization 我有一个源代码:这是初始化

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

here's the init function 这是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;
}

and here's the function doFrame 这是函数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;
}

when I compile this code, it just shows black background.. where's the problem?? 当我编译这段代码时,它只显示黑色背景。.问题出在哪里?

I have a background of opengl. 我有opengl的背景。 I don't know directx much. 我不太了解DirectX。 Correct me if I am wrong. 如果我错了,请纠正我。 But I think you need to set the projection matrix. 但是我认为您需要设置投影矩阵。 I mean without the viewing frustum GPU won't render the scene. 我的意思是,如果没有视锥,GPU将无法渲染场景。

It seems you have declared projection matrix but you haven't set values to it. 似乎您已经声明了投影矩阵,但尚未设置值。 (Value like FOV). (值像FOV)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM