Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Build Data
- void SphereTerrainTopology::GenerateTopology() {
- // Do not compute topology if none exist
- if (!m_pPatch) {
- return;
- };
- // Create Noise
- SimplexNoise newNoise;
- // Create memory
- m_pVertexData = (float *) new float[PATCH_VERTICES_TOTAL * VERTEXELEMENTS
- * 6]; //256 / 768
- m_pIndexData = (unsigned int *) new unsigned int[PATCH_VERTICES_TOTAL * 6];
- // Create Position Map Data
- aeVector4<double>* pPositionMapData = (aeVector4<double>*) new aeVector4<
- double> [TEXELSIZEPLUS * TEXELSIZEPLUS];
- // Create memory
- m_pHeightMapTexture = new Texture2D(context_);
- m_pHeightMapTextureData = new float[TEXELSIZEPLUS * TEXELSIZEPLUS];
- // Create off texture turn off compression
- m_pHeightMapTexture->SetNumLevels(1);
- m_pHeightMapTexture->SetSize(TEXELSIZEPLUS, TEXELSIZEPLUS,
- g_pApp->GetGraphics()->GetFloat32Format(), TEXTURE_STATIC);
- m_pHeightMapTexture->SetData(0, 0, 0, TEXELSIZEPLUS, TEXELSIZEPLUS,
- m_pHeightMapTextureData);
- m_pHeightMapTexture->SetFilterMode(FILTER_NEAREST);
- // Set Cube size
- float CubeSize = 2.0f;
- // Get Visual Radius
- float VisualRadius = m_pPatch->GetVisualRadius();
- // Create a ridgednoise perlon
- RidgedNoise someNoise(VisualRadius);
- // Set Noise
- someNoise.SetRidgedNoiseParameters(m_pPatch->GetHeightScale(),
- m_pPatch->GetOctaves(), m_pPatch->GetGain(),
- m_pPatch->GetLacunarity(), m_pPatch->GetOffset(), m_pPatch->GetH());
- // Reset Cpimts
- m_IndexCount = 0;
- m_VertexCount = 0;
- // size best off vertices
- int PatchSize = PATCH_VERTICES;
- // Get Cube Center - Create Defaults
- Vector3 facePlane = Vector3::ZERO;
- Vector3 direction_x = Vector3::ZERO;
- Vector3 direction_y = Vector3::ZERO;
- Vector3 vertexDirection = Vector3::ZERO;
- // Element total size easier to count
- unsigned int VertexElementTotalSize = 6 * VERTEXELEMENTS;
- // Get Face Direction
- const STFaceDirection faceDirection = m_pPatch->GetFaceDirection();
- const Vector2 Coordinates = m_pPatch->GetCoordinates();
- const unsigned int depth = m_pPatch->GetDepth();
- // Face direction
- unsigned int face = (unsigned int) faceDirection;
- // Calculate face plane
- facePlane = ((float) CubeSize / 2) * TerrainFaceCoordinate[face]; // put a side on plane 1
- // Get depth
- float Depth = 1.0f / pow(2.0f, depth);
- // Debug Face Creation at
- //ALPHAENGINE_LOGINFO(
- // "FacePlane at " + facePlane.ToString() + " with "+Coordinates.ToString()+" and depth of "+String(Depth));
- switch (face) {
- case 0: {
- direction_x = CubeSize * Vector3(0, -1, 0);
- direction_y = CubeSize * Vector3(0, 0, 1);
- }
- break;
- case 1: {
- direction_x = CubeSize * Vector3(0, -1, 0);
- direction_y = CubeSize * Vector3(0, 0, -1);
- }
- break;
- case 2: {
- direction_x = CubeSize * Vector3(0, 0, 1);
- direction_y = CubeSize * Vector3(1, 0, 0);
- }
- break;
- case 3: {
- direction_x = CubeSize * Vector3(0, 0, 1);
- direction_y = CubeSize * Vector3(-1, 0, 0);
- }
- break;
- case 4: {
- direction_x = CubeSize * Vector3(0, -1, 0);
- direction_y = CubeSize * Vector3(-1, 0, 0);
- }
- break;
- case 5: {
- direction_x = CubeSize * Vector3(0, -1, 0);
- direction_y = CubeSize * Vector3(1, 0, 0);
- }
- break;
- }
- // Index Data
- Vector<unsigned int> indexData;
- // Create a index
- unsigned int index = 0;
- // Set Reference to Vertex Data
- m_pVertexReference = &m_pVertexData[0];
- // Create Vectors To Hold Origin X,Y for each generated triangle set - Cache variables
- Vector3 VertexOrigin, Vertex[8], Origin;
- Vector3 nOrigin, nVertex[8];
- double height = 0;
- int originposition, position = 0;
- // size to use
- unsigned int scale = TEXELSIZE / PATCH_VERTICES;
- switch (face) {
- case 0:
- Origin = Vector3(0.0f, 1.0f, -1.0f)
- + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
- Coordinates.y_ * 2.0f);
- break;
- case 1: {
- Origin = Vector3(0.0f, 1.0f, 1.0f)
- + Vector3(0.0f, -(Coordinates.x_ * 2.0f),
- -(Coordinates.y_ * 2.0f));
- }
- break;
- case 2: {
- Origin = Vector3(-1.0f, 0.0f, -1.0f)
- + Vector3(Coordinates.y_ * 2.0f, 0.0f, Coordinates.x_ * 2.0f);
- }
- break;
- case 3: {
- Origin = Vector3(1.0f, 0.0f, -1.0f)
- + Vector3(-(Coordinates.y_ * 2.0f), 0.0f,
- Coordinates.x_ * 2.0f);
- }
- break;
- case 4: {
- Origin = Vector3(1.0f, 1.0f, 0.0f)
- + Vector3(-(Coordinates.y_ * 2.0f), -(Coordinates.x_ * 2.0f),
- 0.0f);
- }
- break;
- case 5: {
- Origin = Vector3(-1.0f, 1.0f, 0.0f)
- + Vector3(Coordinates.y_ * 2.0f, -(Coordinates.x_ * 2.0f),
- 0.0f);
- }
- }
- // Add depth
- direction_x *= Depth;
- direction_y *= Depth;
- // Generate a texture
- for (int u = 0; u < TEXELSIZEPLUS; u++) {
- for (int v = 0; v < TEXELSIZEPLUS; v++) {
- // Use precache values to speed process
- cache_x = (direction_x / TEXELSIZE) * v;
- cache_y = (direction_y / TEXELSIZE) * u;
- // Map origin to a sphere
- VertexOrigin = CubeToSphereMapping(
- facePlane + Origin + cache_x + cache_y);
- // Get the terrain height
- height = someNoise.GetTerrainHeight(VertexOrigin);
- // Set data to generate a position map used once
- pPositionMapData[(u * TEXELSIZEPLUS) + v] = aeVector4<double>(
- VertexOrigin.x_, VertexOrigin.y_, VertexOrigin.z_,
- (double) height);
- // Copy height map to data using float
- m_pHeightMapTextureData[(u * TEXELSIZEPLUS) + v] = (float) height;
- }
- }
- // loop through and create a grid of vertices. // do not draw edge
- for (int u = 1; u < PatchSize; u += 2) {
- for (int v = 1; v < PatchSize; v += 2) {
- // Create Index Data
- for (unsigned int i = 0; i < 24; i++) {
- indexData.Push(index);
- index++;
- }
- // Calculate origin point position
- originposition = (u * scale) * TEXELSIZEPLUS + (v * scale);
- // Get Position
- VertexOrigin = Vector3(
- pPositionMapData[originposition].GetVector3());
- // Multiply by stored height
- VertexOrigin *= pPositionMapData[originposition].GetHeight();
- // Get normal height
- nOrigin = GetNormalHeight(pPositionMapData, (unsigned int) u,
- (unsigned int) v);
- // Loop through each edge
- for (unsigned int i = 0; i < 8; i++) {
- // Create the position in array
- position = originposition
- + ((EdgeVertex[i][1] * scale) * TEXELSIZEPLUS)
- + (EdgeVertex[i][0] * scale);
- // Get Position
- Vertex[i] = Vector3(pPositionMapData[position].GetVector3());
- // Multiply by stored height
- Vertex[i] *= pPositionMapData[position].GetHeight();
- }
- // Add First Triangle
- AddTriangle(VertexOrigin, Vertex[1], Vertex[2]);
- AddTriangle(VertexOrigin, Vertex[2], Vertex[3]);
- AddTriangle(VertexOrigin, Vertex[3], Vertex[4]);
- AddTriangle(VertexOrigin, Vertex[4], Vertex[5]);
- AddTriangle(VertexOrigin, Vertex[5], Vertex[6]);
- AddTriangle(VertexOrigin, Vertex[6], Vertex[7]);
- AddTriangle(VertexOrigin, Vertex[7], Vertex[0]);
- AddTriangle(VertexOrigin, Vertex[0], Vertex[1]);
- }
- }
- // Just use the index ad the vertex count
- m_IndexCount = indexData.Size();
- m_VertexCount = indexData.Size();
- /*// loop through each vertex and chang the normal
- for (unsigned int i = 0; i < m_VertexCount / 3; i += 3) {
- // create a table of vertex
- Vector3 vec[3];
- vec[0] = Vector3(m_pVertexData[(i * 8) + 0], m_pVertexData[(i * 8) + 1],
- m_pVertexData[(i * 8) + 2]);
- vec[1] = Vector3(m_pVertexData[(i + 1) * 8 + 0],
- m_pVertexData[(i + 1) * 8 + 1], m_pVertexData[(i + 1) * 8 + 2]);
- vec[2] = Vector3(m_pVertexData[(i + 2) * 8 + 0],
- m_pVertexData[(i + 2) * 8 + 1], m_pVertexData[(i + 2) * 8 + 2]);
- Vector3 u = vec[0] - vec[1];
- Vector3 v = vec[0] - vec[2];
- Vector3 facenormal = v.CrossProduct(u);
- facenormal.Normalized();
- for (unsigned int vi = 0; vi < 3; vi++) {
- Vector3 vertexnormal = facenormal;
- m_pVertexData[((i + vi) * 8) + 3] = vertexnormal.x_;
- m_pVertexData[((i + vi) * 8) + 4] = vertexnormal.y_;
- m_pVertexData[((i + vi) * 8) + 5] = vertexnormal.z_;
- }
- }*/
- // Copy Index Shortcut
- for (unsigned i = 0; i < m_IndexCount; i++) {
- m_pIndexData[i] = indexData.At(i);
- }
- // Delete Position Data
- delete[] pPositionMapData;
- // Null Ptr
- pPositionMapData = nullptr;
- }
- SphereTerrainTopology::~SphereTerrainTopology() {
- // Delete Texture
- delete m_pHeightMapTexture;
- delete[] m_pHeightMapTextureData;
- m_pHeightMapTextureData = nullptr;
- m_pHeightMapTexture = nullptr;
- // Remove Data
- delete[] m_pVertexData;
- delete[] m_pIndexData;
- // Null Ptr
- m_pVertexData = nullptr;
- m_pIndexData = nullptr;
- // Clear Counter
- m_IndexCount = 0;
- m_VertexCount = 0;
- }
- void SphereTerrainTopology::AddTriangle(Vector3 v1, Vector3 v2, Vector3 v3) {
- cache_u = v3 - v1;
- cache_v = v2 - v1;
- cache_normal = cache_v.CrossProduct(cache_u);
- Vector3 cu2 = v1 - v2;
- Vector3 cv2 = v3 - v2;
- Vector3 n2 = cv2.CrossProduct(cu2);
- Vector3 cu3 = v2 - v3;
- Vector3 cv3 = v1 - v3;
- Vector3 n3 = cv3.CrossProduct(cu3);
- Vector3 NormalizedCross = Vector3(cache_normal + n2 + n3).Normalized();
- *m_pVertexReference = v1.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v1.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v1.z_;
- *m_pVertexReference++;
- cache_normal = Vector3(v1.Normalized()+NormalizedCross).Normalized();
- v1 = cache_normal;
- *m_pVertexReference = v1.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v1.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v1.z_;
- *m_pVertexReference++;
- *m_pVertexReference = v2.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v2.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v2.z_;
- *m_pVertexReference++;
- cache_normal = Vector3(v2.Normalized()+NormalizedCross).Normalized();
- v2 = cache_normal;
- *m_pVertexReference = v2.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v2.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v2.z_;
- *m_pVertexReference++;
- *m_pVertexReference = v3.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v3.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v3.z_;
- *m_pVertexReference++;
- cache_normal = Vector3(v3.Normalized()+NormalizedCross).Normalized();
- v3 = cache_normal;
- *m_pVertexReference = v3.x_;
- *m_pVertexReference++;
- *m_pVertexReference = v3.y_;
- *m_pVertexReference++;
- *m_pVertexReference = v3.z_;
- *m_pVertexReference++;
- }
Advertisement
Add Comment
Please, Sign In to add comment