📄 fixedpoint.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//-----------------------------------------------------------------------------
// File: FixedPoint.cpp
//
// Desc: One of the key features of Direct3D Mobile is its ability to accept
// both floating point and fixed point (16.16) data types. The support for
// fixed point data is beneficial for platforms which do not contain floating
// point hardware.
// This sample builds upon the previous tutorials in that it demonstrates the
// use of lighting, texture mapping, and matrix transformations, however where
// the previous samples submitted numerical data to D3DM in floating point
// format, this sample instead submits fixed point data.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#pragma comment(linker, "/nodefaultlib:oldnames.lib")
#include <windows.h>
#include <d3dm.h>
#include <d3dmx.h>
#include "resource.h"
#include <aygshell.h>
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3DMOBILE g_pD3DM = NULL; // Used to create the D3DMDevice
LPDIRECT3DMOBILEDEVICE g_pd3dmDevice = NULL; // Our rendering device
HMODULE g_hRefDLL = NULL; // DLL handle for d3dmref.dll
bool g_bUseRef = false; // Flag denoting use of d3dmref
LPDIRECT3DMOBILEVERTEXBUFFER g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DMOBILETEXTURE g_pTexture = NULL; // Our texture
// A structure for our custom vertex type. We added a normal, and omitted the
// color (which is provided by the material)
struct CUSTOMVERTEX
{
D3DMXVECTOR3FXD position; // The position
D3DMXVECTOR3FXD normal; // The surface normal for the vertex
#ifndef SHOW_HOW_TO_USE_TCI
FIXED tu, tv; // The texture coordinates
#endif
};
// Our custom FVF, which describes our custom vertex structure
#ifdef SHOW_HOW_TO_USE_TCI
#define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZ_FIXED | D3DMFVF_NORMAL_FIXED)
#else
#define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZ_FIXED | D3DMFVF_NORMAL_FIXED | \
D3DMFVF_TEX1 | \
D3DMFVF_TEXCOORDSIZE2(0) | \
D3DMFVF_TEXCOORDFIXED(0))
#endif
//-----------------------------------------------------------------------------
// Name: IsScreenRotated()
// Desc: Currently the D3DM runtime does not support a rotated screen. If the
// screen is rotated, we should inform the user this is not supported.
// If this routing returns TRUE the caller should cleanup and exit the application
//-----------------------------------------------------------------------------
BOOL IsScreenRotated()
{
DEVMODE devMode = {0};
devMode.dmSize = sizeof(DEVMODE);
devMode.dmFields = DM_DISPLAYORIENTATION;
ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_TEST, NULL);
if (devMode.dmDisplayOrientation != DMDO_0)
{
MessageBox(
NULL,
TEXT("This D3DM sample will not work on a rotated screen.\nThe application will now exit."),
TEXT("Notice"),
MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND
);
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name: InitD3DM()
// Desc: Initializes Direct3D Mobile
//-----------------------------------------------------------------------------
HRESULT InitD3DM( HWND hWnd )
{
// Create the D3DM object, which is needed to create the D3DMDevice.
if( NULL == ( g_pD3DM = Direct3DMobileCreate( D3DM_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Since we are now
// using more complex geometry, we will create a device with a zbuffer.
D3DMPRESENT_PARAMETERS d3dmpp;
memset( &d3dmpp, 0, sizeof(d3dmpp) );
d3dmpp.Windowed = TRUE;
d3dmpp.SwapEffect = D3DMSWAPEFFECT_DISCARD;
d3dmpp.BackBufferFormat = D3DMFMT_UNKNOWN;
d3dmpp.EnableAutoDepthStencil = TRUE;
d3dmpp.AutoDepthStencilFormat = D3DMFMT_D16;
// Create the Direct3D Mobile device.
UINT uAdapter;
if (g_bUseRef)
{
// Load the D3DM reference driver DLL
g_hRefDLL = (HMODULE)LoadLibrary(TEXT("d3dmref.dll"));
if (NULL == g_hRefDLL)
{
OutputDebugString(TEXT("Unable to load D3DM reference driver DLL.\n"));
return E_FAIL;
}
// Get the reference driver's entry point
void* pfnD3DMInit = GetProcAddress(g_hRefDLL, TEXT("D3DM_Initialize"));
if( NULL == pfnD3DMInit )
{
OutputDebugString(TEXT("Unable to retrieve D3DM reference driver entry point.\n"));
return E_FAIL;
}
// Register the software device
if( FAILED( g_pD3DM->RegisterSoftwareDevice(pfnD3DMInit) ) )
{
OutputDebugString(TEXT("Unable to register D3DM reference driver.\n"));
return E_FAIL;
}
uAdapter = D3DMADAPTER_REGISTERED_DEVICE;
}
else
{
// Use the default system D3DM driver
uAdapter = D3DMADAPTER_DEFAULT;
}
if( FAILED( g_pD3DM->CreateDevice( uAdapter,
D3DMDEVTYPE_DEFAULT,
hWnd, 0,
&d3dmpp, &g_pd3dmDevice ) ) )
{
OutputDebugString(TEXT("Unable to create a D3DM device.\n"));
return E_FAIL;
}
// Turn off culling
g_pd3dmDevice->SetRenderState( D3DMRS_CULLMODE, D3DMCULL_NONE );
// Turn on the zbuffer
g_pd3dmDevice->SetRenderState( D3DMRS_ZENABLE, TRUE );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
// Retrieve the D3DM device's capabilities
D3DMCAPS caps;
if( FAILED( g_pd3dmDevice->GetDeviceCaps(&caps) ) )
{
return E_FAIL;
}
// Determine if the device can create textures in video memory
// by testing the device caps bits.
D3DMPOOL texpool;
if (caps.SurfaceCaps & D3DMSURFCAPS_VIDTEXTURE)
{
texpool = D3DMPOOL_VIDEOMEM;
}
else
{
texpool = D3DMPOOL_SYSTEMMEM;
}
// Load the texture map resource (banana.bmp)
if( FAILED( D3DMXCreateTextureFromResourceEx( g_pd3dmDevice, GetModuleHandle(NULL),
MAKEINTRESOURCE(IDB_BANANA), D3DMX_DEFAULT, D3DMX_DEFAULT, 1, 0,
D3DMFMT_UNKNOWN, texpool, D3DMX_FILTER_POINT, D3DMX_FILTER_POINT,
0, NULL, NULL, &g_pTexture ) ) )
{
return E_FAIL;
}
// Determine if the device can create vertex buffers in video memory
// by testing the device caps bits.
D3DMPOOL vbpool;
if (caps.SurfaceCaps & D3DMSURFCAPS_VIDVERTEXBUFFER)
{
vbpool = D3DMPOOL_VIDEOMEM;
}
else
{
vbpool = D3DMPOOL_SYSTEMMEM;
}
// Create the vertex buffer.
if( FAILED( g_pd3dmDevice->CreateVertexBuffer(50*2*sizeof(CUSTOMVERTEX),
0, D3DMFVF_CUSTOMVERTEX,
vbpool, &g_pVB ) ) )
{
return E_FAIL;
}
// Fill the vertex buffer. We are setting the tu and tv texture
// coordinates, which range from 0.0 to 1.0
CUSTOMVERTEX* pVertices;
if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
return E_FAIL;
for( DWORD i=0; i<50; i++ )
{
FLOAT theta = (2*D3DMX_PI*i)/(50-1);
pVertices[2*i+0].position = D3DMXVECTOR3FXD( (float)sin(theta), -1.0f, (float)cos(theta) );
pVertices[2*i+0].normal = D3DMXVECTOR3FXD( (float)sin(theta), 0.0f, (float)cos(theta) );
#ifndef SHOW_HOW_TO_USE_TCI
pVertices[2*i+0].tu = D3DMXToFixed(((float)i)/(50-1));
pVertices[2*i+0].tv = D3DMXToFixed(1.0f);
#endif
pVertices[2*i+1].position = D3DMXVECTOR3FXD( (float)sin(theta), 1.0f, (float)cos(theta) );
pVertices[2*i+1].normal = D3DMXVECTOR3FXD( (float)sin(theta), 0.0f, (float)cos(theta) );
#ifndef SHOW_HOW_TO_USE_TCI
pVertices[2*i+1].tu = D3DMXToFixed(((float)i)/(50-1));
pVertices[2*i+1].tv = D3DMXToFixed(0.0f);
#endif
}
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_pd3dmDevice != NULL)
g_pd3dmDevice->Release();
if( g_pD3DM != NULL)
{
if (g_hRefDLL)
{
g_pD3DM->RegisterSoftwareDevice(NULL);
FreeLibrary(g_hRefDLL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -