void Scene::BindReflectionMaps()
{
	Camera* ac = ActiveCam();
	for (int i = 0; i < models.size(); i++)
	{
		if(!models[i]->GetDrawEnvMap())
		{
			continue;
		}
		GLint viewport[4];
		glGetIntegerv( GL_VIEWPORT, viewport );
		glViewport(0,0,256,256);
		bool aaWasEnabled = glIsEnabled( GL_MULTISAMPLE );
		glDisable( GL_MULTISAMPLE );
		vec4 bbc = models[i]->Transformation() * vec4( models[i]->BoundingBoxCenter(), 1);
		vec3 bbCenter = vec3( bbc.x/bbc.w , bbc.y/ bbc.w, bbc.z/bbc.w);
		vec3 v = bbCenter - ac->Eye();
		vec3 uppp = cross( cross( ac->At() - ac->Eye(), ac->Up()), ac->At() - ac->Eye());
		vec3 xxx = normalize(cross(v, uppp));
		vec3 yyy = normalize(cross(xxx, v));
		vec3 zzz = normalize(cross(xxx, yyy));

		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_X, LookAtMat(bbCenter, bbCenter+xxx, -yyy), PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, LookAtMat(bbCenter, bbCenter-xxx, -yyy), PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, LookAtMat(bbCenter, bbCenter+yyy, zzz),  PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, LookAtMat(bbCenter, bbCenter-yyy, -zzz), PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, LookAtMat(bbCenter, bbCenter+zzz, -yyy), PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		AddReflectionTexture(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, LookAtMat(bbCenter, bbCenter-zzz, -yyy), PerspectiveMat(72.3,1, 0.01, 2* ac->ZFar()), models[i]);
		
		
		glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
		if (aaWasEnabled) {
			glEnable(GL_MULTISAMPLE);
		}
	}
	

}