#include <iostream>
#include <cmath>

using namespace std;


int main(int argc, char* argv[])
{
    unsigned int nSections = 20;
    float shaftLength = 3.0f;
    float headLength = 1.0f;
    float shaftRadius = 0.25f;
    float headRadius = 0.5f;

    if (argc > 1)
    {
    }

    unsigned int vertexCount = nSections * 3 + 2;
    unsigned int triCount = nSections * 6;

    cout << "#celmodel__ascii\n";

    cout << "material\n";
    cout << "diffuse 1 0 0\n";
    cout << "end_material\n";
    cout << "\n";

    cout << "mesh\n";
    cout << "\n";
    cout << "vertexdesc\n";
    cout << "position f3\n";
    cout << "end_vertexdesc\n";
    cout << "\n";

    unsigned int i, j;
    
    cout << "vertices " << vertexCount << "\n";
    cout << "0 0 0\n"; // shaft base center
    cout << "0 0 " << shaftLength + headLength << "\n"; // tip

    for (i = 0; i < 3; i++)
    {
        float z;
        float r;
        switch (i)
        {
        case 0:
            z = 0.0f;
            r = shaftRadius;
            break;
        case 1:
            z = shaftLength;
            r = shaftRadius;
            break;
        default:
            z = shaftLength;
            r = headRadius;
            break;
        }

        for (unsigned int j = 0; j < nSections; j++)
        {
            float theta = j * 2.0f * 3.1415927f / (float) nSections;
            float s = (float) sin(theta);
            float c = (float) cos(theta);
            cout << c * r << " " << s * r << " " << z << "\n";
        }
    }

    cout << "\n";
    cout << "trilist 0 " << triCount * 3 << "\n";

    // base
    for (i = 0; i < nSections; i++)
    {
        unsigned int base = 2;
        cout << 0 << " " << base + (i + 1) % nSections << " " << base + i << "\n";
    }

    // shaft
    for (i = 0; i < nSections; i++)
    {
        unsigned int base = 2;
        unsigned int i0 = base + i;
        unsigned int i1 = base + (i + 1) % nSections;
        base += nSections;
        unsigned int i2 = base + i;
        unsigned int i3 = base + (i + 1) % nSections;

        cout << i0 << " " << i1 << " " << i2 << "\n";
        cout << i1 << " " << i3 << " " << i2 << "\n";
    }

    // annulus
    for (i = 0; i < nSections; i++)
    {
        unsigned int base = 2 + nSections;
        unsigned int i0 = base + i;
        unsigned int i1 = base + (i + 1) % nSections;
        base += nSections;
        unsigned int i2 = base + i;
        unsigned int i3 = base + (i + 1) % nSections;

        cout << i0 << " " << i1 << " " << i2 << "\n";
        cout << i1 << " " << i3 << " " << i2 << "\n";
    }

    // head
    for (i = 0; i < nSections; i++)
    {
        unsigned int base = 2 + nSections * 2;
        cout << base + i << " " << base + (i + 1) % nSections << " " << 1 << "\n";
    }

    cout << "end_mesh\n";
}

