微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在OpenGL中用Perlin Noise生成程序地形?

如何解决如何在OpenGL中用Perlin Noise生成程序地形?

我需要在OpenGL中使用Noise(使用Perlin噪声)生成程序地形。每次应用程序运行新地形时,都需要使用新种子生成。 (请勿使用外部库。)制作噪声地形类时是否需要方法/要求。我需要调用什么函数/计算以及顺序?

PS:我使用Visual Studio 2019。

// copy the array data into a float array,and scale and offset the heights.
    mHeightmap.resize(NumRows * NumCols,0);
    for( int i = 0; i < NumRows * NumCols; ++i)
    {
        mHeightmap[i] = (float)in[i] * HeightScale;
    }

// A height for each vertex
{
    std::vector<unsigned char> in(NumRows * NumCols);

    // Open the file.
    std::ifstream inFile;
    inFile.open(heightmapName.c_str(),std::ios_base::binary);

    if (inFile)
    {
        // Read the RAW bytes.
        inFile.read((char*)&in[0],(std::streamsize)in.size());

        // Done with file.
        inFile.close();
    }

    // copy the array data into a float array,0);
    for( int i = 0; i < NumRows * NumCols; ++i)
    {
        mHeightmap[i] = (float)in[i] * HeightScale;
    }

void Terrain::CreateVAO()
{
    std::vector<GLfloat> vertices;
    vertices.reserve(NumCols * NumRows * 8);
    

    float invTwoDX = 1.0f / (2.0f * CellSpacing);
    float invTwoDZ = 1.0f / (2.0f * CellSpacing);

    //vertices 
    for ( int z = 0; z < NumRows; z++)
    {
        for ( int x = 0; x < NumCols; x++)
        {
            
            //vertex data
            int i = z * NumCols + x;
            vertices.push_back((float)x*CellSpacing);
            vertices.push_back(mHeightmap[i]);
            vertices.push_back((float)z * CellSpacing);

            //normal data

            glm::vec3 _N = { 0.0f,1.0f,0.0f };

            if(z >= 1 && z < NumRows -1 && x >= 1 && z < NumCols - 1)
            { 
                float t = mHeightmap[(z - 1) * NumCols + x];
                float b = mHeightmap[(z + 1) * NumCols + x];
                float l = mHeightmap[z * NumCols + x - 1];
                float r = mHeightmap[z * NumCols + x + 1];

                glm::vec3 tanZ(0.0f,(b - t) * invTwoDZ,1.0f);
                glm::vec3 tanX(1.0f,(r - l) * invTwoDX,0.0f);

                glm::vec3 _C,_N;
                _C = glm::cross(tanZ,tanX);
                _N = glm::normalize(_C);

                
            }
            
            vertices.push_back(_N.x);
            vertices.push_back(_N.y);
            vertices.push_back(_N.z);


            vertices.push_back((float)x);
            vertices.push_back((float)z);


        }
    }
    

    std::vector<gluint> indices;
    vertices.reserve((NumCols-1)*(NumRows -1)*6);
    //indices
    for ( int z = 0; z < NumRows-1; z++)
    {
        for ( int x = 0; x < NumCols-1; x++)
        {
            GLint a = z * NumCols + x;
            GLint b = (z +1) * NumCols + x;
            GLint c = z * NumCols + (x+1);
            GLint d = (z+1) * NumCols + (x+1);

            indices.push_back(c);
            indices.push_back(a);
            indices.push_back(b);

            indices.push_back(c);
            indices.push_back(b);
            indices.push_back(d);
        
        }
    }
    indexcount = indices.size();


    gluint VBO,EBO;


    glGenVertexArrays(1,&VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1,&EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,indices.size() * sizeof(gluint),indices.data(),GL_STATIC_DRAW);

    glGenBuffers(1,&VBO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,vertices.size()*sizeof(GLfloat),vertices.data(),GL_STATIC_DRAW);

    glVertexAttribPointer(
        0,3,GL_FLOAT,GL_FALSE,8 * sizeof(GLfloat),//Strude of the single vertex(pos)
        (GLvoid*)0); //Offset from beginning of Vertex
    glEnabLevertexAttribArray(0);

    glVertexAttribPointer(
        1,//Strude of the single vertex(pos+color)
        (GLvoid*)(3 * sizeof(GLfloat))); //Offset from beginning of Vertex
    glEnabLevertexAttribArray(1);

    glVertexAttribPointer(
        2,2,//2 float component for coordinates
        GL_FLOAT,//Strude of the single vertex(pos+color+texture)
        (GLvoid*)(6 * sizeof(GLfloat)));//Offset from beginning of Vertex
    glEnabLevertexAttribArray(2);

解决方法

我不确定在您的代码中是否看到Perlin噪声的使用。试试这个轻量级,易于集成的库: https://github.com/Auburn/FastNoise,其中包含Perlin和大量其他有用的东西,例如可视化工具。 用法很简单 noise.GetNoise((float)x,(float)y);,您可以将其插入高度功能

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。