#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

#define SEED 31415926
#define MAX_V 10000
#define DIV_V 5000.0
#define SUB_V 1


#define MY_PI 3.14159
#define ANG_INCR MY_PI / 8
#define ANG_ROT 2.5

void renderer(void);
void initialSetup(void);
void changeSize(GLsizei w, GLsizei h);
void tf(int blah);
int rand2();

long long lastseed;

float lastang;

int main (int argc, const char * argv[]) 
{
    glutInit(&argc, (char **)argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Triangles Demo");
    glutDisplayFunc(renderer);
    glutReshapeFunc(changeSize);
    initialSetup();
    glutTimerFunc(25, tf, 1);
    glutMainLoop();
    return 0;
}

void renderer()
{
    int last=0;
    float ang;
    
    lastseed = SEED;
    lastang += ANG_ROT;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(lastang,1,1,0);
    glFrontFace(GL_CW);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0,0,0); //top
    for(ang = 0; ang < 2 * MY_PI + ANG_INCR; ang += ANG_INCR) 
    {
        glColor3f((rand2() % 1000) / 1000.0, (rand2() % 1000) / 1000.0, (rand2() % 1000) / 1000.0);
        glVertex3f(70*sin(ang), 70*cos(ang), 70); //right
    }
    glEnd();
    
    glFrontFace(GL_CCW);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0,0,70); //Middle
    for(ang = 0; ang < 2*MY_PI+ANG_INCR; ang+= ANG_INCR)
    {
        glColor3f(last, !last, 0);
        glVertex3f(70*sin(ang), 70*cos(ang), 70);//right
        last = !last;
    }
    glEnd();
    glPopMatrix();
    glutSwapBuffers();
}

void tf(int blah)
{
    glutPostRedisplay();
    glutTimerFunc(15, tf, 1);
}

void changeSize(GLsizei w, GLsizei h)
{
    GLfloat sideRange = 200;
    
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-sideRange, sideRange, -sideRange, sideRange, sideRange, -sideRange);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


void initialSetup(void)
{
    lastang =0;
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glPolygonMode(GL_BACK, GL_LINE);
    glPolygonMode(GL_FRONT, GL_FILL);
    
    glClearColor(0.0, 0.0, 0.0, 1.0);
}


int rand2()
{
    lastseed = lastseed * 16807 % 2147483647;
    return lastseed;
}

