一 初始化:
//创建纹理对象
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L”shitoub01.jpg”, &g_pTexture ) ) )
{
MessageBox(NULL, L”创建纹理失败”, L”Texture.exe”, MB_OK);
return E_FAIL;
}
HRESULT hr;
hr = D3DXCreateTexture(g_pd3dDevice, 256,256,1,D3DUSAGE_RENDERTARGET|D3DUSAGE_AUTOGENMIPMAP, D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,&g_pTexture2);
D3DSURFACE_DESC desc;
g_pTexture2->GetLevelDesc(0,&desc);
hr = D3DXCreateRenderToSurface(g_pd3dDevice,desc.Width,desc.Height,desc.Format,true,D3DFMT_D24S8,&g_pRTS);
hr = g_pTexture2->GetSurfaceLevel(0,&g_pSurface);
二 使用:
//开始在后台缓冲区绘制图形
if( SUCCEEDED( g_pd3dDevice->BeginScene() ))
{
SetupWorld();
SetupMatrices();
D3DVIEWPORT9 vp = {0,0,256,256,0,1};
g_pd3dDevice->EndScene();
g_pRTS->BeginScene(g_pSurface, &vp);
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0 );
g_pd3dDevice->SetTexture(0, g_pTexture);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
D3DXSaveSurfaceToFileA(“tt.bmp”,D3DXIFF_BMP, g_pSurface, NULL, NULL);
g_pRTS->EndScene(D3DX_FILTER_NONE);
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0 );
g_pd3dDevice->BeginScene() ;
g_pd3dDevice->SetTexture(0, g_pTexture2);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
//结束在后台缓冲区绘制图形
g_pd3dDevice->EndScene();
}
三 程序:
=============================================================================
//
Desc: 纹理影射基础
//
=============================================================================
#include
<
d3dx9.h
>
//
—————————————————————————–
//
Desc: 全局变量
//
—————————————————————————–
LPDIRECT3D9 g_pD3D
=
NULL;
//
Direct3D对象
LPDIRECT3DDEVICE9 g_pd3dDevice
=
NULL;
//
Direct3D设备对象
HWND g_hWnd
=
NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVB
=
NULL;
//
顶点缓冲区对象
LPDIRECT3DTEXTURE9 g_pTexture
=
NULL;
//
纹理对象
LPDIRECT3DTEXTURE9 g_pTexture2
=
NULL;
//
纹理对象
ID3DXRenderToSurface
*
g_pRTS
=
NULL;
LPDIRECT3DSURFACE9 g_pSurface
=
NULL;
//
—————————————————————————–
//
Desc: 顶点结构
//
—————————————————————————–
struct
CUSTOMVERTEX
{
FLOAT x, y, z,w;
//
顶点位置
DWORD diffuse;
FLOAT u,v ;
//
顶点纹理坐标
};
#define
D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
void
SetupWorld()
{
//
创建并设置世界矩阵
D3DXMATRIXA16 matWorld;
D3DXMatrixIdentity(
&
matWorld );
g_pd3dDevice
->
SetTransform( D3DTS_WORLD,
&
matWorld );
}
//
—————————————————————————–
//
Desc: 设置变换矩阵
//
—————————————————————————–
VOID SetupMatrices()
{
//
创建并设置观察矩阵
D3DXVECTOR3 vEyePt(
0.0f
,
0.0f
,
–
5
);
D3DXVECTOR3 vLookatPt(
0.0f
,
0.0f
,
0.0f
);
D3DXVECTOR3 vUpVec(
0.0f
,
1.0f
,
0.0f
);
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH(
&
matView,
&
vEyePt,
&
vLookatPt,
&
vUpVec );
g_pd3dDevice
->
SetTransform( D3DTS_VIEW,
&
matView );
//
创建并设置投影矩阵
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH(
&
matProj, D3DX_PI
/
2
,
1.0f
,
1.0f
,
100.0f
);
g_pd3dDevice
->
SetTransform( D3DTS_PROJECTION,
&
matProj );
}
//
—————————————————————————–
//
Desc: 初始化Direct3D
//
—————————————————————————–
HRESULT InitD3D( HWND hWnd )
{
//
创建Direct3D对象, 该对象用于创建Direct3D设备对象
if
( NULL
==
( g_pD3D
=
Direct3DCreate9( D3D_SDK_VERSION ) ) )
return
E_FAIL;
//
设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(
&
d3dpp,
sizeof
(d3dpp) );
d3dpp.Windowed
=
TRUE;
d3dpp.SwapEffect
=
D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat
=
D3DFMT_X8R8G8B8;
d3dpp.EnableAutoDepthStencil
=
TRUE;
d3dpp.AutoDepthStencilFormat
=
D3DFMT_D24S8;
//
创建Direct3D设备对象
if
( FAILED( g_pD3D
->
CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&
d3dpp,
&
g_pd3dDevice ) ) )
{
return
E_FAIL;
}
//
禁用照明效果
g_pd3dDevice
->
SetRenderState( D3DRS_LIGHTING, FALSE );
//
设置变换矩阵
SetupWorld();
SetupMatrices();
return
S_OK;
}
//
—————————————————————————–
HRESULT InitGriphics()
{
//
创建纹理对象
if
( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, L
“
shitoub01.jpg
“
,
&
g_pTexture ) ) )
{
MessageBox(NULL, L
“
创建纹理失败
“
, L
“
Texture.exe
“
, MB_OK);
return
E_FAIL;
}
HRESULT hr;
hr
=
D3DXCreateTexture(g_pd3dDevice,
256
,
256
,
1
,D3DUSAGE_RENDERTARGET
|
D3DUSAGE_AUTOGENMIPMAP, D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,
&
g_pTexture2);
D3DSURFACE_DESC desc;
g_pTexture2
->
GetLevelDesc(
0
,
&
desc);
hr
=
D3DXCreateRenderToSurface(g_pd3dDevice,desc.Width,desc.Height,desc.Format,
true
,D3DFMT_D24S8,
&
g_pRTS);
hr
=
g_pTexture2
->
GetSurfaceLevel(
0
,
&
g_pSurface);
//
顶点数据
CUSTOMVERTEX g_Vertices[]
=
{
{
0
,
256
,
0.5f
,
1.0f
,
0xffffffff
,
0.0f
,
1.0f
},
{
0
,
0
,
0.5f
,
1.0f
,
0xffffffff
,
0.0f
,
0.0f
},
{
256
,
256
,
0.5f
,
1.0f
,
0xffffffff
,
1.0f
,
1.0f
},
{
256
,
0
,
0.5f
,
1.0f
,
0xffffffff
,
1.0f
,
0.0f
}
};
//
创建顶点缓冲区
if
( FAILED( g_pd3dDevice
->
CreateVertexBuffer(
4
*
sizeof
(CUSTOMVERTEX),
0
, D3DFVF_CUSTOMVERTEX,
D3DPOOL_MANAGED,
&
g_pVB,NULL ) ) )
{
return
E_FAIL;
}
//
填充顶点缓冲区
VOID
*
pVertices;
if
( FAILED( g_pVB
->
Lock(
0
,
sizeof
(g_Vertices), (
void
**
)
&
pVertices,
0
) ) )
return
E_FAIL;
memcpy( pVertices, g_Vertices,
sizeof
(g_Vertices) );
g_pVB
->
Unlock();
return
S_OK;
}
//
—————————————————————————–
//
Desc: 释放创建的对象
//
—————————————————————————–
VOID Cleanup()
{
//
释放Direct3D设备对象
if
( g_pd3dDevice
!=
NULL )
g_pd3dDevice
->
Release();
//
释放Direct3D对象
if
( g_pD3D
!=
NULL )
g_pD3D
->
Release();
}
//
—————————————————————————–
//
Desc: 渲染图形
//
—————————————————————————–
VOID Render()
{
//
清空后台缓冲区
//
开始在后台缓冲区绘制图形
//
if( SUCCEEDED( )
{
SetupWorld();
SetupMatrices();
D3DVIEWPORT9 vp
=
{
0
,
0
,
256
,
256
,
0
,
1
};
g_pRTS
->
BeginScene(g_pSurface,
&
vp);
g_pd3dDevice
->
Clear(
0
, NULL, D3DCLEAR_TARGET
|
D3DCLEAR_ZBUFFER
|
D3DCLEAR_STENCIL,D3DCOLOR_XRGB(
45
,
50
,
170
),
1.0f
,
0
);
g_pd3dDevice
->
SetTexture(
0
, g_pTexture);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
g_pd3dDevice
->
SetStreamSource(
0
, g_pVB,
0
,
sizeof
(CUSTOMVERTEX));
g_pd3dDevice
->
SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice
->
DrawPrimitive(D3DPT_TRIANGLESTRIP,
0
,
2
);
D3DXSaveSurfaceToFileA(
“
tt.bmp
“
,D3DXIFF_BMP, g_pSurface, NULL, NULL);
g_pRTS
->
EndScene(D3DX_FILTER_NONE);
g_pd3dDevice
->
Clear(
0
, NULL, D3DCLEAR_TARGET
|
D3DCLEAR_ZBUFFER
|
D3DCLEAR_STENCIL, D3DCOLOR_XRGB(
45
,
50
,
170
),
1.0f
,
0
);
g_pd3dDevice
->
BeginScene() ;
g_pd3dDevice
->
SetTexture(
0
, g_pTexture2);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_COLORARG1, D3DTA_TEXTURE);
g_pd3dDevice
->
SetTextureStageState(
0
, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
g_pd3dDevice
->
SetStreamSource(
0
, g_pVB,
0
,
sizeof
(CUSTOMVERTEX));
g_pd3dDevice
->
SetFVF(D3DFVF_CUSTOMVERTEX);
g_pd3dDevice
->
DrawPrimitive(D3DPT_TRIANGLESTRIP,
0
,
2
);
//
结束在后台缓冲区绘制图形
g_pd3dDevice
->
EndScene();
}
//
将在后台缓冲区绘制的图形提交到前台缓冲区显示
g_pd3dDevice
->
Present( NULL, NULL, NULL, NULL );
}
//
—————————————————————————–
//
Desc: 消息处理
//
—————————————————————————–
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch
( msg )
{
case
WM_DESTROY:
Cleanup();
PostQuitMessage(
0
);
return
0
;
}
return
DefWindowProc( hWnd, msg, wParam, lParam );
}
//
—————————————————————————–
//
Desc: 入口函数
//
—————————————————————————–
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
//
注册窗口类
WNDCLASSEX wc
=
{
sizeof
(WNDCLASSEX), CS_CLASSDC, MsgProc,
0L
,
0L
,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L
“
ClassName
“
, NULL };
RegisterClassEx(
&
wc );
//
创建窗口
HWND hWnd
=
CreateWindow( L
“
ClassName
“
, L
“
纹理影射基础
“
,
WS_OVERLAPPEDWINDOW,
200
,
100
,
800
,
600
,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
g_hWnd
=
hWnd;
//
初始化Direct3D
if
( SUCCEEDED( InitD3D( hWnd ) ) )
{
//
创建场景图形
if
( SUCCEEDED(InitGriphics()) )
{
//
显示窗口
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//
进入消息循环
MSG msg;
ZeroMemory(
&
msg,
sizeof
(msg) );
while
( msg.message
!=
WM_QUIT )
{
if
( PeekMessage(
&
msg, NULL,
0U
,
0U
, PM_REMOVE ) )
{
TranslateMessage(
&
msg );
DispatchMessage(
&
msg );
}
else
{
Render();
//
渲染图形
}
}
}
}
UnregisterClass( L
“
ClassName
“
, wc.hInstance );
return
0
;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/110646.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...