2 miaojuanfeng miaojuanfeng 于 2014.05.15 10:59 提问

d3d平面阴影不显示,求大神

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

#define WindowWidth 1024
#define WindowHeight 768

LPDIRECT3DDEVICE9 d3dDevice = NULL;
LPD3DXMESH d3dMeshTeapot = NULL;
LPD3DXMESH d3dMeshBox = NULL;

bool Installing3D(HWND hWnd)
{
LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
::ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferHeight = WindowHeight;
d3dpp.BackBufferWidth = WindowWidth;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.hDeviceWindow = hWnd;
d3dpp.MultiSampleQuality = 0;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = true;
d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&d3dDevice);
d3d->Release();

D3DXCreateTeapot(d3dDevice,&d3dMeshTeapot,NULL);
D3DXCreateBox(d3dDevice,8.0f,0,8.0f,&d3dMeshBox,NULL);

D3DXMATRIX mView;
D3DXVECTOR3 vEye(0,0,-10),vAt(0,0,0),vUp(0,1,0);
D3DXMatrixLookAtLH(&mView,&vEye,&vAt,&vUp);
d3dDevice->SetTransform(D3DTS_VIEW,&mView);

D3DXMATRIX mProj;
D3DXMatrixPerspectiveFovLH(&mProj,D3DX_PI/4.0,WindowWidth/WindowHeight,1.0f,100.0f);
d3dDevice->SetTransform(D3DTS_PROJECTION,&mProj);

//d3dDevice->SetRenderState(D3DRS_LIGHTING,false);

D3DLIGHT9 light;
::ZeroMemory(&light,sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
light.Diffuse = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);
light.Specular = D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
light.Direction = D3DXVECTOR3(-1.0f,-1.0f,0);
d3dDevice->SetLight(0,&light);
d3dDevice->LightEnable(0,true);
d3dDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(92,92,92));
//d3dDevice->SetRenderState(D3DRS_AMBIENT,D3DCOLOR_XRGB(255,255,255));
d3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
//d3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);

return true;

}

void Render()
{
d3dDevice->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,D3DCOLOR_XRGB(0,0,0),1.0f,0);
d3dDevice->BeginScene();

D3DXMATRIX mWorld;
D3DXMatrixIdentity(&mWorld);
D3DXMatrixTranslation(&mWorld,0,-2,0);
d3dDevice->SetTransform(D3DTS_WORLD,&mWorld);

//d3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);

D3DMATERIAL9 mtrlBox;
mtrlBox.Diffuse = D3DXCOLOR(1,1,1,1);
mtrlBox.Ambient = D3DXCOLOR(0.6f,0.6f,0.6f,1.0f);
mtrlBox.Specular = D3DXCOLOR(0.1f,0.1f,0.1f,1.0f);
mtrlBox.Emissive = D3DXCOLOR(0,0,0,1.0f);
d3dDevice->SetMaterial(&mtrlBox);

d3dMeshBox->DrawSubset(0);

//d3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);

D3DMATERIAL9 mtrl;
mtrl.Diffuse = D3DXCOLOR(1,1,1,1);
mtrl.Ambient = D3DXCOLOR(1,1,1,1);
mtrl.Specular = D3DXCOLOR(0.3f,0.3f,0.3f,1.0f);
mtrl.Emissive = D3DXCOLOR(0,0,0,1.0f);
d3dDevice->SetMaterial(&mtrl);

D3DXMATRIX mX,mY,mZ;
D3DXMatrixIdentity(&mWorld);
D3DXMatrixRotationX(&mX,::timeGetTime()/1000.0f);
D3DXMatrixRotationY(&mY,::timeGetTime()/1000.0f);
D3DXMatrixRotationZ(&mZ,::timeGetTime()/1000.0f);
mWorld = mWorld * mX * mY * mZ;
d3dDevice->SetTransform(D3DTS_WORLD,&mWorld);
d3dMeshTeapot->DrawSubset(0);

//shadow
D3DXVECTOR4 vLight(-1.0f,-1.0f,0.0f,0.0f);
D3DXPLANE plane(0.0f,-2.0f,0.0f,0.0f);
D3DXMATRIX mShadow;
D3DXMatrixShadow(&mShadow,&vLight,&plane);

d3dDevice->SetRenderState(D3DRS_STENCILENABLE,    true);
d3dDevice->SetRenderState(D3DRS_STENCILFUNC,      D3DCMP_EQUAL);
d3dDevice->SetRenderState(D3DRS_STENCILREF,       0x0);
d3dDevice->SetRenderState(D3DRS_STENCILMASK,      0xffffffff);
d3dDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
d3dDevice->SetRenderState(D3DRS_STENCILZFAIL,     D3DSTENCILOP_KEEP);
d3dDevice->SetRenderState(D3DRS_STENCILFAIL,      D3DSTENCILOP_KEEP);
d3dDevice->SetRenderState(D3DRS_STENCILPASS,      D3DSTENCILOP_INCR);

d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_ZENABLE, false);

D3DMATERIAL9 mtrlShadow;
::ZeroMemory(&mtrlShadow, sizeof(mtrlShadow));
mtrlShadow.Ambient = D3DXCOLOR(0.2f, 0.2f, 0.2f, 0.5f);
mtrlShadow.Diffuse = D3DXCOLOR(0.2f, 0.2f, 0.2f, 0.5f);
d3dDevice->SetMaterial(&mtrlShadow);

mWorld = mWorld * mShadow;
d3dDevice->SetTransform(D3DTS_WORLD, &mWorld);
d3dMeshTeapot->DrawSubset(0);

d3dDevice->SetRenderState(D3DRS_ZENABLE, true);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
d3dDevice->SetRenderState(D3DRS_STENCILENABLE,    false);

d3dDevice->EndScene();
d3dDevice->Present(NULL,NULL,NULL,NULL);

}

LRESULT CALLBACK MsgPro(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
Render();
ValidateRect(hWnd,NULL);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,message,wParam,lParam);
}

void ShutDown()
{
if(d3dDevice!=NULL) d3dDevice->Release();
if(d3dMeshTeapot!=NULL) d3dMeshTeapot->Release();
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
#define ClassName L"ClassName"
#define WindowName L"WindowName"

WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.lpfnWndProc = MsgPro;
wc.lpszClassName = ClassName;
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);

HWND hWnd = CreateWindow(ClassName,WindowName,WS_OVERLAPPEDWINDOW,100,5,WindowWidth,WindowHeight,NULL,NULL,hInstance,NULL);

if(Installing3D(hWnd))
{
    ShowWindow(hWnd,nShowCmd);
    UpdateWindow(hWnd);

    MSG msg;
    ::ZeroMemory(&msg,sizeof(msg));
    while(msg.message!=WM_QUIT)
    {
        if(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Render();
        }
    }
}
ShutDown();
UnregisterClass(ClassName,hInstance);
return 0;

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!