package com.github.hydos.ginger.engine.vulkan.model;

import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32B32_SFLOAT;
import static org.lwjgl.vulkan.VK10.VK_FORMAT_R32G32_SFLOAT;
import static org.lwjgl.vulkan.VK10.VK_VERTEX_INPUT_RATE_VERTEX;

import org.joml.Vector2fc;
import org.joml.Vector3fc;
import org.lwjgl.vulkan.VkVertexInputAttributeDescription;
import org.lwjgl.vulkan.VkVertexInputBindingDescription;

public class VKVertex {

	public static final int SIZEOF = (3 + 3 + 2) * Float.BYTES;
	public static final int OFFSETOF_POS = 0;
	public static final int OFFSETOF_COLOR = 3 * Float.BYTES;
	public static final int OFFSETOF_TEXTCOORDS = (3 + 3) * Float.BYTES;

	public Vector3fc pos;
	public Vector3fc color;
	public Vector2fc texCoords;

	public VKVertex(Vector3fc pos, Vector3fc color, Vector2fc texCoords) {
		this.pos = pos;
		this.color = color;
		this.texCoords = texCoords;
	}

	public static VkVertexInputBindingDescription.Buffer getBindingDescription() {

		VkVertexInputBindingDescription.Buffer bindingDescription =
			VkVertexInputBindingDescription.callocStack(1);

		bindingDescription.binding(0);
		bindingDescription.stride(VKVertex.SIZEOF);
		bindingDescription.inputRate(VK_VERTEX_INPUT_RATE_VERTEX);

		return bindingDescription;
	}

	public static VkVertexInputAttributeDescription.Buffer getAttributeDescriptions() {

		VkVertexInputAttributeDescription.Buffer attributeDescriptions =
			VkVertexInputAttributeDescription.callocStack(3);

		// Position
		VkVertexInputAttributeDescription posDescription = attributeDescriptions.get(0);
		posDescription.binding(0);
		posDescription.location(0);
		posDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
		posDescription.offset(OFFSETOF_POS);

		// Color
		VkVertexInputAttributeDescription colorDescription = attributeDescriptions.get(1);
		colorDescription.binding(0);
		colorDescription.location(1);
		colorDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
		colorDescription.offset(OFFSETOF_COLOR);

		// Texture coordinates
		VkVertexInputAttributeDescription texCoordsDescription = attributeDescriptions.get(2);
		texCoordsDescription.binding(0);
		texCoordsDescription.location(2);
		texCoordsDescription.format(VK_FORMAT_R32G32_SFLOAT);
		texCoordsDescription.offset(OFFSETOF_TEXTCOORDS);

		return attributeDescriptions.rewind();
	}

}