分享

地形教程 - 从图像到高度图_狂人学院|电脑教程|WEB技术|技术文章

 深浅不一 2010-11-23



Terrain Tutorial
地形教程

Height Maps From Images
从图像到高度图

Now that we know how to load an image lets see how to create and draw a height map. This section presents a set of functions from the terrain library that deal with height maps. First lets see how we can build a height map from an image.
既然我们知道如何加载一张图片了,让我们看看如何来创建和绘制高度图。这个章节展示了一些地形库中的函数,用来处理高度图。我们首先来看看如何从一张图像上创建高度图。

There are several ways to interpret an image as an height map. If the image is greyscale the process is more or less straightforward, a pixels intensity corresponed to a height.
有多种方式解释一张图像就是张高度图。如果一张图像是灰度图,过程又是查不多直接了当的,那么一样像素的亮度就代表了一个相应的高度。

If the image is RGB then we can convert it to greyscale using the function tgaRGBtoGreyscale from the tgalib (see the tgalib section). Then we can use the Greyscale as before.
如果图像是RGB格式,那么我们可以通过tgaRGBtoGreyscale函数将它转换成灰度图,见上一章的TGA库。那么我们就可以像以前一样使用灰度图了。

The other possible type is RGBA, in which case we can create a height map from the alpha channel, and use the RGB for the terrain colors.
另一个可能的类型是RGBA格式,我们可以通过alpha通道来创建高度图,并且可以使用RGB信息来作为地形顶点的颜色。

Before presenting the functions lets take a look at the possible status returned:
在展示函数之前,我们先来看看可能的返回值:
TERRAIN_ERROR_LOADING_IMAGE - this value is returned when there is a problem loading the image.读取图像的时候发生错误。
TERRAIN_ERROR_MEMORY_PROBLEM - Oops, no memory to hold the terrain data 分配内存的时候发生错误了
TERRAIN_ERROR_NOT_SAVED - There was a problem saving the file 储存文件的时候发生错误了
TERRAIN_ERROR_NOT_INITIALISED - occurs when a function to act upon the terrain is called but there is no terrain! 没有地形之前操作了相关的地形函数。
TERRAIN_OK - This what we want, no errors! 这个是我们所要的,没有错误。
We need some variables to hold the terrain data:
我们需要一些变量来保存地形数据:
terrainGridWidth - the number of heights available on the x axis. x轴上高度数据的个数
terrainGridLength - the number of heights available on the y axis. y轴上高度数据的个数
terrainHeights - A one-dimensional array containing the heights 一维数组保存高度值
terrainColors - A one-dimensional array containing the RGB colors for each height 一维数组保存RGB颜色
terrainNormals - A one-dimensional array containing the normals for each height 一维数组保存法线数据
Note that the terrainColors and terrainNormals arrays contain 3 times the number of components of terrainHeights. This is because we don\'t store the location in the XZ plane of each height in the array.
注意terrainColors和terrainNormals数组保存了3倍的数据个数相比于terrainHeight. 那时因为terrainHeight里面不需要储存XZ平面的数据。

The function to create the height map based on an image has the following signature:
从图片生成高度图使用以下方式:
int terrainLoadFromImage(char *filename, int normals);

Parameters:
filename - the name of the image file 图像文件名
normals - a non-zero value specifies that normals are to be calculated, zero means that we don\'t want normals
一个非零值代表自动计算法线,零值代表我们不需要计算
The value returned by this function indicates if the operation was successful or not. This function computes the heights as the pixels intensity scaled down to the interval [0,1]. The code that follows shows the steps required to create the height map. The image loading is performed by the tgalib presented in the previous sections.
返回值表达了这个函数是否执行成功了。这个函数将高度像像素亮度一样缩放到了[0,1]的区间。下面的代码展示了创建一个高度图。图像加载是通过tgalib完成的,我们上一章节已经介绍过了。
int terrainLoadFromImage(char *filename, int normals) {

tgaInfo *info;
int mode,aux;
float pointHeight;

// if a terrain already exists, destroy it.
if (terrainHeights != NULL)
terrainDestroy();

// load the image, using the tgalib
info = tgaLoad(filename);

// check to see if the image was properly loaded
// remember: only greyscale, RGB or RGBA noncompressed images
if (info->status != TGA_OK)
return(info->status);

// if the image is RGB, convert it to greyscale.
// mode will store the image\'s number of components
mode = info->pixelDepth / 8;
if (mode == 3) {
tgaRGBtoGreyscale(info);(责任编辑:admin)

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多