#include "renderer.h"
#include <cglm/struct.h>

void setVertexBufferData(GLfloat* vertexBufferData, GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount)
{
    vec3s triangleVerts[3];
    int startingIndex;
    for( unsigned int i = 0; i < triangleCount; i+=3 )
    {
        startingIndex = i*6;
        for( int j = 0; j < 3; ++j )
        {
            triangleVerts[j].x = vertices[triangles[i+j]*3];
            triangleVerts[j].y = vertices[triangles[i+j]*3 + 1];
            triangleVerts[j].z = vertices[triangles[i+j]*3 + 2];

            vertexBufferData[startingIndex + j*6] = triangleVerts[j].x;
            vertexBufferData[startingIndex + j*6 + 1] = triangleVerts[j].y;
            vertexBufferData[startingIndex + j*6 + 2] = triangleVerts[j].z;
        }

        vec3s normal = glms_vec3_crossn(glms_vec3_sub(triangleVerts[0], triangleVerts[2]), 
                                        glms_vec3_sub(triangleVerts[0], triangleVerts[1]));

        vertexBufferData[startingIndex + 3] = normal.x;
        vertexBufferData[startingIndex + 4] = normal.y;
        vertexBufferData[startingIndex + 5] = normal.z;
        vertexBufferData[startingIndex + 9] = normal.x;
        vertexBufferData[startingIndex + 10] = normal.y;
        vertexBufferData[startingIndex + 11] = normal.z;
        vertexBufferData[startingIndex + 15] = normal.x;
        vertexBufferData[startingIndex + 16] = normal.y;
        vertexBufferData[startingIndex + 17] = normal.z;

    }
}
