Yui的狗窝
Yui的狗窝
directx9的纹理功能使用

通过使用“纹理”功能,来达到绘制一个“木箱”的目的。

//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;
}
//header.h
#pragma once
#include<d3d9.h>
#include<d3dx9.h>
struct Vertex
{
    float x,y,z;
    float nx, ny, nz;
    float u, v;
    Vertex(float a, float b, float c, float na, float nb, float nc, float nd, float ne) :x(a), y(b), z(c),
        nx(na), ny(nb), nz(nc), u(nd), v(ne)
    {
    }
};
const WORD              FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
const D3DXCOLOR         WHITE = D3DCOLOR_XRGB(255, 255, 255);
const D3DXCOLOR         BLACK = D3DCOLOR_XRGB(0, 0, 0);
IDirect3D9              *pd3d;
IDirect3DDevice9        *pd3dDevice;
IDirect3DVertexBuffer9  *vb;
IDirect3DIndexBuffer9   *ib;
IDirect3DTexture9       *tex;
D3DCAPS9                caps;
int                     iUseProcess;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wPram, 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"texture";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wc);
    HWND hWnd = CreateWindow(L"texture", TEXT("texture"), 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;
}
void Setup(float width, float height)
{
    pd3dDevice->CreateVertexBuffer(sizeof(Vertex) * 24, D3DUSAGE_WRITEONLY, FVF, D3DPOOL_MANAGED, &vb, NULL);
    pd3dDevice->CreateIndexBuffer(sizeof(WORD) * 36, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &ib, NULL);
    Vertex *v;
    vb->Lock(0, 0, (void**)&v, 0);
    //front
    v[0] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
    v[1] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
    v[2] = Vertex(1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
    v[3] = Vertex(1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
    //back
    v[4] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
    v[5] = Vertex(1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f);
    v[6] = Vertex(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
    v[7] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f);
    //top
    v[8] = Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f);
    v[9] = Vertex(-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f);
    v[10] = Vertex(1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f);
    v[11] = Vertex(1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f);
    //bottom
    v[12] = Vertex(-1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f);
    v[13] = Vertex(1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f);
    v[14] = Vertex(1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f);
    v[15] = Vertex(-1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
    //left
    v[16] = Vertex(-1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
    v[17] = Vertex(-1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    v[18] = Vertex(-1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
    v[19] = Vertex(-1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    //right
    v[20] = Vertex(1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f);
    v[21] = Vertex(1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    v[22] = Vertex(1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f);
    v[23] = Vertex(1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f);
    vb->Unlock();
    WORD *i;
    ib->Lock(0, 0, (void**)&i, 0);
    //front
    i[0] = 0; i[1] = 1; i[2] = 2;
    i[3] = 0; i[4] = 2; i[5] = 3;
    //back
    i[6] = 4; i[7] = 5; i[8] = 6;
    i[9] = 4; i[10] = 6; i[11] = 7;
    //top
    i[12] = 8; i[13] = 9; i[14] = 10;
    i[15] = 8; i[16] = 10; i[17] = 11;
    //bottom
    i[18] = 12; i[19] = 13; i[20] = 14;
    i[21] = 12; i[22] = 14; i[23] = 15;
    //left
    i[24] = 16; i[25] = 17; i[26] = 18;
    i[27] = 16; i[28] = 18; i[29] = 19;
    //right
    i[30] = 20; i[31] = 21; i[32] = 22;
    i[33] = 20; i[34] = 22; i[35] = 23;
    ib->Unlock();
    D3DLIGHT9 light;
    ::ZeroMemory(&light, sizeof(light));
    light.Type = D3DLIGHT_DIRECTIONAL;
    light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f);
    light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
    light.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
    light.Direction = D3DXVECTOR3(1.f, -1.f, 0.f);
    pd3dDevice->SetLight(0, &light);
    pd3dDevice->LightEnable(0, true);
    pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS,true);
    pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE,true);
    D3DMATERIAL9 mtrl;
    mtrl.Ambient = WHITE;
    mtrl.Diffuse = WHITE;
    mtrl.Specular = WHITE;
    mtrl.Emissive = BLACK;
    mtrl.Power = 2.f;
    pd3dDevice->SetMaterial(&mtrl);
    D3DXCreateTextureFromFile(pd3dDevice, L"wood.jpg", &tex);//创建纹理,调整采样模式,并使用
    pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    pd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
    pd3dDevice->SetTexture(0, tex);
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI*0.5f, width / height, 1.f, 1000.f);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
}
void Rel()
{
    vb->Release();
    pd3dDevice->Release();
    pd3d->Release();
}
void Display(float timeDelta)
{
    if (pd3dDevice)
    {
        static float angle = (3.f*D3DX_PI) / 2.f;
        static float height = 2.f;//使用方向键控制摄像机的视角
        if (::GetAsyncKeyState(VK_UP) & 0x8000f)
            height += 5.f*timeDelta;
        if (::GetAsyncKeyState(VK_DOWN) & 0x8000f)
            height -= 5.f*timeDelta;
        if (::GetAsyncKeyState(VK_LEFT) & 0x8000f)
            angle -= 5.f*timeDelta;
        if (::GetAsyncKeyState(VK_RIGHT) & 0x8000f)
            angle += 5.f*timeDelta;
        D3DXVECTOR3 eye(cosf(angle) * 3.0f, height, sinf(angle) * 3.0f);
        D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
        D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
        D3DXMATRIX v;
        D3DXMatrixLookAtLH(&v, &eye, &target, &up);
        pd3dDevice->SetTransform(D3DTS_VIEW, &v);
        pd3dDevice->BeginScene();
        pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.f, 0);
        pd3dDevice->SetStreamSource(0, vb, 0, sizeof(Vertex));
        pd3dDevice->SetFVF(FVF);
        pd3dDevice->SetIndices(ib);
        pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12);
        pd3dDevice->EndScene();
        pd3dDevice->Present(0, 0, 0, 0);
    }
}
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);
}

使用到的纹理素材文件。

http://www.yuihouse.moe:8080/wp-content/uploads/2018/04/crate.jpg

没有标签
首页      读书笔记      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"header.h" int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, i…
扫描二维码继续阅读
2018-04-20


没有激活的小工具