📄 code_temp.txt
字号:
D3D::~D3D()
{
CleanUp();
}
HRESULT D3D::Init(int bool_fullscreen , HWND hwnd)
{
CleanUp();
hWnd = hwnd;
GetClientRect( hWnd, &scr_rect );
ClientToScreen( hWnd, (POINT*)&scr_rect.left );
ClientToScreen( hWnd, (POINT*)&scr_rect.right );
scr_width = scr_rect.right - scr_rect.left;
scr_height = scr_rect.bottom - scr_rect.top;
HRESULT hr;
hr = DirectDrawCreateEx( NULL , (VOID**)&g_pDD, IID_IDirectDraw7,
NULL );
if( FAILED( hr ) ) return hr;
hr = g_pDD->SetCooperativeLevel( hWnd, DDSCL_NORMAL );
if( FAILED( hr ) ) return hr;
DDSURFACEDESC2 ddsd;
ZeroMemory( &ddsd, sizeof(DDSURFACEDESC2) );
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
// Create the primary surface.
hr = g_pDD->CreateSurface( &ddsd, &g_pddsPrimary, NULL );
if( FAILED( hr ) )
return hr;
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
// Set the dimensions of the backbuffer. Note that if our window changes
// size, we need to destroy this surface and create a new one.
ddsd.dwWidth = scr_width;
ddsd.dwHeight = scr_height;
// Create the backbuffer. The most likely reason for failure is running
// out of video memory. (A more sophisticated app should handle this.)
hr = g_pDD->CreateSurface( &ddsd, &g_pddsBackBuffer, NULL );
if( FAILED( hr ) )
return hr;
LPDIRECTDRAWCLIPPER pcClipper;
hr = g_pDD->CreateClipper( 0, &pcClipper, NULL );
if( FAILED( hr ) )
return hr;
pcClipper->SetHWnd( 0, hWnd );
g_pddsPrimary->SetClipper( pcClipper );
pcClipper->Release();
// Query DirectDraw for access to Direct3D
g_pDD->QueryInterface( IID_IDirect3D7, (VOID**)&g_pD3D );
if( FAILED( hr) )
return hr;
// Enumerate the various z-buffer formats, finding a DDPIXELFORMAT
// to use to create the z-buffer surface.
DDPIXELFORMAT ddpfZBuffer;
g_pD3D->EnumZBufferFormats( IID_IDirect3DHALDevice ,
EnumZBufferCallback, (VOID*)&ddpfZBuffer );
char mm[4];
itoa(ddpfZBuffer.dwZBufferBitDepth , mm , 10);
//MessageBox(NULL , mm , "Z Format" , 0);
// If we found a good zbuffer format, then the dwSize field will be
// properly set during enumeration. Else, we have a problem and will exit.
if( sizeof(DDPIXELFORMAT) != ddpfZBuffer.dwSize )
return E_FAIL;
// Get z-buffer dimensions from the render target
// Setup the surface desc for the z-buffer.
ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
ddsd.dwWidth = scr_width;
ddsd.dwHeight = scr_height;
memcpy( &ddsd.ddpfPixelFormat, &ddpfZBuffer, sizeof(DDPIXELFORMAT) );
// For hardware devices, the z-buffer should be in video memory. For
// software devices, create the z-buffer in system memory
if( IsEqualIID( IID_IDirect3DHALDevice , IID_IDirect3DHALDevice ) )
ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
else
ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
// Create and attach a z-buffer. Real apps should be able to handle an
// error here (DDERR_OUTOFVIDEOMEMORY may be encountered). For this
// tutorial, though, we are simply going to exit ungracefully.
if( FAILED( hr = g_pDD->CreateSurface( &ddsd, &g_pddsZBuffer, NULL ) ) )
return hr;
// Attach the z-buffer to the back buffer.
if( FAILED( hr = g_pddsBackBuffer->AddAttachedSurface( g_pddsZBuffer ) ) )
return hr;
// Before creating the device, check that we are NOT in a palettized
// display. That case will cause CreateDevice() to fail, since this simple
// tutorial does not bother with palettes.
ddsd.dwSize = sizeof(DDSURFACEDESC2);
g_pDD->GetDisplayMode( &ddsd );
if( ddsd.ddpfPixelFormat.dwRGBBitCount <= 8 )
return DDERR_INVALIDMODE;
// Create the device. The device is created off of our back buffer, which
// becomes the render target for the newly created device. Note that the
// z-buffer must be created BEFORE the device
if( FAILED( hr = g_pD3D->CreateDevice( IID_IDirect3DHALDevice , g_pddsBackBuffer,
&g_pd3dDevice ) ) )
{
// This call could fail for many reasons. The most likely cause is
// that we specifically requested a hardware device, without knowing
// whether there is even a 3D card installed in the system. Another
// possibility is the hardware is incompatible with the current display
// mode (the correct implementation would use enumeration for this.)
return hr;
}
// Create the viewport
DWORD dwRenderWidth = scr_width;
DWORD dwRenderHeight = scr_height;
D3DVIEWPORT7 vp = { 0, 0, dwRenderWidth, dwRenderHeight, 0.0f, 1.0f };
hr = g_pd3dDevice->SetViewport( &vp );
if( FAILED( hr ) )
return hr;
SetDeviceState();
return S_OK;
}
HRESULT D3D::CleanUp()
{
// Cleanup any objects created for the scene
// Release the DDraw and D3D objects used by the app
if( g_pddsZBuffer ) g_pddsZBuffer->Release();
if( g_pD3D ) g_pD3D->Release();
if( g_pddsBackBuffer ) g_pddsBackBuffer->Release();
if( g_pddsPrimary ) g_pddsPrimary->Release();
// Do a safe check for releasing the D3DDEVICE. RefCount should be zero.
if( g_pd3dDevice )
if( 0 < g_pd3dDevice->Release() )
return E_FAIL;
// Do a safe check for releasing DDRAW. RefCount should be zero.
if( g_pDD )
if( 0 < g_pDD->Release() )
return E_FAIL;
g_pddsZBuffer = NULL;
g_pd3dDevice = NULL;
g_pD3D = NULL;
g_pddsBackBuffer = NULL;
g_pddsPrimary = NULL;
g_pDD = NULL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreSurfaces()
// Desc: Checks for lost surfaces and restores them if lost.
//-----------------------------------------------------------------------------
HRESULT D3D::RestoreSurfaces()
{
// Check/restore the primary surface
if( g_pddsPrimary )
if( g_pddsPrimary->IsLost() )
g_pddsPrimary->Restore();
// Check/restore the back buffer
if( g_pddsBackBuffer )
if( g_pddsBackBuffer->IsLost() )
g_pddsBackBuffer->Restore();
// Check/restore the z-buffer
if( g_pddsZBuffer )
if( g_pddsZBuffer->IsLost() )
g_pddsZBuffer->Restore();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: EnumZBufferCallback()
// Desc: Enumeration function to report valid pixel formats for z-buffers.
//-----------------------------------------------------------------------------
static HRESULT WINAPI EnumZBufferCallback( DDPIXELFORMAT* pddpf,
VOID* pddpfDesired )
{
// For this tutorial, we are only interested in z-buffers, so ignore any
// other formats (e.g. DDPF_STENCILBUFFER) that get enumerated. An app
// could also check the depth of the z-buffer (16-bit, etc,) and make a
// choice based on that, as well. For this tutorial, we'll take the first
// one we get.
if( pddpf->dwFlags == DDPF_ZBUFFER )
{
memcpy( pddpfDesired, pddpf, sizeof(DDPIXELFORMAT) );
// Return with D3DENUMRET_CANCEL to end the search.
return D3DENUMRET_CANCEL;
}
// Return with D3DENUMRET_OK to continue the search.
return D3DENUMRET_OK;
}
/*
//-----------------------------------------------------------------------------
// Name: CalcFileObjectSizeCB()
// Desc: Callback used to calculate the radius of a sphere that encloses all
// the meshes in the file.
//-----------------------------------------------------------------------------
BOOL CalcFileObjectSizeCB( CD3DFileObject* pObject, D3DMATRIX* pmat,
VOID* pContext )
{
FLOAT* pfRadius = (FLOAT*)pContext;
D3DVERTEX* pVertices;
DWORD dwNumVertices;
if( SUCCEEDED( pObject->GetMeshGeometry( &pVertices, &dwNumVertices,
NULL, NULL ) ) )
{
for( DWORD i=0; i<dwNumVertices; i++ )
{
FLOAT x = pVertices[i].x;
FLOAT y = pVertices[i].y;
FLOAT z = pVertices[i].z;
FLOAT mx = x*pmat->_11 + y*pmat->_21 + z*pmat->_31 + pmat->_41;
FLOAT my = x*pmat->_12 + y*pmat->_22 + z*pmat->_32 + pmat->_42;
FLOAT mz = x*pmat->_13 + y*pmat->_23 + z*pmat->_33 + pmat->_43;
// Store the largest r (radius) for any point in the mesh
FLOAT r = sqrtf( mx*mx + my*my + mz*mz );
if( r > (*pfRadius) )
(*pfRadius) = r;
}
}
// Keep enumerating file objects
return FALSE;
}
*/
void X_MODEL::SetupMatrices(LPDIRECT3DDEVICE7 g_pd3dDevice)
{
D3DVECTOR vEyePt = D3DVECTOR( 0.0f, 0.0f, 0.0f );
D3DVECTOR vLookatPt = D3DVECTOR( 0.0f, 0.0f, 0.0f );
D3DVECTOR vUpVec = D3DVECTOR( 0.0f, 1.0f, 0.0f );
vEyePt.z += radius * 3.0f + radius * 0.0f;
// Set the view matrix
D3DMATRIX matView;
D3DUtil_SetViewMatrix( matView, vEyePt, vLookatPt, vUpVec );
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_VIEW, &matView );
D3DVIEWPORT7 vp;
g_pd3dDevice->GetViewport(&vp);
float fAspect = ((FLOAT)vp.dwHeight) / vp.dwWidth;
D3DMATRIX matProj;
D3DUtil_SetProjectionMatrix( matProj, g_PI / 4, fAspect, 1.0f , radius * 10.0f );
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &matProj );
}
/*
D3DX_SURFACEFORMAT PixelFormat = D3DX_SF_R5G6B5;
D3DXCreateTextureFromFile(
g_pd3dDevice ,
0 ,
0 ,
0 ,
&PixelFormat ,
NULL ,
&pBlockSurface ,
0 ,
"block.bmp" ,
D3DX_FT_POINT
);
*/
D3DVERTEXBUFFERDESC vbDesc;
INIT_STRUCT(vbDesc);
vbDesc.dwCaps = D3DVBCAPS_WRITEONLY;
vbDesc.dwFVF = D3DFVF_XYZ|D3DFVF_DIFFUSE;
vbDesc.dwNumVertices = dwVertexAmount;
if(g_pDeviceInfo->bHardware==FALSE)
{
vbDesc.dwCaps|=D3DVBCAPS_SYSTEMMEMORY;
}
HRESULT SCENE::LoadZBuffer()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -