来自HeightMap的法线

时间:2019-05-18 09:48:45

标签: c++ opengl

我正在尝试基于openGL中的噪声生成地图。

但是我的问题是正常的计算。

首先,我在geometryShader中对其进行了尝试,但它并不平滑,因此我尝试在cpu中对其进行计算。

我看到了一些有关如何执行此操作的帖子(例如:Calculate Normals from Heightmap),因此在实施后,它根本就没有执行操作

screenShot

我有点迷茫,我不知道为什么它不能正常工作。

网格生成


    int xTest = (x / (size_*2.0f)) * (len_);
    int zTest = (z / (size_*2.0f)) * (col_);

    float widthStep(2.0f / len), heightStep(2.0f / col);
    float widthTexStep(1.0f / len), heightTexStep(1.0f / col);

    for (size_t i(0); i < col; ++i) {
        for (size_t j(0); j < len; ++j) {

            float actualZ((i / (float)col)*2.0f - 1.0f), actualX((j / (float)len)*2.0f - 1.0f);
            float actualYTex(i / (float)col), actualXTex(j / (float)len);

            glm::vec2 pos1((int)j + xTest, (int)i + zTest), pos2((int)j + 1 + xTest, (int)i + zTest), pos3((int)j + xTest, (int)i + 1 + zTest), pos4((int)j + 1 + xTest, (int)i + 1 + zTest);

            float y1(heightGenerator_->generateHeight(pos1.x, pos1.y)), y2(heightGenerator_->generateHeight(pos2.x, pos2.y)), y3(heightGenerator_->generateHeight(pos3.x, pos3.y)), y4(heightGenerator_->generateHeight(pos4.x, pos4.y));


            //First Triangle
            //First Point
            //Position
            data_.push_back(actualX); //X
            data_.push_back(y1); //Y
            data_.push_back(actualZ); //Z
            heightMap_[i * (col + 1) + j] = y1;
            //Normal
            addNormal(pos1.x, pos1.y);

            //Texture
            data_.push_back(actualXTex); //TexX
            data_.push_back(actualYTex); //TexY


            //Second Point
            //Position
            data_.push_back(actualX + widthStep); //X
            data_.push_back(y2); //Y
            data_.push_back(actualZ); //Z

            //Normal
            addNormal(pos2.x, pos2.y);

            //Texture
            data_.push_back(actualXTex + widthTexStep); //TexX
            data_.push_back(actualYTex); //TexY


            //Thirdpoint
            //Position
            data_.push_back(actualX); //X
            data_.push_back(y3); //Y
            data_.push_back(actualZ + heightStep); //Z
            heightMap_[(i+1) * (col + 1) + j] = y3;

            //Normal
            addNormal(pos3.x, pos3.y);

            //Texture
            data_.push_back(actualXTex); //TexX
            data_.push_back(actualYTex + heightTexStep); //TexY


            //Second Triangle
            //FirstPoint
            //Position
            data_.push_back(actualX); //X
            data_.push_back(y3); //Y
            data_.push_back(actualZ + heightStep); //Z

            //Normal
            addNormal(pos3.x, pos3.y);

            //Texture
            data_.push_back(actualXTex); //TexX
            data_.push_back(actualYTex + heightTexStep); //TexY


            //Second Point
            //Position
            data_.push_back(actualX + widthStep); //X
            data_.push_back(y4); //Y
            data_.push_back(actualZ + heightStep); //Z
            heightMap_[(i + 1) * (col + 1) + j + 1] = y4; 

            //Normal
            addNormal(pos4.x, pos4.y);

            //Texture
            data_.push_back(actualXTex + widthTexStep); //TexX
            data_.push_back(actualYTex + heightTexStep); //TexY


            //Third point
            //Position
            data_.push_back(actualX + widthStep); //X
            data_.push_back(y2); //Y
            data_.push_back(actualZ); //Z
            heightMap_[i * (col + 1) + j + 1] = y2;

            //Normal
            addNormal(pos2.x, pos2.y);

            //Texture
            data_.push_back(actualXTex + widthTexStep); //TexX
            data_.push_back(actualYTex); //TexY
        }

    }

和addNormal函数:

void Terrain::addNormal(int x, int z) {
    float L(heightGenerator_->generateHeight(x - 1, z)), R(heightGenerator_->generateHeight(x + 1, z)), D(heightGenerator_->generateHeight(x, z - 1)), U(heightGenerator_->generateHeight(x, z + 1));
    glm::vec3 normal(glm::normalize(glm::vec3(L - R, 2.0f, D - U)));

    data_.push_back(normal.x);
    data_.push_back(normal.y);
    data_.push_back(normal.z);

}

高度生成器在世代上运行良好,所以我怀疑这是问题所在。

0 个答案:

没有答案