简体   繁体   English

OpenGL Vertexbuffer 对象纹理坐标

[英]OpenGL Vertexbuffer object texture coordinates

I want to create mesh class with VBO in C++.我想在 C++ 中使用 VBO 创建网格类。 The class looks like following:该类如下所示:

mesh::mesh(std::vector<Vector3d>* Vertices, std::vector<unsigned int>* Indices, std::vector<Vector2d>* TextureCoords) {
    if(Vertices) this->vertices = *Vertices;
    if(Indices) this->indices = *Indices;
    if(TextureCoords) this->textureCoords = *TextureCoords;
    chatbox.AddMessageToQueue("vertices.size() : %d", vertices.size());
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d) + textureCoords.size()*sizeof(Vector2d), 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size()*sizeof(Vector3d), vertices.data());
    glBufferSubData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vector3d), textureCoords.size()*sizeof(Vector2d), textureCoords.data());

    glGenBuffers(1, &IND);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
}

void mesh::draw() {
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glVertexPointer(3, GL_FLOAT, 0, (void*)0);
    glTexCoordPointer(2, GL_FLOAT, 0, (void*)(sizeof(float)*3*6));

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IND);
    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (unsigned int*)0 + 0);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

The problem is in the texture coordinates only.问题仅在于纹理坐标。 I'm trying to create the mesh like so:我正在尝试像这样创建网格:

std::vector<unsigned int> indices;
std::vector<mesh::Vector3d> vertices;
std::vector<mesh::Vector2d> texCoords;

vertices.push_back({PosX, PosY, 1.0});
vertices.push_back({PosX + SizeX, PosY, 1.0});
vertices.push_back({PosX + SizeX, PosY + SizeY, 1.0});
vertices.push_back({PosX, PosY + SizeY, 1.0});

indices.push_back(0);
indices.push_back(1);
indices.push_back(2);

indices.push_back(0);
indices.push_back(2);
indices.push_back(3);

texCoords.push_back({0, 1});
texCoords.push_back({1, 0});
texCoords.push_back({0, 0});

texCoords.push_back({0, 1});
texCoords.push_back({1, 1});
texCoords.push_back({1, 0});

gui_checkbox = new mesh(&vertices, &indices, &texCoords);

But the result is wrong, as you can see in the left picture below (the picture on the right is the desired one):但是结果是错误的,如下图左图所示(右图是想要的):

左边图片来自VBO,右边是立即模式

The vertices are in ortho mode, so the coordinate origin for vertices is in top left corner and for texture coordinates the origin is in the bottom left just like in OpenGL.顶点处于正交模式,所以顶点的坐标原点在左上角,纹理坐标的原点在左下角,就像在 OpenGL 中一样。

When one is using indices to address the vertex positions, the texture coordinates also get indexed.当使用索引来寻址顶点位置时,纹理坐标也会被索引。 In your case this means, that you are only using the first four entries in texCoords.在您的情况下,这意味着您仅使用 texCoords 中的前四个条目。 The strange looking comes from texCoords[0] == texCoords[3].奇怪的外观来自 texCoords[0] == texCoords[3]。

The correct texCoords would most probably be正确的 texCoords 很可能是

texCoords.push_back({0, 1});
texCoords.push_back({1, 1});
texCoords.push_back({1, 0});
texCoords.push_back({0, 0});

These can be derived from the vertex-coordinates: texCoords should have the same value at a component whenever their corresponding vertices have the same component value.这些可以从顶点坐标派生:只要它们对应的顶点具有相同的组件值,texCoords 就应该在一个组件上具有相同的值。 eg if vertices[0].y == vertices[1].y => texCoords[0].y == texCoords[1].y and so on.例如,如果 vertices[0].y == vertices[1].y => texCoords[0].y == texCoords[1].y 等等。 In addition, the texCoords y coordinates have to be flipped, since vertex origin as on top-left while texcoords start at bottom left.此外,必须翻转 texCoords y 坐标,因为顶点原点在左上角而 texcoords 从左下角开始。

Edit:编辑:

glTexCoordPointer(2, GL_FLOAT, 0, (void*)(sizeof(float)*3*6));

does not look correct to me.对我来说看起来不正确。 This statement tells that the first texture coordinate starts after the sixth vector3, but shouldn't they start after the fourth one?这个语句说明第一个纹理坐标在第六个 vector3 之后开始,但它们不应该在第四个之后开始吗? (You only have 4 entries in vertices): (顶点中只有 4 个条目):

glTexCoordPointer(2, GL_FLOAT, 0, (void*)(sizeof(float)*3*4));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM