Blob Blame Raw

#include <cstdlib>

#include <GL/gl.h>

#include "scene.h"


Scene::Scene():
    stars_seed(rand()),
    offset(0, 0, -10),
    angles(45.0, 0.0, 30.0),
    wireframe(false),
    water_level(1),
    t(0)
{
    LinkedMesh lsphere;
    lsphere.icosahedron(0);
    lsphere.generate_levels(3);
    lsphere.to_sphere_mesh(sphere, 1, 0);
    
    generate();
}

Scene::~Scene() { }


void Scene::generate() {
    mesh.clear();
    LinkedMesh linked_mesh;
    //linked_mesh.triangle();
    //linked_mesh.tetrahedron();
    linked_mesh.icosahedron();
    linked_mesh.generate_levels(2, 1);
    linked_mesh.generate_levels(5, 0.5);
    linked_mesh.smooth();
    //linked_mesh.to_z_mesh(mesh, 0, 0.1);
    linked_mesh.to_sphere_mesh(mesh, 1, 0.2);
}


void Scene::update(Real dt) {
    t += dt;
    water_level = 1.05 + 0.025*sin(3*t/2/pi);
}


void Scene::draw_stars() {
    unsigned int seed = rand();
    srand(stars_seed);
    
    glEnable(GL_FOG);
    glFogf(GL_FOG_DENSITY, 0.02f);

    glColor4d(1, 1, 1, 1);
    glBegin(GL_POINTS);
    for(int i = 0; i < 1000; ++i) {
        glVertex3d(
            real_random2()*100.0,
            real_random2()*100.0,
            real_random2()*100.0 );
    }
    glEnd();
    
    glDisable(GL_FOG);
    srand(seed);
}


void Scene::draw() {
    glPushMatrix();
    
    glTranslated(offset.x, offset.y, offset.z);
    glRotated(-90, 1, 0, 0);
    glRotated(angles.y, 0, 1, 0);
    glRotated(angles.x, 1, 0, 0);
    glRotated(angles.z, 0, 0, 1);
    
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    //glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);

    draw_stars();
    
    
    glEnable(GL_LIGHTING);
    if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    glColor4d(0, 0.5, 0, 1);
    mesh.draw();
    
    // water
    glDisable(GL_LIGHTING);
    glDepthMask(GL_FALSE);
    glColor4d(0, 0, 1, 0.2);
    for(int i = 10; i > 0; --i) {
        Real s = water_level - 0.01*i;
        glPushMatrix();
        glScaled(s, s, s);
        sphere.draw();
        glPopMatrix();
    }
    glDepthMask(GL_TRUE);
    glEnable(GL_LIGHTING);

    glColor4d(0, 0, 1, 0.5);
    glPushMatrix();
    glScaled(water_level, water_level, water_level);
    sphere.draw();
    glPopMatrix();
    glColor4d(1, 1, 1, 1);

    // fog
    glDisable(GL_LIGHTING);
    glDepthMask(GL_FALSE);
    glColor4d(1, 1, 1, 0.01);
    for(int i = 1; i < 20; ++i) {
        Real s = water_level + 0.01*i;
        glPushMatrix();
        glScaled(s, s, s);
        sphere.draw();
        glPopMatrix();
    }
    glDepthMask(GL_TRUE);
    glEnable(GL_LIGHTING);
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glDisable(GL_LIGHTING);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_BLEND);
    glPopMatrix();
}