I'm trying to load a model using assimp. I'm trying to use indexing, but my model isn't working. My code has no errors (just that the parsing isn't working right for some reason) and this is what it is:
void loadOBJ(const char *path,
int &numOfVertices,
int &numOfFaces,
int &numOfIndices,
std::vector<unsigned int> &outIndices,
std::vector<float> &outVertices,
std::vector<float> &outUVs,
std::vector<float> &outNormals) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcessPreset_TargetRealtime_Fast);
aiMesh* mesh = scene->mMeshes[0];
numOfFaces = mesh->mNumFaces;
numOfIndices = numOfFaces * 3;
outIndices.resize(numOfIndices);
for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
const aiFace &face = mesh->mFaces[i];
assert(face.mNumIndices == 3);
outIndices[i * 3 + 0] = face.mIndices[0];
outIndices[i * 3 + 1] = face.mIndices[1];
outIndices[i * 3 + 2] = face.mIndices[2];
}
numOfVertices = mesh->mNumVertices;
outVertices.resize(numOfVertices * 3);
outNormals.resize(numOfVertices * 3);
outUVs.resize(numOfVertices * 2);
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
if (mesh->HasPositions()) {
outVertices[i * 3 + 0] = mesh->mVertices[i].x;
outVertices[i * 3 + 1] = mesh->mVertices[i].y;
outVertices[i * 3 + 2] = mesh->mVertices[i].z;
}
if (mesh->HasNormals()) {
outNormals[i * 3 + 0] = mesh->mNormals[i].x;
outNormals[i * 3 + 1] = mesh->mNormals[i].x;
outNormals[i * 3 + 2] = mesh->mNormals[i].x;
}
if (mesh->HasTextureCoords(0)) {
outUVs[i * 2 + 0] = mesh->mTextureCoords[0][i].x;
outUVs[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
}
}
}
int main(int argc, char *argv[]) {
...
int numOfVertices;
int numOfFaces;
int numOfIndices;
std::vector<unsigned int> indices;
std::vector<float> vertices;
std::vector<float> uvs;
std::vector<float> normals;
loadOBJ("../src/cube.obj", numOfVertices, numOfFaces, numOfIndices, indices, vertices, uvs, normals);
GLuint elementBuffer;
gl::GenBuffers(1, &elementBuffer);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, elementBuffer);
gl::BufferData(gl::ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], gl::STATIC_DRAW);
GLuint vertexBuffer;
gl::GenBuffers(1, &vertexBuffer);
gl::BindBuffer(gl::ARRAY_BUFFER, vertexBuffer);
gl::BufferData(gl::ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], gl::STATIC_DRAW);
GLuint uvBuffer;
gl::GenBuffers(1, &uvBuffer);
gl::BindBuffer(gl::ARRAY_BUFFER, uvBuffer);
gl::BufferData(gl::ARRAY_BUFFER, uvs.size() * sizeof(float), &uvs[0], gl::STATIC_DRAW);
GLuint normalBuffer;
gl::GenBuffers(1, &normalBuffer);
gl::BindBuffer(gl::ARRAY_BUFFER, normalBuffer);
gl::BufferData(gl::ARRAY_BUFFER, normals.size() * sizeof(float), &normals[0], gl::STATIC_DRAW);
while (!glfwWindowShouldClose(window)) {
...
gl::ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
gl::UseProgram(program);
gl::BindBuffer(gl::ARRAY_BUFFER, vertexBuffer);
gl::EnableVertexAttribArray(0);
gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE_, 0, (void*)0);
gl::BindBuffer(gl::ARRAY_BUFFER, uvBuffer);
gl::EnableVertexAttribArray(1);
gl::VertexAttribPointer(1, 2, gl::FLOAT, gl::FALSE_, 0, (void*)0);
gl::BindBuffer(gl::ARRAY_BUFFER, normalBuffer);
gl::EnableVertexAttribArray(2);
gl::VertexAttribPointer(2, 3, gl::FLOAT, gl::FALSE_, 0, (void*)0);
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, elementBuffer);
gl::DrawElements(gl::ELEMENT_ARRAY_BUFFER, numOfIndices, gl::UNSIGNED_INT, (void*)0);
...
}
...
}
The loadOBJ function doesn't seem to be parsing right.
GL_ELEMENT_ARRAY_BUFFER
has never been a valid argument toglDrawElements()
.Try something like
GL_TRIANGLES
:cube.obj: