void StelCore::moveObserverToSelected()
{
	StelObjectMgr* objmgr = GETSTELMODULE(StelObjectMgr);
	Q_ASSERT(objmgr);
	if (objmgr->getWasSelected())
	{
		Planet* pl = dynamic_cast<Planet*>(objmgr->getSelectedObject()[0].data());
		if (pl)
		{
			// We need to move to the selected planet. Try to generate a location from the current one
			StelLocation loc = getCurrentLocation();
			if (loc.planetName != pl->getEnglishName())
			{
				loc.planetName = pl->getEnglishName();
				loc.name = "-";
				loc.state = "";
				moveObserverTo(loc);

				LandscapeMgr* landscapeMgr = GETSTELMODULE(LandscapeMgr);
				if (pl->getEnglishName() == "Solar System Observer")
				{
					landscapeMgr->setFlagAtmosphere(false);
					landscapeMgr->setFlagFog(false);
					landscapeMgr->setFlagLandscape(false);
				}
				else
				{
					landscapeMgr->setFlagAtmosphere(pl->hasAtmosphere());
					landscapeMgr->setFlagFog(pl->hasAtmosphere());
					landscapeMgr->setFlagLandscape(true);
				}
			}
		}
	}
	StelMovementMgr* mmgr = GETSTELMODULE(StelMovementMgr);
	Q_ASSERT(mmgr);
	mmgr->setFlagTracking(false);
}
void TrailGroup::draw(StelCore* core, StelRenderer* renderer)
{
	renderer->setBlendMode(BlendMode_Alpha);
	float currentTime = core->getJDay();
	StelProjector::ModelViewTranformP transfo = core->getJ2000ModelViewTransform();
	transfo->combine(j2000ToTrailNativeInverted);
	StelProjectorP projector = core->getProjection(transfo);


	// We skip some positions to avoid drawing too many vertices when the history is long.
	int totalPositions = 0;
	foreach(const Trail& trail, allTrails)
	{
		totalPositions += trail.posHistory.size();
	}
	const int desiredMaxTotalTrailVertices = 8192;
	const int skip = std::max(1, totalPositions / desiredMaxTotalTrailVertices);

	for(int t = 0; t < allTrails.size(); t++) 
	{
		Trail& trail(allTrails[t]);
		Planet* hpl = dynamic_cast<Planet*>(trail.stelObject.data());
		if (hpl != NULL)
		{
			// Avoid drawing the trails if the object is the home planet
			QString homePlanetName = hpl == NULL ? "" : hpl->getEnglishName();
			if (homePlanetName == StelApp::getInstance().getCore()->getCurrentLocation().planetName)
			{
				continue;
			}
		}

		const QVector<Vec3f>& posHistory = trail.posHistory;
		// Construct the vertex buffer if not yet constructed, otherwise clear it.
		if(NULL == trail.vertexBuffer)
		{
			trail.vertexBuffer = 
				renderer->createVertexBuffer<ColoredVertex>(PrimitiveType_LineStrip);
		}
		else
		{
			trail.vertexBuffer->unlock();
			trail.vertexBuffer->clear();
		}

		// Hand-optimized as this previously took up to 9% of time when
		// trail groups were enabled. Now it's up to 3%. Further optimization 
		// is possible, but would lead to more uglification.
		int posCount = posHistory.size();
		int p = 0;
		const float timeMult = 1.0f / timeExtent;
		ColoredVertex vertex;
		for (p = 0; p < posCount - 1; ++p)
		{
			// Skip if too many positions, but don't skip the last position.
			if(p % skip != 0)
			{
				continue;
			}
			const float colorRatio = 1.0f - (currentTime - times.at(p)) * timeMult;
			vertex.color = Vec4f(trail.color[0], trail.color[1], trail.color[2], colorRatio * opacity);
			vertex.position = posHistory.at(p);
			trail.vertexBuffer->addVertex(vertex);
		}
		// Last vertex (separate to avoid a branch in the code above)
		const float colorRatio = 1.0f - (currentTime - times.at(p)) * timeMult;
		vertex.color = Vec4f(trail.color[0], trail.color[1], trail.color[2], colorRatio * opacity);
		vertex.position = posHistory.at(p);
		trail.vertexBuffer->addVertex(vertex);

		trail.vertexBuffer->lock();

		renderer->drawVertexBuffer(trail.vertexBuffer, NULL, projector);
	}
}