📄 lights.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: Lights.cpp
//
// Desc: Rendering 3D geometry is much more interesting when dynamic lighting
// is added to the scene. To use lighting in D3DM, you must create one or
// lights, setup a material, and make sure your geometry contains surface
// normals. Lights may have a position, a color, and be of a certain type
// such as directional (light comes from one direction) or point (light
// comes from a specific x,y,z coordinate and radiates in all directions).
// Materials describe the surface of your geometry, specifically, how it
// gets lit (diffuse color, ambient color, etc.). Surface normals are
// part of a vertex, and are needed for the D3DM's internal lighting
// calculations.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#pragma comment(linker, "/nodefaultlib:oldnames.lib")
#include <windows.h>
#include <d3dm.h>
#include <d3dmx.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
// A structure for our custom vertex type. We added a normal, and omitted the
// color (which is provided by the material)
struct CUSTOMVERTEX
{
D3DMXVECTOR3 position; // The 3D position for the vertex
D3DMXVECTOR3 normal; // The surface normal for the vertex
};
// Our custom FVF, which describes our custom vertex structure
#define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZ_FLOAT | D3DMFVF_NORMAL_FLOAT)
//-----------------------------------------------------------------------------
// 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()
{
// Determine if the device can create vertex buffers in video memory
// by testing the device caps bits.
D3DMCAPS caps;
if( FAILED( g_pd3dmDevice->GetDeviceCaps(&caps) ) )
{
return E_FAIL;
}
D3DMPOOL pool;
if (caps.SurfaceCaps & D3DMSURFCAPS_VIDVERTEXBUFFER)
{
pool = D3DMPOOL_VIDEOMEM;
}
else
{
pool = D3DMPOOL_SYSTEMMEM;
}
// Create the vertex buffer.
if( FAILED( g_pd3dmDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
0, D3DMFVF_CUSTOMVERTEX,
pool, &g_pVB ) ) )
{
return E_FAIL;
}
// Fill the vertex buffer. We are algorithmically generating a cylinder
// here, including the normals, which are used for lighting.
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 = D3DMXVECTOR3( (float)sin(theta),-1.0f, (float)cos(theta) );
pVertices[2*i+0].normal = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
pVertices[2*i+1].position = D3DMXVECTOR3( (float)sin(theta), 1.0f, (float)cos(theta) );
pVertices[2*i+1].normal = D3DMXVECTOR3( (float)sin(theta), 0.0f, (float)cos(theta) );
}
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
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 + -