Example #1
0
	bool Graphics::LoadMesh(Mesh * aMesh, GLuint & aVBO, GLuint & aIBO)
	{
		//TODO: Load the mesh data into OpenGL
		if (aMesh == nullptr)
		{
			return false;
		}

		if (aMesh->IsUploaded())
		{
			Debug::Error("Graphics", "Cannot upload the mesh. It's already uploaded. Release the resources before attempting to upload.");
			return false;
		}

		Array<Vector3> positions = aMesh->GetPositions();
		Array<Vector3> normals = aMesh->GetNormals();
		Array<Vector2> texCoords = aMesh->GetTexCoords();
		Array<Color> colors = aMesh->GetColors();
		Array<UInt16> indices = aMesh->GetIndicies();

		UInt32 count = positions.GetCount();
		if (count != normals.GetCount() ||
			count != texCoords.GetCount() ||
			count != colors.GetCount()
			)
		{
			Debug::ErrorFormat("Graphics", nullptr, "There is a different number of attributes in this mesh %s", aMesh->GetName().c_str());
			return false;
		}

		Array<VertexAttribute> attributes;
		attributes.Allocate(count);

		for (unsigned int i = 0; i < count; i++)
		{
			//Position
			attributes[i].position[0] = positions[i].x;
			attributes[i].position[1] = positions[i].y;
			attributes[i].position[2] = positions[i].z;

			//Normals
			attributes[i].normal[0] = normals[i].x;
			attributes[i].normal[1] = normals[i].y;
			attributes[i].normal[2] = normals[i].z;

			//Tex Coords
			attributes[i].texCoord[0] = texCoords[i].x;
			attributes[i].texCoord[1] = texCoords[i].y;

			//Colors
			attributes[i].color[0] = colors[i].r;
			attributes[i].color[1] = colors[i].g;
			attributes[i].color[2] = colors[i].b;
			attributes[i].color[3] = colors[i].a;
		}

		//Send to OpenGL
		glGenBuffers(1, &aVBO);
		glBindBuffer((GLenum)BufferTarget::Array, aVBO);
		glBufferData((GLenum)BufferTarget::Array, sizeof(VertexAttribute)* count, attributes.GetElements(), (GLenum)BufferMode::StaticDraw);
		glBindBuffer((GLenum)BufferTarget::Array, 0);

		glGenBuffers(1, &aIBO);
		glBindBuffer((GLenum)BufferTarget::ElementArray, aIBO);
		glBufferData((GLenum)BufferTarget::ElementArray, sizeof(GLshort)* indices.GetCount(), indices.GetElements(), (GLenum)BufferMode::StaticDraw);
		glBindBuffer((GLenum)BufferTarget::ElementArray, 0);

		return true;
	}