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

📄 shader_linkerdemo.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*********************************************************************NVMH2****
Path:  E:\devrel\NV_SDK_4\DX8\NVEffectsBrowser\Effects\LinkerDemo
File:  shader_LinkerDemo.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 "shader_LinkerDemo.h"
#include "constants.h"
#include "nvlink.h"
#include "linkercommon.h"
#include "lightobject.h"
#include "nvtextureresource.h"
#include "nvtexture.h"

using namespace nv_objects;
using namespace std;

// State manager and resource manager are singletons.
// This just means that there is only one instance of them in the system.
// Access is controlled by the call to NVStateManager::GetSingleton().
// The singleton is valid when it is created.
NVStateManager* g_pNVStateManager = NULL;
NVResourceManager* g_pNVResourceManager = NULL;


// A debug object
#ifdef _DEBUG
NVDebug g_DebugOutput(3, "linkerdemo.log");
#endif


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 CShaderLinkerDemo();
			break;
		default:
			return NULL;
		}
	}
	
};

static const D3DXVECTOR3 EyeStart(0.0f, 0.0f, -1.4f);

// ------------------------------------------------------------------------------
// CShaderLinkerDemo Constructor
//
// Description: Nothing to it -- just straight-forward construction
// ------------------------------------------------------------------------------ 
CShaderLinkerDemo::CShaderLinkerDemo()
: m_bWireframe(false),
  m_pUI(NULL),
  m_pMesh(NULL),
  m_pArrowMesh(NULL),
  m_pSphereMesh(NULL),
  m_bPause(false),
  m_dwPass0Shader(0),
  m_dwPass1Shader(0),
  m_pLinker(NULL),
  m_fAngle(0.0f),
  m_bSetShaderToShow(false),
  m_pWorldBoxVertices(NULL),
  m_pWorldBoxIndices(NULL),
  m_dwBumpPixelShader(0),
  m_hLinker(NULL),
  m_hDialog(NULL),
  m_fBumpScale(1.0f),
  m_AmbientLight(DefaultAmbient),
  m_eDisplayOption(DISPLAY_MODEL),
  m_GeneratedNormalMapID(NVINVALID_RESOURCEID)
{
	m_strEffectLocation = "Tools";
	m_strEffectName = "NVLink Vertex Shader Combiner";
	m_strEffectVertexShader = GetFilePath("miscvertex.nvf");
	m_strEffectPixelShader = "";

	m_strFileName = GetFilePath("bigship1.x");

	// Ensure we have an output file
	std::string strOutFile;
	strOutFile = effect_api::GetModulePath() + "\\" + "output.nvv";
	HANDLE hFile = CreateFile(strOutFile.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
	CloseHandle(hFile);

	ZeroMemory(&m_DialogFunc, sizeof(LinkerDialogFunc));

	// Default setup
	ZeroMemory(&m_Settings, sizeof(DialogSettings));
	m_Settings.m_dwNumPointLights = 3;
	m_Settings.m_dwNumDirectionalLights = 2;
	m_Settings.m_bEyeSpace = FALSE;
	m_Settings.m_bLocalViewer = FALSE;
	m_Settings.m_bSpecular = TRUE;
	m_Settings.m_bSpecularRDotL = TRUE;
	m_Settings.m_bLighting = TRUE;
	m_Settings.m_bTextures = TRUE;
	m_Settings.m_TexGen = TEXGEN_BLINNBUMPREFLECTION;
	m_Settings.m_Fog = FOG_LINEAR_RANGE;
}

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

// ------------------------------------------------------------------------------
// CShaderLinkerDemo::UpdateProperties
//
// Description: This adds options to the menu (click on "File" or right-click
// on the display.
// ------------------------------------------------------------------------------ 
#define STR_DISPLAYOPTIONS "Display Options"
void CShaderLinkerDemo::UpdateProperties()
{
	EBEffect::UpdateProperties();

	AddProperty(new EBProperty("Wireframe", OBJECT_MEMBER(m_bWireframe), EBTYPE_BOOL_PROP));
	AddProperty(new EBProperty("Pause", OBJECT_MEMBER(m_bPause), EBTYPE_BOOL_PROP));

	// Expose our poly count to the framework
	AddProperty(new EBProperty("TriangleCount", OBJECT_MEMBER(m_dwNumTriangles), EBTYPE_DWORD_PROP));
	AddProperty(new EBProperty("VertexCount", OBJECT_MEMBER(m_dwNumVertices), EBTYPE_DWORD_PROP));

	EBEnumProperty* pEnumProp = new EBEnumProperty(STR_DISPLAYOPTIONS, OBJECT_MEMBER(m_eDisplayOption), EBTYPE_DWORD_PROP, PROPFLAG_CALLBACK);
	pEnumProp->AddEnumerant(new EBEnumValue(pEnumProp, "Show Sphere", (DWORD)DISPLAY_SPHERE, EBTYPE_DWORD_PROP));
	pEnumProp->AddEnumerant(new EBEnumValue(pEnumProp, "Show Model", (DWORD)DISPLAY_MODEL, EBTYPE_DWORD_PROP));
	AddProperty(pEnumProp);

	AddProperty(new EBTriggerProperty("Load another model..."));

	// Vertex shaders
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Shared", GetFilePath("miscvertex.nvf"), EBTYPE_STRING_PROP));
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Shared Lighting", GetFilePath("lighting.nvf"), EBTYPE_STRING_PROP));
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "EyeSpace", GetFilePath("eyespace.nvf"), EBTYPE_STRING_PROP));
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "ObjectSpace", GetFilePath("objectspace.nvf"), EBTYPE_STRING_PROP));
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Basis Skinning", GetFilePath("basisskinning.nvf"), EBTYPE_STRING_PROP));
	m_pVertexShaderEnum->AddEnumerant(new EBEnumValue(m_pVertexShaderEnum, "Output", GetFilePath("output.nvv"), EBTYPE_STRING_PROP));
}

 

// ------------------------------------------------------------------------------
// CShaderLinkerDemo::ConfirmDevice
//
// Description: Performs caps-bit checking to make sure the selected device 
//		supports this demo.  In this particular case we check for nothing!
// ------------------------------------------------------------------------------ 
HRESULT CShaderLinkerDemo::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 & D3DPTEXTURECAPS_PROJECTED))
	{
		m_strLastError = "Device does not support 3 element texture coordinates!";
		return E_FAIL;
	}
*/
	return S_OK;
}

HRESULT CShaderLinkerDemo::CreateCubeEnvironment()
{
	HRESULT hr;
    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;
	}

    // -Z face
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f));

    // +Z face
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f));

    // -Y face
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f));

    // +Y face
	*pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f));

    // -X face
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f, 1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3( 1.0f,-1.0f,-1.0f));

    // +X face
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f,-1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f,-1.0f, 1.0f));
    *pVertices++ = WorldBoxVertex(D3DXVECTOR3(-1.0f, 1.0f, 1.0f));

	m_pWorldBoxVertices->Unlock();

	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;
	}

    // 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;

	m_pWorldBoxIndices->Unlock();

	D3DXMatrixIdentity(&m_matCubeRotation);
	D3DXMatrixRotationY(&m_matCubeRotation, D3DXToRadian(180.0f));

	return S_OK;
}

HRESULT CShaderLinkerDemo::LoadFragmentFile(const std::string& strFragmentFile)
{
	void* pFragments = NULL;
	DWORD dwFileSize;

	// In the initialisation phase we give the linker all the info it needs to
	// assemble fragments.
	try
	{
		HANDLE hFile;

		hFile = CreateFile(GetFilePath(strFragmentFile.c_str()).c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
		if(hFile == INVALID_HANDLE_VALUE)
		{
			m_strLastError = "Could not find file " + strFragmentFile;
			return E_FAIL;
		}

		dwFileSize = GetFileSize(hFile, NULL);
		pFragments = (void*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwFileSize);
		if (!pFragments)
		{
			m_strLastError = "Failed to allocate memory for fragments";
			return E_FAIL;
		}

		ReadFile(hFile, pFragments, dwFileSize, &dwFileSize, NULL);
		CloseHandle(hFile);

		DWORD dwRet = m_pLinker->AddFragments(pFragments, dwFileSize);
		if (dwRet != NVLINK_OK)
		{
			m_strLastError = "Failed to give linker fragments";
			return E_FAIL;
		}
		HeapFree(GetProcessHeap(), 0, (void*)pFragments);

	}
	catch(...)
	{
		m_strLastError = "Error opening file " + strFragmentFile;
		return E_FAIL;
	}
	return S_OK;
}



// ------------------------------------------------------------------------------
// CShaderLinkerDemo::Initialize
//
// Description: Initialize render-states, vertex shader and vertex buffers
// ------------------------------------------------------------------------------ 
HRESULT CShaderLinkerDemo::Initialize(IDirect3DDevice8* pDev)
{
	HRESULT hr = S_OK;

	srand(timeGetTime());
	m_strLastError = "";

	m_bSetShaderToShow = false;

	ZeroMemory(&m_DialogFunc, sizeof(LinkerDialogFunc));
	m_hDialog = LoadLibrary(GetFilePath("linkerdialog.dll").c_str());
	if (!m_hDialog)
	{
		m_strLastError = "Could not load linkerdialog.dll";
		return E_FAIL;
	}

	m_DialogFunc.ShowDialog = (LPSHOWDIALOG)GetProcAddress(m_hDialog, "LinkerDialog_ShowDialog");
	m_DialogFunc.Initialise = (LPINITIALISE)GetProcAddress(m_hDialog, "LinkerDialog_Initialise");
	m_DialogFunc.Free = (LPFREE)GetProcAddress(m_hDialog, "LinkerDialog_Free");
	m_DialogFunc.GetSettings = (LPGETSETTINGS)GetProcAddress(m_hDialog, "LinkerDialog_GetSettings");
	m_DialogFunc.SetSettings = (LPSETSETTINGS)GetProcAddress(m_hDialog, "LinkerDialog_SetSettings");

	if (!m_DialogFunc.ShowDialog ||
		!m_DialogFunc.Initialise ||
		!m_DialogFunc.Free ||
		!m_DialogFunc.GetSettings ||
		!m_DialogFunc.SetSettings)
	{
		m_strLastError = "Could not resolve dialog box functions!";
		return E_FAIL;
	}


	D3DDEVICE_CREATION_PARAMETERS CreateParams;	
	pDev->GetCreationParameters(&CreateParams);
	HWND hParent = GetParent(CreateParams.hFocusWindow);
	if (!m_DialogFunc.Initialise(CreateParams.hFocusWindow))
	{
		m_strLastError = "Could not initialize dialog box!";
		return E_FAIL;
	}

	m_DialogFunc.ShowDialog(true);

	// Create the linker
	m_hLinker = LoadLibrary(GetFilePath("nvlinker.dll").c_str());
	if (m_hLinker == NULL)
	{
		m_strLastError = "Could not create linker dll (nvlinker.dll)";
		return E_FAIL;

⌨️ 快捷键说明

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