?? fixedfuncshader.cpp
字號:
//======================================================================================//
// filename: FixedFuncShader.cpp //
// //
// author: Pedro V. Sander //
// ATI Research, Inc. //
// 3D Application Research Group //
// email: psander@ati.com //
// //
// Description: A programmable shader that //
// emulates parts of the fixed function pipeline //
// //
//======================================================================================//
// (C) 2003 ATI Research, Inc. All rights reserved. //
//======================================================================================//
#define STRICT
#include <Windows.h>
#include <commctrl.h>
#include <math.h>
#include <stdio.h>
#include <D3DX9.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include "Resource.h"
#include "Effects/FixedFuncShader.fxh"
#include "Stats.h"
#include "SceneState.h"
//Display modes
enum FFS_MODE {FFS_MODE_SHADER, FFS_MODE_FIXED, FFS_MODE_BOTH, FFS_MODE_DIFF};
//Vertex structure
struct D3DVERTEX
{
FLOAT px, py, pz;
FLOAT nx, ny, nz;
D3DCOLOR c;
FLOAT tu, tv;
static const DWORD FVF;
};
const DWORD D3DVERTEX::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1;
//simple renderquad function
void RenderQuad(LPDIRECT3DDEVICE9 pd3dDevice, float x0, float y0, float x1, float y1, D3DCOLOR c);
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
CD3DFont* m_pFont; // Font for drawing text
CD3DFont* m_pFontSmall; // Font for drawing text
bool m_bUseD3DX; // Whether to use D3DXIntersect
CD3DMesh* m_pObject; // Object to render
CD3DMesh* m_pObject2;
CD3DArcBall m_ArcBall; // ArcBall used for mouse input
D3DXMATRIXA16 m_matArcBall;
//mesh vertex and index buffers
LPDIRECT3DVERTEXBUFFER9 m_pMeshVB;
LPDIRECT3DVERTEXBUFFER9 m_pMesh2VB;
LPDIRECT3DINDEXBUFFER9 m_pMeshIB;
//transformation matrices
D3DXMATRIXA16 m_matView;
D3DXMATRIXA16 m_matViewTrans;
D3DXMATRIXA16 m_matViewRot;
D3DXMATRIXA16 m_matWorld;
D3DXMATRIXA16 m_matProj;
//vertex deplarations
LPDIRECT3DVERTEXDECLARATION9 m_pVertexDeclaration;
LPDIRECT3DVERTEXDECLARATION9 m_pVertexDeclarationTween;
//display mode (shader, fixed, both, or diff)
FFS_MODE m_Mode;
//current settings
CSceneState m_scenestate;
//diff state and settings
bool m_bDiffInit;
bool m_bDiffSensitivity;
bool m_bShowSettingsText;
DWORD m_dwNumVertices;
DWORD m_dwNumFaces;
//shader effects class
LPD3DXEFFECT m_pEffect;
//window dimensions
int m_iWidth;
int m_iHeight;
//time of last FrameMove call
float m_fTimeOld;
//statistics gathering class instances
CStats m_statFrameError;
//state and mesh filenames and directories
TCHAR m_strAppDir[512];
TCHAR m_strMeshInitialDir[512];
TCHAR m_strStateInitialDir[512];
TCHAR m_strMeshFilename[512];
TCHAR m_strStateFilename[512];
//mesh info
D3DXVECTOR3 m_vObjectCenter;
float m_fObjectRadius;
//textures used for diff mode
LPDIRECT3DTEXTURE9 m_pTexture1;
LPDIRECT3DTEXTURE9 m_pTexture2;
LPDIRECT3DTEXTURE9 m_pTexture3;
LPDIRECT3DSURFACE9 m_pTextureSurface1;
LPDIRECT3DSURFACE9 m_pTextureSurface2;
LPDIRECT3DSURFACE9 m_pTextureSurface3;
//the environment map
LPDIRECT3DCUBETEXTURE9 m_pCubeMap;
//vertex buffers for diff rendering
LPDIRECT3DVERTEXBUFFER9 m_pQuadVB;
//currently selected parameters
int m_iCurParam;
int m_iCurParamLight;
float m_fTweenFactor;
//settings from command line
bool m_bLoadState;
// Internal member functions
HRESULT ConfirmDevice(D3DCAPS9*, DWORD, D3DFORMAT, D3DFORMAT);
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT Render();
void RenderSettings(CSceneState *psceneState, float xText = 2.f);
HRESULT FrameMove();
HRESULT FinalCleanup();
HRESULT InitDiff();
HRESULT ReleaseDiff();
void RenderErrorBar();
HRESULT LoadState();
HRESULT LoadFullState();
void SaveState();
void ParseCommandLine();
public:
LRESULT MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// Name: WinMain()
// 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)
{
CMyD3DApplication d3dApp;
InitCommonControls();
if(FAILED(d3dApp.Create(hInst)))
{
return 0;
}
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication() :
m_pFont(NULL),
m_pFontSmall(NULL),
m_bUseD3DX(TRUE),
m_pObject(NULL),
m_pObject2(NULL),
m_pMeshVB(NULL),
m_pMeshIB(NULL),
m_pMesh2VB(NULL),
m_pVertexDeclaration(NULL),
m_pVertexDeclarationTween(NULL),
m_bDiffInit(false),
m_bDiffSensitivity(false),
m_bShowSettingsText(true),
m_pEffect(NULL),
m_pTexture1(NULL),
m_pTexture2(NULL),
m_pTexture3(NULL),
m_pTextureSurface1(NULL),
m_pTextureSurface2(NULL),
m_pTextureSurface3(NULL),
m_pCubeMap(NULL),
m_pQuadVB(NULL),
m_iCurParam(0),
m_iCurParamLight(0),
m_Mode(FFS_MODE_SHADER),
m_fTimeOld(-1.f),
m_statFrameError(CStats("Frame Error", false)),
m_bLoadState(false)
{
m_strWindowTitle = _T("Fixed Function Shader");
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
m_bShowCursorWhenFullscreen = TRUE;
GetCurrentDirectory(512, m_strAppDir);
strcpy(m_strMeshInitialDir, m_strAppDir);
strcpy(m_strStateInitialDir, m_strAppDir);
strcpy(m_strMeshFilename, _T("Data\\car.x"));
strcpy(m_strStateFilename, _T(""));
m_dwCreationWidth = 1024;
m_dwCreationHeight = 768;
// m_pFont = new CD3DFont(_T("Comic Sans MS"), 12, 0);
// m_pFontSmall = new CD3DFont(_T("Comic Sans MS"), 10, 0);
m_pFont = new CD3DFont(_T("Palatino Linotype"), 14, 0);
m_pFontSmall = new CD3DFont(_T("Palatino Linotype"), 10, 0);
m_pObject = new CD3DMesh();
m_pObject2 = new CD3DMesh();
m_bFrameMoving = false;
DXUtil_Timer(TIMER_STOP);
}
//-----------------------------------------------------------------------------
// Name: ParseCommandLine()
// Desc: Parses the command line for options
//-----------------------------------------------------------------------------
void CMyD3DApplication::ParseCommandLine()
{
char *strCmdLine = GetCommandLine();
strlwr(strCmdLine);
char *pc;
//parse for an input state file
if(pc = strstr(strCmdLine, "/s:"))
{
strcpy(m_strStateFilename, pc+3);
if(pc = strstr(m_strStateFilename, " "))
{
*pc = 0;
}
_tcscpy(m_strStateInitialDir, m_strStateFilename);
TCHAR* pLastSlash = _tcsrchr(m_strStateInitialDir, _T('\\'));
if(pLastSlash)
{
*pLastSlash = 0;
}
SetCurrentDirectory(m_strAppDir);
m_bLoadState = true;
}
//parse for an input mesh file
if(pc = strstr(strCmdLine, "/x:"))
{
strcpy(m_strMeshFilename, pc+3);
if(pc = strstr(m_strMeshFilename, " "))
{
*pc = 0;
}
_tcscpy(m_strMeshInitialDir, m_strMeshFilename);
TCHAR* pLastSlash = _tcsrchr(m_strMeshInitialDir, _T('\\'));
if(pLastSlash)
{
*pLastSlash = 0;
}
SetCurrentDirectory(m_strAppDir);
}
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
ParseCommandLine();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
if(m_fTimeOld == -1.f)
{
m_fTimeOld = m_fTime;
}
// Rotate the camera about the y-axis
D3DXMATRIXA16 m_matTmp;
D3DXMatrixRotationY(&m_matTmp, (m_fTime-m_fTimeOld)/2.866f); //2.866 = 9/PI (so 18 time steps == 2PI)
D3DXMatrixMultiply(&m_matViewRot, &m_matViewRot, &m_matTmp);
m_fTimeOld = m_fTime;
// recompute view matrix
D3DXMatrixMultiply(&m_matView, &m_matViewRot, &m_matViewTrans);
D3DXMatrixMultiply(&m_matView, &m_matView, &m_matArcBall);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RenderErrorBar()
// Desc: Renders the errorbar for the diff mode
//-----------------------------------------------------------------------------
void CMyD3DApplication::RenderErrorBar()
{
int iError = (int)(m_statFrameError.GetAvg()*256.f*256.f);
int iErrorMax = (int)(m_statFrameError.GetMax()*256.f);
//NOTE: Two bars are not to same scale.
//Can't scale Max by an additional factor of 256 or it will be too large to fit
RenderQuad(m_pd3dDevice, -(m_iWidth/2.f)+60.f, -(m_iHeight/2.f)+10.f,
-(m_iWidth/2.f)+60.f + iError, -(m_iHeight/2.f)+15.f, 0xFFFFFFFF);
RenderQuad(m_pd3dDevice, -(m_iWidth/2.f)+60.f, -(m_iHeight/2.f)+20.f,
-(m_iWidth/2.f)+60.f + iErrorMax, -(m_iHeight/2.f)+25.f, 0xFFFF0000);
}
//-----------------------------------------------------------------------------
// Name: RenderSettings()
// Desc: Renders the state settings
//-----------------------------------------------------------------------------
void CMyD3DApplication::RenderSettings(CSceneState *psceneState, float xText)
{
//set up the cursor
POINT ptCursor;
GetCursorPos(&ptCursor);
ScreenToClient(m_hWnd, &ptCursor);
m_pd3dDevice->SetCursorPosition(ptCursor.x, ptCursor.y, 0L);
int iCurParam = m_iCurParam;
//render current settings
switch(m_Mode)
{
case FFS_MODE_SHADER:
m_pFont->DrawText(10, (float)m_iHeight - 30, D3DCOLOR_ARGB(255,255,255,0), "Programmable pipeline");//, D3DFONT_CENTERED_X);
break;
case FFS_MODE_FIXED:
m_pFont->DrawText(10, (float)m_iHeight - 30, D3DCOLOR_ARGB(255,255,255,0), "Fixed function pipeline");//, D3DFONT_CENTERED_X);
break;
case FFS_MODE_DIFF:
m_pFont->DrawText(10, (float)m_iHeight - 30, D3DCOLOR_ARGB(255,255,255,0), "DIFF");//, D3DFONT_CENTERED_X);
break;
case FFS_MODE_BOTH:
m_pFont->DrawText(10, (float)m_iHeight - 30, D3DCOLOR_ARGB(255,255,255,0), "Programmable pipeline");
m_pFont->DrawText((float)m_iWidth/2+10, (float)m_iHeight - 30, D3DCOLOR_ARGB(255,255,255,0), "Fixed function pipeline");
break;
}
FLOAT yText = 40.0f;
FLOAT ySpacing = 20.f;
if(xText < m_iWidth / 2)
{
// Output statistics
m_pFont->DrawText(xText, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats);
m_pFont->DrawText(xText, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats);
// Output info
m_pFontSmall->DrawText(xText, yText, D3DCOLOR_ARGB(255,255,255,0), m_bUseD3DX ? _T("Using D3DX") : _T("Not Using D3DX"));
yText += ySpacing;
}
else
yText += ySpacing;
char tmp[512];
char *onoff[2] = {_T("OFF"), _T("ON")};
D3DCOLOR cols[2] = {D3DCOLOR_ARGB(255,0,255,255), D3DCOLOR_ARGB(255,255,0,0)};
ySpacing = 15.f;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -