Ejemplo n.º 1
0
	//--
	CgShaderCel::CgShaderCel(CGcontext cgContext) : 
		CgShader(cgContext, CgShader::VertexFragment)
	{
		// [rad] This is vertex and fragment shader
	
		// [rad] Setup vertex shader:
		
		// [rad] Setup vertex shader entry point
		SetEntry(CgShader::Vertex, "main");

		static const std::string sProgramVertex = "							\
																			\
		void main(	float4 inPosition			: POSITION,					\
					float4 inNormal				: NORMAL,					\
																			\
					out float4 outOPosition		: POSITION,					\
					out float4 outVPosition		: TEXCOORD0,				\
					out float4 outNormal		: TEXCOORD1,				\
																			\
					uniform float4x4 uniModelViewProjMat,					\
					uniform float4x4 uniModelViewITMat,						\
					uniform float4x4 uniModelViewMat)						\
		{																	\
			outOPosition = mul(uniModelViewProjMat, inPosition);			\
			outVPosition = mul(uniModelViewMat, inPosition);				\
																			\
			outNormal = mul(uniModelViewITMat, inNormal);					\
			outNormal.xyz = normalize(outNormal.xyz);						\
		}																	";
		
		// [rad] Setup vertex shader program
		SetProgram(CgShader::Vertex, sProgramVertex);
		
		// [rad] Setup fragment shader:
		
		// [rad] Setup fragment shader entry point
		SetEntry(CgShader::Fragment, "main");
		
		static const std::string sProgramFragment = "						\
																			\
		void main(															\
					float4 inPosition					: TEXCOORD0,		\
					float4 inNormal						: TEXCOORD1,		\
																			\
					out float4 outColor					: COLOR,			\
																			\
					uniform sampler1D uniTexDiffuse,						\
					uniform sampler1D uniTexSpecular,						\
					uniform sampler1D uniTexOutline,						\
																			\
					uniform float3 uniMatDiffuse,							\
					uniform float3 uniMatSpecular,							\
					uniform float3 uniLightColor,							\
					uniform float3 uniLightPosition,						\
					uniform float3 uniEyePosition,							\
					uniform float uniShininess,								\
																			\
					uniform int uniComputeOutline)							\
		{																	\
																			\
			float3 p = inPosition.xyz;										\
			float3 n = normalize(inNormal.xyz);								\
																			\
			float3 l = normalize(uniLightPosition - p);						\
			float diffuseL = max(dot(n, l), 0);								\
			diffuseL = tex1D(uniTexDiffuse, diffuseL).x;					\
			float3 diffuse = uniLightColor * diffuseL * uniMatDiffuse;		\
																			\
			float3 v = normalize(uniEyePosition - p);						\
			float3 h = normalize(l + v);									\
			float specularL = pow(max(dot(n, h), 0), uniShininess);			\
			if(diffuseL <= 0) specularL = 0;								\
			specularL = tex1D(uniTexSpecular, specularL).x;					\
			float3 specular = uniLightColor * specularL * uniMatSpecular;	\
																			\
			float edgeL = 1;												\
																			\
			if(uniComputeOutline)											\
			{																\
				edgeL = max(dot(n, v), 0);									\
				edgeL = tex1D(uniTexOutline, edgeL).x;						\
			}																\
																			\
			outColor.x = edgeL * (diffuse + specular);						\
			outColor.y = edgeL * (diffuse + specular);						\
			outColor.z = edgeL * (diffuse + specular);						\
			outColor.w = 1;													\
		}																	";

		// float3 diffuse = diffuseL * uniMatDiffuse;
		// float3 specular = specularL * uniMatSpecular;
		
		// [rad] Setup fragment shader program
		SetProgram(CgShader::Fragment, sProgramFragment);
		
		// [rad] Create shaders (both fragment and vertex)
		Create();
		
		// [rad] Set params - vertex shader
		m_cgParamModelViewProjMatrix = cgGetNamedParameter(m_cgShaderVertex, "uniModelViewProjMat");
		m_cgParamModelViewMatrix = cgGetNamedParameter(m_cgShaderVertex, "uniModelViewMat");
		m_cgParamModelViewITMatrix = cgGetNamedParameter(m_cgShaderVertex, "uniModelViewITMat");
		
		// [rad] Set params - fragment shader				
		m_cgParamLightColorVector = cgGetNamedParameter(m_cgShaderFragment, "uniLightColor");
		m_cgParamLightPositionVector = cgGetNamedParameter(m_cgShaderFragment, "uniLightPosition");	
		
		m_cgParamEyePositionVector = cgGetNamedParameter(m_cgShaderFragment, "uniEyePosition");	
		
		m_cgParamShininess = cgGetNamedParameter(m_cgShaderFragment, "uniShininess");	
		
		m_cgParamMatDiffuseVector = cgGetNamedParameter(m_cgShaderFragment, "uniMatDiffuse");	
		m_cgParamMatSpecularVector = cgGetNamedParameter(m_cgShaderFragment, "uniMatSpecular");
		
		m_cgParamSamplerDiffuse = cgGetNamedParameter(m_cgShaderFragment, "uniTexDiffuse");	
		m_cgParamSamplerSpecular = cgGetNamedParameter(m_cgShaderFragment, "uniTexSpecular");	
		m_cgParamSamplerOutline = cgGetNamedParameter(m_cgShaderFragment, "uniTexOutline");	
		
		m_cgParamComputeOutline = cgGetNamedParameter(m_cgShaderFragment, "uniComputeOutline");	
		
		// [rad] Manually assign other (material) related params
		cgSetParameter1f(m_cgParamShininess, 128.0f);
	
		static const float af3ColorDiffuse[3] = {0.8f, 0.8f, 0.8f};
		cgSetParameter3fv(m_cgParamMatDiffuseVector, af3ColorDiffuse); 
		
		static const float af3ColorSpecular[3] = {1.0f, 1.0f, 1.0f};
		cgSetParameter3fv(m_cgParamMatSpecularVector, af3ColorSpecular);
		
		static const float af3Color[3] = {1.0f, 1.0f, 1.0f};
		cgSetParameter3fv(m_cgParamLightColorVector, af3Color);
		
		// [rad] Set texture params
		GenerateTextures();
		
		cgGLSetTextureParameter(m_cgParamSamplerDiffuse, m_glIDTextureDiffuse);
		cgGLSetTextureParameter(m_cgParamSamplerSpecular, m_glIDTextureSpecular);
		cgGLSetTextureParameter(m_cgParamSamplerOutline, m_glIDTextureOutline);
		
	}
Ejemplo n.º 2
0
	//--
	void
	CgShaderCel::SetParamLightColor(const float* pVec)
	{
		cgSetParameter3fv(m_cgParamLightColorVector, pVec); 
	}
Ejemplo n.º 3
0
 void ShaderParameterCg::Set(glm::detail::tvec3<float> const& vector)
 {
     cgSetParameter3fv(this->_param, (float*)&vector);
 }
Ejemplo n.º 4
0
	//--
	void
	CgShaderCel::SetParamEyePosition(const float* pVec)
	{
		cgSetParameter3fv(m_cgParamEyePositionVector, pVec); 
	}
Ejemplo n.º 5
0
 //
 // SetFloatVec3
 //
 void CCgUniform::SetFloatVec3( 
                               const Vec3<GLfloat>& v 
                               )
 {
     cgSetParameter3fv( m_Parameter, &v[ 0 ] );
 }