Пример #1
0
void Clouds::createLightMatrices(Matrix4& lightView, Matrix4& lightProjection, float& farPlaneDistance,
								const Matrix4& viewProj, 
								const Vector3& viewDir, const Vector3& cameraPos,
								const Vector3& lightDir)
{
	Vector3 lightUp(viewDir.x, -(lightDir.z*viewDir.z+lightDir.x*viewDir.x)/lightDir.y, viewDir.z);	// why does this work? imported code from a old project ...
	Matrix4 matLightCamera(Matrix4::camera(0.0f, -lightDir, lightUp));
	Matrix4 mViewProjInv_LightCamera(viewProj.invert() * matLightCamera);
	Vector3 edges[8];
	edges[0] = Vector3(-1.0f, -1.0f, -1.0f) * mViewProjInv_LightCamera;
	edges[1] = Vector3(1.0f, -1.0f, -1.0f) * mViewProjInv_LightCamera;
	edges[2] = Vector3(-1.0f, 1.0f, -1.0f) * mViewProjInv_LightCamera;
	edges[3] = Vector3(1.0f, 1.0f, -1.0f) * mViewProjInv_LightCamera;
	edges[4] = Vector3(-1.0f, -1.0f, 1.0f) * mViewProjInv_LightCamera;
	edges[5] = Vector3(1.0f, -1.0f, 1.0f) * mViewProjInv_LightCamera;
	edges[6] = Vector3(-1.0f, 1.0f, 1.0f) * mViewProjInv_LightCamera;
	edges[7] = Vector3(1.0f, 1.0f, 1.0f) * mViewProjInv_LightCamera;

	// create box
	Vector3 min(999999);
	Vector3 max(-999999);
	for(int i=0; i<8; i++)
	{
		min.x = std::min(edges[i].x, min.x);
		min.y = std::min(edges[i].y, min.y);
		min.z = std::min(edges[i].z, min.z);
		max.x = std::max(edges[i].x, max.x);
		max.y = std::max(edges[i].y, max.y);
		max.z = std::max(edges[i].z, max.z);
	}

	// find good camera position
	farPlaneDistance = max.z - min.z;
	Vector3 lightPos = Vector3((min.x + max.x)*0.5f, (min.y + max.y)*0.5f, min.z) * matLightCamera.invert();
	lightView = Matrix4::camera(lightPos, lightPos+lightDir, lightUp);
	// ortho
	lightProjection = Matrix4::projectionOrthogonal(max.x - min.x, max.y - min.y, 0, farPlaneDistance);
}
Пример #2
0
void loop() {
  lightUp();
}