DirectX 基础

我照tutorial的Lights那个例子写的,效果一样,黄色的圆筒在X州滚动。哪位高手帮我看一下为什么运行后是一片空白,什么也没有?由于代码太长,将在一楼帖出。
我改得基本上和sample里的一样,甚至数值最后都改成一样的了,而且我把示例程序各个函数复制过来替换掉我自己的相应函数仍然不行,而全部复制过来却正常了,不知为什么?
最新回答
←何方妖孽

2024-11-28 08:14:32

// DemoLight.cpp:
//
#include <windows.h>
#include <d3dx9.h>
#include <mmsystem.h>

//#pragma comment (lib, "d3d9.lib")
//#pragma comment (lib, "d3dx9d.lib")
//#pragma comment (lib, "winmm.lib")
//#pragma comment (lib, "d3dxof.lib")
//#pragma comment (lib, "dxguid.lib")

struct CUSTOMVERTEX {
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL)

LPDIRECT3D9 d3d9;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 d3dvb;

void InitD3D( HWND hwnd)
{
d3d9 = Direct3DCreate9( D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp));
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = true;

d3d9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

d3ddev->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
d3ddev->SetRenderState( D3DRS_ZENABLE, true);
}

void InitGeometry()
{
CUSTOMVERTEX* pVertices;
d3ddev->CreateVertexBuffer( 50 * 2 * sizeof( CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &d3dvb, NULL);

d3dvb->Lock( 0, 0, ( void**)&pVertices, 0);
for( int i=0; i<50; i++) {
float theta = 2 * D3DX_PI * i / 49;
pVertices[2 * i].normal = D3DXVECTOR3( sinf( theta), -1.0f, cosf( theta));
pVertices[2 * i].position = D3DXVECTOR3( sinf( theta), 0.0f, cosf( theta));
pVertices[2 * i + 1].normal = D3DXVECTOR3( sinf( theta), 1.0f, cosf( theta));
pVertices[2 * i + 1].position = D3DXVECTOR3( sinf( theta), 0.0f, cosf( theta));
}
d3dvb->Unlock();
}

void SetupMatrix()
{
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity( &matWorld);
D3DXMatrixRotationX( &matWorld, timeGetTime() / 500.0f);
d3ddev->SetTransform( D3DTS_WORLD, &matWorld);

D3DXVECTOR3 vEyePt( 0.0f, 0.0f, -5.0f);
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f);
D3DXMATRIXA16 matView;
D3DXMatrixIdentity( &matView);
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec);
d3ddev->SetTransform( D3DTS_VIEW, &matView);

D3DXMATRIXA16 matProj;
D3DXMatrixIdentity( &matProj);
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f);
d3ddev->SetTransform( D3DTS_PROJECTION, &matProj);
}

void SetupLights()
{
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof( mtrl));
mtrl.Ambient.a = mtrl.Diffuse.a = 1.0f;
mtrl.Ambient.b = mtrl.Diffuse.b = 0.0f;
mtrl.Ambient.g = mtrl.Diffuse.g = 1.0f;
mtrl.Ambient.r = mtrl.Diffuse.r = 1.0f;
d3ddev->SetMaterial( &mtrl);

D3DLIGHT9 light;
ZeroMemory( &light, sizeof( light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Position = D3DXVECTOR3( -5.0f, 0.5f, -5.0f);
light.Direction = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);
light.Ambient.r = light.Diffuse.r = 1.0f;
light.Ambient.g = light.Diffuse.g = 1.0f;
light.Ambient.b = light.Diffuse.b = 0.0f;
light.Range = 100.0f;

d3ddev->SetLight( 0, &light);
d3ddev->LightEnable( 0, true);
d3ddev->SetRenderState( D3DRS_AMBIENT, 0x00202020);
}

void Render()
{
d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255), 1.0f, 0);
d3ddev->BeginScene();

SetupMatrix();
SetupLights();

d3ddev->SetStreamSource( 0, d3dvb, 0, sizeof( CUSTOMVERTEX));
d3ddev->SetFVF( D3DFVF_CUSTOMVERTEX);
d3ddev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 100);

d3ddev->EndScene();
d3ddev->Present( NULL, NULL, NULL, NULL);
}

void Cleanup()
{
d3dvb->Release();
d3ddev->Release();
d3d9->Release();
}

LRESULT CALLBACK windowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg) {
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0);
return 0;
}
return DefWindowProc( hwnd, msg, wParam, lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX wce;
wce.cbClsExtra = 0;
wce.cbSize = sizeof( wce);
wce.cbWndExtra = 0;
wce.hbrBackground = ( HBRUSH) GetStockObject( WHITE_BRUSH);
wce.hCursor = LoadCursor( NULL, IDC_ARROW);
wce.hIcon = LoadIcon( NULL, IDI_APPLICATION);
wce.hIconSm = wce.hIcon;
wce.hInstance = hInstance;
wce.lpfnWndProc = &windowProc;
wce.lpszClassName = L"DemoLight";
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx( &wce);

HWND hwnd = CreateWindowEx( 0, wce.lpszClassName, L"Light", WS_OVERLAPPEDWINDOW, 490, 200, 300, 300, NULL, NULL, hInstance, NULL);

InitD3D( hwnd);
InitGeometry();

ShowWindow( hwnd, SW_SHOWNORMAL);
UpdateWindow( hwnd);

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

return 0;
}
试卷看似简单但心机太重

2024-11-28 17:11:06

返回对照一下Sample浏览器里边的源码。
看看缓冲区alpha和RBG值设置是否正确。还有就是法线和顶点不要设置错误了。

好久没玩d3d了,又见新人。。。O(∩_∩)O~~