Example #1
0
void Shader::UpdateMaterialUniforms(const Material& _material, RenderingEngine& _renderer) {
	for (unsigned int i = 0; i < shaderData->uniformNames.size(); i++) {
		string uniformName = shaderData->uniformNames[i];
		string uniformType = shaderData->uniformTypes[i];
		string uniformPrefix = uniformName.substr(0, 2);
		if (uniformPrefix == "M_") {
			string unprefixedName = uniformName.substr(2, uniformName.length());
			if (uniformType == "sampler2D") {
				int samplerSlot = _renderer.GetSamplerSlot(unprefixedName);
				_material.GetTexture(unprefixedName)->Bind(samplerSlot);
				SetInt(uniformName, samplerSlot);
			} else if (uniformType == "vec3") {
				SetVector3(uniformName, *_material.GetVector3(unprefixedName));
			} else if (uniformType == "float") {
				SetFloat(uniformName, *_material.GetFloat(unprefixedName));
			} else {
				throw uniformType + " is not supported by the Material class";
			}
		}
	}
}
Example #2
0
void Shader::UpdateUniforms(const Transform& transform, const Material& material, const RenderingEngine& renderingEngine, const Camera& camera) const
{
	Matrix4f worldMatrix = transform.GetTransformation();
	Matrix4f projectedMatrix = camera.GetViewProjection() * worldMatrix;
	
	for(unsigned int i = 0; i < m_shaderData->GetUniformNames().size(); i++)
	{
		std::string uniformName = m_shaderData->GetUniformNames()[i];
		std::string uniformType = m_shaderData->GetUniformTypes()[i];
		
		if(uniformName.substr(0, 2) == "R_")
		{
			std::string unprefixedName = uniformName.substr(2, uniformName.length());
			
			if(unprefixedName == "lightMatrix")
				SetUniformMatrix4f(uniformName, renderingEngine.GetLightMatrix() * worldMatrix);
			else if(uniformType == "sampler2D")
			{
				int samplerSlot = renderingEngine.GetSamplerSlot(unprefixedName);
				renderingEngine.GetTexture(unprefixedName).Bind(samplerSlot);
				SetUniformi(uniformName, samplerSlot);
			}
			else if(uniformType == "vec3")
				SetUniformVector3f(uniformName, renderingEngine.GetVector3f(unprefixedName));
			else if(uniformType == "float")
				SetUniformf(uniformName, renderingEngine.GetFloat(unprefixedName));
			else if(uniformType == "DirectionalLight")
				SetUniformDirectionalLight(uniformName, *(const DirectionalLight*)&renderingEngine.GetActiveLight());
			else if(uniformType == "PointLight")
				SetUniformPointLight(uniformName, *(const PointLight*)&renderingEngine.GetActiveLight());
			else if(uniformType == "SpotLight")
				SetUniformSpotLight(uniformName, *(const SpotLight*)&renderingEngine.GetActiveLight());
			else
				renderingEngine.UpdateUniformStruct(transform, material, *this, uniformName, uniformType);
		}
		else if(uniformType == "sampler2D")
		{
			int samplerSlot = renderingEngine.GetSamplerSlot(uniformName);
			material.GetTexture(uniformName).Bind(samplerSlot);
			SetUniformi(uniformName, samplerSlot);
		}
		else if(uniformName.substr(0, 2) == "T_")
		{
			if(uniformName == "T_MVP")
				SetUniformMatrix4f(uniformName, projectedMatrix);
			else if(uniformName == "T_model")
				SetUniformMatrix4f(uniformName, worldMatrix);
			else
				throw "Invalid Transform Uniform: " + uniformName;
		}
		else if(uniformName.substr(0, 2) == "C_")
		{
			if(uniformName == "C_eyePos")
				SetUniformVector3f(uniformName, camera.GetTransform().GetTransformedPos());
			else
				throw "Invalid Camera Uniform: " + uniformName;
		}
		else
		{
			if(uniformType == "vec3")
				SetUniformVector3f(uniformName, material.GetVector3f(uniformName));
			else if(uniformType == "float")
				SetUniformf(uniformName, material.GetFloat(uniformName));
			else
				throw uniformType + " is not supported by the Material class";
		}
	}
}