Allow the camera to follow the ball

But when the ground is not textured, this looks just as if the ball was
not moving… :(
This commit is contained in:
Théophile Bastian 2018-02-14 11:36:56 +01:00
parent baaa5e4057
commit 8051f4203f
3 changed files with 23 additions and 5 deletions

View file

@ -52,6 +52,7 @@ int main(int argc, char** argv) {
ground_mesh.get_mesh()->set_color(Color(0.13, 0.82, 0.21));
render.add_mesh(ground_mesh.get_mesh());
}
//render.follow_implicit_position(ball.get_surface());
render.set_idle_func(periodic_update);
render.add_kb_handler(periodic_kb_handler);

View file

@ -12,7 +12,7 @@ GlutRender& GlutRender::get_instance() {
return instance;
}
GlutRender::GlutRender() {
GlutRender::GlutRender() : followed_implicit(nullptr) {
std::default_random_engine rand_engine(time(NULL));
std::uniform_real_distribution<double> distribution;
rand_color = std::bind(distribution, rand_engine);
@ -97,6 +97,10 @@ void GlutRender::add_kb_handler(GlutRender::kb_handler_t handler) {
kb_handlers.push_back(handler);
}
void GlutRender::follow_implicit_position(const ImplicitSurface* surf) {
followed_implicit = surf;
}
void GlutRender::reshape(int wid, int hei) {
if (hei == 0)
hei = 1;
@ -187,10 +191,19 @@ void GlutRender::display() {
// Camera position and orientation
glLoadIdentity();
//glTranslatef(1., 2., -10.);
gluLookAt(0, 2.5, -10,
0, 2.5, 0,
0, 1, 0);
Point look_from(0, 2.5, -10);
if(followed_implicit != nullptr) {
const Point& look_at = followed_implicit->getCenter();
gluLookAt(look_from.x, look_from.y, look_from.z,
look_at.x, look_from.y, look_at.z,
0, 1, 0);
}
else {
gluLookAt(look_from.x, look_from.y, look_from.z,
0, 2.5, 0,
0, 1, 0);
}
for(Mesh* mesh: meshes) {
display_mesh(*mesh);

View file

@ -30,6 +30,8 @@ class GlutRender {
void add_kb_handler(kb_handler_t handler);
void follow_implicit_position(const ImplicitSurface* surf);
private:
struct SurfaceDetails {
SurfaceDetails(ImplicitSurface* surf, const Cuboid& box,
@ -68,4 +70,6 @@ class GlutRender {
std::set<SurfaceDetails> surfaces;
std::vector<kb_handler_t> kb_handlers;
const ImplicitSurface* followed_implicit;
};