# Celestia/Textures
## Introduction

The appearance of any object we see is determined in large part by the light that reaches your eye from each point on the object's surface. It is affected by the characteristics of the light reflected from the surface, the coloration of the surface, and any intervening translucent media (such as an atmosphere). Celestia uses mathematical models to calculate these effects, manipulating data which describe the relevant characteristics of each light source, and each point on the surface of each illuminated object. In reality, that's an enormous amount of data, and for Celestia compromises must be made.
In many cases, Celestia uses images as "maps" to provide the needed information describing the characteristics of the object's surface. Each point on a map corresponds to a point on the surface. Celestia uses image files to determine how the corresponding point on the surface of the object should appear. This process is called texture mapping. If you see an analogy here with the kinds of maps you're familiar with from school, it's no coincidence. The term map is used here with something very close to its conventional meaning.
The term texture may seem a bit puzzling in this context. It comes from the original usage of the technique in early computer graphics. At that time, because of hardware limitations, only very small images could be used, and so they could cover only a very small part of the surface of an object. However, a single small image could be used repeatedly to cover a large area of an object, giving it a textured appearance. Celestia generally uses a single texture image to cover the entire surface of an object, some of which are the size of planets. Technology has come a long way!

## Image basics
### Concepts

Since texture mapping relies on image data, we'll begin with a brief review of basic image concepts. A digital image is essentially a rectangular grid of cells, called pixels (short for "picture element"). Each pixel has a specific digital value. The pixel values are commonly used to represent colors, but can be used for any kind of information. The number of possible values which any pixel can have is called the "depth" of the image. The depth of an image is usually expressed as a number of bits. The more bits provided, the greater the precision of the data representation. The (spatial) resolution of an image is determined by the number of rows and columns of pixels it contains. The more rows and columns, the higher the resolution of the image. Unfortunately, the more rows and columns of pixels, and the more bits per pixel, the greater the amount of storage needed for the image. Image file formats often include compression techniques to reduce the storage requirements, but this can lead to a loss of information, resulting in reduced quality.

### Artistic development of textures

Since textures actually are pictures, you can use your favorite paint program to create them. Often public domain photographs by NASA can be used as starting points for surface textures of imaginary planets.

### Preserving quality

It's usually best to work at a resolution that's at least twice the resolution that you'll be providing in an Addon. When scaled down, the results look much better than if you'd been working at the final resolution. Also, be sure to save your intermediate results using an image format that preserves the detail. Do not use either DDS or JPEG formats for anything other than the final released product.

## Texture File Formats

Celestia supports three image file formats for textures: PNG , JPEG and DDS. Because of their differing characteristics, the different file formats are more suitable for some applications than others.

### PNG

PNG, short for Portable Network Graphics, is a lossless compression format, i.e. it reproduces the original image without change. PNG images can contain an 8-bit alpha layer, which is used to determine where the image is transparent (think of a cloud-layer). Because it offers perfect quality and alpha layer, PNG can be used for all purposes in Celestia. However it typically has the biggest filesizes and may be a bad choice for textures offered for download. PNG is supported by all but the oldest image manipulation programs.

### JPEG

JPEG (initials of Joint Photographic Experts Group, often shortened to JPG) is designed for a good visual appearance and a high compression ratio. It introduces small differences compared to the original image, but this is rarely visible when using default or better quality when saving the JPEG image. The JPEG format is well suited for surface textures, especially when the filesize has to be kept small, as for downloads. JPEG is supported by practically all image manipulation programs. Unfortunately, JPEG does not support an alpha channel, so it cannot be used for textures that require transparency.

### DDS

DDS, short for Direct Draw Surface, supports several different specialized formats. Most relevant for Celestia are DXT1, DXT3 and DXT5. DDS is different from JPEG and PNG in several ways, as it's not a format typically used to store pictures. Instead it's designed to be used by 3D hardware, and is directly loaded into your graphics card. Very old graphic cards don't support DDS textures, so not everybody can use them. In some circumstances, DDS can have the worst quality of the supported image formats, considerably worse than JPEG with default compression. It also is not highly compressed, e.g. 1:6 for DXT1.
Why is it used then? Because the compression of DDS textures also works after the image was loaded. Let's take a 2048x1024 image as an example. When stored as PNG or JPEG, an image may take little space on the harddisk (200 kB for JPEG, 1-2 MB for PNG), but once loaded into memory, it would expand to width*height*3 Bytes = 6MB. A DDS image will take only 1/6th of this, on disk as well as in memory. So you can use more or higher-resolution textures without hurting performance. Furthermore, DDS images don't have to be decompressed by the CPU, so loading is considerably faster too. This is very important when approaching planets which haven't had their textures loaded yet, and for Virtual Textures.
Because of their relatively low quality and the kind of artifacts the compression causes, DDS textures are generally unsuited for Normalmaps. Nevertheless, a specialized NormalMap format has been defined by Nvidia which uses a special DXT5 encoding. Celestia calls this format DXT5nm.
A DDS file usually includes several texture images, called "mipmaps". They are progressively lower-resolution versions of the primary image, automatically created by the software used to create DDS file. The graphics hardware chooses the mipmap that best fits the size of the object on the screen, minimizing the number of pixels that have to be rescaled and thus improving the graphics frame-rate.
(additional details of the DXT formats is needed here)

#### DXT5
## Celestia Texture Map Types

The following are some of the different types of texture maps used by Celestia:

### Surface Texture Maps

A Texture is like a photograph of the surface, looking much like a conventional map of the world. If the image file used as surface texture contains an Alpha layer (usually used to indicate transparency), this layer is used by Celestia as the object's specular texture map.

### Specular Texture Maps

A JPG, PNG or DDS SpecularTexture controls which parts of a planetary surface seem to be shiny and which do not by causing a bright highlight to be drawn. In the texture image, white indicates areas which will display a highlight. Black indicates areas with no highlights. It can be very effective in showing a watery surface on an Oceanic planet.
When using a SpecularTexture, both SpecularColor and SpecularPower must also be specified. See the chapter on SSC Catalog files for more information.
Specular reflection highlights are visible only in Celestia's OpenGL Vertex Program and OpenGL 2.0 render paths. The graphics hardware must support OpenGL v1.4 or better.

### Normal Texture Maps

A NormalMap is not "normal" in the sense of being a conventional map. Rather, it defines surface normal vectors: it contains information about the slope of the surface at each point in the texture image. This makes it possible for Celestia to draw shadows on what actually is a smooth surface, making it look like it contains bumps and creases due to mountains and valleys. This usually is much easier than actually molding craters and mountains into the model.
Shadows drawn by NormalMaps are visible only in Celestia's OpenGL Vertex Program and OpenGL 2.0 render paths.
Tools for creating these normal maps are available on the Celestial Matters Website.

### Bump Maps

A BumpMap is an alternative to a NormalMap. It specifies the relative heights of positions on the surface of an object. A pixel corresponding to a position which has the highest altitude is colored white, while the lowest is black. BumpMaps often are created from heightmaps. Internally, Celestia translates BumpMaps into NormalMaps.
When a BumpMap is used, BumpHeight must also be specified.
Shadows drawn by BumpMaps are visible only in Celestia's OpenGL Vertex Program and OpenGL 2.0 render paths.
Also keep in mind that a black & white copy of a texture is NOT a bump map and should not be used as one. Bump maps contain height or altitude data, while a texture is a "photo" with shadows caused by the sun.
For more information, see:
* http://en.wikipedia.org/wiki/Bumpmap
* http://en.wikipedia.org/wiki/Normal_mapping

### Cloud Layer Texture Maps

A CloudMap is a transparent, or semi-transparent image drawn above the surface Texture, used to render clouds. A Cloudmap must be in either png or dds (dtx3 or dxt5) format with an Alpha channel to provide its transparency. In an SSC catalog file (the properties declaration for the object), one can define the cloud´s height above ground, and the speed at which the clouds move relative to the Surface Texture.
If the CloudMap is a Virtual Texture, CloudSpeed must be 0, or the image will appear to be broken.

### Cloud Layer Normal Maps

This feature requires Celestia v1.5.0 or later.
A CloudNormalMap adds shadows to the surface of a cloud image, making it appear to be bumpy.

### Night Light Texture Maps

A NightTexture shows what the nightside of an object looks like, with lighted areas caused by cities, erupting volcanoes, forest fires, etc. It should include an Alpha channel to allow the surface Texture map to be seen through the NightTexture, but that isn't required.

## Texture resolution

The (spatial) resolution of a texture image is determined by the number of rows and columns of pixels it contains. The more rows and column, the higher the resolution of the image. Most importantly, the higher the resolution of the texture, the more detail it contains, and the closer the distance you can view it from. If you view a texture too closely, it will appear blurry because it does not contain sufficient detail. The higher the resolution of the texture, the closer you can get with a clear view, and the more detail you can see. If a texture is mapped to a planet's surface, the size of the planet together with the resolution of the texture determines how close you can view the planet from with a satisfactory view. A larger planet will require a higher-resolution image to provide comparable detail at the same viewing distance.
As many as three images with identical names but different resolutions can be used for textures if they're put into the subdirectories textures/lores, textures/medres and textures/hires. The keyboard commands "r" and "R" can then be used to select among them.
It is common to refer to the texture resolution as 1k, 2k, 4k, 8k et cetera.
What this means is:
- a 1k texture has a width of 1024 pixels and a height of 512 pixels
- a 2k texture is 2048 x 1024 pixels
- a 4k texture is 4096 x 2048 pixels
... And so on.
Any texture used in Celestia must be a power of two on each side, like the dimensions shown in the above example. This restriction is lifted in Celestia v1.5.0, but works best with graphics cards which support "non-power-of-two" texture sizes. Celestia will shrink textures to fit into cards that don't have that feature, possibly damaging the image significantly.
When a texture becomes very large, it can be advantageous to cut it up into smaller parts, thus creating a Virtual Texture - this economizes on the memory Celestia needs to draw it.

## Virtual Textures

A virtual texture is a special texture format used by Celestia to handle very large textures. It is made up of many little textures showing the small details on planets, moons etc.
There are different levels to a virtual texture. The levels are numbered 0 to 10 (12 for v1.4.0 and later). Level 10 is for very fine details and Level 0 is pretty much a global view showing all the major features of Celestia's worlds. Level 0 is made up of 2 images, Level 10 can be made up of thousands of close-up images showing great detail.
A ctx file defines the virtual texture and its filename is used when referring to the virtual texture in ssc files.
For the details of creating a Virtual Texture, see the documentation on the Celestia MotherLode.