【问题标题】:texture image does not map correctly纹理图像未正确映射
【发布时间】:2020-01-08 11:05:15
【问题描述】:

我正在尝试将纹理图像映射到球体。

我在不同的向量中有顶点和纹理坐标,这就是我使用glBufferSubData的原因。

std::vector<glm::vec3> sphere_vertices;
std::vector<int> sphere_indices;
std::vector<glm::vec2> sphere_texcoords;

我不使用任何颜色,只使用顶点、索引、纹理。

我正在使用:

// upload geometry to GPU
glBindVertexArray(sphere_VAO);
glGenBuffers(1, &sphere_vertices_VBO);
glBindBuffer(GL_ARRAY_BUFFER, sphere_vertices_VBO);


glBufferData(GL_ARRAY_BUFFER, sizeof(float) * sphere_vertices.size() + sizeof(float) * sphere_texcoords.size(),
            0, GL_STATIC_DRAW);

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * sphere_vertices.size(), sphere_vertices.data());

glBufferSubData(GL_ARRAY_BUFFER, sizeof(float) * sphere_vertices.size(), sizeof(float) * sphere_texcoords.size(), sphere_texcoords.data());

glGenBuffers(1, &sphere_indices_VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphere_indices_VBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * sphere_indices.size(), sphere_indices.data(), GL_STATIC_DRAW);


// setup vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

// texture coords attrib
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float) , (void*)(sizeof(float) * sphere_vertices.size()));

并且图像没有正确映射到球体上。

我收到的图片:

-- 更新 ---

我将sphere_texcoords.push_back((glm::vec2((x + 1) / 2.0, (y + 1) / 2.0))); 用于 texcoords,现在可以使用了!

【问题讨论】:

    标签: c++ opengl opengl-3


    【解决方案1】:

    缓冲区的大小和缓冲区偏移量必须以字节为单位。
    注意,一个元素的大小分别是sizeof(glm::vec3)sizeof(glm::vec2)而不是sizeof(float)

    size_t vertices_size  = sizeof(glm::vec3) * sphere_vertices.size();
    size_t texcoords_size = sizeof(glm::vec2) * sphere_texcoords.size();
    
    glBufferData(GL_ARRAY_BUFFER, vertices_size + texcoords_size, 0, GL_STATIC_DRAW);
    
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertices_size, sphere_vertices.data());
    
    glBufferSubData(GL_ARRAY_BUFFER, vertices_size, texcoords_size, sphere_texcoords.data());
    

    当绑定命名缓冲区对象时,glVertexAttribPointer 的最后一个参数被视为此缓冲区的字节偏移量。 纹理坐标的偏移量必须是:

    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), (void*)vertices_size);
    

    一般std::vector&lt;T&gt; v 的数据大小(以字节为单位)可以通过:

    size_t size = sizeof(T) * v.size();
    

    分别

    size_t size = sizeof(*v.data()) * v.size();
    

    【讨论】:

    • 嗨,结果还是一样。图像没有正确映射。我不确定我是否正确处理了sphere_indices_VBO,我在其中调用了`glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * sphere_indices。大小(),sphere_indices.data(),GL_STATIC_DRAW); `
    • @George 索引是正确的。 “图像未正确映射” 是什么意思?纹理坐标来自哪里?可能图像映射正确,但纹理坐标不正确。
    • 我在 [this] (stackoverflow.com/questions/5988686/…) 代码中使用 texcoords。我想将球图像映射到球体上。
    • 好的,我上传了一张图片,谢谢。(注意,球体是黑色的,所以它显示为黑色)
    • 我将sphere_texcoords.push_back((glm::vec2((x + 1) / 2.0, (y + 1) / 2.0))); 用于 texcoords,它有效!但是,当我移动相机时,图像再次失真..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2012-09-07
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多