Blame projects/asteroid/scene.cpp

223ac6
223ac6
#include <cstdlib></cstdlib>
223ac6
223ac6
#include <gl gl.h=""></gl>
223ac6
223ac6
#include "scene.h"
223ac6
223ac6
223ac6
Scene::Scene():
223ac6
    stars_seed(rand()),
223ac6
    offset(0, 0, -10),
223ac6
    angles(45.0, 0.0, 30.0),
223ac6
    wireframe(false),
223ac6
    water_level(1),
223ac6
    t(0)
223ac6
{
223ac6
    LinkedMesh lsphere;
223ac6
    lsphere.icosahedron(0);
223ac6
    lsphere.generate_levels(3);
223ac6
    lsphere.to_sphere_mesh(sphere, 1, 0);
223ac6
    
223ac6
    generate();
223ac6
}
223ac6
223ac6
Scene::~Scene() { }
223ac6
223ac6
223ac6
void Scene::generate() {
223ac6
    mesh.clear();
223ac6
    LinkedMesh linked_mesh;
223ac6
    //linked_mesh.triangle();
223ac6
    //linked_mesh.tetrahedron();
223ac6
    linked_mesh.icosahedron();
223ac6
    linked_mesh.generate_levels(2, 1);
223ac6
    linked_mesh.generate_levels(5, 0.5);
223ac6
    linked_mesh.smooth();
223ac6
    //linked_mesh.to_z_mesh(mesh, 0, 0.1);
223ac6
    linked_mesh.to_sphere_mesh(mesh, 1, 0.2);
223ac6
}
223ac6
223ac6
223ac6
void Scene::update(Real dt) {
223ac6
    t += dt;
223ac6
    water_level = 1.05 + 0.025*sin(3*t/2/pi);
223ac6
}
223ac6
223ac6
223ac6
void Scene::draw_stars() {
223ac6
    unsigned int seed = rand();
223ac6
    srand(stars_seed);
223ac6
    
223ac6
    glEnable(GL_FOG);
223ac6
    glFogf(GL_FOG_DENSITY, 0.02f);
223ac6
223ac6
    glColor4d(1, 1, 1, 1);
223ac6
    glBegin(GL_POINTS);
223ac6
    for(int i = 0; i < 1000; ++i) {
223ac6
        glVertex3d(
223ac6
            real_random2()*100.0,
223ac6
            real_random2()*100.0,
223ac6
            real_random2()*100.0 );
223ac6
    }
223ac6
    glEnd();
223ac6
    
223ac6
    glDisable(GL_FOG);
223ac6
    srand(seed);
223ac6
}
223ac6
223ac6
223ac6
void Scene::draw() {
223ac6
    glPushMatrix();
223ac6
    
223ac6
    glTranslated(offset.x, offset.y, offset.z);
223ac6
    glRotated(-90, 1, 0, 0);
223ac6
    glRotated(angles.y, 0, 1, 0);
223ac6
    glRotated(angles.x, 1, 0, 0);
223ac6
    glRotated(angles.z, 0, 0, 1);
223ac6
    
223ac6
    glEnable(GL_DEPTH_TEST);
223ac6
    glEnable(GL_BLEND);
223ac6
    //glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
223ac6
223ac6
    draw_stars();
223ac6
    
223ac6
    
223ac6
    glEnable(GL_LIGHTING);
223ac6
    if (wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
223ac6
223ac6
    glColor4d(0, 0.5, 0, 1);
223ac6
    mesh.draw();
223ac6
    
223ac6
    // water
223ac6
    glDisable(GL_LIGHTING);
223ac6
    glDepthMask(GL_FALSE);
223ac6
    glColor4d(0, 0, 1, 0.2);
223ac6
    for(int i = 10; i > 0; --i) {
223ac6
        Real s = water_level - 0.01*i;
223ac6
        glPushMatrix();
223ac6
        glScaled(s, s, s);
223ac6
        sphere.draw();
223ac6
        glPopMatrix();
223ac6
    }
223ac6
    glDepthMask(GL_TRUE);
223ac6
    glEnable(GL_LIGHTING);
223ac6
223ac6
    glColor4d(0, 0, 1, 0.5);
223ac6
    glPushMatrix();
223ac6
    glScaled(water_level, water_level, water_level);
223ac6
    sphere.draw();
223ac6
    glPopMatrix();
223ac6
    glColor4d(1, 1, 1, 1);
223ac6
223ac6
    // fog
223ac6
    glDisable(GL_LIGHTING);
223ac6
    glDepthMask(GL_FALSE);
223ac6
    glColor4d(1, 1, 1, 0.01);
223ac6
    for(int i = 1; i < 20; ++i) {
223ac6
        Real s = water_level + 0.01*i;
223ac6
        glPushMatrix();
223ac6
        glScaled(s, s, s);
223ac6
        sphere.draw();
223ac6
        glPopMatrix();
223ac6
    }
223ac6
    glDepthMask(GL_TRUE);
223ac6
    glEnable(GL_LIGHTING);
223ac6
    
223ac6
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
223ac6
    glDisable(GL_LIGHTING);
223ac6
223ac6
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
223ac6
    glDisable(GL_BLEND);
223ac6
    glPopMatrix();
223ac6
}