⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shader_membrane.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************NVMH2****
Path:  C:\Dev\devrel\Nv_sdk_4\Dx8\NVEffectsBrowser\Effects\Membrane
File:  shader_Membrane.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_Membrane.h"
#include <NVFileDialog.h>


using namespace std;

DECLARE_EFFECT_MAIN()


#define   STR_LOADMESH     "Load .X File Mesh..."
#define   STR_LOADTEXTURE  "Load Texture Map..."
#define   STR_WIREFRAME    "Wireframe"


extern "C"
{
	
	__declspec(dllexport) unsigned int GetNumEffects() { return 1; }
	
	__declspec(dllexport) EBEffect* CreateEffect(unsigned int EffectNum)
	{	
		switch(EffectNum)
		{
		case 0:
			return new CShaderMembrane();
			break;
		default:
			return NULL;
		}
	}
	
};

// ------------------------------------------------------------------------------
// CShaderMembrane Constructor
//
// Description: Nothing to it -- just straight-forward construction
// ------------------------------------------------------------------------------ 
CShaderMembrane::CShaderMembrane()
: 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(MembraneDisplay_SILHOUETTESANDSHADING)
{
	m_strEffectLocation = "Vertex Shaders\\Render Styles";
	m_strEffectName = "Membrane Lighting";
	m_strEffectVertexShader = GetFilePath("Membrane.nvv");
	m_strEffectPixelShader = "";

	D3DXMatrixIdentity(&m_world);
	D3DXMatrixIdentity(&m_view);
	D3DXMatrixIdentity(&m_proj);

	m_strFileName = "bigship1.x";

	m_bCycleTexture = true;
	m_bCycleTexCoords = false;
	m_bAppCycleTexCoords = false;

	m_tc[0]  = 0.0f;
	m_tc[1]  = 0.0f;
	m_tc[2]  = 0.0f;
	m_tc[3]  = 0.0f;

}


// ------------------------------------------------------------------------------
// CShaderMembrane Destructor
//
// Description: Nothing to it -- just straight-forward destruction
// ------------------------------------------------------------------------------ 
CShaderMembrane::~CShaderMembrane()
{
	Free(); 
}

// ------------------------------------------------------------------------------
// CShaderMembrane::UpdateProperties
//
// Description: This adds options to the menu (click on "File" or right-click
// on the display.
// ------------------------------------------------------------------------------ 

void CShaderMembrane::UpdateProperties()
{
	EBEffect::UpdateProperties();

	EBEnumProperty* pEnumProp = new EBEnumProperty("Display Options", OBJECT_MEMBER(m_eDisplayOption),
														EBTYPE_DWORD_PROP);

	AddProperty(new EBProperty(STR_WIREFRAME, OBJECT_MEMBER(m_bWireframe), EBTYPE_BOOL_PROP));
	AddProperty(new EBProperty("Cycle Texture", OBJECT_MEMBER(m_bCycleTexture), EBTYPE_BOOL_PROP));
	AddProperty(new EBProperty("Cycle Tex Coord", OBJECT_MEMBER(m_bCycleTexCoords), EBTYPE_BOOL_PROP));


	AddProperty(pEnumProp);

	AddProperty(new EBTriggerProperty(STR_LOADTEXTURE));
	AddProperty(new EBTriggerProperty(STR_LOADMESH));

	// Vertex shaders
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Membrane Lighting",
										GetFilePath("Membrane.nvv"), EBTYPE_STRING_PROP));
}



// ------------------------------------------------------------------------------
// CShaderMembrane::ConfirmDevice
//
// Description: Performs caps-bit checking to make sure the selected device 
//		supports this demo.  In this particular case we check for nothing!
// ------------------------------------------------------------------------------ 
HRESULT CShaderMembrane::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;
}

// ------------------------------------------------------------------------------
// CShaderMembrane::Initialize
//
// Description: Initialize render-states, vertex shader and vertex buffers
// ------------------------------------------------------------------------------ 
HRESULT CShaderMembrane::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_END());
	
	// Load the vertex shader object file and create it.
	m_dwCurrentShader = 0;
	hr = LoadAndCreateShader(GetFilePath("Membrane.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(), MembraneVertex::FVF_Flags );
	if(FAILED(hr))
		return hr;
	
	// use this vertex buffer as our source for vertices
	hr = m_pD3DDev->SetStreamSource(0, m_pVertexBuffer, sizeof(MembraneVertex));
	if (FAILED(hr))
		return hr;
	
	//set render states
	m_pD3DDev->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	m_pD3DDev->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS );	    // drawing transparent things
	m_pD3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	m_pD3DDev->SetRenderState(D3DRS_LIGHTING, FALSE);

	m_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW );


	m_pD3DDev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE );	// additive blending
	m_pD3DDev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE );
	
	//load textures
	D3DXCreateTextureFromFile(m_pD3DDev, GetFilePath("PurpleGreenBright.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);



	SetDefaultView();


	return S_OK;
}


void CShaderMembrane::SetView()
{	
	m_pD3DDev->SetVertexShaderConstant(10, &m_vEye, 1);

	// Create view matrix
	D3DXMatrixLookAtLH( &m_view, &m_vEye, &m_vLookAt, &m_vUp);
	
	SetVertexShaderMatrices();
}



void CShaderMembrane::SetDefaultView()
{
	m_vEye.x    =   0.0f;
	m_vEye.y	=   0.0f;
	m_vEye.z	= -25.0f;

	m_vLookAt.x = 0.0f;
	m_vLookAt.y = 0.0f;
	m_vLookAt.z = 0.0f;

	m_vUp.x = 0.0f;
	m_vUp.y = 1.0f;
	m_vUp.z = 0.0f;


	// Set world matrix to view from an angle
	D3DXMatrixIdentity(&m_world);

	m_pUI->Reset();

	// rotate world matrix a bit:
	// We do it this way instead of changing eye position as the
	//   controls would not make sense for non-z axis eye position.
	m_pUI->OnLButtonDown( 40, 10 );
	m_pUI->OnMouseMove( 3, 30 );
	m_pUI->OnLButtonUp( 3, 30 );


	// Set field of view	
	D3DXMatrixPerspectiveFovLH(&m_proj,
		D3DXToRadian(60.0f),
		1,
		1.0f,
		5000.0f);

	m_pD3DDev->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&m_proj);
	
	SetView();
}



HRESULT CShaderMembrane::SetVertexShaderMatrices()
{
	D3DXMATRIX mvpMat, worldITMat, worldMat;
	
	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 CShaderMembrane::PropertyUpdateCallback(const EBProperty* pProperty, bool bWritten)
{
	if (!bWritten)
		return;

	EBString	name;


	if (pProperty->IsKindOf(EBTYPE_TRIGGER_PROP))
	{

		name = pProperty->GetPropertyName();

		
		if( name == EBString(STR_LOADMESH) )
		{
			// Now do file dialog
			NVXFileDialog aDialog;

			std::string theResult;
			bWritten = ( aDialog.Open( theResult ) );
			
			if ( bWritten )
			{
				m_strFileName = theResult;
				bWritten = ( LoadXFile( m_strFileName.c_str(), MembraneVertex::FVF_Flags ) == S_OK );
			}
		}
		else if( name == EBString(STR_LOADTEXTURE) )
		{
			// Now do file dialog
			NVTextureFileDialog aDialog;

			std::string theResult;
			bWritten = ( aDialog.Open( theResult ) );

			m_strFileName = theResult;

			if ( bWritten )
			{
				LoadTexture( &m_pShadeTexture, m_strFileName );				
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -