English 中文(简体)
如何使用具有位置、电交coord和正常位置的VAO
原标题:Howto use a VAO with position, texcoord and normal

我得到了一个波浪前 OBJ Parser 运行......但经过无休止的小时 试图弄清楚 我如何装入数据 在OpenGL 我最终不得不放弃。

这就是代码是如何构建的:

struct ObjMeshVertex{
    Vector3f pos;
    Vector2f texcoord;
    Vector3f normal;
};

struct ObjMeshFace{
    ObjMeshVertex vertices[3];
};

struct ObjMesh{
    std::vector<ObjMeshFace> faces;
};

ObjMesh myMesh;

for(size_t i = 0; i < faces.size(); ++i){
    ObjMeshFace face;
    for(size_t j = 0; j < 3; ++j){
        face.vertices[j].pos        = positions[faces[i].pos_index[j] - 1];
        face.vertices[j].texcoord   = texcoords[faces[i].tex_index[j] - 1];
        face.vertices[j].normal     = normals[faces[i].nor_index[j] - 1];
    }
    myMesh.faces.push_back(face);
}

通常当我创建一个简单的立方体时, 阵列看起来是这样的 :

通过使用 glVertexAttribpointer 的偏移来加载数据非常简单。

但我不知道怎么做到的吗?

EDIT Setup:

    glGenVertexArraysOES(1, &_boxVAO);
    glBindVertexArrayOES(_boxVAO);

    int sizeOfFaces = myMesh.faces.size() * sizeof(ObjMeshFace);
    glGenBuffers(1, &_boxBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _boxBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeOfFaces, &(myMesh.faces[0]), GL_STATIC_DRAW);


    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, texcoord));        
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE ,sizeof(ObjMeshVertex), (void*)offsetof(ObjMeshVertex, normal));

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(2);

    glBindVertexArrayOES(0);

绘图 :

glBindVertexArrayOES(_boxVAO);
glDrawArrays(GL_TRIANGLES, 0, indicesize);  

索引化 = myMesh.faces. size ()

最佳回答

如果你只是问如何将数据发送到 glBufferData, 那你只要把指针指到我的Mesh的第一个脸部, 然后打电话给glBufferData(假设所有东西都打得很紧)。

int sizeOfFaces = myMesh.faces.size() * sizeof(ObjMeshFace);
glBufferData(GL_ARRAY_BUFFER, sizeOfFaces, &(myMesh.faces[0]), usage);

这样你就可以把指示器 设置到这个断裂的结构上:

glVertexPointer(3, GL_FLOAT, sizeof(ObjMeshVertex), 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(ObjMeshVertex), (void*)(sizeof(Vector3f)));
glNormalPointer(3, GL_FLOAT, sizeof(ObjMeshVertex), (void*)(sizeof(Vector3f) + sizeof(Vector2f)));

你可能需要检查的一件事是,你的支架没有被加压(我不太确定如何启用/禁用)。

只要重复检查并断言:

sizeof(ObjMeshVertex) == 2 * sizeOf(Vector3f) + sizeOf(Vector2f);
sizeof(ObjMeshFace) == 3 * sizeOf(ObjMeshVertex);

如果这不是真的,你可能需要做一些调整 以适应支架垫。

问题回答

暂无回答




相关问题
OpenGL ES iPhone - drawing anti aliased lines

Normally, you d use something like: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glLineWidth(2.0f); glVertexPointer(2, GL_FLOAT, 0, points); ...

iPhone: Quartz2d vs. OpenGL ES

OK, I m still brand new to iPhone development. I have a free game on the app store, Winner Pong, but it s just a Pong clone (who would ve guessed) that uses the standard UIImageViews for the sprites. ...

GLPaint with white background

I m trying draw on a white background by reverse engineering GLPaint. I ve gone through every combination of kSaturation, kLuminosity and glBlendFunc, AND just about every combination I can think of ...

OpenGL ES - how to keep some object at a fixed size?

I m working on a little game in OpenGL ES. In the background, there is a world/map. The map is just a large texture. Zoom/pinch/pan is used to move around. And I m using glOrthof (left, right, bottom,...

Any free polygon library for the iPhone?

I need a library that can manage polygon modeling and basic transformation like rotating and translating. I d rather not redevelop everything from scratch Thanks

热门标签