从高度图计算纹理坐标

Calculating texture coordinates from a heightmap

我目前正在使用 OpenGL 构建高度图地形生成器。这是一个加载高度图图像、迭代图像数据并生成顶点、索引和法线的简单程序。在其当前状态下,它可以基于法线渲染具有单一颜色的高度图。

我的问题是为漫反射贴图生成正确的 UV 坐标。结果是错误的:

这是我要加载的漫反射贴图:

这是我目前拥有的:

生成顶点、法线和索引

// Generate Vertices and texture coordinates
    for (int row = 0; row <= this->imageHeight; row++)
    {
        for (int column = 0; column <= this->imageWidth; column++)
        {
            float x = (float)column / (float)this->imageWidth;
            float y = (float)row / (float)this->imageHeight;

            float pixel = this->imageData[this->imageWidth * row + column];

            float z;
            if (row == this->imageHeight || column == this->imageWidth || row == 0 || column == 0)
            {
                z = 0.0f;
            }
            else
            {
                z = float(pixel / 256.0)*this->scale; 
            }

            MeshV3 mesh;
            mesh.position = glm::vec3(x, y, z);
            mesh.normal = glm::vec3(0.0, 0.0, 0.0);
            mesh.texture = glm::vec2(x, y);

            this->mesh.push_back(mesh);

        }
    }

// Generate indices
    for (int row = 0; row < this->imageHeight; row++)
    {
        for (int column = 0; column < this->imageWidth; column++)
        {
            int row1 = row * (this->imageWidth + 1);
            int row2 = (row + 1) * (this->imageWidth + 1);

            // triangle 1
            this->indices.push_back(glm::uvec3(row1 + column, row1 + column + 1, row2 + column + 1));

            // triangle 2
            this->indices.push_back(glm::uvec3(row1 + column, row2 + column + 1, row2 + column));
        }
    }

// Generate normals
    for (int i = 0; i < this->indices.size(); i++)
    {
        glm::vec3 v1 = this->mesh[this->indices[i].x].position;
        glm::vec3 v2 = this->mesh[this->indices[i].y].position;
        glm::vec3 v3 = this->mesh[this->indices[i].z].position;

        glm::vec3 edge1 = v1 - v2;
        glm::vec3 edge2 = v1 - v3;
        glm::vec3 normal = glm::normalize(glm::cross(edge1, edge2));

        this->mesh[this->indices[i].x].normal += normal;
        this->mesh[this->indices[i].y].normal += normal;
        this->mesh[this->indices[i].z].normal += normal;
    }

我用下面的方法加载漫反射贴图

void Terrein::getDIffuseMap()
{
    glGenTextures(1, &this->texture);
    glBindTexture(GL_TEXTURE_2D, this->texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    int width, height, nrChannels;

    std::string path = "assets/diffuse.jpg";
    this->diffuseData = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
    if (this->diffuseData)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, this->diffuseData);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        std::cout << "Failed to load diffuse texture" << std::endl;
    }
}

我似乎无法弄清楚这里可能出了什么问题。我加载图像的方式有问题吗?或者我不是在计算纹理坐标吗?如果还有什么我应该提供的,请告诉我。我已经坚持了几天了。谢谢!

默认情况下,OpenGL 假定图像的每一行的开头对齐到 4 个字节。

这是因为GL_UNPACK_ALIGNMENT参数默认为4.

由于图像有 3 个颜色通道 (GL_RGB),并且是紧凑的,所以图像的一行大小可能不会对齐到 4 个字节。

当具有 3 个颜色通道的 RGB 图像加载到纹理对象时,则必须将 GL_UNPACK_ALIGNMENT 设置为 1:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
             GL_RGB, GL_UNSIGNED_BYTE, this->diffuseData);

问题中的漫反射图像尺寸为 390x390。所以图像的每一行都有 390 * 3 = 1170 字节的大小。
由于 1170 不能被 4 整除 (1170 / 4 = 292,5),因此一行的开头未对齐到 4 个字节。

相关问题: