|
发表于 2004-5-9 18:20:00
|
显示全部楼层
Re:dx9中怎样读入一个位图?
//-----------------------------------------------------------------------------
// Desc: load texture map
//-----------------------------------------------------------------------------
#include <d3d9.h>
#include <d3dx9.h>
#include <stdio.h>
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
HWND g_hWnd;
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE| D3DFVF_TEX1)
struct CUSTOMVERTEX
{ FLOAT x, y, z, rhw; // 经过坐标转换的顶点坐标
DWORD color; // 顶点漫反射颜色值
FLOAT u,v ; //顶点纹理坐标
};
bool LoadBmpTexture24Bit(
LPDIRECT3DDEVICE9 pDevice,
LPCSTR pSrcFile,
LPDIRECT3DTEXTURE9* ppTexture);
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Get the current desktop display mode, so we can set up a back
// buffer of the same format
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Device state would normally be set here
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices.
//-----------------------------------------------------------------------------
HRESULT InitVB() // D3DXCreateTextureFromFile
{
if( !( LoadBmpTexture24Bit( g_pd3dDevice, "leaf2.bmp", &g_pTexture ) ) )
{
// If texture is not in current folder, try parent folder
return E_FAIL;
}
// Initialize three vertices for rendering a triangle
CUSTOMVERTEX g_Vertices[] =
{
{ 50.0f, 50.0f, 0.5f, 1.0f, 0xffffffff, 0.f, 0},
{ 250.0f , 50.0f, 0.5f, 1.0f, 0xffffffff, 1.0f, 0},
{ 50.0f , 250.0f, 0.5f, 1.0f, 0xffffffff, 0, 1.0f},
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xffffffff, 1.0f, 1.0f }
};
// Create the vertex buffer.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB,NULL ) ) )
{
return E_FAIL;
}
// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
// gain access to the vertices. This mechanism is required becuase vertex
// buffers may be in device memory.
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;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{ if( g_pTexture != NULL )
g_pTexture->Release();
if( g_pVB != NULL )
g_pVB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer to a blue color
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene
g_pd3dDevice->BeginScene();
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
// Draw the triangles in the vertex buffer. This is broken into a few
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
// End the scene
g_pd3dDevice->EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice-> resent( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
Render();
ValidateRect( hWnd, NULL );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
HINSTANCE g_Inst;
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
g_Inst=hInst;
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D Tutorial", NULL };
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial : loadbmp",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
g_hWnd=hWnd;
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the vertex buffer
if( SUCCEEDED( InitVB() ) )
{
// 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 );
}
}
}
}
// Clean up everything and exit the app
Cleanup();
UnregisterClass( "D3D Tutorial", wc.hInstance );
return 0;
}
bool LoadBmpTexture24Bit (LPDIRECT3DDEVICE9 pDevice,
LPCSTR pSrcFile,
LPDIRECT3DTEXTURE9* ppTexture)
{
FILE * fp;
if (!(fp=fopen (pSrcFile,"rb")))
return 0;
BITMAPFILEHEADER bmfh;
fread(&bmfh, sizeof(BITMAPFILEHEADER),1,fp);
if (bmfh.bfType!=0x4d42)
return 0;
BITMAPINFOHEADER bmih;
fread(&bmih, sizeof(BITMAPINFOHEADER), 1,fp);
if (bmih.biCompression||bmih.biBitCount!=24)
return 0;
fseek(fp,bmfh.bfOffBits,0);
char * bmpdata=new char [bmih.biWidth*bmih.biHeight*3];
if (!bmpdata)
return 0;
fread(bmpdata,bmih.biWidth*bmih.biHeight*3,1,fp);
fclose(fp);
bool res=0;
if (pDevice->CreateTexture (bmih.biWidth,bmih.biHeight,
1,0,D3DFMT_X8R8G8B8 ,
D3DPOOL_MANAGED,ppTexture,0)==D3D_OK)
{
res=1;
D3DLOCKED_RECT stLockedRect;
(*ppTexture)->LockRect( 0, &stLockedRect, 0, 0 );
char *pch=(char *)stLockedRect.pBits;
for (int i=bmih.biHeight-1;i>=0;i--)
{
for (int j=0;j<bmih.biWidth;j++)
{
pch[0]=bmpdata[i*bmih.biWidth*3+j*3+0];
pch[1]=bmpdata[i*bmih.biWidth*3+j*3+1];
pch[2]=bmpdata[i*bmih.biWidth*3+j*3+2];
pch[3]=0xff;
pch+=4;
}
}
(*ppTexture)->UnlockRect(0);
}
delete bmpdata;
return res;
} |
|