DirectX10 Rendering Engine – Material_Lua.cpp

 

#include "material.h"
#include "engine.h"

extern CMaterial* g_pLuaSharedMaterial;

//-------------------------------------------------------------------------
// Lua helper to get values from table
//-------------------------------------------------------------------------
void Lua_GetField( lua_State* L, const char* key, const char** szResult )
{
	lua_pushstring(L, key);
	lua_gettable(L, -2);  /* get key */

	// not valid
	if (!lua_isstring(L, -1))
	{
		*szResult = "";
	}
	else
	{
		*szResult = lua_tostring(L, -1);
	}

	lua_pop(L, 1);  /* remove entry */
}

//-------------------------------------------------------------------------
// Lua function to set up a material
//-------------------------------------------------------------------------
int CMaterial::Lua_SetMaterial( lua_State* L )
{
	lua_getglobal(L, g_pLuaSharedMaterial->GetName() );

	if (!lua_istable(L, -1))
		return 0;

	const char* szFieldValue = new char[255];
	szFieldValue = "";

	// load diffuse texture
	Lua_GetField(L, "Diffuse", &szFieldValue );

	if (strcmp(szFieldValue,"") != 0)
		g_pLuaSharedMaterial->m_pDiffuseTexture =  Engine()->GetTextureManager()->AddResource( szFieldValue, "..//Content//Textures//" );

	szFieldValue = "";

	// load normal map texture
	Lua_GetField(L, "NormalMap", &szFieldValue );

	if (strcmp(szFieldValue,"") != 0)
		g_pLuaSharedMaterial->m_pNormalTexture =  Engine()->GetTextureManager()->AddResource( szFieldValue, "..//Content//Textures//" );

	szFieldValue = "";

	// load height map texture
	Lua_GetField(L, "HeightMap", &szFieldValue );

	if (strcmp(szFieldValue,"") != 0)
		g_pLuaSharedMaterial->m_pHeightTexture =  Engine()->GetTextureManager()->AddResource( szFieldValue, "..//Content//Textures//" );

	szFieldValue = "";

	// load shader file
	Lua_GetField(L, "Shader", &szFieldValue );

	if (strcmp(szFieldValue,"") != 0)
	{
		char* szFilename = new char[ strlen(szFieldValue) + 4 ];
		sprintf_s( szFilename, strlen(szFieldValue) + 4, "%s.fx", szFieldValue );

		CEffect* pEffect = Engine()->GetEffectManager()->AddResource( szFilename, "..//Content//Shaders//" );
		g_pLuaSharedMaterial->m_pEffect = pEffect;

		szFieldValue = "";

		// get rendering technique
		Lua_GetField(L, "Technique", &szFieldValue );

		if ( pEffect && strcmp(szFieldValue,"") != 0)
		{
			pEffect->SetTechnique( szFieldValue );
		}
	}

	return 0;
}