CTerrain.h
上传用户:arsena_zhu
上传日期:2022-07-12
资源大小:399k
文件大小:3k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /*
  2. Class Name:
  3. CTerrain.
  4. Created by:
  5. Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).
  6. Description:
  7. This class is a base class used to load and save a terrain.
  8. */
  9. #ifndef _CTERRAIN_H_
  10. #define _CTERRAIN_H_
  11. #include "main.h"
  12. struct TextureRegions
  13. {
  14. int lowHeight;  // Lowest height.
  15. int optimalHeight; // Ideal height.
  16. int highHeight; // Highest possible height.
  17. };
  18. struct TerrainTiles
  19. {
  20. TextureRegions regions[4];  // Texture regions.
  21. TGATexture texTiles[4]; // Texture tiles.
  22. int numTiles;   // Total number of tiles.
  23. };
  24. class CTerrain
  25. {
  26. public:
  27. CTerrain();
  28. ~CTerrain();
  29. void SetDataHeight(int x, int z, float set);
  30. void SetHeight(int x, int z, float set);
  31. void SetVertex(int x, int z, int index, float set);
  32. float GetDataHeight(int x, int z);
  33. float GetHeight(int x, int z);
  34. float GetVertex(int x, int z, int index);
  35. float GetRealHeight(float x, float z);
  36. void AddToHeight(int x, int z, float add);
  37. void AddToVertex(int x, int z, int index, float add);
  38. int ConvertToI(int x, int z, int index);
  39. float GetMaxHeight();
  40. float GetMinHeight();
  41. void SetHeightScale(int val);
  42. void CreatePlaneFromTri(float tri[9], float *plane);
  43. float *GetTriangle(float x, float z);
  44. void ConvertMesh();
  45. void CreateTerrainMesh();
  46. void CreateTexCoords();
  47. bool LoadMap(char *filename, int detailRepeats=0);
  48. bool SaveMap(char *filename);
  49. float RegionPercent(int tileType, float height);
  50. void GetTexCoords(GLuint texWidth, GLuint texHeight, GLuint x,
  51. GLuint z, float &tu, float &tv);
  52. float InterpolateDataHeight(int x, int z, float textureMapRatio);
  53. float InterpolateHeight(int x, int z, float textureMapRatio);
  54. unsigned char *GenerateTextureMap(GLuint imageSize);
  55. bool LoadTile(int tileType, char *filename);
  56. void ShutDown();
  57. float *hMap;
  58. int side;
  59. int side_; // side-1
  60. float fScale;
  61. bool bInvert;
  62. // A list of images used to generate terrain texture.
  63. TerrainTiles m_tiles;
  64. // Vertex list and vertex colors.
  65. float *pVertex;
  66. float *pTexCoords;
  67. float *pTexCoords2;
  68. int iVerts;
  69. int iTriangles;
  70. int iDetail;
  71. };
  72. class CTerrainData
  73. {
  74. public:
  75. CTerrainData();
  76. CTerrain *terr;
  77. GLuint iTexID;
  78. int mode; // rendering mode.. GL_LINES, etc
  79. TGATexture terrDetailTex;
  80. PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
  81. PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB;
  82. void Renderterrain();
  83. void RenderAll();
  84. void Destroy();
  85. bool InitExtensions();
  86. bool InitTextures();
  87. bool LoadDetailTexture(char *detail_tex);
  88. bool Init(CTerrain *tt);
  89. bool Generate(char *map, int detail, char *t0, char *t1, char *t2, char *t3, char *detail_tex);
  90. void SetMode(int m);
  91. };
  92. class CTerrainEdit
  93. {
  94. public:
  95. CTerrainEdit() {}
  96. void Init(CTerrain *terr_p) { t = terr_p; }
  97. CTerrain *t;
  98. float xPos, yPos;
  99. void Normalize(float scale);
  100. void Multiply(float m);
  101. void Flatten(float power);
  102. void Increase(float power);
  103. void Zero();
  104. void DoHill(int x, int z, int radius, float power);
  105. void CreateHill(int x, int y, int radius, int iters, float power=1);
  106. void GenerateTerrain(int iters, int max_radius, int max_iters);
  107. void GenerateIsland(int width_x, int width_z, int x_pos, int y_pos, int iters, int max_radius, int max_iters);
  108. };
  109. #endif
  110. // Copyright October 2004
  111. // All Rights Reserved!
  112. // Allen Sherrod
  113. // ProgrammingAce@UltimateGameProgramming.com
  114. // www.UltimateGameProgramming.com