/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "quad.h"

void quadBuffer(primitive_t *primitive, float z,
  float x0, float y0, float u0, float v0,
  float x1, float y1, float u1, float v1,
  int32_t verticeStart, int32_t indiceStart
) {
  vertice_t *vertices = malloc(sizeof(vertice_t) * QUAD_VERTICE_COUNT);
  indice_t *indices = malloc(sizeof(indice_t) * QUAD_INDICE_COUNT);

  vertices[0].x = x0, vertices[0].y = y0, vertices[0].z = z;
  vertices[0].u = u0, vertices[0].v = v0;
  
  vertices[1].x = x1, vertices[1].y = y0, vertices[1].z = z;
  vertices[1].u = u1, vertices[1].v = v0;
  
  vertices[2].x = x0, vertices[2].y = y1, vertices[2].z = z;
  vertices[2].u = u0, vertices[2].v = v1;
  
  vertices[3].x = x1, vertices[3].y = y1, vertices[3].z = z;
  vertices[3].u = u1, vertices[3].v = v1;

  indices[0] = (indice_t)(verticeStart + 0);
  indices[1] = (indice_t)(verticeStart + 1);
  indices[2] = (indice_t)(verticeStart + 2);

  indices[3] = (indice_t)(verticeStart + 1);
  indices[4] = (indice_t)(verticeStart + 2);
  indices[5] = (indice_t)(verticeStart + 3);

  primitiveBufferVertices(primitive,verticeStart,QUAD_VERTICE_COUNT,vertices);
  primitiveBufferIndices( primitive,indiceStart, QUAD_INDICE_COUNT, indices );

  free(vertices);
  free(indices);
}

void quadInit(primitive_t *primitive, float z,
  float x0, float y0, float u0, float v0,
  float x1, float y1, float u1, float v1
) {
  primitiveInit(primitive, QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);

  quadBuffer(primitive, z,
    x0, y0, u0, v0,
    x1, y1, u1, v1,
    0, 0
  );
}