/*
 * Copyright (c) 2021 Callum Newlands
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     The additional term of 7.b applies: Requiring preservation of specified
 *     reasonable legal notices or author attributions in that material or in the
 *     Appropriate Legal Notices displayed by works containing it
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package utils;

import java.util.List;
import java.util.stream.Collectors;
import modeldata.meshdata.Mesh;
import modeldata.meshdata.Vertex;
import org.joml.Matrix4f;
import org.joml.Vector3f;

public final class MeshUtils {
	private MeshUtils() {
	}

	public static List<Vertex> transform(List<Vertex> vertices, Matrix4f model) {
		if (vertices.get(0).getTangent() != null) {
			return vertices.stream()
					.map(v -> new Vertex(
							model.transformPosition(new Vector3f(v.getPosition())),
							model.transformDirection(new Vector3f(v.getNormal())).normalize(),
							model.transformDirection(new Vector3f(v.getTangent())).normalize(),
							v.getTexCoord()))
					.collect(Collectors.toList());
		}
		return vertices.stream()
				.map(v -> new Vertex(
						model.transformPosition(new Vector3f(v.getPosition())),
						model.transformDirection(new Vector3f(v.getNormal())).normalize(),
						v.getTexCoord()))
				.collect(Collectors.toList());
	}

	public static Mesh transform(Mesh mesh, Matrix4f model) {
		List<Vertex> transformedVertices = transform(mesh.getVertices(), model);
		return new Mesh(mesh, transformedVertices);
	}

}
