void GeometryTerrain::addTerrain(const int& size_, const float& octaves_, const float& height_, float* RGBFloats_, const float& scale2_, const float& persistance_) { for (int z = 0; z < size_; ++z) { for (int x = 0; x < size_; ++x) { glm::vec4 colourBottom(RGBFloats_[0], RGBFloats_[1], RGBFloats_[2], 1.0f); glm::vec4 colourTop(RGBFloats_[3], RGBFloats_[4], RGBFloats_[5], 1.0f); float temp_height = octave_noise_2d(octaves_, persistance_, scale2_, x, z); points[x][z] = glm::vec3(x, height_ * temp_height, z); glm::vec4 newcolour = glm::lerp(colourBottom, colourTop, temp_height); //gen colour based on Y height colours[x][z] = newcolour; } } //create the triangles from the points for (int z = 0; z < size_ - 1; ++z) { for (int x = 0; x < size_ - 1; ++x) { addTri(TriType::TRI_TERRAIN, points[x][z], points[x][z+1], points[x+1][z+1], colours[x][z], 0); addTri(TriType::TRI_TERRAIN, points[x][z], points[x + 1][z + 1], points[x + 1][z], colours[x][z], 1); } } }
// 2D Scaled Multi-octave Simplex noise. // // Returned value will be between loBound and hiBound. double scaled_octave_noise_2d( const int octaves, const double persistence, const double scale, const double loBound, const double hiBound, const double x, const double y ) { return octave_noise_2d(octaves, persistence, scale, x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2; }
// 2D Scaled Multi-octave Simplex noise. // // Returned value will be between loBound and hiBound. float scaled_octave_noise_2d( const float octaves, const float persistence, const float scale, const float loBound, const float hiBound, const float x, const float y ) { return octave_noise_2d(octaves, persistence, scale, x, y) * (hiBound - loBound) / 2 + (hiBound + loBound) / 2; }
// 2D Marble Noise: x-axis. float marble_noise_2d( const float octaves, const float persistence, const float scale, const float x, const float y ) { return cosf( x * scale + octave_noise_2d(octaves, persistence, scale / 3, x, y) ); }
void Chunk::Setup() { ChunkStorageLoader* pChunkStorage = m_pChunkManager->GetChunkStorage(m_gridX, m_gridY, m_gridZ, false); for (int x = 0; x < CHUNK_SIZE; x++) { for (int z = 0; z < CHUNK_SIZE; z++) { float xPosition = m_position.x + x; float zPosition = m_position.z + z; float noise = octave_noise_2d(m_pVoxSettings->m_landscapeOctaves, m_pVoxSettings->m_landscapePersistence, m_pVoxSettings->m_landscapeScale, xPosition, zPosition); float noiseNormalized = ((noise + 1.0f) * 0.5f); float mountainNoise = octave_noise_2d(m_pVoxSettings->m_mountainOctaves, m_pVoxSettings->m_mountainPersistence, m_pVoxSettings->m_mountainScale, xPosition, zPosition); float mountainNoiseNormalise = (mountainNoise + 1.0f) * 0.5f; float mountainMultiplier = m_pVoxSettings->m_mountainMultiplier * mountainNoiseNormalise; float noiseHeight = noiseNormalized * CHUNK_SIZE; noiseHeight *= mountainMultiplier; if (m_gridY < 0) { noiseHeight = CHUNK_SIZE; } for (int y = 0; y < CHUNK_SIZE; y++) { float yPosition = m_position.y + y; if (pChunkStorage != NULL && pChunkStorage->m_blockSet[x][y][z] == true) { SetColour(x, y, z, pChunkStorage->m_colour[x][y][z]); } else { if (y + (m_gridY*CHUNK_SIZE) < noiseHeight) { float colorNoise = octave_noise_3d(4.0f, 0.3f, 0.005f, xPosition, yPosition, zPosition); float colorNoiseNormalized = ((colorNoise + 1.0f) * 0.5f); float red1 = 0.65f; float green1 = 0.80f; float blue1 = 0.00f; float red2 = 0.00f; float green2 = 0.46f; float blue2 = 0.16f; if (noise < -0.5f) { red1 = 0.10f; green1 = 0.25f; blue1 = 1.00f; red2 = 0.10f; green2 = 0.25f; blue2 = 1.00f; } else if (noise < -0.25f) { red1 = 0.94f; green1 = 0.74f; blue1 = 0.34f; red2 = 0.50f; green2 = 0.29f; blue2 = 0.20f; } else if (noise < 0.5f) { red1 = 0.65f; green1 = 0.80f; blue1 = 0.00f; red2 = 0.00f; green2 = 0.46f; blue2 = 0.16f; } else if (noise < 1.0f) { red1 = 0.85f; green1 = 0.85f; blue1 = 0.85f; red2 = 0.77f; green2 = 0.65f; blue2 = 0.80f; } float alpha = 1.0f; float r = red1 + ((red2 - red1) * colorNoiseNormalized); float g = green1 + ((green2 - green1) * colorNoiseNormalized); float b = blue1 + ((blue2 - blue1) * colorNoiseNormalized); SetColour(x, y, z, r, g, b, alpha); } } } // Tree generation if (m_gridY >= 0) // Only above ground { // Trees if ((GetRandomNumber(0, 1000) >= 1000)) { if (noiseNormalized >= 0.5f) { vec3 treePos = vec3(xPosition, noiseHeight, zPosition); m_pChunkManager->ImportQubicleBinary("media/gamedata/terrain/plains/smalltree.qb", treePos, QubicleImportDirection_Normal); } } // Scenery if ((GetRandomNumber(0, 1000) >= 995)) { if (noiseNormalized >= 0.5f) { vec3 pos = vec3(xPosition, noiseHeight, zPosition); //m_pSceneryManager->AddSceneryObject("flower", "media/gamedata/terrain/plains/flower1.qb", pos, vec3(0.0f, 0.0f, 0.0f), QubicleImportDirection_Normal, QubicleImportDirection_Normal, 0.08f, GetRandomNumber(0, 360, 2)); } } } } } // Remove the chunk storage loader since we no longer need it if (pChunkStorage != NULL) { m_pChunkManager->RemoveChunkStorageLoader(pChunkStorage); } m_setup = true; SetNeedsRebuild(true, true); }