在Direct3D中,颜色用RGB三元组来表示,RGB数据可用两种不同的数据结构来保存,第一种是D3DCOLOR,它实际与DWORD完全相同,共有32位,D3DCOLOR类型中的各位被分成4个八位项同,每顶存放了一种颜色的分量值,
32bit
alpha red,green,blue
typedef DWORD D3DCOLOR
#define D3DCOLOR_ARGB(a,r,g,b) \
((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
#define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)
#define D3DCOLOR_XRGB(r,g,b) D3DCOLOR_ARGB(0xff,r,g,b)
//亮度,0-1
#define D3DCOLOR_COLORVALUE(r,g,b,a) \
32bit
alpha red,green,blue
typedef DWORD D3DCOLOR
#define D3DCOLOR_ARGB(a,r,g,b) \
((D3DCOLOR)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff)))
#define D3DCOLOR_RGBA(r,g,b,a) D3DCOLOR_ARGB(a,r,g,b)
#define D3DCOLOR_XRGB(r,g,b) D3DCOLOR_ARGB(0xff,r,g,b)
//亮度,0-1
#define D3DCOLOR_COLORVALUE(r,g,b,a) \
D3DCOLOR_RGBA((DWORD)((r)*255.f),(DWORD)((g)*255.f),(DWORD)((b)*255.f),(DWORD)((a)*255.f))
着色模式 看下面测试代码
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
IDirect3D9* g_pD3d = NULL;
IDirect3DDevice9* g_pDevice = NULL;
IDirect3DVertexBuffer9 * triangle= NULL;
struct ColorVertex{
float x,y,z;
D3DCOLOR clr;
static const DWORD FVF;
};
const DWORD ColorVertex::FVF = D3DFVF_DIFFUSE | D3DFVF_XYZ;
bool initd3d(HWND hwnd)
{
g_pD3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DDISPLAYMODE display;
ZeroMemory(&display,sizeof(display));
g_pD3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&display);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferFormat = display.Format;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferWidth = 800;
d3dpp.Windowed = TRUE;
d3dpp.hDeviceWindow = hwnd;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
g_pD3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&g_pDevice);
return true;
}
bool InitObject()
{
ColorVertex t[3]={
-1.0f,0.0f,2.0f,D3DCOLOR_XRGB(255,0,0),
0.0f,1.0f,2.0f,D3DCOLOR_XRGB(0,255,0),
1.0f,0.0f,2.0f,D3DCOLOR_XRGB(0,0,255),
};
g_pDevice->CreateVertexBuffer(3*sizeof(ColorVertex),
D3DUSAGE_WRITEONLY,
ColorVertex::FVF,
D3DPOOL_MANAGED,
&triangle,
0);
void *p = NULL;
triangle->Lock(0,sizeof(t),(void**)&p,0);
memcpy(p,t,sizeof(t));
triangle->Unlock();
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,
D3DX_PI*0.5f,
float(800)/float(600),
1.0f,
1000.0f);
g_pDevice->SetTransform(D3DTS_PROJECTION,&proj);
g_pDevice->SetRenderState(D3DRS_LIGHTING,false);
return true;
}
void RenderScence()
{
g_pDevice->Clear(0,NULL,D3DCLEAR_TARGET,0xffffffff,1.0f,0);
g_pDevice->BeginScene();
g_pDevice->SetStreamSource(0,triangle,0,sizeof(ColorVertex));
g_pDevice->SetFVF(ColorVertex::FVF);
//draw left
D3DXMATRIX world;
D3DXMatrixTranslation(&world,-1.25f,0.0f,0.0f);
g_pDevice->SetTransform(D3DTS_WORLD,&world);
g_pDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_FLAT);
g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
/draw right
D3DXMatrixTranslation(&world,1.25f,0.0f,0.0f);
g_pDevice->SetTransform(D3DTS_WORLD,&world);
g_pDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
g_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST,0,1);
g_pDevice->EndScene();
g_pDevice->Present(0,0,0,0);
}
void Shutdown()
{
if(g_pD3d)
{
g_pD3d->Release();
}
if(g_pDevice)
{
g_pDevice->Release();
}
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("COLOR");
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex = {0};
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = szAppName;
wndclassex.hIconSm = wndclassex.hIcon;
if (!RegisterClassEx (&wndclassex))
{
MessageBox (NULL, TEXT ("RegisterClassEx failed!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW,
szAppName,
TEXT ("WindowTitle"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
initd3d(hwnd);
InitObject();
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else
{
RenderScence();
}
}
Shutdown();
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage (0);
return (0);
}
return DefWindowProc (hwnd, message, wParam, lParam);
}