back to home

Tutorial 5: Planet Earth

Planet Earth

To create a earth with clouds we need 2 different textures: the earth surface and the clouds. After applying both textures in OSG we need to mix them with a fragment shader.

Load texture

	Image *image = osgDB::readImageFile("EarthMap_2500x1250.jpg");
	if (!image) {
		std::cout << "Couldn't load texture " << "EarthMap_2500x1250.jpg" << std::endl;
		return NULL;
	}
	Texture2D *texture = new Texture2D;
	texture->setDataVariance(Object::DYNAMIC);
	texture->setFilter(Texture::MIN_FILTER, Texture::LINEAR_MIPMAP_LINEAR);
	texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR);
	texture->setWrap(Texture::WRAP_S, Texture::REPEAT);
	texture->setWrap(Texture::WRAP_T, Texture::CLAMP);
	texture->setImage(image);

	sphereStateSet->setTextureAttributeAndModes(
		// use texture unit 0
		0, texture, StateAttribute::ON
	);

	// use texture unit 0
	sphereStateSet->addUniform(new Uniform("earth_map", 0));

For more details check the complete source code at the bottom of this page

Fragment shader for Planet Earth

This shader will mix the textures

	uniform sampler2D earth_map;
	uniform sampler2D earth_map_clouds;

	uniform vec2 cloud_position;

	const vec4 specular_color = vec4(0.8, 0.7, 0.3, 1.0);

	varying float specular_intensity;
	varying float diffuse_intensity;

	void main(void) {
		vec4 land = texture2D(earth_map, gl_TexCoord[0].xy);
		vec4 clouds = texture2D(earth_map_clouds, gl_TexCoord[0].xy+cloud_position);

		vec4 texMix = mix(land, clouds, 0.6);
		gl_FragColor = diffuse_intensity * texMix;
	}

Complete Source Code