|Demeter Terrain Engine >> Documentation >> Loaders|
Elevation Loaders and Texture Loaders are simply shared objects (DLL’s on Windows) that are able to load data
from some data source and pass that data to the Demeter core library on your behalf. Loaders are loaded dynamically
at runtime if and when your application requests that they load elevation or texture data. Demeter bundles several
elevation and texture loaders for your use and it is a trivial matter for you to develop your own Loaders.
This Code snippet specifies that elevation files will be loaded from the “data” directory of the application,
which is presumed to be a peer directory of the application’s working directory.
Elevation Loaders provide instances of the Terrain class with digital elevation data. An Elevation Loader simply does work that you could have done for yourself via the core library, which is to call the SetAllElevations() method of the Terrain class with an array of floats representing terrain vertices. Elevation Loaders derive these arrays of floats from data sources such as digital elevation files on your behalf.
Access to Elevation Loaders is provided through a Singleton object called Loader. The Loader object automatically loads the shared object or DLL that your application is requesting service from and passes your request along to the loaded library. To use an Elevation Loader, first construct an instance of the Terrain class and then tell the Loader object to load elevation data into your instance of Terrain. For example:
Terrain* pTerrain = new Terrain();
From the example above, you can see that the Loader object’s
LoadElevations() method takes 3
If you wish to implement your own Elevation Loader, you must create a shared object that exports the following function:
extern "C" TERRAIN_API void LoadElevations(int argc, const char** argv, Terrain* pTerrain)
As in the main() function of any C program, the argc and argv arguments tell you what parameters have been passed to your Loader. Your function is obligated to set all of the vertices of the passed in Terrain object (typically by loading or generating some kind of data and using it to call the Terrain object’s SetAllElevations() method).
This Elevation Loader is bundled with Demeter. It uses the GDAL library, which is an open source digital elevation I/O library led by Frank Warmerdam. You can find GDAL at http://www.remotesensing.org/gdal/. It enables your application to read an incredible variety of files. Using this Elevation Loader, you can load your Terrain object with everything from simple grayscale image files to real digital elevation data files. As of GDAL 1.1.7, supported file formats are:
The GDAL Elevation Loader takes 3 parameters: the filename to load, the distance in world units between vertices
in the resultant terrain, and a vertical scaling factor to be applied to the elevation of each loaded vertex. This
Elevation Loader will guarantee that the Terrain being loaded is the requisite power of 2 in width and height by
zero-filling to the next power of 2 in both directions. Therefore, if you have a choice, try to make sure that the
elevation data you’re loading is already a power of 2 in width and height to avoid waste.
This Elevation Loader only loads a single type of file: those generated by the Demeter Texture Editor
application. If you use the Demeter Texture Editor to create a detail-painted terrain, you can use this Loader in
your application to load it at runtime. There is also a corresponding DemeterTextureLoader
that you will want to use in this scenario.
Texture Loaders provide image data which can be used for three purposes: as overall terrain textures, as
“common” terrain textures, or as “shared” textures that can be used for detail painting on a terrain’s surface.
An overall terrain texture is a single image that will be draped across the entire surface of a single terrain. Therefore, these are generally large images (on the order of 2048x2048 or larger in size). Demeter will automatically chop these large images into smaller images that can be managed as textures on the user’s 3D accelerator hardware.
A “common” texture is an inexpensive, easy, but inferior, alternative to using shared textures. A common texture
is simply a small texture, usually on the order of 256x256 pixels, that is repeated infinitely across a terrain’s
surface. This “common” texture is blended with the overall texture on the terrain’s surface. The purpose of the
common texture is to provide the user with the impression of detail when the camera gets very close to the ground
in the scene.
A superior, but more expensive, alternative to using a common texture, shared textures allow you to actually
blend any number of unique, ground-specific textures at different places on a terrain’s surface. For example, in
the areas that are grassy, you can use a shared grass texture and blend a high-detail grass texture with the
overall terrain texture in those areas. In other areas that are dirt instead of grass, you can blend in a
high-detail dirt texture, etc. With this approach, when the camera gets close to the ground, the user sees the
appropriate kind of detail rather than a generic texture as you would get by using the “common” texture approach.
The API for accessing Texture Loaders is very similar to that of using Elevation Loaders. The main difference is
that where the Loader singleton only offers a single method for Elevation Loaders called
LoadElevations(), it offers 3 methods for Texture
LoadTexture(). These 3 methods correspond to the
above sections on overall terrain textures, common terrain textures, and shared textures, respectively.
Terrain* pTerrain = new Terrain();
To load a shared texture, call the Loader object’s LoadTexture() method. This method returns a pointer to a Texture object. You can use this Texture object in calls to your Terrain object’s Paint() method to paint the texture onto the terrain’s surface, like so:
Texture* pTexture = Loader::GetInstance()->LoadTexture("SDLTextureLoader",
See the bundled sample application called SamplePaintApplication.cpp to see how this works.
Implementing your own Texture Loaders is very similar to implementing Elevation Loaders. Simply make sure that your shared object exports the following functions:
extern "C" TERRAIN_API
void LoadTerrainTexture(int argc, const char**
argv, Terrain* pTerrain)
This Texture Loader is bundled with Demeter and uses the SDL and SDL_image libraries to load image files. SDL and SDL_image are open source libraries led by Sam Lantinga. You can get them both at http://www.libsdl.org. Be sure to get the development versions of these libraries in order to build the SDLTextureLoader.
This Texture Loader loads textures from a terrain file that was saved from the Demeter Texture Editor application. See the bundled sample SampleDemeterApplication.cpp to see this Loader in action.
|Copyright ©2002 Clay Fowler|