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

📄 fxshader.cpp

📁 使用shader语言在vs阶段编写光照效果的演示程序及全部源代码
💻 CPP
字号:
#include "FXShader.h"





//按照名字来设置参数
bool CEffectShader::setParamValue(const char* Name , float    v  )
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	if(FAILED(m_pEffect->SetFloat(hD3DXHandle,v  ) ) )
	{
		return false;
	}
	return true;
}
bool CEffectShader::setParamValue(const char* Name , const float*   v , int Count, int baseIndex)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	hD3DXHandle = m_pEffect->GetParameterElement(hD3DXHandle,baseIndex);
	if(hD3DXHandle == NULL) 
		return false;

	if(FAILED(m_pEffect->SetFloatArray(hD3DXHandle,v , Count ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , bool    v  )
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	if(FAILED(m_pEffect->SetBool(hD3DXHandle, v ) ) )
	{
		return false;
	}
	return true;
}
bool CEffectShader::setParamValue(const char* Name ,const  bool*   v , int Count, int baseIndex)
{
	BOOL* array = new BOOL[Count];
	for(int i = 0 ; i < Count ; ++i) array[i] =  v[i]?TRUE:FALSE;

	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	if(FAILED(m_pEffect->SetBoolArray(hD3DXHandle,array , Count ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , int      v  )
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;
	if(FAILED(m_pEffect->SetInt( hD3DXHandle,   v ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name ,const  int*     v , int Count, int baseIndex)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	hD3DXHandle = m_pEffect->GetParameterElement(hD3DXHandle,baseIndex);
	if(hD3DXHandle == NULL) 
		return false;

	if(FAILED(m_pEffect->SetIntArray( hD3DXHandle,  v , Count ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , const D3DXVECTOR4* vec)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;
	if(FAILED(m_pEffect->SetVector( hD3DXHandle,  vec ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , const D3DXVECTOR4* vecs, int nVector, int baseIndex)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	hD3DXHandle = m_pEffect->GetParameterElement(hD3DXHandle,baseIndex);
	if(hD3DXHandle == NULL) 
		return false;

	if(FAILED(m_pEffect->SetVectorArray( hD3DXHandle,  (D3DXVECTOR4*)vecs , nVector ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , const  D3DXMATRIX* mat)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;
	if(FAILED(m_pEffect->SetMatrix( hD3DXHandle,  mat ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValue(const char* Name , const  D3DXMATRIX* mats, int nMat, int baseIndex)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	hD3DXHandle = m_pEffect->GetParameterElement(hD3DXHandle,baseIndex);
	if(hD3DXHandle == NULL) 
		return false;

	if(FAILED(m_pEffect->SetMatrixArray( hD3DXHandle, (D3DXMATRIX*)mats , nMat  ) ) )
	{
		return false;
	}
	return true;
}
bool CEffectShader::setParamValueT(const char* Name , const  D3DXMATRIX* mat)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;
	if(FAILED(m_pEffect->SetMatrixTranspose( hD3DXHandle,  mat ) ) )
	{
		return false;
	}
	return true;
}

bool CEffectShader::setParamValueT(const char* Name , const  D3DXMATRIX* mats, int nMat, int baseIndex)
{
	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;

	hD3DXHandle = m_pEffect->GetParameterElement(hD3DXHandle,baseIndex);
	if(hD3DXHandle == NULL) 
		return false;

	if(FAILED(m_pEffect->SetMatrixTransposeArray( hD3DXHandle, (D3DXMATRIX*)mats , nMat  ) ) )
	{
		return false;
	}
	return true;
}
bool CEffectShader::setTexture(const char* Name                , IDirect3DBaseTexture9* pTexture)
{

	

	D3DXHANDLE hD3DXHandle = m_pEffect->GetParameterByName(NULL,Name);
	if(hD3DXHandle == NULL)
		return false;
	if(FAILED(m_pEffect->SetTexture(hD3DXHandle,pTexture ) ) )
	{
		return false;
	}
	return true;
}

//创建删除等操作
bool CEffectShader::load(const char* file_name)
{
	IDirect3DDevice9* pDevice =  CD3DApplication::getApp()->getDevice();
	ID3DXBuffer *errorBuffer = NULL; // A buffer for an error string if our effect file fails
	// to load
	// Load the effect file and check for errors
	HRESULT mResult = D3DXCreateEffectFromFile(pDevice, file_name,	NULL, NULL,	0, 
		NULL, &m_pEffect, &errorBuffer);

	// If an error occurred								   
	if(mResult != D3D_OK)
	{
		// Get the error string
		const char *str = NULL;

		if(errorBuffer)
			str = (const char*)errorBuffer->GetBufferPointer();
		else
			str = "Error loading effect file";
		// Print it to the screen
		//MessageBox(hwnd, str, "Shader Compilation Error", MB_OK | MB_ICONERROR);
		OutputDebugString(str);
		return false;
	}


	return true;
}



//渲染操作
bool CEffectShader::begin(size_t *pPasses, unsigned long Flags)
{
	m_pEffect->Begin(pPasses,Flags);
	return true;
}

bool CEffectShader::beginPass(size_t Pass)
{
	m_pEffect->BeginPass((UINT)Pass);
	return true;
}

bool CEffectShader::commitChanges()
{
	m_pEffect->CommitChanges();
	return true;
}

bool CEffectShader::endPass()
{
	m_pEffect->EndPass();
	return true;
}

bool CEffectShader::end()
{
	m_pEffect->End();
	return true;
}


bool CEffectShader::setTechnique(const char * technique)
{
	D3DXHANDLE hTech =  m_pEffect->GetTechniqueByName(technique);
	if(FAILED(m_pEffect->SetTechnique(technique)  ) )
		return false;
	return true;
}

bool CEffectShader::unload()
{
	m_pEffect->Release();
	return true;
}


⌨️ 快捷键说明

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