c++ - 从高度图计算纹理坐标

标签 c++ opengl textures

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

enter image description here

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

enter image description here

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

enter image description here

这是我目前拥有的:

生成顶点、法线和索引

// 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 个颜色 channel (GL_RGB),并且被紧密打包,因此图像一行的大小可能不会对齐到 4 个字节。

当具有 3 个颜色 channel 的 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 字节对齐。

相关问题:Failing to map a simple unsigned byte rgb texture to a quad

关于c++ - 从高度图计算纹理坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55739024/

相关文章:

c++ - 在 OpenGL 中。如何将纹理的一部分复制到另一个纹理

浮点 gpgpu 的 opengl 纹理格式

c++ - 编译器是否管理使用模板元编程生成的重复类型

c++ - 使用 Qt 在小部件上绘制图像

c++ - GtkGLArea 清除背景但不绘制

c++ - OpenGl纹理: wrong Pixels

c++ - GL_SHADER_STORAGE_BUFFER 位置是否与其他着色器位置发生冲突?

java - Superpowered NDK Android : returning int succeeds in "extern" block, 调用函数失败

c++ - 用OpenGL画圆

opengl - 关于 glTexImage3D