opengl-es - 使用 OpenGL ES 2.0 在 glBufferData 中加载顶点

标签 opengl-es opengl-es-2.0

我正在使用处理 Wavefront OBJ 3D 对象文件的解析器,我不太确定我是否正确地加载到 OpenGL 中。

基本上我所做的就是读取我的 Wavefront OBJ 文件并解析所有数据。

通常在 OpenGL ES 1.1 中,我在加载数据时执行以下操作:

glBegin(GL_TRIANGLES);
glNormal3f(normals[faces[i].normal[0]].v[0], normals[faces[i].normal[0]].v[1], normals[faces[i].normal[0]].v[2]);
glVertex3f(vertices[faces[i].vertex[0]].v[0], vertices[faces[i].vertex[0]].v[1], vertices[faces[i].vertex[0]].v[2]);
glNormal3f(normals[faces[i].normal[1]].v[0], normals[faces[i].normal[1]].v[1], normals[faces[i].normal[1]].v[2]);
glVertex3f(vertices[faces[i].vertex[1]].v[0], vertices[faces[i].vertex[1]].v[1], vertices[faces[i].vertex[1]].v[2]);
glNormal3f(normals[faces[i].normal[2]].v[0], normals[faces[i].normal[2]].v[1], normals[faces[i].normal[2]].v[2]);
glVertex3f(vertices[faces[i].vertex[2]].v[0], vertices[faces[i].vertex[2]].v[1], vertices[faces[i].vertex[2]].v[2]);
glEnd();

至于 OpenGL ES 2.0,我已经尝试跟随顶点但没有任何运气:

glBufferData(GL_ARRAY_BUFFER, obj.vertices.size()*sizeof(float), &(obj.vertices[0].v), GL_STATIC_DRAW);

我的数据结构:

struct vertex {
    std::vector<float> v;
}

v 向量是为偏离路线的每个顶点创建的,具有 {x,y,z}

class waveObj {
    public:
        std::vector<vertex> vertices;
        std::vector<vertex> texcoords;
        std::vector<vertex> normals;
        std::vector<vertex> parameters;
        std::vector<face> faces;
}

struct face {
    std::vector<int> vertex;
    std::vector<int> texture;
    std::vector<int> normal;
};

如何像在 2.0 中使用 OpenGL ES 1.1 那样加载数据?

还有可能加载一个向量(v),而不是单独的位置(float x,y,z)吗?

最佳答案

有几个选项:

  • 为每个数据创建单独的 VBO:一个用于位置,一个用于法线等
  • 使用交错数据创建单个 VBO - 但这需要在您的代码中进行一些代码更改。

一开始我建议对一个顶点属性使用一个缓冲区 + 索引缓冲区:

关于索引缓冲区的一件事:

  • 你有单独的 pos、normal、texture 索引(你直接从 OBJ 文件中获取这些值),但是如果你想使用 IBO(索引缓冲区对象)绘制几何图形,你需要创建 sinlge索引。

这是我的一些代码:

map<FaceIndex, GLushort, FaceIndexComparator>::iterator 
           cacheIndex = cache.find(fi);

if (cacheIndex != cache.end()) {
    node->mIndices.push_back(cacheIndex->second);   
}
else {
    node->mPositions.push_back(positions[fi.v]);
    node->mNormals.push_back(normals[fi.n]);
    node->mTexCoords.push_back(texCoords[fi.t]);
    node->mIndices.push_back((unsigned int)node->mPositions.size()-1);

    cache[fi] = ((unsigned int)node->mPositions.size()-1);
}

它的作用:

  • 每个 pos、nomal 和 tex cood 都有一个向量...但是当 OBJ 文件中有一个“f”标志时,我检查我的缓存中是否有一个三元组。
  • 如果有这样的三元组,我将该索引放入节点的索引中
  • 如果不是我需要创建新的索引

关于opengl-es - 使用 OpenGL ES 2.0 在 glBufferData 中加载顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10681210/

相关文章:

opengl-es - 如何使用 OpenGL SL 进行通用计算

ios - Objective-C 中有哪些好的 Open GL 抽象?

c++ - 确定发送到 VBO 的数据限制?

c - openGLES 2 - 有没有一种方法可以在不扩展为无符号字节的情况下,以每 1 位加载一个 alpha 元素的纹理?

opengl-es - OpenGL ES 2.x : Bind both `GL_TEXTURE_2D` and `GL_TEXTURE_CUBE_MAP` in the same texture image unit?

glsl - glsl 中的带边框圆角矩形

ios - 如何在 OpenGL ES 2.0 中独立操作对象?

android - 如何手动创建 mipmap?

java - Android opengles 20 渲染问题

android - 在 Android 上使用 OpenGL ES 2.0 绘制到后台缓冲区