void Model_Impl::insert_vbo(int vertex_count, const struct aiScene* sc, const struct aiNode* nd) { int i; unsigned int n = 0, t; bool use_texcoords = !vbo_texcoords.is_null(); // All meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; int num_vertex = mesh->mNumFaces * 3; if (!num_vertex) continue; std::vector<CL_Vec3f> normals; std::vector<CL_Vec3f> vertices; std::vector<CL_Vec2f> tex_coords; normals.reserve(num_vertex); vertices.reserve(num_vertex); if (use_texcoords) { if (mesh->mTextureCoords == NULL || mesh->mTextureCoords[0] == NULL) throw CL_Exception("This example expects texcoords to be set for this object"); tex_coords.reserve(num_vertex); } for (t = 0; t < mesh->mNumFaces; ++t) { const struct aiFace* face = &mesh->mFaces[t]; if (face->mNumIndices != 3) throw CL_Exception("This example only supports triangles"); for(i = 0; i < face->mNumIndices; i++) { int index = face->mIndices[i]; normals.push_back(&mesh->mNormals[index].x); vertices.push_back( &mesh->mVertices[index].x); if (use_texcoords) tex_coords.push_back( &mesh->mTextureCoords[0][index].x); } } vbo_positions.upload_data(vertex_count * sizeof(CL_Vec3f), &vertices[0], num_vertex * sizeof(CL_Vec3f)); vbo_normals.upload_data(vertex_count * sizeof(CL_Vec3f), &normals[0], num_vertex * sizeof(CL_Vec3f)); if (use_texcoords) vbo_texcoords.upload_data(vertex_count * sizeof(CL_Vec2f), &tex_coords[0], num_vertex * sizeof(CL_Vec2f)); vertex_count += num_vertex; } // All children for (n = 0; n < nd->mNumChildren; ++n) { insert_vbo(vertex_count, sc, nd->mChildren[n]); } }
void Model_Impl::Load(CL_GraphicContext &gc, const char *filename, bool we_do_not_want_texures_on_this_object) { generate_texture_coords = !we_do_not_want_texures_on_this_object; const struct aiScene* scene = aiImportFile(filename,aiProcessPreset_TargetRealtime_MaxQuality); if (!scene) throw CL_Exception("Cannot load a model"); try { vbo_size = count_vertices(scene, scene->mRootNode); if (!vbo_size) throw CL_Exception("No vertices found in the model"); vbo_positions = CL_VertexArrayBuffer(gc, vbo_size * sizeof(CL_Vec3f), cl_usage_static_draw); vbo_normals = CL_VertexArrayBuffer(gc, vbo_size * sizeof(CL_Vec3f), cl_usage_static_draw); if (generate_texture_coords) vbo_texcoords = CL_VertexArrayBuffer(gc, vbo_size * sizeof(CL_Vec2f), cl_usage_static_draw); insert_vbo(0, scene, scene->mRootNode); }catch(...) { aiReleaseImport(scene); throw; } aiReleaseImport(scene); }
int Model_Impl::insert_vbo(GraphicContext &gc, int vertex_count, const struct aiScene* sc, const struct aiNode* nd) { int i; unsigned int n = 0, t; // All meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; int num_vertex = mesh->mNumFaces * 3; if (!num_vertex) continue; std::vector<Vec3f> normals; std::vector<Vec3f> vertices; normals.reserve(num_vertex); vertices.reserve(num_vertex); for (t = 0; t < mesh->mNumFaces; ++t) { const struct aiFace* face = &mesh->mFaces[t]; if (face->mNumIndices != 3) throw Exception("This example only supports triangles"); for(i = 0; i < face->mNumIndices; i++) { int index = face->mIndices[i]; normals.push_back(Vec3f(&mesh->mNormals[index].x)); vertices.push_back(Vec3f( &mesh->mVertices[index].x)); } } vbo_positions.upload_data(gc, vertex_count, &vertices[0], num_vertex); vbo_normals.upload_data(gc, vertex_count, &normals[0], num_vertex); vertex_count += num_vertex; } // All children for (n = 0; n < nd->mNumChildren; ++n) { vertex_count = insert_vbo(gc, vertex_count, sc, nd->mChildren[n]); } return vertex_count; }
void Model_Impl::Load(GraphicContext &gc, GraphicStore *gs, const char *filename) { const struct aiScene* scene = aiImportFileExWithProperties(filename,aiProcessPreset_TargetRealtime_MaxQuality, NULL, gs->store); if (!scene) throw Exception("Cannot load a model"); try { vbo_size = count_vertices(scene, scene->mRootNode); if (!vbo_size) throw Exception("No vertices found in the model"); vbo_positions = VertexArrayVector<Vec3f>(gc, vbo_size); vbo_normals = VertexArrayVector<Vec3f>(gc, vbo_size); insert_vbo(gc, 0, scene, scene->mRootNode); }catch(...) { aiReleaseImport(scene); throw; } aiReleaseImport(scene); }