青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當(dāng)自強(qiáng)而不息

高級(jí)紋理映射技術(shù)(10)

立體紋理

立體紋理(volume texture)是一組應(yīng)用到二維圖元(如一個(gè)三角形或一條直線)的三維紋理元素的集合,可以使用立體紋理實(shí)現(xiàn)一些特殊效果,如迷霧、爆炸等。當(dāng)對(duì)一個(gè)圖元使用立體紋理時(shí),它的每個(gè)頂點(diǎn)都需要一組三元紋理坐標(biāo)。當(dāng)繪制該圖元時(shí),它中間的每個(gè)像素都將用立體紋理中的一些紋理元素的顏色值進(jìn)行填充,這與二維紋理映射的情況相似。

立體紋理以薄片為單元組織起來,可以把它想象成將(寬 x 高)的二維表面轉(zhuǎn)換成(寬 x 高 x 深)的三維立體,每個(gè)薄片是單獨(dú)的一行,立體紋理可以有一系列級(jí)別,每一級(jí)都較上一級(jí)縮小一倍,如下圖所示:

要?jiǎng)?chuàng)建立體紋理,首先必須檢查顯卡是否支持立體紋理:

// check whether device support volume texture
if((pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP) == 0)
return false;

接著,我們需要在靈活頂點(diǎn)格式中指定每個(gè)頂點(diǎn)都需要三個(gè)紋理坐標(biāo),如下所示:

struct sCustomVertex
{
float x, y, z;
float u, v, w;
};
#define D3DFVF_CUSTOM_VERTEX	(D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0))

D3DFVF_TEXCOORDSIZEN

Constructs bit patterns that are used to identify texture coordinate formats within a FVF description. The results of these macros can be combined within a FVF description by using the OR operator.

#define D3DFVF_TEXCOORDSIZEN(CoordIndex) 
#define D3DFVF_TEXCOORDSIZE1(CoordIndex) (D3DFVF_TEXTUREFORMAT1 << (CoordIndex*2 + 16))
#define D3DFVF_TEXCOORDSIZE2(CoordIndex) (D3DFVF_TEXTUREFORMAT2)
#define D3DFVF_TEXCOORDSIZE3(CoordIndex) (D3DFVF_TEXTUREFORMAT3 << (CoordIndex*2 + 16))
#define D3DFVF_TEXCOORDSIZE4(CoordIndex) (D3DFVF_TEXTUREFORMAT4 << (CoordIndex*2 + 16))

Parameters

CoordIndex
Value that identifies the texture coordinate set at which the texture coordinate size (1-, 2-, 3-, or 4Dimensional) applies.

Remarks

The D3DFVF_TEXCOORDSIZEN macros use the following constants.

#define D3DFVF_TEXTUREFORMAT1 3 // one floating point value
#define D3DFVF_TEXTUREFORMAT2 0 // two floating point values
#define D3DFVF_TEXTUREFORMAT3 1 // three floating point values
#define D3DFVF_TEXTUREFORMAT4 2 // four floating point values

The following FVF description identifies a vertex format that has a position; a normal; diffuse and specular colors; and two sets of texture coordinates. The first set of texture coordinates includes a single element, and the second set includes two elements:

DWORD dwFVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE |
D3DFVF_SPECULAR | D3DFVF_TEX2 |
D3DFVF_TEXCOORDSIZE1(0) | // Uses 1D texture coordinates for texture coordinate set 1 (index 0).
D3DFVF_TEXCOORDSIZE2(1); // And 2D texture coordinates for texture coordinate set 2 (index 1).

接著,為每個(gè)頂點(diǎn)指定數(shù)據(jù),創(chuàng)建頂點(diǎn)緩沖區(qū)并填充它:

// create vertex buffer and fill data
sCustomVertex vertices[] = 	
{
{ -3.0f, -3.0f, 0.0f, 0.0f, 1.0f, 0.0f},
{ -3.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f},
{ 3.0f, -3.0f, 0.0f, 1.0f, 1.0f, 0.0f},
{ 3.0f, 3.0f, 0.0f, 1.0f, 0.0f, 0.0f}
};
pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();

可通過IDirect3DDevice9::CreateVolumeTexture()來創(chuàng)建立體紋理,該函數(shù)聲明如下:

Creates a volume texture resource.

HRESULT CreateVolumeTexture(
UINT Width,
UINT Height,
UINT Depth,
UINT Levels,
DWORD Usage,
D3DFORMAT Format,
D3DPOOL Pool,
IDirect3DVolumeTexture9** ppVolumeTexture,
HANDLE* pSharedHandle
);

Parameters

Width
[in] Width of the top-level of the volume texture, in pixels. This value must be a power of two if the D3DPTEXTURECAPS_VOLUMEMAP_POW2 member of D3DCAPS9 is set. The pixel dimensions of subsequent levels will be the truncated value of half of the previous level's pixel dimension (independently). Each dimension clamps at a size of 1 pixel. Thus, if the division by two results in 0 (zero), 1 will be taken instead. The maximum dimension that a driver supports (for width, height, and depth) can be found in MaxVolumeExtent in D3DCAPS9.
Height
[in] Height of the top-level of the volume texture, in pixels. This value must be a power of two if the D3DPTEXTURECAPS_VOLUMEMAP_POW2 member of D3DCAPS9 is set. The pixel dimensions of subsequent levels will be the truncated value of half of the previous level's pixel dimension (independently). Each dimension clamps at a size of 1 pixel. Thus, if the division by 2 results in 0 (zero), 1 will be taken instead. The maximum dimension that a driver supports (for width, height, and depth) can be found in MaxVolumeExtent in D3DCAPS9.
Depth
[in] Depth of the top-level of the volume texture, in pixels. This value must be a power of two if the D3DPTEXTURECAPS_VOLUMEMAP_POW2 member of D3DCAPS9 is set. The pixel dimensions of subsequent levels will be the truncated value of half of the previous level's pixel dimension (independently). Each dimension clamps at a size of 1 pixel. Thus, if the division by 2 results in 0 (zero), 1 will be taken instead. The maximum dimension that a driver supports (for width, height, and depth) can be found in MaxVolumeExtent in D3DCAPS9.
Levels
[in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels down to 1x1 pixels for hardware that supports mipmapped volume textures. Call IDirect3DBaseTexture9::GetLevelCount to see the number of levels generated.
Usage
[in] Usage can be 0, which indicates no usage value. If usage is desired, use D3DUSAGE_DYNAMIC or D3DUSAGE_SOFTWAREPROCESSING. For more information, see D3DUSAGE.
Format
[in] Member of the D3DFORMAT enumerated type, describing the format of all levels in the volume texture.
Pool
[in] Member of the D3DPOOL enumerated type, describing the memory class into which the volume texture should be placed.
ppVolumeTexture
[out, retval] Address of a pointer to an IDirect3DVolumeTexture9 interface, representing the created volume texture resource.
pSharedHandle
[in] Reserved. Set this parameter to NULL.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY.

示例代碼如下:

HRESULT hr;
UINT width = 16, height = 16, depth = 16;

V_RETURN(device->CreateVolumeTexture(width, height, depth, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
&g_volume_texture, NULL));

接著調(diào)用IDirect3DVolumeTexture9::LockBox()來鎖定立體紋理頂點(diǎn)緩沖區(qū),該函數(shù)的聲明如下:

Locks a box on a volume texture resource.

HRESULT LockBox(
UINT Level,
D3DLOCKED_BOX * pLockedVolume,
CONST D3DBOX * pBox,
DWORD Flags
);

Parameters

Level
[in] Specifies the level of the volume texture resource to lock.
pLockedVolume
[out] Pointer to a D3DLOCKED_BOX structure, describing the locked region.
pBox
[in] Pointer to the volume to lock. This parameter is specified by a pointer to a D3DBOX structure. Specifying NULL for this parameter locks the entire volume level.
Flags
[in] Combination of zero or more locking flags that describe the type of lock to perform. For this method, the valid flags are:
  • D3DLOCK_DISCARD
  • D3DLOCK_NO_DIRTY_UPDATE
  • D3DLOCK_NOSYSLOCK
  • D3DLOCK_READONLY
For a description of the flags, see D3DLOCK.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

Remarks

For performance reasons, dirty regions are only recorded for level zero of a texture. Dirty regions are automatically recorded when IDirect3DVolumeTexture9::LockBox is called without D3DLOCK_NO_DIRTY_UPDATE or D3DLOCK_READONLY. For more information, see IDirect3DDevice9::UpdateTexture.

D3DLOCKED_BOX

Describes a locked box (volume).

typedef struct D3DLOCKED_BOX {
int RowPitch;
int SlicePitch;
void * pBits;
} D3DLOCKED_BOX, *LPD3DLOCKED_BOX;

Members

RowPitch
Byte offset from the left edge of one row to the left edge of the next row.
SlicePitch
Byte offset from the top-left of one slice to the top-left of the next deepest slice.
pBits
Pointer to the beginning of the volume box. If a D3DBOX was provided to the LockBox call, pBits will be appropriately offset from the start of the volume.

Remarks

Volumes can be visualized as being organized into slices of width x height 2D surfaces stacked up to make a width x height x depth volume. For more information, see Volume Texture Resources (Direct3D 9).

D3DBOX

Defines a volume.

typedef struct D3DBOX {
UINT Left;
UINT Top;
UINT Right;
UINT Bottom;
UINT Front;
UINT Back;
} D3DBOX, *LPD3DBOX;

Members

Left
Position of the left side of the box on the x-axis.
Top
Position of the top of the box on the y-axis.
Right
Position of the right side of the box on the x-axis.
Bottom
Position of the bottom of the box on the y-axis.
Front
Position of the front of the box on the z-axis.
Back
Position of the back of the box on the z-axis.

Remarks

D3DBOX includes the left, top, and front edges; however, the right, bottom, and back edges are not included. For example, a box that is 100 units wide and begins at 0 (thus, including the points up to and including 99) would be expressed with a value of 0 for the Left member and a value of 100 for the Right member. Note that a value of 99 is not used for the Right member.

The restrictions on side ordering observed for D3DBOX are left to right, top to bottom, and front to back.

示例代碼如下:

D3DLOCKED_BOX locked_box;
g_volume_texture->LockBox(0, &locked_box, NULL, 0);
for(UINT w = 0; w < depth; w++)
{
BYTE* slice_start = (BYTE*) locked_box.pBits;
	for(UINT v = 0; v < height; v++)
{
for(UINT u = 0; u < width; u++)
{
// create texel color and fill it
			float du = (u - 7.5f) / 7.5f;
float dv = (v - 7.5f) / 7.5f;
float dw = (w - 7.5f) / 7.5f;
float scale = sqrt(du * du + dv * dv + dw * dw);
			if(scale > 1.0f)
scale = 0.0f;
else
scale = 1.0f - scale;
			DWORD r = (DWORD)((w << 4) * scale);
DWORD g = (DWORD)((v << 4) * scale);
DWORD b = (DWORD)((u << 4) * scale);
			((DWORD*) locked_box.pBits)[u] = 0xFF000000 + (r << 16) + (g << 8) + b;
}
		locked_box.pBits = (BYTE*) locked_box.pBits + locked_box.RowPitch;
}
	locked_box.pBits = slice_start + locked_box.SlicePitch;
}
g_volume_texture->UnlockBox(0);

還必須對(duì)立體紋理緩沖區(qū)進(jìn)行解鎖,這需要調(diào)用IDirect3DVolumeTexture9::UnlockBox(),代碼如上所示,該函數(shù)聲明如下:

Unlocks a box on a volume texture resource.

HRESULT UnlockBox(
UINT Level
);

Parameters

Level
[in] Specifies the level of the volume texture resource to unlock.

Return Values

If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

為了生成立體紋理,還必須改變紋理的w坐標(biāo):

//--------------------------------------------------------------------------------------
// Handle updates to the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
// change w coordinate of volume texture
	float angle = (float) fTime / 2.0f;
	sCustomVertex* ptr;
g_vertex_buffer->Lock(0, 0, (void**)&ptr, 0);
	for(int i = 0; i < 4; i++)
ptr[i].w = sin(angle) * sin(angle);
	g_vertex_buffer->Unlock();
}

最后設(shè)置要渲染的紋理層和立體紋理,紋理階段混合方法,頂點(diǎn)數(shù)據(jù),并渲染它:

// set textue and stream source and vertex format
pd3dDevice->SetTexture(0, g_volume_texture);
pd3dDevice->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
pd3dDevice->SetFVF(D3DFVF_CUSTOM_VERTEX);
// set texture color blend method
pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
 // Clear the render target and the zbuffer 
V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
// Render the scene
if( SUCCEEDED( pd3dDevice->BeginScene() ) )
{
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
RenderText();
	V(g_button_dlg.OnRender(fElapsedTime));
V( pd3dDevice->EndScene() );
}

運(yùn)行效果圖:

立體紋理和普通二維紋理的主要區(qū)別是:頂點(diǎn)紋理坐標(biāo)是三維的,紋理對(duì)象指針的類型不同,立體紋理的創(chuàng)建也不同,而立體紋理在使用上和普通二維紋理基本相同。

在示例程序中,立體紋理是由程序生成的,比較復(fù)雜。Direct3D提供了輔助函數(shù)D3DXCreateVolumeTextureFromFile()來從圖片文件中生成立體紋理,該函數(shù)的聲明如下:

Creates a volume texture from a file.

HRESULT D3DXCreateVolumeTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCTSTR pSrcFile,
LPDIRECT3DVOLUMETEXTURE9 * ppVolumeTexture
);

Parameters

pDevice
[in] Pointer to an IDirect3DDevice9 interface, representing the device to be associated with the volume texture.
pSrcFile
[in] Pointer to a string that specifies the file name. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
ppVolumeTexture
[out] Address of a pointer to an IDirect3DVolumeTexture9 interface representing the created texture object.

Return Values

If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following:

D3DERR_NOTAVAILABLE
D3DERR_OUTOFVIDEOMEMORY
D3DERR_INVALIDCALL
D3DXERR_INVALIDDATAE_OUTOFMEMORY

Remarks

The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateVolumeTextureFromFileW. Otherwise, the function call resolves to D3DXCreateVolumeTextureFromFileA because ANSI strings are being used.

The function is equivalent to D3DXCreateVolumeTextureFromFileEx(pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppVolumeTexture).

This function supports the following file formats: .bmp, .dds, .dib, .hdr, .jpg, .pfm, .png, .ppm, and .tga. See D3DXIMAGE_FILEFORMAT.

Mipmapped textures automatically have each level filled with the loaded texture.

When loading images into mipmapped textures, some devices are unable to go to a 1x1 image and this function will fail. If this happens, then the images need to be loaded manually.

Note that a resource created with this function will be placed in the memory class denoted by D3DPOOL_MANAGED.

Filtering is automatically applied to a texture created using this method. The filtering is equivalent to D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER in D3DX_FILTER.

通過文件創(chuàng)建立體紋理時(shí)文件格式必須是立體紋理格式,對(duì)于普通格式的圖片文件,可以通過DirectX提供的紋理工具"DirectX Texture Tool"轉(zhuǎn)換成立體紋理格式的文件。

 

主程序:

#include "dxstdafx.h"
#include 
"resource.h"

#pragma warning(disable : 
4127 4995 4996)

#define release_com(p)    do { if(p) { (p)->Release(); (p) = NULL; } } while(0)

#define IDC_TOGGLE_FULLSCREEN        1
#define IDC_TOGGLE_REF                2
#define IDC_CHANGE_DEVICE            3

struct sCustomVertex
{
    
float x, y, z;
    
float u, v, w;
};

#define D3DFVF_CUSTOM_VERTEX    (D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0))

const D3DXCOLOR FONT_COLOR(1.0f0.5f0.25f1.0f);

ID3DXFont
*                    g_font;
ID3DXSprite
*                g_text_sprite;
bool                        g_show_help;

CDXUTDialogResourceManager    g_dlg_resource_manager;
CD3DSettingsDlg                g_settings_dlg;
CDXUTDialog                    g_button_dlg;

IDirect3DVertexBuffer9
*        g_vertex_buffer;
IDirect3DVolumeTexture9
*    g_volume_texture;

//--------------------------------------------------------------------------------------
// Rejects any devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                  D3DFORMAT BackBufferFormat, 
bool bWindowed, void* pUserContext )
{
    
// Typically want to skip backbuffer formats that don't support alpha blending

    IDirect3D9
* pD3D = DXUTGetD3DObject(); 

    
if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, 
                    D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
        
return false;

    
// check whether device support volume texture
    if((pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP) == 0)
        
return false;

    
return true;
}


//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed.
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
{
    
// If video card does not support hardware vertex processing, then uses sofaware vertex processing.
    if((pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0)
        pDeviceSettings
->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    
static bool is_first_time = true;

    
if(is_first_time)
    {
        is_first_time 
= false;

        
// if using reference device, then pop a warning message box.
        if(pDeviceSettings->DeviceType == D3DDEVTYPE_REF)
            DXUTDisplaySwitchingToREFWarning();
    }

    
return true;
}

//--------------------------------------------------------------------------------------
// Create volume texture and fill data.
//--------------------------------------------------------------------------------------
HRESULT CreateVolumeTexture(IDirect3DDevice9* device)
{
    HRESULT hr;
    UINT width 
= 16, height = 16, depth = 16;

    V_RETURN(device
->CreateVolumeTexture(width, height, depth, 10, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
                                         
&g_volume_texture, NULL));

    D3DLOCKED_BOX locked_box;
    g_volume_texture
->LockBox(0&locked_box, NULL, 0);

    
for(UINT w = 0; w < depth; w++)
    {
        BYTE
* slice_start = (BYTE*) locked_box.pBits;

        
for(UINT v = 0; v < height; v++)
        {
            
for(UINT u = 0; u < width; u++)
            {
                
// create texel color and fill it

                
float du = (u - 7.5f/ 7.5f;
                
float dv = (v - 7.5f/ 7.5f;
                
float dw = (w - 7.5f/ 7.5f;
                
float scale = sqrt(du * du + dv * dv + dw * dw);

                
if(scale > 1.0f)
                    scale 
= 0.0f;
                
else
                    scale 
= 1.0f - scale;

                DWORD r 
= (DWORD)((w << 4* scale);
                DWORD g 
= (DWORD)((v << 4* scale);
                DWORD b 
= (DWORD)((u << 4* scale);

                ((DWORD
*) locked_box.pBits)[u] = 0xFF000000 + (r << 16+ (g << 8+ b;
            }

            locked_box.pBits 
= (BYTE*) locked_box.pBits + locked_box.RowPitch;
        }

        locked_box.pBits 
= slice_start + locked_box.SlicePitch;
    }

    g_volume_texture
->UnlockBox(0);

    
return S_OK;
}

//--------------------------------------------------------------------------------------
// Create any D3DPOOL_MANAGED resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, 
                                 
const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                 
void* pUserContext )
{
    HRESULT    hr;

    V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice));
    V_RETURN(g_settings_dlg.OnCreateDevice(pd3dDevice));

    D3DXCreateFont(pd3dDevice, 
180, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
                   DEFAULT_PITCH 
| FF_DONTCARE, L"Arial"&g_font);
    
    
// create vertex buffer and fill data

    sCustomVertex vertices[] 
=     
    {
        { 
-3.0f-3.0f,  0.0f0.0f1.0f0.0f},
        { 
-3.0f,  3.0f,  0.0f0.0f0.0f0.0f},
        {  
3.0f-3.0f,  0.0f1.0f1.0f0.0f},
        {  
3.0f,  3.0f,  0.0f1.0f0.0f0.0f}
    };

    pd3dDevice
->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);

    
void* ptr;
    g_vertex_buffer
->Lock(0sizeof(vertices), (void**)&ptr, 0);
    memcpy(ptr, vertices, 
sizeof(vertices));
    g_vertex_buffer
->Unlock();

    
return CreateVolumeTexture(pd3dDevice);
}


//--------------------------------------------------------------------------------------
// Create any D3DPOOL_DEFAULT resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
                                
const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                
void* pUserContext )
{
    HRESULT hr;

    V_RETURN(g_dlg_resource_manager.OnResetDevice());
    V_RETURN(g_settings_dlg.OnResetDevice());
    V_RETURN(g_font
->OnResetDevice());
    V_RETURN(D3DXCreateSprite(pd3dDevice, 
&g_text_sprite));

    
// set dialog position and size

    g_button_dlg.SetLocation(pBackBufferSurfaceDesc
->Width - 1700);
    g_button_dlg.SetSize(
170170);

    
// setup world matrix
    D3DXMATRIX mat_world;
    D3DXMatrixIdentity(
&mat_world);
    pd3dDevice
->SetTransform(D3DTS_WORLD, &mat_world);

    
// setup view matrix

    D3DXMATRIX mat_view;
    D3DXVECTOR3 eye(
0.0f0.0f-8.0f);
    D3DXVECTOR3  at(
0.0f0.0f,  0.0f);
    D3DXVECTOR3  up(
0.0f1.0f,  0.0f);

    D3DXMatrixLookAtLH(
&mat_view, &eye, &at, &up);
    pd3dDevice
->SetTransform(D3DTS_VIEW, &mat_view);

    
// set projection matrix
    D3DXMATRIX mat_proj;
    
float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;
    D3DXMatrixPerspectiveFovLH(
&mat_proj, D3DX_PI/4, aspect, 1.0f100.0f);
    pd3dDevice
->SetTransform(D3DTS_PROJECTION, &mat_proj);

    
// set textue and stream source and vertex format
    pd3dDevice->SetTexture(0, g_volume_texture);
    pd3dDevice
->SetStreamSource(0, g_vertex_buffer, 0sizeof(sCustomVertex));
    pd3dDevice
->SetFVF(D3DFVF_CUSTOM_VERTEX);

    
// set texture color blend method
    pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1,        D3DTA_TEXTURE);    
    pd3dDevice
->SetTextureStageState(0, D3DTSS_COLOROP,            D3DTOP_SELECTARG1);    
    pd3dDevice
->SetSamplerState(0,        D3DSAMP_MINFILTER,        D3DTEXF_LINEAR);
    pd3dDevice
->SetSamplerState(0,        D3DSAMP_MAGFILTER,        D3DTEXF_LINEAR);

    
return S_OK;
}

//--------------------------------------------------------------------------------------
// Release resources created in the OnResetDevice callback here 
//--------------------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
    g_dlg_resource_manager.OnLostDevice();
    g_settings_dlg.OnLostDevice();
    g_font
->OnLostDevice();

    release_com(g_text_sprite);
}


//--------------------------------------------------------------------------------------
// Release resources created in the OnCreateDevice callback here
//--------------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
    g_dlg_resource_manager.OnDestroyDevice();
    g_settings_dlg.OnDestroyDevice();    

    release_com(g_font);
    release_com(g_vertex_buffer);
    release_com(g_volume_texture);
}

//--------------------------------------------------------------------------------------
// Handle updates to the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    
// change w coordinate of volume texture
    
    
float angle = (float) fTime / 2.0f;

    sCustomVertex
* ptr;
    g_vertex_buffer
->Lock(00, (void**)&ptr, 0);

    
for(int i = 0; i < 4; i++)
        ptr[i].w 
= sin(angle) * sin(angle);

    g_vertex_buffer
->Unlock();
}

//--------------------------------------------------------------------------------------
// Render the helper information
//--------------------------------------------------------------------------------------
void RenderText()
{
    CDXUTTextHelper text_helper(g_font, g_text_sprite, 
20);
    
    text_helper.Begin();

    
// show frame and device states
    text_helper.SetInsertionPos(55);
    text_helper.SetForegroundColor(FONT_COLOR);
    text_helper.DrawTextLine( DXUTGetFrameStats(
true) );
    text_helper.DrawTextLine( DXUTGetDeviceStats() );

    
// show helper information
    
    
const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();

    
if(g_show_help)
    {
        text_helper.SetInsertionPos(
10, surface_desc->Height - 18 * 5);
        text_helper.SetForegroundColor(FONT_COLOR);
        text_helper.DrawTextLine(L
"Controls (F1 to hide):");
        
        text_helper.SetInsertionPos(
40, surface_desc->Height - 18 * 4);
        text_helper.DrawTextLine(L
"Quit: ESC");
    }
    
else
    {
        text_helper.SetInsertionPos(
10, surface_desc->Height - 15 * 4);
        text_helper.SetForegroundColor( D3DXCOLOR(
1.0f1.0f1.0f1.0f) );
        text_helper.DrawTextLine(L
"Press F1 for help");
    }

    text_helper.End();
}

//--------------------------------------------------------------------------------------
// Render the scene 
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;

    
if(g_settings_dlg.IsActive())
    {
        g_settings_dlg.OnRender(fElapsedTime);
        
return;
    }

    
// Clear the render target and the zbuffer 
    V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0000), 1.0f0) );

    
// Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
        pd3dDevice
->DrawPrimitive(D3DPT_TRIANGLESTRIP, 02);
        RenderText();

        V(g_button_dlg.OnRender(fElapsedTime));
        V( pd3dDevice
->EndScene() );
    }
}


//--------------------------------------------------------------------------------------
// Handle messages to the application 
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
                          
bool* pbNoFurtherProcessing, void* pUserContext )
{
    
*pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg, wParam, lParam);
    
if(*pbNoFurtherProcessing)
        
return 0;

    
if(g_settings_dlg.IsActive())
    {
        g_settings_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
        
return 0;
    }

    
*pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
    
if(*pbNoFurtherProcessing)
        
return 0;

    
return 0;
}


//--------------------------------------------------------------------------------------
// Handle keybaord event
//--------------------------------------------------------------------------------------
void CALLBACK OnKeyboardProc(UINT charater, bool is_key_down, bool is_alt_down, void* user_context)
{
    
if(is_key_down)
    {
        
switch(charater)
        {
        
case VK_F1:
            g_show_help 
= !g_show_help;
            
break;
        }
    }
}

//--------------------------------------------------------------------------------------
// Handle events for controls
//--------------------------------------------------------------------------------------
void CALLBACK OnGUIEvent(UINT eventint control_id, CDXUTControl* control, void* user_context)
{
    
switch(control_id)
    {
    
case IDC_TOGGLE_FULLSCREEN:
        DXUTToggleFullScreen();
        
break;

    
case IDC_TOGGLE_REF:
        DXUTToggleREF();
        
break;

    
case IDC_CHANGE_DEVICE:
        g_settings_dlg.SetActive(
true);
        
break;
    }
}

//--------------------------------------------------------------------------------------
// Initialize dialogs
//--------------------------------------------------------------------------------------
void InitDialogs()
{
    g_settings_dlg.Init(
&g_dlg_resource_manager);
    g_button_dlg.Init(
&g_dlg_resource_manager);

    g_button_dlg.SetCallback(OnGUIEvent);

    
int x = 35, y = 10, width = 125, height = 22;

    g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L
"Toggle full screen", x, y,         width, height);
    g_button_dlg.AddButton(IDC_TOGGLE_REF,          L
"Toggle REF (F3)",     x, y += 24, width, height);
    g_button_dlg.AddButton(IDC_CHANGE_DEVICE,      L
"Change device (F2)", x, y += 24, width, height, VK_F2);    
}

//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
    
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF 
| _CRTDBG_LEAK_CHECK_DF );
#endif

    
// Set the callback functions
    DXUTSetCallbackDeviceCreated( OnCreateDevice );
    DXUTSetCallbackDeviceReset( OnResetDevice );
    DXUTSetCallbackDeviceLost( OnLostDevice );
    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
    DXUTSetCallbackMsgProc( MsgProc );
    DXUTSetCallbackFrameRender( OnFrameRender );
    DXUTSetCallbackFrameMove( OnFrameMove );
    DXUTSetCallbackKeyboard(OnKeyboardProc);
   
    
// TODO: Perform any application-level initialization here
    InitDialogs();

    
// Initialize DXUT and create the desired Win32 window and Direct3D device for the application
    DXUTInit( truetruetrue ); // Parse the command line, handle the default hotkeys, and show msgboxes
    DXUTSetCursorSettings( truetrue ); // Show the cursor and clip it when in full screen
    DXUTCreateWindow( L"Create Volume Texture" );
    DXUTCreateDevice( D3DADAPTER_DEFAULT, 
true640480, IsDeviceAcceptable, ModifyDeviceSettings );

    
// Start the render loop
    DXUTMainLoop();

    
// TODO: Perform any application-level cleanup here

    
return DXUTGetExitCode();
}

 

下載示例工程


posted on 2008-05-22 15:26 lovedday 閱讀(2471) 評(píng)論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

隨筆分類(178)

3D游戲編程相關(guān)鏈接

搜索

最新評(píng)論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲午夜黄色| 日韩亚洲综合在线| 久久国产精品免费一区| 亚洲夜间福利| 一区二区精品在线| 99国产麻豆精品| 亚洲视频一起| 新狼窝色av性久久久久久| 午夜激情综合网| 久久久精品一区二区三区| 久久久午夜精品| 欧美黄色免费网站| 欧美日韩一区二区三区在线| 欧美日一区二区在线观看 | 亚洲欧洲一级| 亚洲视频每日更新| 欧美综合国产| 亚洲电影观看| 日韩一区二区精品在线观看| 一区二区激情| 久久久久一区二区三区四区| 欧美经典一区二区| 国产精品影视天天线| 亚洲成人在线| 午夜在线a亚洲v天堂网2018| 麻豆免费精品视频| 亚洲免费精品| 久久人人97超碰国产公开结果| 欧美精品不卡| 国产区精品视频| 日韩视频免费在线| 久久久久久**毛片大全| 亚洲精品一区二区三区樱花| 亚洲欧美美女| 亚洲大片在线观看| 欧美在线免费观看亚洲| 欧美久色视频| 在线精品视频在线观看高清| 亚洲一区二区欧美日韩| 欧美成人性网| 久久gogo国模裸体人体| 欧美日韩国产精品一区| 在线欧美三区| 久久三级视频| 午夜视频精品| 国产精品男人爽免费视频1 | 国产伦精品一区二区三区高清| 国产又爽又黄的激情精品视频| 久久国产精品第一页| 亚洲黄网站在线观看| 亚洲欧美日韩精品久久亚洲区| 免费不卡中文字幕视频| 国产一级精品aaaaa看| 亚洲综合电影| 亚洲毛片av| 欧美激情偷拍| 欧美77777| 极品尤物一区二区三区| 欧美一区国产二区| 欧美一区二区三区精品电影| 亚洲日本成人| 欧美日韩一区二区视频在线| 亚洲精品国产精品国自产观看| 欧美aⅴ一区二区三区视频| 欧美在线一二三区| 国产在线精品一区二区夜色| 久久黄色影院| 久久国产精品电影| 狠狠综合久久av一区二区小说| 欧美一区二区在线视频| 亚洲男人的天堂在线aⅴ视频| 欧美日韩视频在线一区二区 | 中文亚洲欧美| 国产精品久久久久91| 午夜精品一区二区在线观看 | 久久成人在线| 欧美一区二区在线免费播放| 国产日韩欧美中文在线播放| 久久精品国产亚洲5555| 久久精品视频免费| 亚洲国产精品悠悠久久琪琪| 亚洲高清精品中出| 欧美日韩国产成人精品| 亚洲资源av| 欧美在线免费观看视频| 亚洲第一精品久久忘忧草社区| 欧美高清不卡在线| 一本色道精品久久一区二区三区 | 日韩视频在线观看国产| 欧美色视频日本高清在线观看| 亚洲欧美日韩在线一区| 久久国产精品久久精品国产| 亚洲国产精品久久精品怡红院| 亚洲国产精品一区二区www| 欧美色一级片| 久久人人97超碰人人澡爱香蕉| 欧美一区二区三区免费在线看| 亚洲作爱视频| 国产精品久久久久永久免费观看| 欧美亚洲三区| 久久一区二区三区四区五区| 在线视频日韩精品| 欧美中文在线视频| 亚洲乱码国产乱码精品精天堂| 一区二区三区成人| 激情小说另类小说亚洲欧美| 亚洲日韩中文字幕在线播放| 国产香蕉久久精品综合网| 亚洲国产成人不卡| 国产视频精品xxxx| 日韩亚洲成人av在线| 国产综合色产在线精品| 99视频超级精品| 亚洲国产日韩欧美| 性色av一区二区三区在线观看 | 国产精品久久夜| 欧美激情中文字幕乱码免费| 国产精品日韩专区| 亚洲精品国产精品乱码不99按摩| 国产在线一区二区三区四区| 一区二区三区福利| 亚洲日本欧美| 久久亚洲精品一区| 亚洲国产精品999| 欧美一区三区二区在线观看| 亚洲自拍偷拍视频| 欧美日韩国产经典色站一区二区三区| 蜜桃av综合| 国产亚洲成av人在线观看导航 | 欧美成人精品在线播放| 久久免费99精品久久久久久| 国产精品一级久久久| 一本综合久久| 宅男66日本亚洲欧美视频| 欧美福利视频网站| 亚洲国产精品视频| 亚洲精品激情| 这里是久久伊人| 亚洲无限乱码一二三四麻| 欧美国产精品劲爆| 91久久在线播放| 亚洲精品一区二区三区不| 麻豆成人小视频| 亚洲国产高清一区| 亚洲美女免费精品视频在线观看| 乱码第一页成人| 欧美国产国产综合| 亚洲日本免费电影| 欧美日韩国产在线观看| 99视频精品全部免费在线| 亚洲一区二区伦理| 国产精品久久久久久久久久久久久| 日韩视频免费观看| 国产欧美一区二区精品忘忧草| 亚洲欧美中文在线视频| 99国产精品久久久久久久久久 | 欧美v亚洲v综合ⅴ国产v| 麻豆乱码国产一区二区三区| 91久久精品日日躁夜夜躁欧美| 欧美成人精品激情在线观看| 亚洲区欧美区| 午夜综合激情| 影音先锋中文字幕一区| 欧美国产一区二区在线观看| 亚洲精品在线视频观看| 亚洲欧美日韩在线播放| 国产亚洲午夜| 久久久视频精品| 亚洲精品一二| 欧美一区三区二区在线观看| 在线观看亚洲一区| 欧美精品日韩三级| 亚洲欧美日韩一区二区在线 | 亚洲素人一区二区| 久久久蜜桃一区二区人| 亚洲另类视频| 国产一区二区高清不卡| 欧美精品尤物在线| 亚洲欧美日韩第一区| 欧美激情视频一区二区三区在线播放 | 欧美a级片网站| 亚洲私拍自拍| 亚洲国产欧美一区| 国产日韩欧美成人| 欧美日韩高清在线播放| 久久夜色精品国产噜噜av| 亚洲一区精彩视频| 亚洲精品四区| 你懂的视频欧美| 欧美自拍偷拍午夜视频| 一本色道婷婷久久欧美| 尤物九九久久国产精品的特点 | 国产精品成人在线| 老司机精品视频网站| 性一交一乱一区二区洋洋av| 日韩视频中文字幕| 91久久久国产精品| 每日更新成人在线视频| 午夜天堂精品久久久久| 中文一区在线|