cgprojectsfx

Terrain

Feb 8th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.21 KB | None | 0 0
  1. // Build Data
  2. void SphereTerrainTopology::GenerateTopology() {
  3.     // Do not compute topology if none exist
  4.     if (!m_pPatch) {
  5.         return;
  6.     };
  7.  
  8.     // Create Noise
  9.     SimplexNoise newNoise;
  10.  
  11.     // Create memory
  12.     m_pVertexData = (float *) new float[PATCH_VERTICES_TOTAL * VERTEXELEMENTS
  13.             * 6]; //256 / 768
  14.     m_pIndexData = (unsigned int *) new unsigned int[PATCH_VERTICES_TOTAL * 6];
  15.  
  16.     // Create Position Map Data
  17.     aeVector4<double>* pPositionMapData = (aeVector4<double>*) new aeVector4<
  18.             double> [TEXELSIZEPLUS * TEXELSIZEPLUS];
  19.  
  20.     // Create memory
  21.     m_pHeightMapTexture = new Texture2D(context_);
  22.  
  23.     m_pHeightMapTextureData = new float[TEXELSIZEPLUS * TEXELSIZEPLUS];
  24.  
  25.     // Create off texture turn off compression
  26.     m_pHeightMapTexture->SetNumLevels(1);
  27.     m_pHeightMapTexture->SetSize(TEXELSIZEPLUS, TEXELSIZEPLUS,
  28.             g_pApp->GetGraphics()->GetFloat32Format(), TEXTURE_STATIC);
  29.     m_pHeightMapTexture->SetData(0, 0, 0, TEXELSIZEPLUS, TEXELSIZEPLUS,
  30.             m_pHeightMapTextureData);
  31.     m_pHeightMapTexture->SetFilterMode(FILTER_NEAREST);
  32.  
  33.     // Set Cube size
  34.     float CubeSize = 2.0f;
  35.  
  36.     // Get Visual Radius
  37.     float VisualRadius = m_pPatch->GetVisualRadius();
  38.  
  39.     // Create a ridgednoise perlon
  40.     RidgedNoise someNoise(VisualRadius);
  41.  
  42.     // Set Noise
  43.     someNoise.SetRidgedNoiseParameters(m_pPatch->GetHeightScale(),
  44.             m_pPatch->GetOctaves(), m_pPatch->GetGain(),
  45.             m_pPatch->GetLacunarity(), m_pPatch->GetOffset(), m_pPatch->GetH());
  46.  
  47.     // Reset Cpimts
  48.     m_IndexCount = 0;
  49.     m_VertexCount = 0;
  50.  
  51.     // size best off vertices
  52.     int PatchSize = PATCH_VERTICES;
  53.  
  54.     // Get Cube Center - Create Defaults
  55.     Vector3 facePlane = Vector3::ZERO;
  56.     Vector3 direction_x = Vector3::ZERO;
  57.     Vector3 direction_y = Vector3::ZERO;
  58.     Vector3 vertexDirection = Vector3::ZERO;
  59.  
  60.     // Element total size easier to count
  61.     unsigned int VertexElementTotalSize = 6 * VERTEXELEMENTS;
  62.  
  63.     // Get Face Direction
  64.     const STFaceDirection faceDirection = m_pPatch->GetFaceDirection();
  65.     const Vector2 Coordinates = m_pPatch->GetCoordinates();
  66.     const unsigned int depth = m_pPatch->GetDepth();
  67.  
  68.     // Face direction
  69.     unsigned int face = (unsigned int) faceDirection;
  70.  
  71.     // Calculate face plane
  72.     facePlane = ((float) CubeSize / 2) * TerrainFaceCoordinate[face]; // put a side on plane 1
  73.  
  74.     // Get depth
  75.     float Depth = 1.0f / pow(2.0f, depth);
  76.  
  77.     // Debug  Face Creation at
  78.     //ALPHAENGINE_LOGINFO(
  79.     //      "FacePlane at " + facePlane.ToString() + " with "+Coordinates.ToString()+" and depth of "+String(Depth));
  80.  
  81.     switch (face) {
  82.     case 0: {
  83.         direction_x = CubeSize * Vector3(0, -1, 0);
  84.         direction_y = CubeSize * Vector3(0, 0, 1);
  85.  
  86.     }
  87.         break;
  88.     case 1: {
  89.         direction_x = CubeSize * Vector3(0, -1, 0);
  90.         direction_y = CubeSize * Vector3(0, 0, -1);
  91.     }
  92.         break;
  93.     case 2: {
  94.         direction_x = CubeSize * Vector3(0, 0, 1);
  95.         direction_y = CubeSize * Vector3(1, 0, 0);
  96.     }
  97.         break;
  98.     case 3: {
  99.         direction_x = CubeSize * Vector3(0, 0, 1);
  100.         direction_y = CubeSize * Vector3(-1, 0, 0);
  101.     }
  102.         break;
  103.     case 4: {
  104.         direction_x = CubeSize * Vector3(0, -1, 0);
  105.         direction_y = CubeSize * Vector3(-1, 0, 0);
  106.     }
  107.         break;
  108.     case 5: {
  109.         direction_x = CubeSize * Vector3(0, -1, 0);
  110.         direction_y = CubeSize * Vector3(1, 0, 0);
  111.     }
  112.         break;
  113.     }
  114.  
  115.     // Index Data
  116.     Vector<unsigned int> indexData;
  117.  
  118.     // Create a index
  119.     unsigned int index = 0;
  120.  
  121.     // Set Reference to Vertex Data
  122.     m_pVertexReference = &m_pVertexData[0];
  123.  
  124.     // Create Vectors To Hold Origin X,Y for each generated triangle set - Cache variables
  125.     Vector3 VertexOrigin, Vertex[8], Origin;
  126.     Vector3 nOrigin, nVertex[8];
  127.  
  128.     double height = 0;
  129.  
  130.     int originposition, position = 0;
  131.  
  132.     // size to use
  133.     unsigned int scale = TEXELSIZE / PATCH_VERTICES;
  134.  
  135.     switch (face) {
  136.     case 0:
  137.         Origin = Vector3(0.0f, 1.0f, -1.0f)
  138.                 + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
  139.                         Coordinates.y_ * 2.0f);
  140.         break;
  141.     case 1: {
  142.  
  143.         Origin = Vector3(0.0f, 1.0f, 1.0f)
  144.                 + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
  145.                         -(Coordinates.y_ * 2.0f));
  146.     }
  147.         break;
  148.     case 2: {
  149.         Origin = Vector3(-1.0f, 0.0f, -1.0f)
  150.                 + Vector3(Coordinates.y_ * 2.0f, 0.0f, Coordinates.x_ * 2.0f);
  151.     }
  152.         break;
  153.     case 3: {
  154.         Origin = Vector3(1.0f, 0.0f, -1.0f)
  155.                 + Vector3(-(Coordinates.y_ * 2.0f), 0.0f,
  156.                         Coordinates.x_ * 2.0f);
  157.     }
  158.         break;
  159.     case 4: {
  160.         Origin = Vector3(1.0f, 1.0f, 0.0f)
  161.                 + Vector3(-(Coordinates.y_ * 2.0f), -(Coordinates.x_ * 2.0f),
  162.                         0.0f);
  163.     }
  164.         break;
  165.     case 5: {
  166.         Origin = Vector3(-1.0f, 1.0f, 0.0f)
  167.                 + Vector3(Coordinates.y_ * 2.0f, -(Coordinates.x_ * 2.0f),
  168.                         0.0f);
  169.     }
  170.  
  171.     }
  172.  
  173.     // Add depth
  174.     direction_x *= Depth;
  175.     direction_y *= Depth;
  176.  
  177.     // Generate a texture
  178.     for (int u = 0; u < TEXELSIZEPLUS; u++) {
  179.         for (int v = 0; v < TEXELSIZEPLUS; v++) {
  180.             // Use precache values to speed process
  181.             cache_x = (direction_x / TEXELSIZE) * v;
  182.             cache_y = (direction_y / TEXELSIZE) * u;
  183.  
  184.             // Map origin to a sphere
  185.             VertexOrigin = CubeToSphereMapping(
  186.                     facePlane + Origin + cache_x + cache_y);
  187.  
  188.             // Get the terrain height
  189.             height = someNoise.GetTerrainHeight(VertexOrigin);
  190.  
  191.             // Set data to generate a position map used once
  192.             pPositionMapData[(u * TEXELSIZEPLUS) + v] = aeVector4<double>(
  193.                     VertexOrigin.x_, VertexOrigin.y_, VertexOrigin.z_,
  194.                     (double) height);
  195.  
  196.             // Copy height map to data using float
  197.             m_pHeightMapTextureData[(u * TEXELSIZEPLUS) + v] = (float) height;
  198.         }
  199.     }
  200.  
  201.     // loop through and create a grid of vertices. // do not draw edge
  202.     for (int u = 1; u < PatchSize; u += 2) {
  203.         for (int v = 1; v < PatchSize; v += 2) {
  204.  
  205.             // Create Index Data
  206.             for (unsigned int i = 0; i < 24; i++) {
  207.                 indexData.Push(index);
  208.                 index++;
  209.             }
  210.  
  211.             // Calculate origin point position
  212.             originposition = (u * scale) * TEXELSIZEPLUS + (v * scale);
  213.  
  214.             // Get Position
  215.             VertexOrigin = Vector3(
  216.                     pPositionMapData[originposition].GetVector3());
  217.  
  218.             // Multiply by stored height
  219.             VertexOrigin *= pPositionMapData[originposition].GetHeight();
  220.  
  221.             // Get normal height
  222.             nOrigin = GetNormalHeight(pPositionMapData, (unsigned int) u,
  223.                     (unsigned int) v);
  224.  
  225.             // Loop through each edge
  226.             for (unsigned int i = 0; i < 8; i++) {
  227.                 // Create the position in array
  228.                 position = originposition
  229.                         + ((EdgeVertex[i][1] * scale) * TEXELSIZEPLUS)
  230.                         + (EdgeVertex[i][0] * scale);
  231.  
  232.                 // Get Position
  233.                 Vertex[i] = Vector3(pPositionMapData[position].GetVector3());
  234.  
  235.                 // Multiply by stored height
  236.                 Vertex[i] *= pPositionMapData[position].GetHeight();
  237.  
  238.             }
  239.  
  240.             // Add First Triangle
  241.             AddTriangle(VertexOrigin, Vertex[1], Vertex[2]);
  242.             AddTriangle(VertexOrigin, Vertex[2], Vertex[3]);
  243.  
  244.             AddTriangle(VertexOrigin, Vertex[3], Vertex[4]);
  245.             AddTriangle(VertexOrigin, Vertex[4], Vertex[5]);
  246.  
  247.             AddTriangle(VertexOrigin, Vertex[5], Vertex[6]);
  248.             AddTriangle(VertexOrigin, Vertex[6], Vertex[7]);
  249.  
  250.             AddTriangle(VertexOrigin, Vertex[7], Vertex[0]);
  251.             AddTriangle(VertexOrigin, Vertex[0], Vertex[1]);
  252.         }
  253.     }
  254.  
  255.     // Just use the index ad the vertex count
  256.     m_IndexCount = indexData.Size();
  257.     m_VertexCount = indexData.Size();
  258.  
  259.     /*// loop through each vertex and chang the normal
  260.      for (unsigned int i = 0; i < m_VertexCount / 3; i += 3) {
  261.      // create a table of vertex
  262.  
  263.      Vector3 vec[3];
  264.      vec[0] = Vector3(m_pVertexData[(i * 8) + 0], m_pVertexData[(i * 8) + 1],
  265.      m_pVertexData[(i * 8) + 2]);
  266.      vec[1] = Vector3(m_pVertexData[(i + 1) * 8 + 0],
  267.      m_pVertexData[(i + 1) * 8 + 1], m_pVertexData[(i + 1) * 8 + 2]);
  268.      vec[2] = Vector3(m_pVertexData[(i + 2) * 8 + 0],
  269.      m_pVertexData[(i + 2) * 8 + 1], m_pVertexData[(i + 2) * 8 + 2]);
  270.  
  271.      Vector3 u = vec[0] - vec[1];
  272.      Vector3 v = vec[0] - vec[2];
  273.      Vector3 facenormal = v.CrossProduct(u);
  274.  
  275.      facenormal.Normalized();
  276.  
  277.      for (unsigned int vi = 0; vi < 3; vi++) {
  278.      Vector3 vertexnormal = facenormal;
  279.  
  280.      m_pVertexData[((i + vi) * 8) + 3] = vertexnormal.x_;
  281.      m_pVertexData[((i + vi) * 8) + 4] = vertexnormal.y_;
  282.      m_pVertexData[((i + vi) * 8) + 5] = vertexnormal.z_;
  283.  
  284.      }
  285.      }*/
  286.  
  287.     // Copy Index Shortcut
  288.     for (unsigned i = 0; i < m_IndexCount; i++) {
  289.         m_pIndexData[i] = indexData.At(i);
  290.     }
  291.  
  292.     // Delete Position Data
  293.     delete[] pPositionMapData;
  294.  
  295.     // Null Ptr
  296.     pPositionMapData = nullptr;
  297. }
  298.  
  299. SphereTerrainTopology::~SphereTerrainTopology() {
  300.  
  301.     // Delete Texture
  302.     delete m_pHeightMapTexture;
  303.  
  304.     delete[] m_pHeightMapTextureData;
  305.  
  306.     m_pHeightMapTextureData = nullptr;
  307.     m_pHeightMapTexture = nullptr;
  308.  
  309.     // Remove Data
  310.     delete[] m_pVertexData;
  311.     delete[] m_pIndexData;
  312.  
  313.     // Null Ptr
  314.     m_pVertexData = nullptr;
  315.     m_pIndexData = nullptr;
  316.  
  317.     // Clear Counter
  318.     m_IndexCount = 0;
  319.     m_VertexCount = 0;
  320. }
  321.  
  322. void SphereTerrainTopology::AddTriangle(Vector3 v1, Vector3 v2, Vector3 v3) {
  323.  
  324.     cache_u = v3 - v1;
  325.     cache_v = v2 - v1;
  326.     cache_normal = cache_v.CrossProduct(cache_u);
  327.  
  328.     Vector3 cu2 = v1 - v2;
  329.     Vector3 cv2 = v3 - v2;
  330.     Vector3 n2 = cv2.CrossProduct(cu2);
  331.  
  332.     Vector3 cu3 = v2 - v3;
  333.     Vector3 cv3 = v1 - v3;
  334.     Vector3 n3 = cv3.CrossProduct(cu3);
  335.  
  336.     Vector3 NormalizedCross = Vector3(cache_normal + n2 + n3).Normalized();
  337.  
  338.     *m_pVertexReference = v1.x_;
  339.     *m_pVertexReference++;
  340.     *m_pVertexReference = v1.y_;
  341.     *m_pVertexReference++;
  342.     *m_pVertexReference = v1.z_;
  343.     *m_pVertexReference++;
  344.  
  345.     cache_normal = Vector3(v1.Normalized()+NormalizedCross).Normalized();
  346.  
  347.  
  348.     v1 = cache_normal;
  349.  
  350.     *m_pVertexReference = v1.x_;
  351.     *m_pVertexReference++;
  352.     *m_pVertexReference = v1.y_;
  353.     *m_pVertexReference++;
  354.     *m_pVertexReference = v1.z_;
  355.     *m_pVertexReference++;
  356.  
  357.     *m_pVertexReference = v2.x_;
  358.     *m_pVertexReference++;
  359.     *m_pVertexReference = v2.y_;
  360.     *m_pVertexReference++;
  361.     *m_pVertexReference = v2.z_;
  362.     *m_pVertexReference++;
  363.  
  364.     cache_normal = Vector3(v2.Normalized()+NormalizedCross).Normalized();
  365.  
  366.     v2 = cache_normal;
  367.  
  368.     *m_pVertexReference = v2.x_;
  369.     *m_pVertexReference++;
  370.     *m_pVertexReference = v2.y_;
  371.     *m_pVertexReference++;
  372.     *m_pVertexReference = v2.z_;
  373.     *m_pVertexReference++;
  374.  
  375.     *m_pVertexReference = v3.x_;
  376.     *m_pVertexReference++;
  377.     *m_pVertexReference = v3.y_;
  378.     *m_pVertexReference++;
  379.     *m_pVertexReference = v3.z_;
  380.     *m_pVertexReference++;
  381.  
  382.     cache_normal = Vector3(v3.Normalized()+NormalizedCross).Normalized();
  383.  
  384.     v3 = cache_normal;
  385.  
  386.     *m_pVertexReference = v3.x_;
  387.     *m_pVertexReference++;
  388.     *m_pVertexReference = v3.y_;
  389.     *m_pVertexReference++;
  390.     *m_pVertexReference = v3.z_;
  391.     *m_pVertexReference++;
  392.  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment