Example #1
0
bool CManipRot::UIEventHandler( const ui::Event& EV )
{	
	int ex = EV.miX;
	int ey = EV.miY;
	
	CVector2 posubp = EV.GetUnitCoordBP();

	CCamera *pcam = mManager.GetActiveCamera();
	
	bool brval = false;
			
	bool isshift = false; //CSystem::IsKeyDepressed(VK_SHIFT );
	bool isctrl = false; //CSystem::IsKeyDepressed(VK_CONTROL );
	
	switch( EV.miEventCode )
	{
		case ui::UIEV_PUSH:
		{	
			mManager.mManipHandler.Init(posubp, pcam->mCameraData.GetIVPMatrix(), pcam->QuatC );
			mBaseTransform = mManager.mCurTransform;

			SelectBestPlane(posubp);

			brval = true;
		}
		break;

		case ui::UIEV_RELEASE:
		{
			mManager.DisableManip();

			brval = true;
		}
		break;

		case ui::UIEV_DRAG:
		{
			IntersectWithPlanes( posubp );

			if ( CheckIntersect() )
			{	
				///////////////////////////////////////////
				// calc normalvectors from base:origin to point on activeintersection plane (in world space)
				const CVector3 & Origin = mBaseTransform.GetTransform().GetPosition();
				CVector3 D1 = (Origin-mActiveIntersection->mIntersectionPoint).Normal();
				CVector3 D0 = (Origin-mActiveIntersection->mBaseIntersectionPoint).Normal();
				///////////////////////////////////////////
				// calc matrix to put worldspace vector into plane local space
				CMatrix4 MatWldToObj = mBaseTransform.GetTransform().GetMatrix(); //GetRotation();
				MatWldToObj.Inverse();
				CVector4 bAxisAngle = mLocalRotationAxis; 
				CQuaternion brq;
				brq.FromAxisAngle(bAxisAngle);
				CMatrix4 MatObjToPln = brq.ToMatrix();
				MatObjToPln.Inverse();
				CMatrix4 MatWldToPln = MatObjToPln*MatWldToObj;
				//CMatrix4 MatInvRot = InvQuat.ToMatrix();
				///////////////////////////////////////////
				// calc plane local rotation
				CVector4 AxisAngle = mLocalRotationAxis; 
				CVector4 D0I = CVector4(D0,CReal(0.0f)).Transform(MatWldToPln);
				CVector4 D1I = CVector4(D1,CReal(0.0f)).Transform(MatWldToPln);
				//orkprintf( "D0 <%f %f %f>\n", float(D0.GetX()), float(D0.GetY()), float(D0.GetZ()) );
				//orkprintf( "D1 <%f %f %f>\n", float(D1.GetX()), float(D1.GetY()), float(D1.GetZ()) );
				//orkprintf( "D0I <%f %f %f>\n", float(D0I.GetX()), float(D0I.GetY()), float(D0I.GetZ()) );
				//orkprintf( "D1I <%f %f %f>\n", float(D1I.GetX()), float(D1I.GetY()), float(D1I.GetZ()) );
				AxisAngle.SetW( CalcAngle(D0I,D1I) );
				CQuaternion RotQ;
				RotQ.FromAxisAngle( AxisAngle );
				///////////////////
				// Rot Snap
				if( isshift )
				{	CReal SnapAngleVal( PI2/16.0f );
					CVector4 NewAxisAngle = RotQ.ToAxisAngle();
					CReal Angle = NewAxisAngle.GetW();
					Angle = SnapReal( Angle, SnapAngleVal );
					NewAxisAngle.SetW( Angle );
					RotQ.FromAxisAngle( NewAxisAngle );
				}
				///////////////////
				// accum rotation
				CQuaternion oq = mBaseTransform.GetTransform().GetRotation();
				CQuaternion NewQ = RotQ.Multiply(oq);
				///////////////////
				// Rot Reset To Identity
				if( isctrl && isshift )
				{
					NewQ.FromAxisAngle( CVector4( CReal(0.0f), CReal(1.0f), CReal(0.0f), CReal(0.0f) ) );
				}
				///////////////////
				TransformNode mset = mManager.mCurTransform;
				mset.GetTransform().SetRotation( NewQ );
				mManager.ApplyTransform( mset );
				///////////////////
			}

			brval = true;
		}
		break;

		default:
			break;
	}

	return brval;
}
void CDx11UmbralEffect::UpdateConstants(const Palleon::DX11VIEWPORT_PARAMS& viewportParams, Palleon::CMaterial* material, const CMatrix4& worldMatrix)
{
	//Update vertex shader params
	{
		auto worldITMatrix = worldMatrix.Inverse().Transpose();
		auto viewITMatrix = viewportParams.viewMatrix.Inverse().Transpose();
		auto worldViewMatrix = worldMatrix * viewportParams.viewMatrix;
		auto worldViewProjMatrix = worldViewMatrix * viewportParams.projMatrix;
		CVector3 modelBBoxOffset(0, 0, 0);
		CVector3 modelBBoxScale(1, 1, 1);

		auto vertexOcclusionScale = material->GetEffectParamOrDefault("vs_vertexOcclusionScale", 0.0f, m_vertexOcclusionScaleOffset != -1);
		auto vertexColorBias = material->GetEffectParamOrDefault("vs_vertexColorBias", CVector4(0, 0, 0, 0), m_vertexColorBiasOffset != -1);

		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		HRESULT result = m_deviceContext->Map(m_vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
		assert(SUCCEEDED(result));
		auto constantBufferPtr = reinterpret_cast<uint8*>(mappedResource.pData);

		memset(constantBufferPtr, 0, mappedResource.RowPitch);

		SetParamValue(constantBufferPtr, m_modelBBoxOffsetOffset, modelBBoxOffset);
		SetParamValue(constantBufferPtr, m_modelBBoxScaleOffset, modelBBoxScale);
		SetParamValue(constantBufferPtr, m_vertexOcclusionScaleOffset, vertexOcclusionScale);
		SetParamValue(constantBufferPtr, m_vertexColorBiasOffset, vertexColorBias);
		SetParamValue<uint32>(constantBufferPtr, m_isUseInstancingOffset, 0);
		if(m_viewITMatrixOffset != -1)			*reinterpret_cast<CMatrix4*>(constantBufferPtr + m_viewITMatrixOffset) = viewITMatrix.Transpose();
		if(m_worldITMatrixOffset != -1)			*reinterpret_cast<CMatrix4*>(constantBufferPtr + m_worldITMatrixOffset) = worldITMatrix.Transpose();
		if(m_worldMatrixOffset != -1)			*reinterpret_cast<CMatrix4*>(constantBufferPtr + m_worldMatrixOffset) = worldMatrix.Transpose();
		if(m_worldViewMatrixOffset != -1)		*reinterpret_cast<CMatrix4*>(constantBufferPtr + m_worldViewMatrixOffset) = worldViewMatrix.Transpose();
		if(m_worldViewProjMatrixOffset != -1)	*reinterpret_cast<CMatrix4*>(constantBufferPtr + m_worldViewProjMatrixOffset) = worldViewProjMatrix.Transpose();

		m_deviceContext->Unmap(m_vertexConstantBuffer, 0);
	}

	//Update pixel shader params
	{
		auto modulateColor = material->GetEffectParamOrDefault("ps_modulateColor", CVector4(1, 1, 1, 1), m_modulateColorOffset != -1);
		auto ambientColor = material->GetEffectParamOrDefault("ps_ambientColor", CVector4(0, 0, 0, 0), m_ambientColorOffset != -1);
		auto diffuseColor = material->GetEffectParamOrDefault("ps_diffuseColor", CVector4(0, 0, 0, 0), m_diffuseColorOffset != -1);
		auto specularColor = material->GetEffectParamOrDefault("ps_specularColor", CVector4(0, 0, 0, 0), m_specularColorOffset != -1);
		auto shininess = material->GetEffectParamOrDefault("ps_shininess", 128.f, m_shininessOffset != -1);
		auto reflectivity = material->GetEffectParamOrDefault("ps_reflectivity", CVector3(0, 0, 0), m_reflectivityOffset != -1);
		auto normalPower = material->GetEffectParamOrDefault("ps_normalPower", 1.f, m_normalPowerOffset != -1);

		auto multiDiffuseColor = material->GetEffectParamOrDefault("ps_multiDiffuseColor", CVector4(0, 0, 0, 0), m_multiDiffuseColorOffset != -1);
		auto multiSpecularColor = material->GetEffectParamOrDefault("ps_multiSpecularColor", CVector4(0, 0, 0, 0), m_multiSpecularColorOffset != -1);
		auto multiShininess = material->GetEffectParamOrDefault("ps_multiShininess", 128.f, m_multiShininessOffset != -1);
		auto multiReflectivity = material->GetEffectParamOrDefault("ps_multiReflectivity", CVector3(0, 0, 0), m_multiReflectivityOffset != -1);
		auto multiNormalPower = material->GetEffectParamOrDefault("ps_multiNormalPower", 1.f, m_multiNormalPowerOffset != -1);

		auto fresnelExp = material->GetEffectParamOrDefault("ps_fresnelExp", 1.f, m_fresnelExpOffset != -1);
		auto fresnelLightDiffBias = material->GetEffectParamOrDefault("ps_fresnelLightDiffBias", 1.f, m_fresnelLightDiffBiasOffset != -1);
		auto specularInfluence = material->GetEffectParamOrDefault("ps_specularInfluence", 0.f, m_specularInfluenceOffset != -1);
		auto lightDiffusePower = material->GetEffectParamOrDefault("ps_lightDiffusePower", 0.67f, m_lightDiffusePowerOffset != -1);
		auto lightDiffuseInfluence = material->GetEffectParamOrDefault("ps_lightDiffuseInfluence", 0.56f, m_lightDiffuseInfluenceOffset != -1);
		auto reflectMapInfluence = material->GetEffectParamOrDefault("ps_reflectMapInfluence", 0.8f, m_reflectMapInfluenceOffset != -1);

		auto glareLdrScale = material->GetEffectParamOrDefault("ps_glareLdrScale", 1.0f, m_glareLdrScaleOffset != -1);
		auto refAlphaRestrain = material->GetEffectParamOrDefault("ps_refAlphaRestrain", 0.f, m_refAlphaRestrainOffset != -1);
		auto normalVector = material->GetEffectParamOrDefault("ps_normalVector", CVector4(0, 0, 0, 0), m_normalVectorOffset != -1);
		auto depthBias = material->GetEffectParamOrDefault("ps_depthBias", 0.f, m_depthBiasOffset != -1);
		auto velvetParam = material->GetEffectParamOrDefault("ps_velvetParam", CVector2(0, 0), m_velvetParamOffset != -1);

		auto ambientOcclusionColor = material->GetEffectParamOrDefault("ps_ambentOcclusionColor", CVector3(1, 1, 1), m_ambientOcclusionColorOffset != -1);
		auto mainLightOcclusionColor = material->GetEffectParamOrDefault("ps_mainLightOcclusionColor", CVector3(0, 0, 0), m_mainLightOcclusionColorOffset != -1);
		auto subLightOcclusionColor = material->GetEffectParamOrDefault("ps_subLightOcclusionColor", CVector3(0, 0, 0), m_subLightOcclusionColorOffset != -1);
		auto pointLightOcclusionColor = material->GetEffectParamOrDefault("ps_pointLightOcclusionColor", CVector3(0.5f, 0.5f, 0.5f), m_pointLightOcclusionColorOffset != -1);
		auto lightMapOcclusionColor = material->GetEffectParamOrDefault("ps_lightMapOcclusionColor", CVector3(0.3f, 0.3f, 0.3f), m_lightMapOcclusionColorOffset != -1);
		auto reflectMapOcclusionColor = material->GetEffectParamOrDefault("ps_reflectMapOcclusionColor", CVector3(0.3f, 0.3f, 0.3f), m_reflectMapOcclusionColorOffset != -1);
		auto specularOcclusionColor = material->GetEffectParamOrDefault("ps_specularOcclusionColor", CVector3(0, 0, 0), m_specularOcclusionColorOffset != -1);

		float lightDiffuseMapLod = 0;
		float reflectMapLod = 0;

		CVector2 pixelClippingDistance(-10000, 10000);
		CVector3 enableShadowFlag(1, 0, 1);
		CVector3 latitudeParam(1, 0, 1);
		CVector4 ambientLightColor = viewportParams.viewport->GetEffectParamOrDefault("ps_ambientLightColor", CVector4(0.1f, 0.1f, 0.1f, 0));

		CVector3 dirLightDirections[2] = 
		{
			viewportParams.viewport->GetEffectParamOrDefault("ps_dirLightDirection0", CVector3(0, 0, 0)),
			viewportParams.viewport->GetEffectParamOrDefault("ps_dirLightDirection1", CVector3(0, 0, 0))
		};

		CVector4 dirLightColors[2] =
		{
			viewportParams.viewport->GetEffectParamOrDefault("ps_dirLightColor0", CVector4(0, 0, 0, 0)),
			viewportParams.viewport->GetEffectParamOrDefault("ps_dirLightColor1", CVector4(0, 0, 0, 0))
		};

		D3D11_MAPPED_SUBRESOURCE mappedResource = {};
		HRESULT result = m_deviceContext->Map(m_pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
		assert(SUCCEEDED(result));
		auto constantBufferPtr = reinterpret_cast<uint8*>(mappedResource.pData);

		memset(constantBufferPtr, 0, mappedResource.RowPitch);

		SetParamValue(constantBufferPtr, m_pixelClippingDistanceOffset, pixelClippingDistance);

		SetParamValue(constantBufferPtr, m_modulateColorOffset, modulateColor);
		SetParamValue(constantBufferPtr, m_ambientColorOffset, ambientColor);
		SetParamValue(constantBufferPtr, m_diffuseColorOffset, diffuseColor);
		SetParamValue(constantBufferPtr, m_specularColorOffset, specularColor);
		SetParamValue(constantBufferPtr, m_shininessOffset, shininess);
		SetParamValue(constantBufferPtr, m_reflectivityOffset, reflectivity);
		SetParamValue(constantBufferPtr, m_normalPowerOffset, normalPower);

		SetParamValue(constantBufferPtr, m_multiDiffuseColorOffset, multiDiffuseColor);
		SetParamValue(constantBufferPtr, m_multiSpecularColorOffset, multiSpecularColor);
		SetParamValue(constantBufferPtr, m_multiShininessOffset, multiShininess);
		SetParamValue(constantBufferPtr, m_multiReflectivityOffset, multiReflectivity);
		SetParamValue(constantBufferPtr, m_multiNormalPowerOffset, multiNormalPower);

		SetParamValue(constantBufferPtr, m_fresnelExpOffset, fresnelExp);
		SetParamValue(constantBufferPtr, m_fresnelLightDiffBiasOffset, fresnelLightDiffBias);
		SetParamValue(constantBufferPtr, m_specularInfluenceOffset, specularInfluence);
		SetParamValue(constantBufferPtr, m_lightDiffusePowerOffset, lightDiffusePower);
		SetParamValue(constantBufferPtr, m_lightDiffuseInfluenceOffset, lightDiffuseInfluence);
		SetParamValue(constantBufferPtr, m_lightDiffuseMapLodOffset, lightDiffuseMapLod);
		SetParamValue(constantBufferPtr, m_reflectMapInfluenceOffset, reflectMapInfluence);
		SetParamValue(constantBufferPtr, m_reflectMapLodOffset, reflectMapLod);

		SetParamValue(constantBufferPtr, m_glareLdrScaleOffset, glareLdrScale);
		SetParamValue(constantBufferPtr, m_normalVectorOffset, normalVector);
		SetParamValue(constantBufferPtr, m_depthBiasOffset, depthBias);
		SetParamValue(constantBufferPtr, m_refAlphaRestrainOffset, refAlphaRestrain);
		SetParamValue(constantBufferPtr, m_velvetParamOffset, velvetParam);

		SetParamValue(constantBufferPtr, m_ambientOcclusionColorOffset, ambientOcclusionColor);
		SetParamValue(constantBufferPtr, m_specularOcclusionColorOffset, specularOcclusionColor);
		SetParamValue(constantBufferPtr, m_pointLightOcclusionColorOffset, pointLightOcclusionColor);
		SetParamValue(constantBufferPtr, m_mainLightOcclusionColorOffset, mainLightOcclusionColor);
		SetParamValue(constantBufferPtr, m_subLightOcclusionColorOffset, subLightOcclusionColor);
		SetParamValue(constantBufferPtr, m_lightMapOcclusionColorOffset, lightMapOcclusionColor);
		SetParamValue(constantBufferPtr, m_reflectMapOcclusionColorOffset, reflectMapOcclusionColor);

		SetParamValue(constantBufferPtr, m_ambientLightColorOffset, ambientLightColor);
		SetParamValue(constantBufferPtr, m_latitudeParamOffset, latitudeParam);
		SetParamValue(constantBufferPtr, m_enableShadowFlagOffset, enableShadowFlag);
		if(m_dirLightDirectionsOffset != -1) reinterpret_cast<CVector3*>(constantBufferPtr + m_dirLightDirectionsOffset)[0] = dirLightDirections[0];
		if(m_dirLightDirectionsOffset != -1) reinterpret_cast<CVector3*>(constantBufferPtr + m_dirLightDirectionsOffset)[1] = dirLightDirections[1];
		if(m_dirLightColorsOffset != -1) reinterpret_cast<CVector4*>(constantBufferPtr + m_dirLightColorsOffset)[0] = dirLightColors[0];
		if(m_dirLightColorsOffset != -1) reinterpret_cast<CVector4*>(constantBufferPtr + m_dirLightColorsOffset)[1] = dirLightColors[1];

		m_deviceContext->Unmap(m_pixelConstantBuffer, 0);
	}
}