19. 多纹理贴图

多纹理贴图的基本要素就是使用多个纹理。创建和加载多个Direct3D纹理对象即可实现。当使用硬件多纹理渲染纹理时,要为每个顶点指定两套纹理坐标。这只需在顶点结构中另外添加两个浮点成员变量即可完成,即在FVF中用D3DFVF_TEX2替换D3DFVF_TEX。

code:

#include<d3d9.h>
#include
<d3dx9.h>

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "D3D Multi-Texture Mapping"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480

// Function Prototypes...
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();


// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice
= NULL;

// Matrices.
D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;

// Vertex buffer to hold the geometry.
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

// Holds a texture image.
LPDIRECT3DTEXTURE9 g_Texture = NULL;
LPDIRECT3DTEXTURE9 g_Texture2
= NULL;

// A structure for our custom vertex type
struct stD3DVertex
{
float x, y, z;
unsigned
long color;
float tu, tv;
float tu2, tv2;
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2)


LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(
0);
return 0;
break;

case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(
&wc);

// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, WINDOW_WIDTH, WINDOW_HEIGHT,
GetDesktopWindow(), NULL, wc.hInstance, NULL);

// Initialize Direct3D
if(InitializeD3D(hWnd, false))
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

// Enter the message loop
MSG msg;
ZeroMemory(
&msg, sizeof(msg));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(
&msg);
DispatchMessage(
&msg);
}
else
RenderScene();
}
}

// Release any and all resources.
Shutdown();

// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;

// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;

// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
return false;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(
&d3dpp, sizeof(d3dpp));

if(fullscreen)
{
d3dpp.Windowed
= FALSE;
d3dpp.BackBufferWidth
= WINDOW_WIDTH;
d3dpp.BackBufferHeight
= WINDOW_HEIGHT;
}
else
d3dpp.Windowed
= TRUE;
d3dpp.SwapEffect
= D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat
= displayMode.Format;

// Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_D3DDevice)))
{
return false;
}

// Initialize any objects we will be displaying.
if(!InitializeObjects()) return false;

return true;
}


bool InitializeObjects()
{
// Fill in our structure to draw an object.
// x, y, z, color, texture coords.
stD3DVertex objData[] =
{
{
-0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f, 0.0f, 1.0f},
{
0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f, 1.0f, 1.0f},
{
0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f, 1.0f, 0.0f},

{
0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f, 1.0f, 0.0f},
{
-0.3f, 0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f, 0.0f, 0.0f},
{
-0.3f, -0.4f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f, 0.0f, 1.0f}
};

// Create the vertex buffer.
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT,
&g_VertexBuffer, NULL))) return false;

// Fill the vertex buffer.
void *ptr;

if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
(
void**)&ptr, 0))) return false;

memcpy(ptr, objData,
sizeof(objData));

g_VertexBuffer
->Unlock();


// Load the texture image from file.
if(D3DXCreateTextureFromFile(g_D3DDevice, "Image1.bmp",
&g_Texture) != D3D_OK) return false;

if(D3DXCreateTextureFromFile(g_D3DDevice, "Image2.bmp",
&g_Texture2) != D3D_OK) return false;


// Set default rendering states.
g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_D3DDevice
->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);


// Set the projection matrix.
D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
WINDOW_WIDTH
/WINDOW_HEIGHT, 0.1f, 1000.0f);

g_D3DDevice
->SetTransform(D3DTS_PROJECTION, &g_projection);


// Define camera information.
D3DXVECTOR3 cameraPos(0.0f, 0.0f, -1.0f);
D3DXVECTOR3 lookAtPos(
0.0f, 0.0f, 0.0f);
D3DXVECTOR3 upDir(
0.0f, 1.0f, 0.0f);

// Build view matrix.
D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
&lookAtPos, &upDir);

return true;
}


void RenderScene()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(
0,0,0), 1.0f, 0);

// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();

// Apply the view (camera).
g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

// Set the texture stages for the first texture unit (image).
g_D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_D3DDevice
->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

//设置索引为0的Texture的Stage的颜色混合操作(RGB混合)为MODULATE(各颜色分量相乘)
//COLORARG1与COLORARG2分别指代该Texture Stage要混合的两个纹理(或一个纹理和一个顶点集)
//的颜色部分(RGB)
//该纹理使用纹理坐标的索引设置为0(共有2套纹理坐标)
g_D3DDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
//纹理的颜色混合方式设置为D3DTOP_MODULATE(将参数的每个成分相乘)
g_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
//颜色混合的第一个参数设置为D3DTA_TEXTURE(D3DTA_TEXTURE就是取该纹理本身的颜色)
g_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
//颜色混合的第二个参数设置为D3DTA_DIFFUSE是取按顶点间插值计算出的散射光颜色
g_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);

// Set the texture stages for the second texture unit (image).
g_D3DDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_D3DDevice
->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

g_D3DDevice
->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
g_D3DDevice
->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
g_D3DDevice
->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
// D3DTA_CURRENT是取上一个Texture Stage计算出的颜色值
g_D3DDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

// Draw square.
g_D3DDevice->SetTexture(0, g_Texture);
g_D3DDevice
->SetTexture(1, g_Texture2);
g_D3DDevice
->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
g_D3DDevice
->SetFVF(D3DFVF_VERTEX);
g_D3DDevice
->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

// End the scene. Stop rendering.
g_D3DDevice->EndScene();

// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
g_D3DDevice
= NULL;

if(g_D3D != NULL) g_D3D->Release();
g_D3D
= NULL;

if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
g_VertexBuffer
= NULL;

if(g_Texture != NULL) g_Texture->Release();
g_Texture
= NULL;

if(g_Texture2 != NULL) g_Texture2->Release();
g_Texture2
= NULL;
}

/*
HRESULT SetTextureStageState(
DWORD Stage, // //当前设置的多级纹理的索引(指第几层纹理)0,1,2,3,4,5,6,7
D3DTEXTURESTAGESTATETYPE Type, // 纹理渲染状态的类型(所设置的纹理阶段),定义对该纹理的哪个属进行设置,值很多。
DWORD Value //设置阶段的标识符,指的是前面所选属性的值
);

*/

  

  

  InitializeObjects()函数首先创建了一个对象,它指定了顶点位置、颜色和两套纹理坐标。当谈到硬件多纹理贴图时,即使这两套纹理坐标的值相同,也必须指定。使用两套纹理坐标可以为每个顶点使用不同的坐标。这是希望出现的特征,尤其是在采用如光照映射这样的使用不同纹理坐标的技术时,更是如此。

        对象被创建和放置在顶点缓存中以后,加载了两个纹理对象。同样可以使用在第一个纹理映射演示程序中的方式实现多纹理贴图,而这一次是在两个纹理对象而不是一个纹理对象上使用纹理贴图。加载纹理文件后,就采用以前的方式调用该函数。

RenderScene()函数也用于清屏、启动新场景和设置视图矩阵。之后程序调用SetTextureStageState()函数为第一个纹理设置多纹理状态。这里的Stage参数是要处理的纹理索引,Type是所设置的纹理阶段,Value是设置阶段的标识符。在谈到纹理索引时,第一个纹理单元为0,第二个为1,依次类推。

HRESULT SetTextureStageState(
DWORD Stage,
// //当前设置的多级纹理的索引(指第几层纹理)0,1,2,3,4,5,6,7
D3DTEXTURESTAGESTATETYPE Type, // 纹理渲染状态的类型(所设置的纹理阶段),定义对该纹理的哪个属进行设置,值很多。
DWORD Value //设置阶段的标识符,指的是前面所选属性的值
);

  为了给第一个纹理单元设置多纹理贴图,先要将D3DTSS_TEXCOORDINDEX的纹理索引设为0,这样Direct3D就会知道正在处理第一个索引。接下来告诉Direct3D通过将D3DTSS_COLORARG2分别设置参数1和参数2。方法是使用D3DTA_TEXTURE将纹理发送给参数1,使用D3DTA_DIFFUSE将对象颜色发送给参数2。

        所要做的全部工作就是设置第一个纹理单元。以完全相同的方式处理第二个纹理,但是调整纹理时所用的第二个参数,就是用第二个纹理调整第一个纹理的结果。这样可以如期望的那样用第一个纹理和顶点颜色混合第二个纹理。

        设置纹理阶段之后,使用SetTexture()函数绑定这两个纹理。如所知道的一样,SetTexture()函数的参数包括纹理单位以及要设置的纹理对象。这之后,对程序的其余内容像以往一样进行处理。

你可能感兴趣的:(图)