📄 shader_reflect_and_refract.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 "nvmesh.h"
#include "nvdevice.h"
#include "shader_Reflect_and_Refract.h"
#include "Reflect_and_Refract.h"
using namespace nv_objects;
using namespace std;
DECLARE_EFFECT_MAIN()
extern "C"
{
__declspec(dllexport) unsigned int GetNumEffects() { return 1; }
__declspec(dllexport) EBEffect* CreateEffect(unsigned int EffectNum)
{
return new CShaderReflectRefract();
}
}
CShaderReflectRefract::CShaderReflectRefract()
: m_pCubeTexture(NULL),
m_pSphere(NULL),
m_bMipMap(true),
m_bWireframe(false),
m_pUI(NULL),
m_pNVDevice(NULL),
m_pWorldBoxVertices(NULL),
m_pWorldBoxIndices(NULL),
m_fRefraction(0.4f)
{
for (int i = 0; i < 6; i++)
{
m_pWorldTextures[i] = NULL;
}
m_strEffectName = "Reflect and Refract";
m_strEffectLocation = "Vertex Shaders\\Texture Coordinate Generation";
m_strEffectPixelShader = "";
m_strEffectVertexShader = GetFilePath("Reflect_and_Refract.nvv");
}
CShaderReflectRefract::~CShaderReflectRefract()
{
Free();
}
void CShaderReflectRefract::UpdateProperties()
{
EBEffect::UpdateProperties();
AddProperty(new EBProperty("MipMap the CubeMap", OBJECT_MEMBER(m_bMipMap), EBTYPE_BOOL_PROP));
AddProperty(new EBProperty("Wireframe", OBJECT_MEMBER(m_bWireframe), EBTYPE_BOOL_PROP));
m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Reflection and Refraction Calculation", GetFilePath("Reflect_and_Refract.nvv"), EBTYPE_STRING_PROP));
}
HRESULT CShaderReflectRefract::ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
if (!(pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
{
m_strLastError = "Device does not support cubemaps!";
return E_FAIL;
}
if (!(pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED))
{
m_strLastError = "Device does not support 3 element texture coordinates!";
return E_FAIL;
}
return S_OK;
}
#define NUM_CUBE_VERTICES (4*6)
#define NUM_CUBE_INDICES (6*6)
HRESULT CShaderReflectRefract::CreateCube(WorldBoxVertex* pVertices, WORD* pIndices)
{
// Set up the vertices for the cube.
// -Z face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f), D3DXVECTOR2(1.0f, 0.0f));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f), D3DXVECTOR2(0.0f, 0.0f));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f), D3DXVECTOR2(0.0f, 1.0f));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f), D3DXVECTOR2(1.0f, 1.0f));
// +Z face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f), D3DXVECTOR2(1.0f, 0.0f ));
// -Y face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f), D3DXVECTOR2(1.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f), D3DXVECTOR2(0.0f, 0.0f ));
// +Y face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f), D3DXVECTOR2(0.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f), D3DXVECTOR2(1.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f), D3DXVECTOR2(1.0f, 0.0f ));
// -X face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f), D3DXVECTOR2(1.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f), D3DXVECTOR2(0.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f), D3DXVECTOR2(0.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f), D3DXVECTOR2(1.0f, 1.0f ));
// +X face
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f), D3DXVECTOR2(0.0f, 0.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f), D3DXVECTOR2(0.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f), D3DXVECTOR2(1.0f, 1.0f ));
*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f), D3DXVECTOR2(1.0f, 0.0f ));
// Set up the indices for the cube
*pIndices++ = 0+0; *pIndices++ = 0+1; *pIndices++ = 0+2;
*pIndices++ = 0+2; *pIndices++ = 0+3; *pIndices++ = 0+0;
*pIndices++ = 4+0; *pIndices++ = 4+1; *pIndices++ = 4+2;
*pIndices++ = 4+2; *pIndices++ = 4+3; *pIndices++ = 4+0;
*pIndices++ = 8+0; *pIndices++ = 8+1; *pIndices++ = 8+2;
*pIndices++ = 8+2; *pIndices++ = 8+3; *pIndices++ = 8+0;
*pIndices++ = 12+0; *pIndices++ = 12+1; *pIndices++ = 12+2;
*pIndices++ = 12+2; *pIndices++ = 12+3; *pIndices++ = 12+0;
*pIndices++ = 16+0; *pIndices++ = 16+1; *pIndices++ = 16+2;
*pIndices++ = 16+2; *pIndices++ = 16+3; *pIndices++ = 16+0;
*pIndices++ = 20+0; *pIndices++ = 20+1; *pIndices++ = 20+2;
*pIndices++ = 20+2; *pIndices++ = 20+3; *pIndices++ = 20+0;
return S_OK;
}
HRESULT CShaderReflectRefract::Initialize(IDirect3DDevice8* pDev)
{
HRESULT hr;
m_pD3DDev = pDev;
pDev->AddRef();
m_pNVDevice = new NVDevice(pDev);
//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);
DWORD dwVBFlags = D3DUSAGE_WRITEONLY;
vector<DWORD> Declaration;
Declaration.push_back(D3DVSD_STREAM(0));
Declaration.push_back(D3DVSD_REG(0, D3DVSDT_FLOAT3));
Declaration.push_back(D3DVSD_REG(1, D3DVSDT_FLOAT3));
Declaration.push_back(D3DVSD_END());
hr = LoadAndCreateShader(GetFilePath("Reflect_and_REFRACT.vso"), &Declaration[0], 0, SHADERTYPE_VERTEX, &m_dwCurrentShader);
if (FAILED(hr))
return hr;
m_pSphere = new NVMesh();
hr = m_pSphere->Create(m_pNVDevice, GetFilePath("Sphere4.x"));
if (FAILED(hr))
{
m_strLastError = "Could not load Sphere4.x";
return hr;
}
m_pSphere->SetFVF(m_pNVDevice, D3DFVF_XYZ | D3DFVF_NORMAL);
m_pSphere->SetVertexShader(m_dwCurrentShader);
hr = m_pD3DDev->CreateVertexBuffer(NUM_CUBE_VERTICES * sizeof(WorldBoxVertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &m_pWorldBoxVertices);
if (FAILED(hr))
{
m_strLastError = "Could not create vertex buffer!";
return hr;
}
hr = m_pD3DDev->CreateIndexBuffer(NUM_CUBE_INDICES * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pWorldBoxIndices);
if (FAILED(hr))
{
m_strLastError = "Could not create index buffer!";
return hr;
}
WorldBoxVertex* pVertices = NULL;
WORD* pIndices = NULL;
hr = m_pWorldBoxVertices->Lock(0, sizeof(WorldBoxVertex) * NUM_CUBE_VERTICES,(BYTE**)&pVertices, 0);
if (FAILED(hr))
{
m_strLastError = "Could not lock vertex buffer!";
return hr;
}
hr = m_pWorldBoxIndices->Lock(0, sizeof(WORD) * NUM_CUBE_INDICES,(BYTE**)&pIndices, 0);
if (FAILED(hr))
{
m_strLastError = "Could not lock vertex buffer!";
return hr;
}
CreateCube(pVertices, pIndices);
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_negz.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_Z]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_posz.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_Z]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_negy.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_Y]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_posy.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_Y]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_posx.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_X]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
hr = D3DXCreateTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_negx.bmp").c_str(),
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_FILTER_LINEAR,
D3DX_FILTER_LINEAR,
0,
NULL,
NULL,
&m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_X]);
if (FAILED(hr))
{
m_strLastError = "Could not create ";
return hr;
}
m_pWorldBoxVertices->Unlock();
m_pWorldBoxIndices->Unlock();
// allocate our cube texture
hr = D3DXCreateCubeTextureFromFileEx(m_pD3DDev,
GetFilePath("nvlobby_cube_mipmap.dds").c_str(),
D3DX_DEFAULT,
0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -