Blame scene.cpp

57bda0
57bda0
#include <cstdlib></cstdlib>
1bcd85
#include <iostream></iostream>
57bda0
57bda0
#include <gl gl.h=""></gl>
57bda0
57bda0
#include "scene.h"
57bda0
#include "simulator.h"
8ee194
#include "model.h"
57bda0
57bda0
57bda0
Scene::Scene():
57bda0
    angles(45.0, 0.0, 30.0),
57bda0
    offset(0, 0, -100),
57bda0
    stars_seed(rand()),
57bda0
    time(),
57bda0
    time_forward(0.0),
0a0ecd
    time_backward(100.0),
0a0ecd
    time_speed(10.0),
1bcd85
    model_angle(),
8ee194
    model(),
8ee194
    simulator(),
8ee194
    tool_dir(0, 0, -1),
8ee194
    tool(),
1bcd85
    collider(),
1bcd85
    move_tool(true),
1bcd85
    rotate_model(true)
57bda0
{ }
57bda0
57bda0
Scene::~Scene() { }
57bda0
57bda0
57bda0
Vector4 Scene::color_by_time(Real t) {
57bda0
    return t <= time
57bda0
         ? color_by_time_past(t)
57bda0
         : color_by_time_future(t);
57bda0
}
57bda0
57bda0
Vector4 Scene::color_by_time_past(Real t) {
57bda0
    Vector4 far_past(0.33, 0.33, 0.33, 0);
57bda0
    Vector4 past(1, 1, 0.33, 1);
57bda0
57bda0
    Vector4 color;
57bda0
    if (time_backward <= 0.0001 || t < time - time_backward)
57bda0
        return far_past;
57bda0
    Real l = (time - t)/time_backward;
57bda0
    Real k = 1 - l;
57bda0
    for(int i = 0; i < 4; ++i)
57bda0
        color.c[i] = past.c[i]*k + far_past.c[i]*l;
57bda0
    return color;
57bda0
}
57bda0
57bda0
Vector4 Scene::color_by_time_future(Real t) {
57bda0
    Vector4 future(1, 1, 0.33, 1);
57bda0
    Vector4 far_future(0.166, 0.166, 0.66, 0);
57bda0
57bda0
    Vector4 color;
57bda0
    if (time_forward <= 0.0001 || time + time_forward < t)
57bda0
        return far_future;
57bda0
    Real l = (t - time)/time_forward;
57bda0
    Real k = 1 - l;
57bda0
    for(int i = 0; i < 4; ++i)
57bda0
        color.c[i] = future.c[i]*k + far_future.c[i]*l;
57bda0
    return color;
57bda0
}
57bda0
57bda0
void Scene::update(Real dt) {
57bda0
    time += time_speed*dt;
57bda0
    if (time < 0 || track.points.empty()) {
57bda0
        time = 0;
57bda0
        if (time_speed < 0) time_speed = 0;
57bda0
    } else
57bda0
    if (time > track.points.back().time) {
57bda0
        time = track.points.back().time;
57bda0
        if (time_speed > 0) time_speed = 0;
57bda0
    }
57bda0
e31ea0
    int index = track.index_by_time(time);
e31ea0
    if (index > 0) {
e31ea0
        const TrackPoint &p0 = track.points[index-1];
e31ea0
        const TrackPoint &p1 = track.points[index];
e31ea0
        Real l = p1.duration > 1e-5 ? (p0.time - time)/p1.duration : 0.5;
e31ea0
        TrackPoint p = TrackPoint::median(l, p0, p1);
1bcd85
        //std::cout << p.angle << std::endl;
1bcd85
        
1bcd85
        if (move_tool) {
1bcd85
            Real a = p.angle*(M_PI/180);
1bcd85
            Real s = sin(a);
1bcd85
            Real c = cos(a);
1bcd85
            tool_pos = p.real_pos;
1bcd85
            tool_dir = Vector3(0, p.position.z*s, -p.position.z*c).norm();
1bcd85
        }
1bcd85
        
1bcd85
        if (rotate_model) model_angle = p.angle;
1bcd85
        
e31ea0
        if (simulator) simulator->scroll_to(index);
57bda0
    }
e31ea0
    
57bda0
}
57bda0
57bda0
Real Scene::random()
57bda0
    { return (rand()/(Real)RAND_MAX - 0.5)*2; }
57bda0
57bda0
void Scene::draw_stars() {
57bda0
    int seed = rand();
57bda0
    srand(stars_seed);
57bda0
    
57bda0
    glEnable(GL_FOG);
57bda0
    glFogf(GL_FOG_DENSITY, 0.002f);
57bda0
57bda0
    glColor4d(1, 1, 1, 1);
57bda0
    glBegin(GL_POINTS);
57bda0
    for(int i = 0; i < 1000; ++i) {
57bda0
        glVertex3d(
57bda0
            random()*1000.0,
57bda0
            random()*1000.0,
57bda0
            random()*1000.0 );
57bda0
    }
57bda0
    glEnd();
57bda0
    
57bda0
    glDisable(GL_FOG);
57bda0
    srand(seed);
57bda0
}
57bda0
57bda0
void Scene::draw_axes() {
57bda0
    glBegin(GL_LINES);
57bda0
57bda0
    glColor3d(1, 0, 0);
57bda0
    glVertex3d(0, 0, 0);
57bda0
    glVertex3d(50, 0, 0);
57bda0
57bda0
    glColor3d(0, 1, 0);
57bda0
    glVertex3d(0, 0, 0);
57bda0
    glVertex3d(0, 50, 0);
57bda0
57bda0
    glColor3d(0, 0, 1);
57bda0
    glVertex3d(0, 0, 0);
57bda0
    glVertex3d(0, 0, 50);
57bda0
57bda0
    glEnd();
57bda0
}
57bda0
57bda0
void Scene::draw_track() {
57bda0
    int index = track.index_by_time(time);
57bda0
    if (index <= 0) return;
57bda0
        
d2b2b5
    const TrackPoint &p0 = track.points[index-1];
d2b2b5
    const TrackPoint &p1 = track.points[index];
57bda0
    Real l = p1.duration > 1e-5 ? (p0.time - time)/p1.duration : 0.5;
d2b2b5
    TrackPoint p = TrackPoint::median(l, p0, p1);
57bda0
    
57bda0
    glBegin(GL_LINE_STRIP);
57bda0
    for(int i = 0; i < index; ++i) {
57bda0
        glColor4dv( color_by_time_past(track.points[i].time).c );
57bda0
        glVertex3dv( track.points[i].real_pos.c );
57bda0
    }
57bda0
    glColor4dv( color_by_time_past(p.time).c );
57bda0
    glVertex3dv( p.real_pos.c );
57bda0
    glEnd();
57bda0
57bda0
    glBegin(GL_LINE_STRIP);
57bda0
    for(int i = (int)track.points.size() - 1; i >= index; --i) {
57bda0
        glColor4dv( color_by_time_future(track.points[i].time).c );
57bda0
        glVertex3dv( track.points[i].real_pos.c );
57bda0
    }
57bda0
    glColor4dv( color_by_time_future(p.time).c );
57bda0
    glVertex3dv( p.real_pos.c );
57bda0
    glEnd();
57bda0
}
57bda0
57bda0
void Scene::draw() {
57bda0
    glPushMatrix();
57bda0
    
57bda0
    glTranslated(offset.x, offset.y, offset.z);
57bda0
    glRotated(-90, 1, 0, 0);
57bda0
    glRotated(angles.y, 0, 1, 0);
57bda0
    glRotated(angles.x, 1, 0, 0);
57bda0
    glRotated(angles.z, 0, 0, 1);
57bda0
    
57bda0
    glEnable(GL_BLEND);
57bda0
    glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
57bda0
57bda0
    draw_stars();
57bda0
    draw_axes();
1bcd85
1bcd85
    glPushMatrix();
1bcd85
        glRotated(-model_angle, 1, 0, 0);
1bcd85
        if (model) model->draw();
1bcd85
        if (simulator) simulator->draw();
1bcd85
        if (tool) tool->draw(tool_pos, tool_dir);
b2ca59
        glRotated(-model_angle, 1, 0, 0);
1bcd85
        draw_track();
1bcd85
    glPopMatrix();
b2ca59
57bda0
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
57bda0
    glDisable(GL_BLEND);
57bda0
    glPopMatrix();
57bda0
}