Example #1
0
	void Model::Load(const String& path)
	{
		std::vector<String> lines = utils::SplitString(utils::ReadFile(path), '\n');
		VertexSet inputVertices;
		std::vector<Vertex> vertices;
		std::vector<uint> indices;
		std::unordered_map<IndexSet, int32> mapping;

		uint i = 0;
		for (String line : lines)
		{
			const char* cstr = line.c_str();
			if (strstr(cstr, "#")) // Comment
			{
				continue;
			}
			else if (strstr(cstr, "v"))
			{
				if (strstr(cstr, "vt"))
				{
					vec2 uv;
					int32 result = sscanf(cstr, "%*s %f %f", &uv.x, &uv.y);
					if (result == 0)
						continue;
					inputVertices.uvs.push_back(uv);
				}
				else if (strstr(cstr, "vn"))
				{
					vec3 normal;
					int32 result = sscanf(cstr, "%*s %f %f %f", &normal.x, &normal.y, &normal.z);
					if (result == 0)
						continue;
					inputVertices.normals.push_back(normal);
				}
				else
				{
					vec3 position;
					int32 result = sscanf(cstr, "%*s %f %f %f", &position.x, &position.y, &position.z);
					if (result == 0)
						continue;
					inputVertices.positions.push_back(position);
				}
			}
			else if (strstr(cstr, "f"))
			{
				IndexSet indexSet[3];
				int32 result = sscanf(cstr, "%*s %d/%d/%d %d/%d/%d %d/%d/%d", &indexSet[0].position, &indexSet[0].uv, &indexSet[0].normal, &indexSet[1].position, &indexSet[1].uv, &indexSet[1].normal, &indexSet[2].position, &indexSet[2].uv, &indexSet[2].normal);
				if (result == 0)
					continue;

				InsertVertex(vertices, indices, mapping, inputVertices, indexSet[0]);
				InsertVertex(vertices, indices, mapping, inputVertices, indexSet[1]);
				InsertVertex(vertices, indices, mapping, inputVertices, indexSet[2]);
			}
			if (i++ % 1000 == 0)
				SP_INFO((int32)((i / (float)lines.size()) * 100.0f), "%");
		}
		SP_INFO("100%");
		SP_INFO("Loaded ", path);

		API::Buffer* buffer = new API::Buffer(GL_ARRAY_BUFFER, GL_STATIC_DRAW);
		buffer->Bind();
		buffer->SetData(vertices.size() * sizeof(Vertex), &vertices[0]);

		buffer->layout.Push<vec3>("position");
		buffer->layout.Push<vec3>("normal");
		buffer->layout.Push<vec2>("uv");

		VertexArray* va = new VertexArray();
		va->Bind();
		va->PushBuffer(buffer);

		IndexBuffer* ib = new IndexBuffer(&indices[0], indices.size());
		m_Mesh = new Mesh(va, ib, nullptr);
	}