📄 shader_aniso.cpp
字号:
/*********************************************************************NVMH2****
Path: E:\devrel\NV_SDK_4\DX8\NVEffectsBrowser\Effects\Aniso
File: shader_Aniso.cpp
Copyright (C) 1999, 2000 NVIDIA Corporation
This file is provided without support, instruction, or implied warranty of any
kind. NVIDIA makes no guarantee of its fitness for a particular purpose and is
not liable under any circumstances for any damages or loss whatsoever arising
from the use or inability to use this file or items derived from it.
Comments:
******************************************************************************/
#include "eb_effect.h"
#include "nvinc.h"
#include "shader_Aniso.h"
#include <NVFileDialog.h>
using namespace std;
DECLARE_EFFECT_MAIN()
extern "C"
{
__declspec(dllexport) unsigned int GetNumEffects() { return 1; }
__declspec(dllexport) EBEffect* CreateEffect(unsigned int EffectNum)
{
switch(EffectNum)
{
case 0:
return new CShaderAniso();
break;
default:
return NULL;
}
}
};
// ------------------------------------------------------------------------------
// CShaderAniso Constructor
//
// Description: Nothing to it -- just straight-forward construction
// ------------------------------------------------------------------------------
CShaderAniso::CShaderAniso()
: m_pVertexBuffer(NULL),
m_pIndexBuffer(NULL),
m_pShadeTexture(NULL),
m_pEdgeTexture(NULL),
m_pAttributes(NULL),
m_bWireframe(false),
m_pUI(NULL),
m_bUseTrilinear(true),
m_eDisplayOption(AnisoDisplay_SILHOUETTESANDSHADING)
{
m_strEffectLocation = "Vertex Shaders\\Render Styles";
m_strEffectName = "Anisotropic Lighting";
m_strEffectVertexShader = GetFilePath("Aniso.nvv");
m_strEffectPixelShader = "";
D3DXMatrixIdentity(&m_world);
D3DXMatrixIdentity(&m_view);
D3DXMatrixIdentity(&m_proj);
m_strFileName = "bigship1.x";
}
// ------------------------------------------------------------------------------
// CShaderAniso Destructor
//
// Description: Nothing to it -- just straight-forward destruction
// ------------------------------------------------------------------------------
CShaderAniso::~CShaderAniso()
{
Free();
}
// ------------------------------------------------------------------------------
// CShaderAniso::UpdateProperties
//
// Description: This adds options to the menu (click on "File" or right-click
// on the display.
// ------------------------------------------------------------------------------
void CShaderAniso::UpdateProperties()
{
EBEffect::UpdateProperties();
EBEnumProperty* pEnumProp = new EBEnumProperty("Display Options", OBJECT_MEMBER(m_eDisplayOption), EBTYPE_DWORD_PROP);
AddProperty(new EBProperty("Wireframe", OBJECT_MEMBER(m_bWireframe), EBTYPE_BOOL_PROP));
AddProperty(pEnumProp);
AddProperty(new EBTriggerProperty("Load Another X File Mesh..."));
// Vertex shaders
m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Aniso Lighting", GetFilePath("Aniso.nvv"), EBTYPE_STRING_PROP));
}
// ------------------------------------------------------------------------------
// CShaderAniso::ConfirmDevice
//
// Description: Performs caps-bit checking to make sure the selected device
// supports this demo. In this particular case we check for nothing!
// ------------------------------------------------------------------------------
HRESULT CShaderAniso::ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
if (!(pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP))
{
m_strLastError = "Device does not support mipmaps!";
return E_FAIL;
}
if (!(pCaps->TextureCaps & D3DPTFILTERCAPS_MIPFLINEAR))
{
m_bUseTrilinear = false;
}
if(pCaps->MaxSimultaneousTextures < 2)
{
m_strLastError = "Device does not support two simultaneous textuers!";
return E_FAIL;
}
return S_OK;
}
// ------------------------------------------------------------------------------
// CShaderAniso::Initialize
//
// Description: Initialize render-states, vertex shader and vertex buffers
// ------------------------------------------------------------------------------
HRESULT CShaderAniso::Initialize(IDirect3DDevice8* pDev)
{
HRESULT hr;
// get the D3D device
m_pD3DDev = pDev;
pDev->AddRef();
//initialize mouse UI
RECT rect;
rect.left = rect.top = 0;
D3DVIEWPORT8 viewport;
m_pD3DDev->GetViewport(&viewport);
rect.bottom = viewport.Height;
rect.right = viewport.Width;
m_pUI = new MouseUI((const RECT)rect);
vector<DWORD> Declaration;
Declaration.push_back(D3DVSD_STREAM(0));
Declaration.push_back(D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3)); // position
Declaration.push_back(D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3)); // normal
Declaration.push_back(D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT1)); // texture coords 0
Declaration.push_back(D3DVSD_REG(D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT1)); // texture coords 1
Declaration.push_back(D3DVSD_END());
// Load the vertex shader object file and create it.
m_dwCurrentShader = 0;
hr = LoadAndCreateShader(GetFilePath("Aniso.vso"), &Declaration[0], 0, SHADERTYPE_VERTEX, &m_dwCurrentShader);
if (FAILED(hr))
return hr;
// select the just created vertex shader as the thing to transform and light vertices
hr = m_pD3DDev->SetVertexShader(m_dwCurrentShader);
if (FAILED(hr))
return hr;
hr = LoadXFile(GetFilePath( m_strFileName ).c_str(), AnisoVertex::FVF_Flags );
if(FAILED(hr))
return hr;
// use this vertex buffer as our source for vertices
hr = m_pD3DDev->SetStreamSource(0, m_pVertexBuffer, sizeof(AnisoVertex));
if (FAILED(hr))
return hr;
//set render states
m_pD3DDev->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
m_pD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
m_pD3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);
m_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
m_pD3DDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO );
//load textures
D3DXCreateTextureFromFile(m_pD3DDev, GetFilePath("Aniso2.tga").c_str(), &m_pShadeTexture);
m_pD3DDev->SetTexture(0, m_pShadeTexture);
m_pD3DDev->SetTexture(1, m_pShadeTexture);
m_pD3DDev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_MIRROR);
m_pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
m_pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
m_pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_NONE);
if(m_bUseTrilinear)
m_pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
// write the constant constants to constant memory now
m_pD3DDev->SetVertexShaderConstant(9, D3DXVECTOR4(0.0f, 0.5f, 1.0f, -1.0f), 1);
D3DXVECTOR4 lightDir(1.0f, 0.0f, -1.0f, 0.0f);
D3DXVec4Normalize(&lightDir, &lightDir);
m_pD3DDev->SetVertexShaderConstant(4, &lightDir, 1);
D3DXVECTOR4 constNums(0.0f, 0.5f, 1.0f, -1.0f);
m_pD3DDev->SetVertexShaderConstant(9, &constNums, 1);
D3DXVECTOR3 eye, lookAt, up;
eye.x = 0.0f; eye.y = 0.0f; eye.z = -28.0f;
lookAt.x = 0.0f; lookAt.y = 0.0f; lookAt.z = 0.0f;
up.x = 0.0f; up.y = 1.0f; up.z = 0.0f;
m_pD3DDev->SetVertexShaderConstant(10, &eye, 1);
D3DXMatrixIdentity(&m_world);
D3DXMatrixLookAtLH(&m_view, &eye, &lookAt, &up);
D3DXMatrixPerspectiveFovLH(&m_proj,
D3DXToRadian(60.0f),
1,
1.0f,
50.0f);
m_pD3DDev->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&m_proj);
//matrices
SetVertexShaderMatrices();
return S_OK;
}
HRESULT CShaderAniso::SetVertexShaderMatrices()
{
D3DXMATRIX mvpMat, worldITMat, worldMat;
//D3DXMatrixTranspose(&worldMat, &m_world);
D3DXMATRIX tempMat;
D3DXMatrixMultiply( &tempMat, &m_pUI->GetRotationMatrix(), &m_pUI->GetTranslationMatrix() );
D3DXMatrixTranspose(&worldMat, &tempMat);
D3DXMatrixIdentity(&mvpMat);
D3DXMatrixMultiply(&mvpMat, &tempMat, &m_view);
D3DXMatrixInverse(&worldITMat, NULL, &tempMat );
D3DXMatrixMultiply(&mvpMat, &mvpMat, &m_proj);
D3DXMatrixTranspose(&mvpMat, &mvpMat);
m_pD3DDev->SetVertexShaderConstant(0, &mvpMat, 4);
m_pD3DDev->SetVertexShaderConstant(5, &worldITMat, 4);
m_pD3DDev->SetVertexShaderConstant(11, &worldMat, 4);
return S_OK;
}
void CShaderAniso::PropertyUpdateCallback(const EBProperty* pProperty, bool bWritten)
{
if (!bWritten)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -