Skip to content

Instantly share code, notes, and snippets.

@sigman78
Created August 7, 2012 01:25
Show Gist options
  • Save sigman78/3280433 to your computer and use it in GitHub Desktop.
Save sigman78/3280433 to your computer and use it in GitHub Desktop.
//-------------------------------------------------------------------
// File: NxGrid.h
// Author: Choi, Changrak
// Date: 2007.10.18.
// Update:
//
//-------------------------------------------------------------------
#ifndef _NXGRID_H_
#define _NXGRID_H_
#include <NxNode.h>
#include <NxGeode.h>
#include <NxTexture.h>
#define NXGRID_SIZE (64)
#define NXGRID_VERTEX_NUM (NXGRID_SIZE+1)
#define NXGRID_GRID_NUM (NXGRID_SIZE*NXGRID_SIZE)
#define NXGRID_HEIGHTMAP_SIZE (NXGRID_VERTEX_NUM*NXGRID_VERTEX_NUM)
#define NXTERRAIN_HASHSIZE 41
// @brief Grid description
struct NxGridDesc
{
unsigned short nIndex; //< Current grid's index
unsigned short usTexture[4]; //< Texture set
short sHeightMap[NXGRID_HEIGHTMAP_SIZE]; //< Height information
unsigned short usInfoMap[NXGRID_GRID_NUM]; //< Terrain information
};
// @brief World object spawn information
struct NxObjectSpawn
{
ushort nIndex; //< Object model number
short x, y, z; //< Position (x100 scale meter)
short h, p, r; //< Rotation (x1000 scale radian)
short scale; //< Scaling (x1000 scale)
};
class NxVector3;
struct NxWorldTexture;
struct NxWorldModel;
/// @breif NxGrid class.
/// This class manages one grid.
class NxGrid
{
public:
enum LOADING_STATUS
{
LOADING_NONE,
LOADING_OBJECT,
LOADING_COMPLETE,
NEED_UPDATE_HEIGHTMAP,
LOADING_MAX_NUMBER
};
enum EDIT_COMMAND
{
CHANGE_HEIGHT,
DELETE_OBJECT,
TRANSFORM_OBJECT,
END_EDIT_COMMAND
};
public:
/// @brief Create new grid.
/// @param pkTerrainRoot [IN] Terrain root node
/// @param pkModelRoot [IN] Model root node
/// @param nIndex [IN] Grid index
/// @param nWorldX, nWorldY [IN] World coordinate (Left bottom)
/// @return Return object's pointer, if sucessful, return NULL, otherwise.
static NxGrid *Create(NxNode *pkTerrainRoot, NxNode *pkModelRoot, int nIndex, int nWorldX, int nWorldY);
/// @brief Set parameter for heightmap.
/// �������� ���� �������� ������ ����.
/// @param fRefHeight [IN] 0�� ���� ���� ���� (������ 0.0f)
/// @param fUnitHeight [IN] 1�� ���� ���� ���� (������ 0.1f)
static void SetParameters(float fRefHeight, float fUnitHeight);
/// @brief Destructor
~NxGrid();
/// @brief ��������
void Update(float fTime);
/// @brief ���� ������ ���������� ���� ����
void CreateGeometry();
/// @brief Check collidee
/// @param kPosition [IN] Checking position
/// @param kOffset [IN] Target position from checking position
float CheckCollidee(const NxVector3 &kPosition, const NxVector3 &kOffset);
/// @brief Check terrain.
/// @param kPosition [IN] Checking position
/// @param kOffset [IN] Target position from checking position
float CheckTerrain(const NxVector3 &kPosition, const NxVector3 &kOffset);
/// @brief Get terrain's height and slant.
/// �������� ������ �������� ��������.
/// @param kPosition [IN] Position
/// @param pfHeight [OUT] Height
/// @param pfSlantU [OUT] Slant value in radian of U-axis
/// @param pfSlantV [OUT] Slant value in radian of V-axis
/// @return Ths world material information for sound (see SJWORLDMANAGER_MATERIAL_*)
int GetHeightAndSlant(const NxVector3 &kPosition, float *pfHeight, float *pfSlantU, float *pfSlantV);
/// @brief Calculate square distance from the position
/// ������ ���������� ���� ���������� ���� ������ ��������.
/// @param nRefX, nRefY [IN] ���� ����
/// @return (nRefX, nRefY) ���������� �������������� ���� ������ ��������.
int SqrDistance(int nRefX, int nRefY) const
{
nRefX -= m_nWorldX+NXGRID_SIZE/2;
nRefY -= m_nWorldY+NXGRID_SIZE/2;
return nRefX*nRefX + nRefY*nRefY;
}
/// @brief Get node's center position in x-axis.
/// ������ ���� X ������ ������.
/// @return ������ ���� X ������ ��������.
int GetCenterX() const { return m_nWorldX; }
/// @brief Get node's center position in y-axis.
/// ������ ���� Y ������ ������.
/// @return ������ ���� Y ������ ��������.
int GetCenterY() const { return m_nWorldY; }
/// @brief Get current loading status.
/// ������ ���� ������ ��������.
/// @return ������ ���� ������ ��������.
int GetLoadingStatus() const { return m_nLoadingStatus; }
/// @brief Get current grid's index.
/// @return Return current grid's index.
int GetIndex() const { return m_kGridDesc.nIndex; }
/// @brief Make mini map image
/// @param pkRenderer [IN] renderer
/// @param fX, fY [IN] left-bottom position to take shot
void MakeMapImage(NxRenderer *pkRenderer, float fX, float fY);
/// @brief Set/Get unit terrain height
/// �������� ���� ������ ���������� ��������.
static float GetUnitHeight() { return ms_fUnitHeight; }
static void SetUnitHeight(float fUnitHeight) { ms_fUnitHeight = fUnitHeight; }
static void SetRefHeight(float fRefHeight) { ms_fRefHeight = fRefHeight; }
static void ChangeWolrd(int nWorldID) { ms_nWorldID = nWorldID; }
/// @brief Load texture.cfg
/// texture.cfg ������ ������ m_pkWorldTextureHash �������� ��������.
static void LoadTextureConfig();
/// @brief Load model.cfg
/// model.cfg ������ ������ m_pkWorldModelHash �������� ��������.
static void LoadModelConfig();
static void Init(const wchar_t *pszPath);
static void Shutdown();
/// --------------------------------------------------------------
/// Editing functions
/// --------------------------------------------------------------
/// @brief Get height map information
void GetHeightMap(short *pnHeightMap);
/// @brief Set edit mode flag
/// ���� �������� �������� �������� ��������.
void SetEditMode(bool bEditMode);
/// @brief Get edit mode flag
/// ���� �������� �������� �������� ��������.
bool GetEditMode() { return m_bEditMode; }
/// @brief Save
void Save();
/// @brief Begin editing for terrain height map
void BeginEdit(short *psEditHeightMap);
/// @brief End editing for terrain height map
void EndEdit();
/// @brief Undo last edit
void UndoEdit();
/// @brief Process edit
/// @param nCmd [IN] Edit command
/// @param pvData [IN] Edit data
void ProcessEdit(int nCmd, const void *pvData);
/// @brief Change height
/// @param x1, y1 [IN] Start position
/// @param x2, y2 [IN] End position
/// @param fValue [IN] Modifiying value
/// @param fRadius [IN] Solid radius
/// @param fFallRadius [IN] Falling radius
void ChangeHeight(float x1, float y1, float x2, float y2, float fValue, float fRadius, float fFallRadius);
/// @brief Flatten height
/// @param x1, y1 [IN] Start position
/// @param x2, y2 [IN] End position
/// @param fValue [IN] Modifiying value
/// @param fRadius [IN] Solid radius
void FlattenHeight(float x1, float y1, float x2, float y2, float fValue, float fRadius);
/// @brief Add terrain object
/// @param nObject [IN] Object model ID
/// @param kPos [IN] The position to place the object
int AddObject(int nObject, const NxVector3 &kPos);
void MoveObject(int nIndex, const NxVector3 &kPos);
void RotateObject(int nIndex, int h, int p, int r);
void ScaleObject(int nIndex, float s);
void DeleteObject(int nIndex);
void SelectObject(int nIndex, bool bSelect);
int PickObject(const NxVector3 &kOrigin, const NxVector3 &kDir);
void ResetObject(int nIndex);
void GetTransform(int nIndex, short *p);
protected:
/// @brief Constructor(pretected member)
NxGrid();
/// @brief Update height map
void UpdateHeightMap();
/// @brief Add collidee
void AddCollidee(NxLinkObject *pkObject);
private:
int m_nWorldX, m_nWorldY; //< ��������(������ ���� ����)
int m_nLoadingStatus; //< ���� ���� ����
int m_nLoadingObjectCount; //< �������� ���� ������
NxGridDesc m_kGridDesc; //< ������ ����
NxObjectSpawn *m_pkObjectSpawn; //< �������� ���� ����
int m_nObjectCount; //< �������� ���� ����
int m_nObjectSize; //< �������� ���� ������ ���� ����
short *m_psEditHeightMap; //< ������ ������
NxNodePtr m_spTerrainRoot; //< Terrain root node
NxNodePtr m_spModelRoot; //< Model root node
NxTArray<NxLinkObject *> m_tCollidee; //< Collidees
NxGeodePtr m_spShape; //< Terrain
bool m_bEditMode; //< ���� ����
bool m_bDirty; //< Current grid is dirty or not
int m_nUndoCmd; //< Command for undo
char m_cUndoData[64]; //< Data for undo
static NxWorldTexture *m_pkTextureHash[NXTERRAIN_HASHSIZE]; //< ���������� ���� ������(������)
static NxWorldModel *m_pkModelHash[NXTERRAIN_HASHSIZE]; //< �������� ���� ������(������)
static const wchar_t *m_pszPath; //< ���������� ����(������)
static NxTexturePtr m_spGridTex; //< ������ ������
static int ms_nWorldID; //< ���� ������
static float ms_fUnitHeight; //< �������� ���� ����
static float ms_fRefHeight; //< �������� ���� ����
#ifdef TODO
static NxAmbientLightPtr ms_spLight; //< Ambient Light for highlighting
#endif
static bool ms_bUseGrid; //< Use grid
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment