// Compile with
// g++ -o gltest gltest.cpp `pkg-config --cflags --libs clanCore-0.8 clanApp-0.8 clanDisplay-0.8 clanGL-0.8`

const int width = 400, height = 400;

#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/display.h>
#include <ClanLib/gl.h>

struct App: public CL_ClanApplication {
	CL_SetupCore setup_init;
	CL_SetupDisplay setup_display;
	CL_SetupGL setup_gl;
	int main(int argc, char** argv) {
		unsigned rounds = 1000;
		if (argc == 2) rounds = atoi(argv[1]);
		CL_DisplayWindow window("gltest", width, height);
		const unsigned detail = 1 << 19;
		float sinarr[detail];
		for (unsigned i = 0; i < detail; ++i) sinarr[i] = sin(2.0f * PI * i / detail);
		for (unsigned i = 0; i < rounds; ++i) {
			glLoadIdentity();
			glClearColor(0.4f, 0.6f, 1.0f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			gluPerspective(45.0, 1.0, 0.01, 100.0);
			gluLookAt(0.0, 0.0, -5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
			glDisable(GL_CULL_FACE);
			glEnable(GL_DEPTH_TEST);
			glRotatef(i, 0.0f, 1.0f, 0.0f);
			glRotatef(3.0f * i, 1.0f, 0.0f, 0.0f);
			glBegin(GL_TRIANGLE_STRIP);
			for (unsigned t = 0; t <= detail; ++t) {
				float w = 2.0 + sinarr[(3 * t + detail / 4) % detail];
				float x = w * sinarr[(2 * t + detail / 4) % detail];
				float y = w * sinarr[2 * t % detail];
				float z = sinarr[3 * t % detail];
				glColor3f(1.0f, float(t) / detail, 0.0f);
				glVertex3f(x - .3 * y, y - .3 * z, z - .3 * x);
				glVertex3f(x + .3 * z, y + .3 * x, z + .3 * y);
			}
			glEnd();
			window.flip(0);
		}
		return 0;
	}
} app;


