cgprojectsfx

SphereTerrainTopology.cpp

Feb 10th, 2018
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.39 KB | None | 0 0
  1. //
  2. // AlphaEngine API/SDK+AlphaEditor
  3. //
  4. // Programmers
  5. // Vivienne Anthony <[email protected]>
  6. // Andrej Skorinko. email - <[email protected]>
  7. //#include "AlphaEngineStd.h"
  8.  
  9. #include "AlphaEngine/Graphics3D/SphereTerrain/STDefines.h"
  10.  
  11. #include <Graphics3D/SphereTerrain/Face/SphereTerrainFace.h>
  12. #include <Graphics3D/SphereTerrain/Face/Node/SphereTerrainNode.h>
  13. #include <Graphics3D/SphereTerrain/Face/Node/Patch/SphereTerrainPatch.h>
  14.  
  15. #include <Graphics3D/SphereTerrain/Face/Node/Patch/Topology/SphereTerrainTopology.h>
  16.  
  17. #include <Graphics3D/SphereTerrain/SimplexNoise/SimplexNoise.h>
  18. #include <Graphics3D/SphereTerrain/RidgedNoise/RidgedNoise.h>
  19.  
  20. #include <AlphaEngine/Math/aeVector4.h>
  21.  
  22. #include <vector>
  23. #include <cmath>
  24.  
  25. // Notes
  26. // The Topology Code
  27. // 1) Creates a Vector4 array as Vector4<double> that contains a cube face in a specfic region based on quadtree patch division. A height map texture is also created at the same time.
  28. //   Vector3 facePlane = Vector3::ZERO;
  29. //   Vector3 tempXStep = Vector3::ZERO;
  30. //   Vector3 tempYStep = Vector3::ZERO;
  31. //   aeVector4<double>* pPositionMapData = (aeVector4<double>*) new aeVector4<
  32. //          double> [TEXELSIZEPLUS * TEXELSIZEPLUS];
  33. //  m_pHeightMapTexture = new Texture2D(context_);
  34. //  m_pHeightMapTextureData = new float[TEXELSIZEPLUS * TEXELSIZEPLUS];
  35. //
  36. //  aeVector4<double> = (x,y,z, height);
  37. //
  38. // 2) A normal map data is generated by patch size. Patch size is the vertex defined. In this code 32 is the size.
  39. //
  40. //    Normals are calculated by triangle face normal. Each vertex is calculated based on the average of surrounding normals
  41. //
  42. //    1-2-3
  43. //    |\|/|
  44. //    0-X-4
  45. //    |/|\|
  46. //    7-6-5
  47. //
  48. //   vertex e1 = v2-v1, vertex e2 = v3-v1, vertex normal = e2.crossproduct(e1)
  49. //   then divide by amount of normals
  50. //
  51. //   In function Vector3 SphereTerrainTopology::CalculateNormal2()
  52. //
  53. //   When processing if(a vertex is outside) the edge boundary the next triangle face is calculated.
  54. //   The total number of calculated normals is used for the division
  55. //
  56. // 3) Create geometry by U, V by 2 (the equivalent of U, V * 8 to much the 257) texel size
  57. //
  58. //    Each vertex point use the above method to add geometry
  59. //
  60. //    Add each triangle to the geometry VertexData
  61. //
  62.  
  63. SphereTerrainTopology::SphereTerrainTopology(Context * context,
  64.         SphereTerrainPatch * Patch = nullptr) :
  65.         Object(context), m_pPatch(Patch), m_pVertexData(nullptr), m_pIndexData(
  66.                 nullptr), m_pHeightMapTextureData(nullptr), m_pHeightMapTexture(
  67.                 nullptr), m_IndexCount(0), m_VertexCount(0) {
  68. }
  69.  
  70. void SphereTerrainTopology::RegisterObject(Context* context) {
  71.     context->RegisterFactory<SphereTerrainTopology>();
  72. }
  73.  
  74. // Build Data
  75. void SphereTerrainTopology::GenerateTopology() {
  76.     // Do not compute topology if none exist
  77.     if (!m_pPatch) {
  78.         return;
  79.     };
  80.  
  81.     // Create Noise
  82.     SimplexNoise newNoise;
  83.  
  84.     // Create memory
  85.     m_pVertexData = (float *) new float[PATCH_VERTICES_TOTAL * VERTEXELEMENTS
  86.             * 6]; //256 / 768
  87.     m_pIndexData = (unsigned int *) new unsigned int[PATCH_VERTICES_TOTAL * 6];
  88.  
  89.     // Create Position Map Data
  90.     aeVector4<double>* pPositionMapData = (aeVector4<double>*) new aeVector4<
  91.             double> [TEXELSIZEPLUS * TEXELSIZEPLUS];
  92.  
  93.     // temporary Vertex Data
  94.     Vector3 tempVertexNormalData[33 * 33];
  95.  
  96.     // Create memory
  97.     m_pHeightMapTexture = new Texture2D(context_);
  98.  
  99.     m_pHeightMapTextureData = new float[TEXELSIZEPLUS * TEXELSIZEPLUS];
  100.  
  101.     // Create off texture turn off compression
  102.     m_pHeightMapTexture->SetNumLevels(1);
  103.     m_pHeightMapTexture->SetSize(TEXELSIZEPLUS, TEXELSIZEPLUS,
  104.             g_pApp->GetGraphics()->GetFloat32Format(), TEXTURE_STATIC);
  105.     m_pHeightMapTexture->SetData(0, 0, 0, TEXELSIZEPLUS, TEXELSIZEPLUS,
  106.             m_pHeightMapTextureData);
  107.     m_pHeightMapTexture->SetFilterMode(FILTER_NEAREST);
  108.  
  109.     // Set Cube size
  110.     float CubeSize = 2.0f;
  111.  
  112.     // Get Visual Radius
  113.     float SphereTerrainVisualRadius = m_pPatch->GetVisualRadius();
  114.  
  115.     // Create a ridgednoise perlon
  116.     RidgedNoise someNoise(SphereTerrainVisualRadius);
  117.  
  118.     // Set Noise
  119.     someNoise.SetRidgedNoiseParameters(m_pPatch->GetHeightScale(),
  120.             m_pPatch->GetOctaves(), m_pPatch->GetGain(),
  121.             m_pPatch->GetLacunarity(), m_pPatch->GetOffset(), m_pPatch->GetH());
  122.  
  123.     // size best off vertices
  124.     unsigned int PatchSize = PATCH_VERTICES;
  125.  
  126.     // Get Cube Center - Create Defaults
  127.     Vector3 facePlane = Vector3::ZERO;
  128.     Vector3 tempXStep = Vector3::ZERO;
  129.     Vector3 tempYStep = Vector3::ZERO;
  130.  
  131.     // Element total size easier to count
  132.     unsigned int VertexElementTotalSize = 6 * VERTEXELEMENTS;
  133.  
  134.     // Get Face Direction
  135.     const STFaceDirection faceDirection = m_pPatch->GetFaceDirection();
  136.     const Vector2 Coordinates = m_pPatch->GetCoordinates();
  137.     const unsigned int depth = m_pPatch->GetDepth();
  138.  
  139.     // Face direction
  140.     unsigned int face = (unsigned int) faceDirection;
  141.  
  142.     // Calculate face plane
  143.     facePlane = ((float) CubeSize / 2) * TerrainFaceCoordinate[face]; // put a side on plane 1
  144.  
  145.     // Get depth
  146.     float Depth = 1.0f / pow(2.0f, depth);
  147.  
  148.     switch (face) {
  149.     case 0: {
  150.         tempXStep = CubeSize * Vector3(0, -1, 0);
  151.         tempYStep = CubeSize * Vector3(0, 0, 1);
  152.  
  153.     }
  154.         break;
  155.     case 1: {
  156.         tempXStep = CubeSize * Vector3(0, -1, 0);
  157.         tempYStep = CubeSize * Vector3(0, 0, -1);
  158.     }
  159.         break;
  160.     case 2: {
  161.         tempXStep = CubeSize * Vector3(0, 0, 1);
  162.         tempYStep = CubeSize * Vector3(1, 0, 0);
  163.     }
  164.         break;
  165.     case 3: {
  166.         tempXStep = CubeSize * Vector3(0, 0, 1);
  167.         tempYStep = CubeSize * Vector3(-1, 0, 0);
  168.     }
  169.         break;
  170.     case 4: {
  171.         tempXStep = CubeSize * Vector3(0, -1, 0);
  172.         tempYStep = CubeSize * Vector3(-1, 0, 0);
  173.     }
  174.         break;
  175.     case 5: {
  176.         tempXStep = CubeSize * Vector3(0, -1, 0);
  177.         tempYStep = CubeSize * Vector3(1, 0, 0);
  178.     }
  179.         break;
  180.     }
  181.  
  182.     // Index Data
  183.     Vector<unsigned int> indexData;
  184.  
  185.     // Create a index
  186.     unsigned int index = 0;
  187.  
  188.     // Set Reference to Vertex Data
  189.     m_pVertexReference = &m_pVertexData[0];
  190.  
  191.     // SphereTerrain Patch Origin;
  192.     Vector3 tempPatchOrigin;
  193.  
  194.     // Temporary variables when creating geometry
  195.     Vector3 tempVertexOrigin, tempVertex[8];
  196.  
  197.     // Temporary variables when creating geometry - Normal Data
  198.     Vector3 tempVertexNormalOrigin, tempVertexNormal[8];
  199.  
  200.     // Stemp increments when calculating Texel position and height map
  201.     Vector3 tempXIncrement, tempYIncrement;
  202.  
  203.     // temporary Vertex Height Computerd by Noise
  204.     double tempVertexHeight = 0;
  205.  
  206.     // temporary Vertex Origin Position For Calculation
  207.     // Note: When Convert Patch Size to Vertex Geomometry
  208.     unsigned int temptempVertexOriginPosition = 0;
  209.  
  210.     // temporary Vertex Position for For Calcuation
  211.     // Note: When Convert Patch Size to Vertex Geomometry
  212.     unsigned int tempVertexPosition = 0;
  213.  
  214.     // size to use
  215.     unsigned int scale = TEXELSIZE / PATCH_VERTICES;
  216.  
  217.     switch (face) {
  218.     case 0:
  219.         tempPatchOrigin = Vector3(0.0f, 1.0f, -1.0f)
  220.                 + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
  221.                         Coordinates.y_ * 2.0f);
  222.         break;
  223.     case 1: {
  224.  
  225.         tempPatchOrigin = Vector3(0.0f, 1.0f, 1.0f)
  226.                 + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
  227.                         -(Coordinates.y_ * 2.0f));
  228.     }
  229.         break;
  230.     case 2: {
  231.         tempPatchOrigin = Vector3(-1.0f, 0.0f, -1.0f)
  232.                 + Vector3(Coordinates.y_ * 2.0f, 0.0f, Coordinates.x_ * 2.0f);
  233.     }
  234.         break;
  235.     case 3: {
  236.         tempPatchOrigin = Vector3(1.0f, 0.0f, -1.0f)
  237.                 + Vector3(-(Coordinates.y_ * 2.0f), 0.0f,
  238.                         Coordinates.x_ * 2.0f);
  239.     }
  240.         break;
  241.     case 4: {
  242.         tempPatchOrigin = Vector3(1.0f, 1.0f, 0.0f)
  243.                 + Vector3(-(Coordinates.y_ * 2.0f), -(Coordinates.x_ * 2.0f),
  244.                         0.0f);
  245.     }
  246.         break;
  247.     case 5: {
  248.         tempPatchOrigin = Vector3(-1.0f, 1.0f, 0.0f)
  249.                 + Vector3(Coordinates.y_ * 2.0f, -(Coordinates.x_ * 2.0f),
  250.                         0.0f);
  251.     }
  252.  
  253.     }
  254.  
  255.     // Add depth
  256.     tempXStep *= Depth;
  257.     tempYStep *= Depth;
  258.  
  259.     // Generate a position map
  260.     for (int u = 0; u < TEXELSIZEPLUS; u++) {
  261.         for (int v = 0; v < TEXELSIZEPLUS; v++) {
  262.             // Use precache values to speed process
  263.             tempXIncrement = (tempXStep / TEXELSIZE) * v;
  264.             tempYIncrement = (tempYStep / TEXELSIZE) * u;
  265.  
  266.             // Map origin to a sphere
  267.             tempVertexOrigin = CubeToSphereMapping(
  268.                     facePlane + tempPatchOrigin + tempXIncrement
  269.                             + tempYIncrement);
  270.  
  271.             // Get the terrain height
  272.             tempVertexHeight = someNoise.GetTerrainHeight(tempVertexOrigin);
  273.  
  274.             // Set data to generate a position map used once
  275.             pPositionMapData[(u * TEXELSIZEPLUS) + v] = aeVector4<double>(
  276.                     tempVertexOrigin.x_, tempVertexOrigin.y_,
  277.                     tempVertexOrigin.z_, (double) tempVertexHeight);
  278.  
  279.             // Copy height map to data using float
  280.             m_pHeightMapTextureData[(u * TEXELSIZEPLUS) + v] =
  281.                     (float) tempVertexHeight;
  282.         }
  283.     }
  284.  
  285.     for (unsigned int u = 0; u <= PatchSize; u++) {
  286.         for (unsigned int v = 0; v <= PatchSize; v++) {
  287.             tempVertexNormalData[(u * 33) + v] = CalculateNormal2(
  288.                     pPositionMapData, u, v);
  289.         }
  290.     }
  291.  
  292.     // loop through and create a grid of vertices. // do not draw edge
  293.     for (int u = 1; u < PatchSize; u += 2) {
  294.         for (int v = 1; v < PatchSize; v += 2) {
  295.  
  296.             // Create Index Data
  297.             for (unsigned int i = 0; i < 24; i++) {
  298.                 indexData.Push(index);
  299.                 index++;
  300.             }
  301.  
  302.             // Calculate origin point position
  303.             temptempVertexOriginPosition = (u * scale) * TEXELSIZEPLUS
  304.                     + (v * scale);
  305.  
  306.             // Get Position
  307.             tempVertexOrigin =
  308.                     Vector3(
  309.                             pPositionMapData[temptempVertexOriginPosition].GetVector3());
  310.  
  311.             // Multiply by stored height
  312.             tempVertexOrigin *=
  313.                     pPositionMapData[temptempVertexOriginPosition].GetHeight();
  314.  
  315.             tempVertexNormalOrigin = tempVertexNormalData[(u * 33) + v];
  316.  
  317.             // Loop through each edge
  318.             for (unsigned int i = 0; i < 8; i++) {
  319.                 // Create the position in array
  320.                 tempVertexPosition = temptempVertexOriginPosition
  321.                         + ((EdgeVertex[i][1] * scale) * TEXELSIZEPLUS)
  322.                         + (EdgeVertex[i][0] * scale);
  323.  
  324.                 // Get Position
  325.                 tempVertex[i] = Vector3(
  326.                         pPositionMapData[tempVertexPosition].GetVector3());
  327.  
  328.                 // Multiply by stored height
  329.                 tempVertex[i] *=
  330.                         pPositionMapData[tempVertexPosition].GetHeight();
  331.  
  332.                 // normalize
  333.                 tempVertexNormal[i] = tempVertexNormalData[(((u
  334.                         + EdgeVertex[i][1]) * 33) + v + EdgeVertex[i][0])];
  335.             }
  336.  
  337.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  338.                     tempVertex[1], tempVertexNormal[1], tempVertex[2],
  339.                     tempVertexNormal[2]);
  340.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  341.                     tempVertex[2], tempVertexNormal[2], tempVertex[3],
  342.                     tempVertexNormal[3]);
  343.  
  344.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  345.                     tempVertex[3], tempVertexNormal[3], tempVertex[4],
  346.                     tempVertexNormal[4]);
  347.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  348.                     tempVertex[4], tempVertexNormal[4], tempVertex[5],
  349.                     tempVertexNormal[5]);
  350.  
  351.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  352.                     tempVertex[5], tempVertexNormal[5], tempVertex[6],
  353.                     tempVertexNormal[6]);
  354.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  355.                     tempVertex[6], tempVertexNormal[6], tempVertex[7],
  356.                     tempVertexNormal[7]);
  357.  
  358.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  359.                     tempVertex[7], tempVertexNormal[7], tempVertex[0],
  360.                     tempVertexNormal[0]);
  361.             AddTriangleN(tempVertexOrigin, tempVertexNormalOrigin,
  362.                     tempVertex[0], tempVertexNormal[0], tempVertex[1],
  363.                     tempVertexNormal[1]);
  364.  
  365.         }
  366.     }
  367.  
  368.     // Just use the index ad the vertex count
  369.     m_IndexCount = indexData.Size();
  370.     m_VertexCount = indexData.Size();
  371.  
  372.     // Copy Index Shortcut
  373.     for (unsigned i = 0; i < m_IndexCount; i++) {
  374.         m_pIndexData[i] = indexData.At(i);
  375.     }
  376.  
  377.     // Delete Position Data
  378.     delete[] pPositionMapData;
  379.  
  380.     // Null Ptr
  381.     pPositionMapData = nullptr;
  382. }
  383.  
  384. SphereTerrainTopology::~SphereTerrainTopology() {
  385.  
  386.     // Delete Texture
  387.     delete m_pHeightMapTexture;
  388.  
  389.     delete[] m_pHeightMapTextureData;
  390.  
  391.     m_pHeightMapTextureData = nullptr;
  392.     m_pHeightMapTexture = nullptr;
  393.  
  394.     // Remove Data
  395.     delete[] m_pVertexData;
  396.     delete[] m_pIndexData;
  397.  
  398.     // Null Ptr
  399.     m_pVertexData = nullptr;
  400.     m_pIndexData = nullptr;
  401.  
  402.     // Clear Counter
  403.     m_IndexCount = 0;
  404.     m_VertexCount = 0;
  405. }
  406.  
  407. void SphereTerrainTopology::AddTriangle(Vector3 v1, Vector3 v2, Vector3 v3) {
  408.  
  409.     Vector3 cache_normal = Vector3::ZERO;
  410.  
  411.     *m_pVertexReference = v1.x_;
  412.     *m_pVertexReference++;
  413.     *m_pVertexReference = v1.y_;
  414.     *m_pVertexReference++;
  415.     *m_pVertexReference = v1.z_;
  416.     *m_pVertexReference++;
  417.  
  418.     v1 = cache_normal.Normalized();
  419.  
  420.     *m_pVertexReference = v1.x_;
  421.     *m_pVertexReference++;
  422.     *m_pVertexReference = v1.y_;
  423.     *m_pVertexReference++;
  424.     *m_pVertexReference = v1.z_;
  425.     *m_pVertexReference++;
  426.  
  427.     *m_pVertexReference = v2.x_;
  428.     *m_pVertexReference++;
  429.     *m_pVertexReference = v2.y_;
  430.     *m_pVertexReference++;
  431.     *m_pVertexReference = v2.z_;
  432.     *m_pVertexReference++;
  433.  
  434.     v2 = cache_normal.Normalized();
  435.  
  436.     *m_pVertexReference = v2.x_;
  437.     *m_pVertexReference++;
  438.     *m_pVertexReference = v2.y_;
  439.     *m_pVertexReference++;
  440.     *m_pVertexReference = v2.z_;
  441.     *m_pVertexReference++;
  442.  
  443.     *m_pVertexReference = v3.x_;
  444.     *m_pVertexReference++;
  445.     *m_pVertexReference = v3.y_;
  446.     *m_pVertexReference++;
  447.     *m_pVertexReference = v3.z_;
  448.     *m_pVertexReference++;
  449.  
  450.     v3 = cache_normal.Normalized();
  451.  
  452.     *m_pVertexReference = v3.x_;
  453.     *m_pVertexReference++;
  454.     *m_pVertexReference = v3.y_;
  455.     *m_pVertexReference++;
  456.     *m_pVertexReference = v3.z_;
  457.     *m_pVertexReference++;
  458.  
  459. }
  460.  
  461. void SphereTerrainTopology::AddTriangleN(Vector3 v1, Vector3 n1, Vector3 v2,
  462.         Vector3 n2, Vector3 v3, Vector3 n3) {
  463.  
  464. // Save Vertex information
  465.     *m_pVertexReference = v1.x_;
  466.     *m_pVertexReference++;
  467.     *m_pVertexReference = v1.y_;
  468.     *m_pVertexReference++;
  469.     *m_pVertexReference = v1.z_;
  470.     *m_pVertexReference++;
  471.  
  472.     *m_pVertexReference = n1.x_;
  473.     *m_pVertexReference++;
  474.     *m_pVertexReference = n1.y_;
  475.     *m_pVertexReference++;
  476.     *m_pVertexReference = n1.z_;
  477.     *m_pVertexReference++;
  478.  
  479.     *m_pVertexReference = v2.x_;
  480.     *m_pVertexReference++;
  481.     *m_pVertexReference = v2.y_;
  482.     *m_pVertexReference++;
  483.     *m_pVertexReference = v2.z_;
  484.     *m_pVertexReference++;
  485.  
  486.     *m_pVertexReference = n2.x_;
  487.     *m_pVertexReference++;
  488.     *m_pVertexReference = n2.y_;
  489.     *m_pVertexReference++;
  490.     *m_pVertexReference = n2.z_;
  491.     *m_pVertexReference++;
  492.  
  493.     *m_pVertexReference = v3.x_;
  494.     *m_pVertexReference++;
  495.     *m_pVertexReference = v3.y_;
  496.     *m_pVertexReference++;
  497.     *m_pVertexReference = v3.z_;
  498.     *m_pVertexReference++;
  499.  
  500.     *m_pVertexReference = n3.x_;
  501.     *m_pVertexReference++;
  502.     *m_pVertexReference = n3.y_;
  503.     *m_pVertexReference++;
  504.     *m_pVertexReference = n3.z_;
  505.     *m_pVertexReference++;
  506. }
  507.  
  508. Vector3 SphereTerrainTopology::SurfaceVectorToCoordinates(Vector3 surfacePos,
  509.         float radius, float height) {
  510. // Create a return veriable.
  511.     Vector3 loReturnData = surfacePos;
  512.  
  513. // Get a unit vector ( this will 'point' in the correct direction, from (0,0,0) to
  514. // the position of the vertex on the sphere ).
  515.     loReturnData.Normalize();
  516.     Vector3 pos = loReturnData.Normalized() * radius;
  517.  
  518. // Add the planet radius and the height of the vertex, and return the vector.
  519.     return loReturnData.Normalized();
  520. }
  521.  
  522. Vector3 SphereTerrainTopology::CubeToSphereMapping(Vector3 surfacePos) {
  523.     // Cube to Sphere Mapping
  524.     // http://mathproofs.blogspot.co.uk/2005/07/mapping-cube-to-sphere.html
  525.     /*
  526.      float x = surfacePos.x_;
  527.      float y = surfacePos.y_;
  528.      float z = surfacePos.z_;
  529.  
  530.      // Create a square root
  531.      float x2 = x * x;
  532.      float y2 = y * y;
  533.      float z2 = z * z;
  534.  
  535.      Vector3 ret;
  536.  
  537.      ret.x_ = x * sqrtf(1.0f - (y2 / 2.0f) - (z2 / 2.0f) + (y2 * z2 / 3.14f));
  538.      ret.y_ = y * sqrtf(1.0f - (z2 / 2.0f) - (x2 / 2.0f) + (x2 * z2 / 3.14f));
  539.      ret.z_ = z * sqrtf(1.0f - (x2 / 2.0f) - (y2 / 2.0f) + (x2 * y2 / 3.14f));*/
  540.  
  541.     cubetosphere_x = surfacePos.x_;
  542.     cubetosphere_y = surfacePos.y_;
  543.     cubetosphere_z = surfacePos.z_;
  544.  
  545.     // Create a square root
  546.     cubetosphere_x2 = cubetosphere_x * cubetosphere_x;
  547.     cubetosphere_y2 = cubetosphere_y * cubetosphere_y;
  548.     cubetosphere_z2 = cubetosphere_z * cubetosphere_z;
  549.  
  550.     cubetosphere_ret.x_ = cubetosphere_x
  551.             * sqrtf(
  552.                     1.0f - (cubetosphere_y2 / 2.0f) - (cubetosphere_z2 / 2.0f)
  553.                             + (cubetosphere_y2 * cubetosphere_z2 / 3.14f));
  554.     cubetosphere_ret.y_ = cubetosphere_y
  555.             * sqrtf(
  556.                     1.0f - (cubetosphere_z2 / 2.0f) - (cubetosphere_x2 / 2.0f)
  557.                             + (cubetosphere_x2 * cubetosphere_z2 / 3.14f));
  558.     cubetosphere_ret.z_ = cubetosphere_z
  559.             * sqrtf(
  560.                     1.0f - (cubetosphere_x2 / 2.0f) - (cubetosphere_y2 / 2.0f)
  561.                             + (cubetosphere_x2 * cubetosphere_y2 / 3.14f));
  562.  
  563.     return cubetosphere_ret;
  564. }
  565.  
  566. Vector3 SphereTerrainTopology::GetNormalHeight(
  567.         const aeVector4<double> * positionMap, unsigned int u, unsigned int v) {
  568.     unsigned int scale = 8;
  569.  
  570.     Vector3 normalVector;
  571.  
  572.     if (u == 0 || v == 0) {
  573.         return normalVector;
  574.     }
  575.  
  576. // cakculate
  577.     float heightL = (float) positionMap[((u - 1) * scale * TEXELSIZEPLUS)
  578.             + ((v - 1) * scale)].h;
  579.     float heightR = (float) positionMap[((u - 1) * scale * TEXELSIZEPLUS)
  580.             + ((v + 1) * scale)].h;
  581.     float heightD = (float) positionMap[((u + 1) * scale * TEXELSIZEPLUS)
  582.             + ((v - 1) * scale)].h;
  583.     float heightU = (float) positionMap[((u + 1) * scale * TEXELSIZEPLUS)
  584.             + ((v + 1) * scale)].h;
  585.  
  586.     if (heightR > TEXELSIZEPLUS || heightL > TEXELSIZEPLUS
  587.             || heightD > TEXELSIZEPLUS || heightU > TEXELSIZEPLUS) {
  588.         return normalVector;
  589.     }
  590.  
  591.     normalVector.x_ = heightL - heightR;
  592.     normalVector.y_ = 1.0f;
  593.     normalVector.z_ = heightD - heightU;
  594.  
  595.     return normalVector;
  596. }
  597.  
  598. Vector3 SphereTerrainTopology::CalculateNormal2(
  599.         const aeVector4<double> * positionMap, unsigned int u, unsigned int v) {
  600.     Vector3 edgeVertexSum = Vector3::ZERO;
  601.  
  602.     unsigned int scale = 8;
  603.     unsigned int faces = 0;
  604.  
  605.     Vector3 v0 =
  606.             positionMap[(u * scale) * TEXELSIZEPLUS + (v * scale)].GetVector3()
  607.                     * positionMap[(u * scale) * TEXELSIZEPLUS + (v * scale)].GetHeight();
  608.  
  609.     for (unsigned int i = 0; i < 8; i++) {
  610.         unsigned int vp = i;
  611.  
  612.         if (u + EdgeVertex[vp][1] < 0 || u + EdgeVertex[vp][1] > 32)
  613.             continue;
  614.         if (v + EdgeVertex[vp][0] < 0 || v + EdgeVertex[vp][0] > 32)
  615.             continue;
  616.  
  617.         // Calculate origin point position
  618.         Vector3 v1 =
  619.                 positionMap[((u + EdgeVertex[vp][1]) * scale) * TEXELSIZEPLUS
  620.                         + ((v + EdgeVertex[vp][0]) * scale)].GetVector3()
  621.                         * positionMap[((u + EdgeVertex[vp][1]) * scale)
  622.                                 * TEXELSIZEPLUS
  623.                                 + ((v + EdgeVertex[vp][0]) * scale)].GetHeight();
  624.  
  625.         // Skip to Next Vertex
  626.         vp++;
  627.         if (vp == 8) {
  628.             vp = 0;
  629.         }
  630.  
  631.         if (u + EdgeVertex[vp][1] < 0 || u + EdgeVertex[vp][1] > 32)
  632.             continue;
  633.         if (v + EdgeVertex[vp][0] < 0 || v + EdgeVertex[vp][0] > 32)
  634.             continue;
  635.  
  636.         Vector3 v2 =
  637.                 positionMap[((u + EdgeVertex[vp][1]) * scale) * TEXELSIZEPLUS
  638.                         + ((v + EdgeVertex[vp][0]) * scale)].GetVector3()
  639.                         * positionMap[((u + EdgeVertex[vp][1]) * scale)
  640.                                 * TEXELSIZEPLUS
  641.                                 + ((v + EdgeVertex[vp][0]) * scale)].GetHeight();
  642.  
  643.         // Skip to Next Vertex
  644.         vp++;
  645.         if (vp == 8) {
  646.             vp = 0;
  647.         }
  648.  
  649.         Vector3 a = v2 - v0;
  650.         Vector3 b = v1 - v0;
  651.         Vector3 n = b.CrossProduct(a);
  652.  
  653.         edgeVertexSum += n.Normalized();
  654.  
  655.         faces++;
  656.     }
  657.  
  658. // Renormalize
  659.     edgeVertexSum.Normalize();
  660.  
  661. // Average the vertexes normalze
  662.     edgeVertexSum /= faces;
  663.  
  664.     return edgeVertexSum;
  665. }
  666.  
  667. // Get Vertex Data
  668. float * SphereTerrainTopology::GetVertexData() const {
  669.     return m_pVertexData;
  670. }
  671.  
  672. // Get Index Data
  673. unsigned int * SphereTerrainTopology::GetIndexData() const {
  674.     return m_pIndexData;
  675. }
  676.  
  677. // Get Vertex Count (Same as Index count)
  678. unsigned int SphereTerrainTopology::GetVertexCounts() const {
  679.     return m_VertexCount;
  680. }
  681.  
  682. // Get Index Count (Same as Vertex count)
  683. unsigned int SphereTerrainTopology::GetIndexCounts() const {
  684.     return m_IndexCount;
  685. }
  686.  
  687. Texture2D * SphereTerrainTopology::GetHeightMap() const {
  688.     return m_pHeightMapTexture;
  689. }
Advertisement
Add Comment
Please, Sign In to add comment