1.DirectX åºç¡
DirectX åºç¡
// 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_D;
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( * 2 * sizeof( CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &d3dvb, NULL);
d3dvb->Lock( 0, 0, ( void**)&pVertices, 0);
for( int i=0; i<; i++) {
float theta = 2 * D3DX_PI * i / ;
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()
{
D3DXMATRIXA matWorld;
D3DXMatrixIdentity( &matWorld);
D3DXMatrixRotationX( &matWorld, timeGetTime() / .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);
D3DXMATRIXA matView;
D3DXMatrixIdentity( &matView);
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec);
d3ddev->SetTransform( D3DTS_VIEW, &matView);
D3DXMATRIXA matProj;
D3DXMatrixIdentity( &matProj);
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, .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 = .0f;
d3ddev->SetLight( 0, &light);
d3ddev->LightEnable( 0, true);
d3ddev->SetRenderState( D3DRS_AMBIENT, 0x);
}
void Render()
{
d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, ), 1.0f, 0);
d3ddev->BeginScene();
SetupMatrix();
SetupLights();
d3ddev->SetStreamSource( 0, d3dvb, 0, sizeof( CUSTOMVERTEX));
d3ddev->SetFVF( D3DFVF_CUSTOMVERTEX);
d3ddev->DrawPrimitive( D3DPT_TRIANGLELIST, 0, );
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, , , , , 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-12-24 19:47
2024-12-24 19:32
2024-12-24 19:31
2024-12-24 19:25
2024-12-24 19:14
2024-12-24 18:46
2024-12-24 18:46
2024-12-24 18:36