コード例 #1
0
ファイル: CityOnPlanet.cpp プロジェクト: GizmoR13/pioneer
//Note: models get some ambient colour added when dark as the camera moves closer
void CityOnPlanet::Render(Graphics::Renderer *r, const Camera *camera, const SpaceStation *station, const vector3d &viewCoords, const matrix4x4d &viewTransform, double illumination, double minIllumination)
{
	matrix4x4d rot[4];
	station->GetRotMatrix(rot[0]);

	// change detail level if necessary
	if (m_detailLevel != Pi::detail.cities) {
		RemoveStaticGeomsFromCollisionSpace();
		AddStaticGeomsToCollisionSpace();
	}

	rot[0] = viewTransform * rot[0];
	for (int i=1; i<4; i++) {
		rot[i] = rot[0] * matrix4x4d::RotateYMatrix(M_PI*0.5*double(i));
	}

	const Graphics::Frustum frustum = Graphics::Frustum::FromGLState();
	//modelview seems to be always identity

	memset(&cityobj_params, 0, sizeof(LmrObjParams));
	cityobj_params.time = Pi::game->GetTime();
	
	for (std::vector<BuildingDef>::const_iterator i = m_buildings.begin();
			i != m_buildings.end(); ++i) {

		if (!(*i).isEnabled) continue;
		
		vector3d pos = viewTransform * (*i).pos;
		if (!frustum.TestPoint(pos, (*i).clipRadius))
			continue;

		const Color oldSceneAmbientColor = r->GetAmbientColor();

		// fade conditions for models
		double fadeInEnd, fadeInLength;
		if (Graphics::AreShadersEnabled()) {
			fadeInEnd = 10.0;
			fadeInLength = 500.0;
		}
		else {
			fadeInEnd = 2000.0;
			fadeInLength = 6000.0;
		}

		FadeInModelIfDark(r, (*i).clipRadius, pos.Length(), fadeInEnd, fadeInLength, illumination, minIllumination);

		matrix4x4f _rot;
		for (int e=0; e<16; e++) _rot[e] = float(rot[(*i).rotation][e]);
		_rot[12] = float(pos.x);
		_rot[13] = float(pos.y);
		_rot[14] = float(pos.z);
		(*i).model->Render(_rot, &cityobj_params);

		// restore old ambient colour
		if (illumination <= minIllumination) 
			r->SetAmbientColor(oldSceneAmbientColor);
	}
}
コード例 #2
0
ファイル: SpaceStation.cpp プロジェクト: Nikoraito/pioneer
// Renders space station and adjacent city if applicable
// For orbital starports: renders as normal
// For surface starports: 
//	Lighting: Calculates available light for model and splits light between directly and ambiently lit
//            Lighting is done by manipulating global lights or setting uniforms in atmospheric models shader
//            Adds an ambient light at close ranges if dark by manipulating the global ambient level
void SpaceStation::Render(Graphics::Renderer *r, const Camera *camera, const vector3d &viewCoords, const matrix4x4d &viewTransform)
{
	LmrObjParams &params = GetLmrObjParams();
	params.label = GetLabel().c_str();
	SetLmrTimeParams();

	for (int i=0; i<MAX_DOCKING_PORTS; i++) {
		params.animStages[ANIM_DOCKING_BAY_1 + i] = m_shipDocking[i].stage;
		params.animValues[ANIM_DOCKING_BAY_1 + i] = m_shipDocking[i].stagePos;
	}

	Body *b = GetFrame()->m_astroBody;
	assert(b);

	if (!b->IsType(Object::PLANET)) {
		// orbital spaceport -- don't make city turds or change lighting based on atmosphere
		RenderLmrModel(r, viewCoords, viewTransform);
	}
	
	else {
		Planet *planet = static_cast<Planet*>(b);
		
		// calculate lighting
		// available light is calculated and split between directly (diffusely/specularly) lit and ambiently lit
		const std::vector<Camera::LightSource> &lightSources = camera->GetLightSources();
		double ambient, intensity;
		CalcLighting(planet, ambient, intensity, lightSources);

		std::vector<Graphics::Light> origLights, newLights;
		
		for(size_t i = 0; i < lightSources.size(); i++) {
			Graphics::Light light(lightSources[i].GetLight());

			origLights.push_back(light);

			Color c = light.GetDiffuse();
			Color ca = light.GetAmbient();
			Color cs = light.GetSpecular();
			ca.r = c.r * float(ambient);
			ca.g = c.g * float(ambient);
			ca.b = c.b * float(ambient);
			c.r*=float(intensity);
			c.g*=float(intensity);
			c.b*=float(intensity);
			cs.r*=float(intensity);
			cs.g*=float(intensity);
			cs.b*=float(intensity);
			light.SetDiffuse(c);
			light.SetAmbient(ca);
			light.SetSpecular(cs);

			newLights.push_back(light);
		}

		r->SetLights(newLights.size(), &newLights[0]);

		double overallLighting = ambient+intensity;

		// turn off global ambient color
		const Color oldAmbient = r->GetAmbientColor();
		r->SetAmbientColor(Color::BLACK);

		// as the camera gets close adjust scene ambient so that intensity+ambient = minIllumination
		double fadeInEnd, fadeInLength, minIllumination;
		if (Graphics::AreShadersEnabled()) {
			minIllumination = 0.125;
			fadeInEnd = 800.0;
			fadeInLength = 2000.0;
		}
		else {
			minIllumination = 0.25;
			fadeInEnd = 1500.0;
			fadeInLength = 3000.0;
		}

		/* don't render city if too far away */
		if (viewCoords.Length() < 1000000.0){
			r->SetAmbientColor(Color::BLACK);
			if (!m_adjacentCity) {
				m_adjacentCity = new CityOnPlanet(planet, this, m_sbody->seed);
			}
			m_adjacentCity->Render(r, camera, this, viewCoords, viewTransform, overallLighting, minIllumination);
		} 

		r->SetAmbientColor(Color::BLACK);

		FadeInModelIfDark(r, GetCollMesh()->GetBoundingRadius(),
							viewCoords.Length(), fadeInEnd, fadeInLength, overallLighting, minIllumination);

		RenderLmrModel(r, viewCoords, viewTransform);

		// restore old lights
		r->SetLights(origLights.size(), &origLights[0]);

		// restore old ambient color
		r->SetAmbientColor(oldAmbient);
	}
}