📄 main.cpp
字号:
//-----------------------------------------------------------------------------
// Name: D3DFrame_Quad
//
// Description: Example code showing how to display a textured quad
//
// File Function: Main body of the function (main.cpp)
//
// Code:
// Copyright (c) 2002 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
// d3d9.lib dxguid.lib d3dx9dt.lib d3dxof.lib comctl32.lib winmm.lib
//
// DX Files Required:
// d3dapp.cpp
// dedenumeration.cpp
// d3dfont.cpp
// d3dsettings.cpp
// d3dutil.cpp
// dxutil.cpp
//
// Local Files Required:
// resourcedata.rc
// main.cpp
// main.h
//
// Original D3D Application Shell Code:
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "main.h"
LPDIRECT3DTEXTURE9 g_pTexture[1];
#define RADIAN_TO_DEGREES 57.29577951308232286465f
#define D3DFVF_MYVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE|D3DFVF_TEX1)
typedef struct MYVERTEX
{
D3DXVECTOR3 vecPos;
D3DXVECTOR3 vecNorm;
DWORD dwDiffuse;
float u;
float v;
} MYVERTEX, *LPMYVERTEX;
MYVERTEX* vCreateSquare(float fx, float fy, float fz, float fsizex, float fsizey, DWORD dwColor);
class MeshClass
{
private:
public:
LPDIRECT3DDEVICE9 pd3dDevice;
unsigned long m_lBufferSize;
unsigned long m_lNumbTris;
void *m_lpVertices;
LPDIRECT3DVERTEXBUFFER9 m_lpVertexBuffer;
DWORD m_dwVertexFormat;
LPDIRECT3DTEXTURE9 m_pTexture[8]; // Max 8 texture stages
MeshClass();
~MeshClass();
void vSetDevice( LPDIRECT3DDEVICE9 p3d );
void vClearMemory( void );
void vSetTexture( int iStage, LPDIRECT3DTEXTURE9 lpTexture );
bool bLock( void );
bool bUnlock( void );
bool bCreateBuffer( unsigned long numbtris, DWORD dwFormat, D3DPOOL MemType, int vsize );
};
void MeshClass::vSetTexture( int iStage, LPDIRECT3DTEXTURE9 lpTexture )
{
m_pTexture[ iStage ] = lpTexture;
}
MeshClass::MeshClass()
{
pd3dDevice = NULL;
m_lpVertexBuffer = NULL;
m_lpVertices = NULL;
m_lBufferSize = 0;
m_lNumbTris = 0;
}
MeshClass::~MeshClass()
{
vClearMemory();
}
void MeshClass::vSetDevice(LPDIRECT3DDEVICE9 p3d)
{
pd3dDevice = p3d;
}
void MeshClass::vClearMemory(void)
{
SAFE_RELEASE( m_lpVertexBuffer );
m_lBufferSize = 0;
m_lNumbTris = 0;
}
bool MeshClass::bLock(void)
{
if(FAILED(m_lpVertexBuffer->Lock(0, m_lBufferSize, (void**)&m_lpVertices, 0))) {
return(0);
}
return(1);
}
bool MeshClass::bUnlock(void)
{
if(FAILED(m_lpVertexBuffer->Unlock())) {
return(0);
}
return(1);
}
bool MeshClass::bCreateBuffer(unsigned long numbtris, DWORD dwFormat, D3DPOOL MemType, int vsize )
{
m_lNumbTris = numbtris;
m_dwVertexFormat = dwFormat;
m_lBufferSize = ( m_lNumbTris * 3 * vsize );
// Release it if already created
SAFE_RELEASE( m_lpVertexBuffer );
// Create the vertex buffer
if(FAILED(pd3dDevice->CreateVertexBuffer(
m_lBufferSize,
NULL,
m_dwVertexFormat,
MemType,
&m_lpVertexBuffer,
NULL ))) {
return(0);
}
return(1);
}
MeshClass mTerrain;
//-----------------------------------------------------------------------------
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Instance of the framework application
CD3DFramework d3dApp;
InitCommonControls();
// Create the application environment
// ----------------------------------
// This initializes the display and loads all appropriate data.
//
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
// Start up the application
// ------------------------
// Starts the program running by activating the message loop
// and render functions.
//
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CD3DFramework::CD3DFramework()
{
m_strWindowTitle = _T("D3D Frame Quad");
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
// Create fonts
lstrcpy( m_strFont, _T("Arial") );
m_pStatsFont = NULL;
}
//-----------------------------------------------------------------------------
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::OneTimeSceneInit()
{
m_pStatsFont = new CD3DFont( _T("Arial"), 8, NULL );
if( m_pStatsFont == NULL )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FrameMove()
{
// Rotate the camera about the y-axis
D3DXVECTOR3 vFromPt = D3DXVECTOR3( 5.0f, 5.0f, 5.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::Render()
{
D3DXMATRIX matTranslation;
D3DXMATRIX matRotation;
D3DXMATRIX matRotation2;
D3DXMATRIX matScale;
// Clear the viewport
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(120,120,120), 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// Show frame rate
m_pStatsFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pStatsFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// Set the vertex buffer to render
m_pd3dDevice->SetStreamSource( 0, mTerrain.m_lpVertexBuffer, 0, sizeof(MYVERTEX) );
// Set the vertex format
m_pd3dDevice->SetFVF( D3DFVF_MYVERTEX );
// Put the quad in its place
D3DXMatrixScaling( &matScale, 1.0f, 1.0f, 1.0f );
D3DXMatrixTranslation( &matTranslation, 0.0f, 0.0f, 0.0f );
D3DXMatrixRotationX( &matRotation, -90.0f/RADIAN_TO_DEGREES );
matScale *= matRotation;
matScale *= matTranslation;
m_pd3dDevice->SetTransform(D3DTS_WORLD, &matScale);
m_pd3dDevice->SetTexture( 0, g_pTexture[0] );
// Draw the quad
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, mTerrain.m_lNumbTris);
// End the scene
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InitDeviceObjects()
{
HRESULT hr;
if( FAILED( hr = m_pStatsFont->InitDeviceObjects( m_pd3dDevice ) ) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::RestoreDeviceObjects()
{
char szFileName[ 256 ];
int i, j;
int iDepth = 1;
int iWidth = 1;
MYVERTEX *retVertices;
MYVERTEX g_Vertices[ 900 ];
D3DLIGHT9 light;
// Restore the fonts
m_pStatsFont->RestoreDeviceObjects();
// Set up the textures
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
// Set miscellaneous render states
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00888888 );
// Set the world matrix
D3DXMATRIXA16 matIdentity;
D3DXMatrixIdentity( &matIdentity );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matIdentity );
// Set the projection matrix
D3DXMATRIXA16 matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// Set up lighting states
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.1f, -1.0f, 0.1f );
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
// Create the grid based on width and depth
for( i = 0; i < iDepth; i++) {
for( j = 0; j < iWidth; j++ ) {
retVertices = vCreateSquare((j*5.0f),(i*5.0f),0.0f,5.0f,5.0f,D3DCOLOR_XRGB(255,255,255));
memcpy( &g_Vertices[(((i*iWidth)+j)*6)],retVertices,(sizeof(MYVERTEX)*6) );
delete [] retVertices;
}
}
// Set the device used
mTerrain.vSetDevice( m_pd3dDevice );
// Create the buffer to hold the quads
mTerrain.bCreateBuffer( (i*j*2), D3DFVF_MYVERTEX, D3DPOOL_DEFAULT, sizeof(MYVERTEX) );
// Lock the buffer for editing
mTerrain.bLock( );
// Copy the quad data to the buffer
memcpy( mTerrain.m_lpVertices, g_Vertices, mTerrain.m_lBufferSize );
// Unlock the buffer since done editing
mTerrain.bUnlock( );
// Load the texture for the terrain
sprintf(szFileName,"texcords.tga");
if( FAILED( D3DXCreateTextureFromFile( m_pd3dDevice, szFileName, &g_pTexture[0] ) ) ) {
return S_OK;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::InvalidateDeviceObjects()
{
m_pStatsFont->InvalidateDeviceObjects();
SAFE_RELEASE( g_pTexture[0] );
mTerrain.vClearMemory();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::DeleteDeviceObjects()
{
m_pStatsFont->DeleteDeviceObjects();
//SAFE_RELEASE( g_pTexture[0] );
mTerrain.vClearMemory();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CD3DFramework::FinalCleanup()
{
SAFE_DELETE( m_pStatsFont );
mTerrain.vClearMemory();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CD3DFramework::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
MYVERTEX* vCreateSquare(float fx, float fy, float fz, float fsizex, float fsizey, DWORD dwColor)
{
MYVERTEX *vertices = new MYVERTEX[6];
vertices[0].vecPos = D3DXVECTOR3(-fsizex/2+fx,fsizey/2+fy,fz);
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;
vertices[0].dwDiffuse = dwColor;
vertices[0].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
vertices[1].vecPos = D3DXVECTOR3(-fsizex/2+fx,-fsizey/2+fy,fz);
vertices[1].u = 0.0f;
vertices[1].v = 1.0f; // Y
vertices[1].dwDiffuse = dwColor;
vertices[1].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
vertices[2].vecPos = D3DXVECTOR3(fsizex/2+fx,fsizey/2+fy,fz);
vertices[2].u = -1.0f; // X
vertices[2].v = 0.0f;
vertices[2].dwDiffuse = dwColor;
vertices[2].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
vertices[3].vecPos = D3DXVECTOR3(-fsizex/2+fx,-fsizey/2+fy,fz);
vertices[3].u = 0.0f; // X
vertices[3].v = 1.0f; // Y
vertices[3].dwDiffuse = dwColor;
vertices[3].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
vertices[4].vecPos = D3DXVECTOR3(fsizex/2+fx,-fsizey/2+fy,fz);
vertices[4].u = -1.0f; // X
vertices[4].v = 1.0f; // Y
vertices[4].dwDiffuse = dwColor;
vertices[4].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
vertices[5].vecPos = D3DXVECTOR3(fsizex/2+fx,fsizey/2+fy,fz);
vertices[5].u = -1.0f; // X
vertices[5].v = 0.0f; // Y
vertices[5].dwDiffuse = dwColor;
vertices[5].vecNorm = D3DXVECTOR3(0.0f,0.0f,1.0f);
return(vertices);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -