heightmap.cpp

来自「这是一个有关3d的小程序」· C++ 代码 · 共 79 行

CPP
79
字号
//////////////////////////////////////////////////////////////////////
// heightmap.cpp     by Tobias Franke (webmaster@cyberhead.de) 2001 //
//////////////////////////////////////////////////////////////////////

#include "heightmap.h"

heightmap::heightmap(void)
{
	hmap = 0;
	width=height=0;
}

bool heightmap::create(int x, int y)
{
	hmap = new float[x*y];
	if (hmap)
	{
		width=x; height=y;
		return true;
	}
	else
		return false;
}

bool heightmap::load(char *filename)
{
	bitmap map;
	
	if (!map.load (filename)) return false;
	create (map.getwidth(), map.getheight());

	if (hmap) 
	{
		for (int z=0; z<map.getheight(); z++) 
			for (int x=0; x<map.getwidth(); x++)
			{
				hmap[x+(z*width)]=map.data[(x+(z*map.getwidth()))*3 + 1];	//Check for green value
			}
		return true;
	}
	else return false;
}

bool heightmap::save(char *filename)
{
	unsigned char r, g, b;
	bitmap map;

	if (!map.create (width, height)) return false;

	if (hmap)
	{
		for (int z=0; z<height; z++)
			for (int x=0; x<width; x++)
			{
				r=g=b=(unsigned)hmap[x+(z*width)];	
				map.setcolor(x, z, r, g, b);
			}
		if (!map.save (filename)) return false;
		return true;
	}
	else return false;
}

void heightmap::reset(void)
{
	if (hmap)
	{
		delete hmap;
		width=height=0;
	}
}

heightmap::~heightmap(void)
{
	if (hmap)
		delete hmap;
	hmap = 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?