Yui的狗窝
Yui的狗窝
使用directx9的光照功能

来自龙书上的例程,通过使用光照功能,来让“金字塔”显示出颜色。
使用方向光的例程。

//Source.cpp
#include<windows.h>
#include"header1.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void InitWindow(HINSTANCE, int);
bool ddd;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow)
{
    InitWindow(hInstance, cmdShow);
    Setup(800, 600);
    MSG msg = { 0 };
    while (msg.message!=WM_QUIT)
    {
        ddd=Display(0.001);
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    Del();
    return msg.wParam;
}
void InitWindow(HINSTANCE hInstance, int cmdShow)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(wc);
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"light";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClassEx(&wc);
    HWND hWnd = CreateWindowEx(NULL, L"light", L"light", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
        800, 600, NULL, NULL, hInstance, NULL);
    InitDevice(hWnd);
    ShowWindow(hWnd, cmdShow);
    UpdateWindow(hWnd);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch (message)
    {
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE)
            DestroyWindow(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
</code>
<code lang="cpp">
#pragma once
#include<d3d9.h>
#include<d3dx9.h>
//使用光照前修改数据结构成员
struct Vertex
{
    float x, y, z;
    float nx, ny, nz;//顶点法向量
    Vertex(float a, float b, float c, float na, float nb, float nc) :x(a), y(b), z(c), nx(na), ny(nb), nz(nc)
    {
    }
};
IDirect3D9              *pd3d;
IDirect3DDevice9        *pd3dDevice;
IDirect3DVertexBuffer9  *vb;
D3DCAPS9                caps;
int                     iUseProcess;
const D3DXCOLOR         WHITE = D3DCOLOR_XRGB(255, 255, 255);
const D3DXCOLOR         BLACK = D3DCOLOR_XRGB(0, 0, 0);
const static DWORD      FVF = D3DFVF_XYZ | D3DFVF_NORMAL;//顶点格式修改
bool InitDevice(HWND hWnd)
{
    pd3d = Direct3DCreate9(D3D_SDK_VERSION);
    pd3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
    if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
        iUseProcess = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
        iUseProcess = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferHeight = 600;
    d3dpp.BackBufferWidth = 800;
    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;
    if (FAILED(pd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, iUseProcess, &d3dpp, &pd3dDevice)))
        return false;
    return true;
}
void Del()
{
    pd3dDevice->Release();
    vb->Release();
}
void Setup(float width, float height)
{
    pd3dDevice->SetRenderState(D3DRS_LIGHTING, true);//显式启用光照渲染模式
    pd3dDevice->CreateVertexBuffer(sizeof(Vertex) * 12, D3DUSAGE_WRITEONLY, FVF, D3DPOOL_MANAGED, &vb, 0);
    Vertex *v;
    vb->Lock(0, 0, (void**)&v, 0);
    v[0] = Vertex(-1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
    v[1] = Vertex(0.0f, 1.0f, 0.0f, 0.0f, 0.707f, -0.707f);
    v[2] = Vertex(1.0f, 0.0f, -1.0f, 0.0f, 0.707f, -0.707f);
    v[3] = Vertex(-1.0f, 0.0f, 1.0f, -0.707f, 0.707f, 0.0f);
    v[4] = Vertex(0.0f, 1.0f, 0.0f, -0.707f, 0.707f, 0.0f);
    v[5] = Vertex(-1.0f, 0.0f, -1.0f, -0.707f, 0.707f, 0.0f);
    v[6] = Vertex(1.0f, 0.0f, -1.0f, 0.707f, 0.707f, 0.0f);
    v[7] = Vertex(0.0f, 1.0f, 0.0f, 0.707f, 0.707f, 0.0f);
    v[8] = Vertex(1.0f, 0.0f, 1.0f, 0.707f, 0.707f, 0.0f);
    v[9] = Vertex(1.0f, 0.0f, 1.0f, 0.0f, 0.707f, 0.707f);
    v[10] = Vertex(0.0f, 1.0f, 0.0f, 0.0f, 0.707f, 0.707f);
    v[11] = Vertex(-1.0f, 0.0f, 1.0f, 0.0f, 0.707f, 0.707f);
    vb->Unlock();
    D3DMATERIAL9 mtrl;//创建材质,填充各结构字段
    mtrl.Ambient = WHITE;
    mtrl.Diffuse = WHITE;
    mtrl.Specular = WHITE;
    mtrl.Emissive = BLACK;
    mtrl.Power = 5.f;
    pd3dDevice->SetMaterial(&mtrl);//使用材质
    D3DLIGHT9 dir;//创建方向光
    ::ZeroMemory(&dir, sizeof(dir));
    dir.Type = D3DLIGHT_DIRECTIONAL;
    dir.Diffuse = WHITE;
    dir.Specular = WHITE * 0.3f;
    dir.Ambient = WHITE * 0.6f;
    dir.Direction = D3DXVECTOR3(1.f, 0.f, 0.f);
    pd3dDevice->SetLight(0, &dir);
    pd3dDevice->LightEnable(0, true);//使用方向光
    pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
    pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
    D3DXVECTOR3 eye{ 0.f,1.f,-3.f };
    D3DXVECTOR3 at{ 0.f,0.f,0.f };
    D3DXVECTOR3 up{ 0.f,1.f,0.f };
    D3DXMATRIX cam;
    D3DXMatrixLookAtLH(&cam, &eye, &at, &up);
    pd3dDevice->SetTransform(D3DTS_VIEW, &cam);
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, width / height, 1.f, 1000.f);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
}
bool Display(float timeDelta)
{
    if (pd3dDevice)
    {
        D3DXMATRIX ry;
        static float y = 0.f;
        D3DXMatrixRotationY(&ry, y);
        y += timeDelta;
        if (y >= 6.28)
            y = 0;
        pd3dDevice->SetTransform(D3DTS_WORLD, &ry);
        pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.f, 0);
        pd3dDevice->BeginScene();
        pd3dDevice->SetStreamSource(0, vb, 0, sizeof(Vertex));
        pd3dDevice->SetFVF(FVF);
        pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);//绘制4个三角形
        pd3dDevice->EndScene();
        pd3dDevice->Present(0, 0, 0, 0);
        return true;
    }
    return false;
}
</code>
使用点光源,绘制正方体,让正方体表面呈现红色。
<code lang="cpp">
//header.h
#pragma once
#include<d3d9.h>
#include<d3dx9.h>
struct Vertex
{
    float x, y, z;
    float nx, ny, nz;
    Vertex(float a, float b, float c, float na, float nb, float nc) :x(a), y(b), z(c), nx(na), ny(nb), nz(nc)
    {
    }
};
const D3DXCOLOR         WHITE = D3DCOLOR_XRGB(255, 255, 255);
const D3DXCOLOR         BLACK = D3DCOLOR_XRGB(0, 0, 0);
const D3DXCOLOR         PINK = D3DCOLOR_XRGB(255, 171, 171);
const D3DXCOLOR         RED = D3DCOLOR_XRGB(255, 0, 0);
IDirect3D9              *pd3d;
IDirect3DDevice9        *pd3dDevice;
IDirect3DVertexBuffer9  *vb;
D3DCAPS9                caps;
int                     iUseProcess;
const WORD              FVF = D3DFVF_XYZ | D3DFVF_NORMAL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
bool InitD3d(HINSTANCE hInstance,int cmdShow)
{
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"light2";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wc);
    HWND hWnd = CreateWindow(L"light2", L"   light2", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, cmdShow);
    UpdateWindow(hWnd);
    pd3d = Direct3DCreate9(D3D_SDK_VERSION);
    pd3d->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
    if (caps.Caps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
        iUseProcess = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
        iUseProcess = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferHeight = 600;
    d3dpp.BackBufferWidth = 800;
    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;
    if (FAILED(pd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, iUseProcess, &d3dpp, &pd3dDevice)))
        return false;
    return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE)
            DestroyWindow(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProcA(hWnd, message, wParam, lParam);
}
void rel()
{
    vb->Release();
    pd3d->Release();
    pd3dDevice->Release();
}
void Setup(float width,float height)
{
    if (pd3dDevice->CreateVertexBuffer(sizeof(Vertex) * 36, D3DUSAGE_WRITEONLY, FVF, D3DPOOL_MANAGED, &vb, 0))
        MessageBox(NULL, L"dd", 0, 0);
    Vertex *v;
    vb->Lock(0, 0, (void**)&v, 0);
    //back
    v[0] = Vertex{ -1.f, -1.f, -1.f,  0.f,  0.f, -1.f };
    v[1] = Vertex{ -1.f,  1.f, -1.f,  0.f,  0.f, -1.f };
    v[2] = Vertex{  1.f,  1.f, -1.f,  0.f,  0.f, -1.f };
    v[3] = Vertex{ -1.f, -1.f, -1.f,  0.f,  0.f, -1.f };
    v[4] = Vertex{  1.f,  1.f, -1.f,  0.f,  0.f, -1.f };
    v[5] = Vertex{  1.f, -1.f, -1.f,  0.f,  0.f, -1.f };
    //left
    v[6] = Vertex{ -1.f, -1.f, -1.f, -1.f,  0.f,  0.f };
    v[7] = Vertex{ -1.f,  1.f,  1.f, -1.f,  0.f,  0.f };
    v[8] = Vertex{ -1.f,  1.f, -1.f, -1.f,  0.f,  0.f };
    v[9] = Vertex{ -1.f,  1.f,  1.f, -1.f,  0.f,  0.f };
    v[10] = Vertex{-1.f, -1.f, -1.f, -1.f,  0.f,  0.f };
    v[11] = Vertex{-1.f, -1.f,  1.f, -1.f,  0.f,  0.f };
    //right
    v[12] = Vertex{  1.f, -1.f, -1.f,  1.f, 0.f,  0.f };
    v[13] = Vertex{  1.f,  1.f, -1.f,  1.f, 0.f,  0.f };
    v[14] = Vertex{  1.f,  1.f,  1.f,  1.f, 0.f,  0.f };
    v[15] = Vertex{  1.f,  1.f,  1.f,  1.f, 0.f,  0.f };
    v[16] = Vertex{  1.f, -1.f,  1.f,  1.f, 0.f,  0.f };
    v[17] = Vertex{  1.f, -1.f, -1.f,  1.f, 0.f,  0.f };
    //front
    v[18] = Vertex{  1.f, -1.f,  1.f,  0.f,  0.f,  1.f };
    v[19] = Vertex{  1.f,  1.f,  1.f,  0.f,  0.f,  1.f };
    v[20] = Vertex{ -1.f,  1.f,  1.f,  0.f,  0.f,  1.f };
    v[21] = Vertex{ -1.f,  1.f,  1.f,  0.f,  0.f,  1.f };
    v[22] = Vertex{ -1.f, -1.f,  1.f,  0.f,  0.f,  1.f };
    v[23] = Vertex{  1.f, -1.f,  1.f,  0.f,  0.f,  1.f };
    //top
    v[24] = Vertex{ -1.f,  1.f, -1.f,  0.f,  1.f,  0.f };
    v[25] = Vertex{  1.f,  1.f,  1.f,  0.f,  1.f,  0.f };
    v[26] = Vertex{  1.f,  1.f, -1.f,  0.f,  1.f,  0.f };
    v[27] = Vertex{  1.f,  1.f,  1.f,  0.f,  1.f,  0.f };
    v[28] = Vertex{ -1.f,  1.f, -1.f,  0.f,  1.f,  0.f };
    v[29] = Vertex{ -1.f,  1.f,  1.f,  0.f,  1.f,  0.f };
    //bottom
    v[30] = Vertex{ -1.f, -1.f, -1.f,  0.f, -1.f,  0.f };
    v[31] = Vertex{  1.f, -1.f, -1.f,  0.f, -1.f,  0.f };
    v[32] = Vertex{  1.f, -1.f,  1.f,  0.f, -1.f,  0.f };
    v[33] = Vertex{  1.f, -1.f,  1.f,  0.f, -1.f,  0.f };
    v[34] = Vertex{ -1.f, -1.f,  1.f,  0.f, -1.f,  0.f };
    v[35] = Vertex{ -1.f, -1.f, -1.f,  0.f, -1.f,  0.f };
    vb->Unlock();
    D3DMATERIAL9 mtrl;
    mtrl.Ambient = WHITE;
    mtrl.Diffuse = WHITE;
    mtrl.Specular = WHITE;
    mtrl.Emissive = BLACK;
    mtrl.Power = 5.f;
    pd3dDevice->SetMaterial(&mtrl);
    D3DLIGHT9 light;
    ::ZeroMemory(&light, sizeof(light));
    light.Type = D3DLIGHT_POINT;
    light.Diffuse = RED;
    light.Ambient = RED * 0.6f;
    light.Specular = RED * 0.3f;
    light.Range = 5.f;
    light.Position = D3DXVECTOR3(0.f, 0.f, 0.f);
    pd3dDevice->SetLight(0, &light);
    pd3dDevice->LightEnable(0, true);
    pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
    pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
    D3DXVECTOR3 eye{ 0.f,0.f,-5.f };
    D3DXVECTOR3 target{ 0.f,0.f,0.f };
    D3DXVECTOR3 up{ 0.f,1.f,0.f };
    D3DXMATRIX cam;
    D3DXMatrixLookAtLH(&cam, &eye, &target, &up);
    pd3dDevice->SetTransform(D3DTS_VIEW, &cam);
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, width / height, 1.f, 1000.f);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
}
void Display(float timeDelta)
{
    if (pd3dDevice)
    {
        D3DXMATRIX ry;
        static float y = 0;
        D3DXMatrixRotationY(&ry, y);
        y += timeDelta;
        if (y >= 6.28f)
            y = 0;
        pd3dDevice->SetTransform(D3DTS_WORLD, &ry);
        pd3dDevice->BeginScene();
        pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.f, 0);
        pd3dDevice->SetFVF(FVF);
        pd3dDevice->SetStreamSource(0, vb, 0, sizeof(Vertex));
        pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 12);
        pd3dDevice->EndScene();
        pd3dDevice->Present(0, 0, 0, 0);
    }
}
//source.cpp
#include"header.h"
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow)
{
    InitD3d(hInstance, cmdShow);
    Setup(800, 600);
    MSG msg = { 0 };
    while (msg.message != WM_QUIT)
    {
        Display(0.0001f);
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    rel();
    return msg.wParam;
}
没有标签
首页      读书笔记      directx龙书的学习笔记      使用directx9的光照功能
https://secure.gravatar.com/avatar/d0fe7f1b17d5e9122db921e3a8cc327f?s=256&d=mm&r=g

Suzumiya, Yui

文章作者

发表评论

textsms
account_circle
email

Yui的狗窝

使用directx9的光照功能
来自龙书上的例程,通过使用光照功能,来让“金字塔”显示出颜色。 使用方向光的例程。 //Source.cpp #include<windows.h> #include"header1.h" LRESULT CALLBACK WndProc…
扫描二维码继续阅读
2018-04-18


没有激活的小工具