I'm trying to make a small application that loads and renders a model on iOS using assimp and GL ES. I've worked with OpenGL before but not GL ES; and never anything on iOS.
我正在尝试创建一个小应用程序,使用assimp和GL ES在iOS上加载和呈现模型。我以前使用过OpenGL而不是GL ES; iOS上没有任何东西。
Currently I'm getting a EXC_BAD_ACCESS error upon calling glDrawElements; and I can't see what I'm doing wrong.
目前我在调用glDrawElements时遇到EXC_BAD_ACCESS错误;我看不出我做错了什么。
This is my Vertex type:
这是我的Vertex类型:
typedef struct {
float Position[3];
} Vertex;
And this is my Mesh type:
这是我的Mesh类型:
class Mesh {
public:
Vertex* vertices;
GLuint verticesCount;
GLubyte* indices;
GLuint indicesCount;
std::vector<Texture> textures;
GLuint vertexBuffer, indexBuffer;
Mesh(Vertex* vertices, GLubyte* indices, std::vector<Texture> textures);
};
I am pretty confident that I'm loading the models correctly through assimp as I've done it before and lifted this code from another project. So I'm using that data to pass into Mesh constructor and I am populating the VBO's using this:
我非常有信心我正在通过assimp正确加载模型,因为我之前已经完成并从另一个项目中解除了这个代码。所以我使用这些数据传递给Mesh构造函数,我使用这个填充VBO:
glGenBuffers(1, &this->vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
int sizeOfVerts = sizeof(Vertex) * this->verticesCount;
std::cout << "Size of verts in bytes " << unsigned(sizeOfVerts) << std::endl;
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)sizeOfVerts, &this->vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &this->indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->indexBuffer);
int sizeOfInd = sizeof(GLubyte) * this->indicesCount;
std::cout << "Size of inds in bytes " << unsigned(sizeOfInd) << std::endl;
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)sizeOfInd, &this->indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
With the projection and view model matricies already set up in a pre-render function I am then calling this to render my meshes:
由于投影和视图模型matricies已经在预渲染函数中设置,我然后调用它来渲染我的网格:
- (void)renderMesh:(Mesh*)mesh
{
glBindBuffer(GL_ARRAY_BUFFER, mesh->vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->indexBuffer);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
int meshSize = mesh->indicesCount;
glDrawElements(GL_TRIANGLES, meshSize, GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
With _positionSlot being an attrib location in my shader. I know my shader works as I have already used it to draw boring squared and such - so the error is definitely in the above code - somewhere.
_positionSlot是我的着色器中的attrib位置。我知道我的着色器工作,因为我已经用它来绘制无聊的方形等等 - 所以错误肯定在上面的代码 - 某处。
Thanks in advance guys, I'll make sure I both upvote and accept :)
在先谢谢你们,我会确保我们都赞成并接受:)
1 个解决方案
#1
0
The reason this was happening is because I was using GLubyte for the Indices type; which is too small for my high poly model. Not sure why the error manifested itself with this error message though; on other platforms the same code would render a junk model. Perhaps iOS specific? Hopefully this can help someone else.
发生这种情况的原因是因为我使用GLubyte作为指数类型;这对我的高多边形模型而言太小了。不知道为什么错误表明这个错误消息;在其他平台上,相同的代码将呈现垃圾模型。也许iOS具体?希望这可以帮助别人。
#1
0
The reason this was happening is because I was using GLubyte for the Indices type; which is too small for my high poly model. Not sure why the error manifested itself with this error message though; on other platforms the same code would render a junk model. Perhaps iOS specific? Hopefully this can help someone else.
发生这种情况的原因是因为我使用GLubyte作为指数类型;这对我的高多边形模型而言太小了。不知道为什么错误表明这个错误消息;在其他平台上,相同的代码将呈现垃圾模型。也许iOS具体?希望这可以帮助别人。