⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 terrain.h

📁 game programing code
💻 H
📖 第 1 页 / 共 2 页
字号:
//==============================================================
//==============================================================
//= terrain.h ==================================================
//= Original coders: Trent Polack (trent@voxelsoft.com)		   =
//==============================================================
//= This header file contains the information for the abstract =
//= terrain class, which all specific implementations are	   =
//= derived from, also contains other general data structures  =
//= and constants.											   =
//==============================================================
//==============================================================
#ifndef __TERRAIN_H__
#define __TERRAIN_H__


//--------------------------------------------------------------
//--------------------------------------------------------------
//- HEADERS AND LIBRARIES --------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
#include <stdlib.h>

#include "../Base Code/image.h"


//--------------------------------------------------------------
//--------------------------------------------------------------
//- CONSTANTS --------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
#define TRN_NUM_TILES 5


//--------------------------------------------------------------
//--------------------------------------------------------------
//- STRUCTURES -------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
enum ETILE_TYPES
{
	LOWEST_TILE= 0,		//sand, dirt, etc.
	LOW_TILE,			//grass
	HIGH_TILE,			//mountainside
	HIGHEST_TILE		//tip of mountain
};

enum ELIGHTING_TYPES
{
	HEIGHT_BASED= 0,
	LIGHTMAP,
	SLOPE_LIGHT
};

struct STRN_LIGHTMAP_DATA
{
	unsigned char* m_ucpData;
	int m_iSize;
};

struct STRN_HEIGHT_DATA
{
	unsigned char* m_ucpData;	//the height data
	int m_iSize;				//the height size (must be a power of 2)
};

struct STRN_TEXTURE_REGIONS
{
	int m_iLowHeight;			//lowest possible height (0%)
	int m_iOptimalHeight;		//optimal height (100%)
	int m_iHighHeight;			//highest possible height (0%)
};

struct STRN_TEXTURE_TILES
{
	STRN_TEXTURE_REGIONS m_regions[TRN_NUM_TILES];	//texture regions
	CIMAGE textureTiles[TRN_NUM_TILES];				//texture tiles
	int iNumTiles;
};


//--------------------------------------------------------------
//--------------------------------------------------------------
//- CLASS ------------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
class CTERRAIN
{
	protected:
		STRN_HEIGHT_DATA m_heightData;	//the height data

		CVECTOR m_vecScale;				//scaling variable

		//texture information
		STRN_TEXTURE_TILES m_tiles;
		CIMAGE m_texture;
		CIMAGE m_detailMap;
		int	   m_iRepeatDetailMap;
		bool   m_bMultitexture;
		bool   m_bTextureMapping;
		bool   m_bDetailMapping;

		//lighting information
		ELIGHTING_TYPES m_lightingType;
		STRN_LIGHTMAP_DATA m_lightmap;
		CVECTOR m_vecLightColor;
		float m_fMinBrightness, m_fMaxBrightness;
		float m_fLightSoftness;
		int m_iDirectionX, m_iDirectionZ;



	//fractal terrain generation
	void NormalizeTerrain( float* fpHeightData );
	void FilterHeightBand( float* fpBand, int iStride, int iCount, float fFilter );
	void FilterHeightField( float* fpHeightData, float fFilter );

	//texture map generation functions
	float RegionPercent( int tileType, unsigned char ucHeight );
	void GetTexCoords( CIMAGE texture, unsigned int* x, unsigned int* y );
	unsigned char InterpolateHeight( int x, int z, float fHeightToTexRatio );

	//--------------------------------------------------------------
	// Name:			CTERRAIN::Limit - private
	// Description:		Limit the given unsigned char value to 0-255
	// Arguments:		-ucValue: Value to check
	// Return Value:	An unsigned char value: the limited value
	//--------------------------------------------------------------
	unsigned char Limit( unsigned char ucValue )
	{
		if( ucValue>255 )
			return 255;
		else if( ucValue<0 )
			return 0;

		return ucValue;
	}


	public:
		int m_iSize;	//the size of the heightmap, must be a power of two


	virtual void Render( void )= 0;

	bool LoadHeightMap( char* szFilename, int iSize );
	bool SaveHeightMap( char* szFilename );
	void UnloadHeightMap( void );

	bool MakeTerrainFault( int iSize, int iIterations, int iMinDelta, int iMaxDelta, float fFilter );
	bool MakeTerrainPlasma( int iSize, float fRoughness );

	//texture map generation
	void GenerateTextureMap( unsigned int uiSize );

	//lighting functions
	bool LoadLightMap( char* szFilename, int iSize );
	bool SaveLightMap( char* szFilename );
	void UnloadLightMap( void );
	
	void CalculateLighting( void );

	//--------------------------------------------------------------
	// Name:			CTERRAIN::GetNumVertsPerFrame - public
	// Description:		Get the number of vertices being sent to the
	//					API every frame
	// Arguments:		None
	// Return Value:	An integer value: number of vertices rendered per frame
	//--------------------------------------------------------------
	inline int GetNumVertsPerFrame( void )
	{	return m_iVertsPerFrame;	}

	//--------------------------------------------------------------
	// Name:			CTERRAIN::GetNumTrisPerSec - public
	// Description:		Get the number of triangles being rendered every frame
	// Arguments:		None
	// Return Value:	An integer value: number of triangles rendered every frame
	//--------------------------------------------------------------
	inline int GetNumTrisPerFrame( void )
	{	return m_iTrisPerFrame;	}

	//--------------------------------------------------------------
	// Name:			CTERRAIN::RangedRandom - public
	// Description:		Get a random value between the two arguments
	// Arguments:		-f1, f2: Random boundaries
	// Return Value:	A floating point value: the random number
	//--------------------------------------------------------------
	inline float RangedRandom( float f1, float f2 )
	{	return ( f1+( f2-f1 )*( ( float )rand( ) )/( ( float )RAND_MAX ) );	}

	//--------------------------------------------------------------
	// Name:			CTERRAIN::Scale - public
	// Description:		Scale the width/height/depth of the terrain
	// Arguments:		-x, y, z: how much to scale the terrain
	// Return Value:	None
	//--------------------------------------------------------------
	inline void Scale( float x, float y, float z )
	{	m_vecScale.Set( x, y, z );	}

	//--------------------------------------------------------------
	// Name:			CTERRAIN::SetHeightAtPoint - public
	// Description:		Set the true height value at the given point
	// Arguments:		-ucHeight: the new height value for the point
	//					-iX, iZ: which height value to retrieve
	// Return Value:	None
	//--------------------------------------------------------------
	inline void SetHeightAtPoint( unsigned char ucHeight, int x, int z)
	{	m_heightData.m_ucpData[( z*m_iSize )+x]= ucHeight;	}

	//--------------------------------------------------------------
	// Name:			CTERRAIN::GetTrueHeightAtPoint - public

⌨️ 快捷键说明

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