#include "PhysiksDrawer.h"

#include <physiksvector.h>

PhysiksDrawer::PhysiksDrawer(QWidget *parent) : QGLWidget(parent) {}

PhysiksDrawer::PhysiksDrawer(Physiks::Universe *univ,
        QWidget *parent) : QGLWidget(parent), universe(univ) {}

void PhysiksDrawer::bindUniverse(Physiks::Universe *univ) {
    universe = univ;
}

void PhysiksDrawer::updateGL() {
    universe->step();
    glDraw();
}

void PhysiksDrawer::initializeGL() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glClearDepth(1.0);
}

void PhysiksDrawer::resizeGL(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-5.0, 5.0, -5.0, 5.0, 4.5, 10.0);
}

void PhysiksDrawer::paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glColor3f(1.0, 0.0, 0.0);

    if (universe)
        for (int i = 0; i < universe->dimension; i++) 
            drawObject(*(universe->object(i)));

    glFlush();
} 

void PhysiksDrawer::drawSphere(const Physiks::Vector &vec, 
        const PHYSCALAR radius) {
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(vec[0], vec[1], vec[2]);
    GLUquadricObj *obj = gluNewQuadric();
    gluSphere(obj, static_cast<float>(radius), 180, 180);
    gluDeleteQuadric(obj);
    glPopMatrix();
}

void PhysiksDrawer::drawObject(const Physiks::PhysicalObject &part) {
#if 0
    switch (part.type) {
        case POINT_PARTICLE: drawSphere(part.position, 1.0);
            break;
        case SPHERE: drawSphere(part.position, part.radius);
            break;
        case PLANE:
            break;
    }
#endif
}

#include "PhysiksDrawer.moc"
