可以使用一些帮助实现Perlin噪声来在我的游戏引擎中生成地形。

时间:2018-03-31 19:47:13

标签: c++ perlin-noise

所以我正在通过一些YouTube教程来构建游戏引擎。我使用C ++编写代码,并使用SDL2和SDL2纹理库来实现功能。为了产生噪声,我使用A头和从GitHub获取的名为Fast Noise的.cpp。我用来绘制地图的.cpp是

#include "Map.h"
#include "TextureManager.h"
#include "FastNoise.h"


int lvl1[20][25] = {




{ 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
};

Map::Map() 
{
dirt = TextureManager::LoadTexture("Assets/TileSets/Dirt.png");
grass = TextureManager::LoadTexture("Assets/TileSets/Grass.png");
water = TextureManager::LoadTexture("Assets/TileSets/Water.png");


LoadMap(lvl1);

src.x = src.y = 0;
src.w = dest.w = 32;
src.h = dest.h = 32;

dest.x = dest.y = 0;
}

void Map::LoadMap(int arr[20][25])
{
for (int row = 0; row < 20; row++)
{
    for (int column = 0; column < 25; column++)
    {
        map[row][column] = arr[row][column];
    }
}


}

void Map::DrawMap()
{
int type = 0;

for (int row = 0; row < 20; row++)
{
    for (int column = 0; column < 25; column++)
    {
        type = map[row][column];

        dest.x = column * 32;
        dest.y = row * 32;

        switch (type) 
        {
        case 0:
            TextureManager::Draw(water, src, dest);
            break;
        case 1:
            TextureManager::Draw(grass, src, dest);
            break;
        case 2:
            TextureManager::Draw(dirt, src, dest);


        default:
            break;
        }

    }
 }
}

创建噪音的代码是

FastNoise myNoise; // Create a FastNoise object
myNoise.SetNoiseType(FastNoise::SimplexFractal); // Set the desired noise 
type

float heightMap[32][32]; // 2D heightmap to create terrain

for (int x = 0; x < 32; x++)
{
for (int y = 0; y < 32; y++)
{
    heightMap[x][y] = myNoise.GetNoise(x,y);
}
}

我正在努力的是如何实现它,以便它不是使用lvl1 int数组,而是存储并使用由噪声创建的值来绘制地形。如果帖子不明显我对编程很新,我创建的游戏引擎功能相当,但我想创建程序生成的地形,而不是手工制作。

0 个答案:

没有答案