diff --git a/include/glumodel.h b/include/glumodel.h index 359835b..85bcb49 100644 --- a/include/glumodel.h +++ b/include/glumodel.h @@ -8,12 +8,13 @@ class GenericModel : public Thing { public: + bool visible; GLUquadricObj* pt; Vec* pos; Vec* rot; Material* mat; float scale; - bool enabled; + GenericModel(); GenericModel(Vec _pos, Material _mat); void init(Vec _pos, Material _mat); @@ -24,23 +25,26 @@ void translate(Vec d); void rotate(Vec r); + + void show(); + void hide(); }; class GluSphere : public GenericModel { - public: - float rad; - GluSphere(); - GluSphere(Vec _pos, Material _mat, float _rad); - void init(Vec _pos, Material _mat, float _rad); - void draw(); + public: + float rad; + GluSphere(); + GluSphere(Vec _pos, Material _mat, float _rad); + void init(Vec _pos, Material _mat, float _rad); + void draw(); }; class Fractal : public GenericModel { - public: - int iters; - float len; - Fractal(); - void init(Vec pos, Material m, int _iters, float _len); + public: + int iters; + float len; + Fractal(); + void init(Vec pos, Material m, int _iters, float _len); }; class SierpinskiPyramid : public Fractal { @@ -56,15 +60,15 @@ }; class KochFractal : public Fractal { - public: - float multiplicity; - float mult_angle; + public: + float multiplicity; + float mult_angle; - KochFractal(Vec pos, int iters, float len); - //TODO: move draw function to Fractal class, it's common for all - //inheriting fractals.... - void draw(); - void iter(float nbit); + KochFractal(Vec pos, int iters, float len); + //TODO: move draw function to Fractal class, it's common for all + //inheriting fractals.... + void draw(); + void iter(float nbit); }; #endif diff --git a/sources/animation.cpp b/sources/animation.cpp index 4ccf371..4d1bfe8 100644 --- a/sources/animation.cpp +++ b/sources/animation.cpp @@ -87,8 +87,8 @@ if ( kbd[SDLK_8] ) { current = 8; } if ( kbd[SDLK_9] ) { current = 9; } - if ( kbd[SDLK_RIGHT] ) { current++; kbdlock = 3; } - if ( kbd[SDLK_LEFT] ) { current--; kbdlock = 3; } + if ( kbd[SDLK_RIGHT] ) { current++; kbdlock = 20; } + if ( kbd[SDLK_LEFT] ) { current--; kbdlock = 20; } if ( things.size() > 0 ) { if ( current < 0 ) { @@ -154,7 +154,7 @@ if ( colors.size() > 0 ) { colorpt = colorpt % colors.size(); things.at(current)->setColor(colors.at(colorpt)); - kbdlock = 3; + kbdlock = 20; } } @@ -167,7 +167,7 @@ things.at(current)->showNormals(); showNormals = true; } - kbdlock = 3; + kbdlock = 20; } /* show/hide object */ @@ -179,7 +179,7 @@ } showMe = !showMe; - kbdlock = 3; + kbdlock = 20; } /* delta */ @@ -194,11 +194,11 @@ delta = PI; } things.at(current)->setDelta(delta); - kbdlock = 3; + kbdlock = 20; } /* trace */ - if ( kbd[SDLK_t] ) { things.at(current)->trace(trace); trace = !trace; kbdlock = 3; } + if ( kbd[SDLK_t] ) { things.at(current)->trace(trace); trace = !trace; kbdlock = 20; } } } } diff --git a/sources/glumodel.cpp b/sources/glumodel.cpp index 85db1ee..3b6f669 100644 --- a/sources/glumodel.cpp +++ b/sources/glumodel.cpp @@ -5,20 +5,28 @@ #include // This file offers some OpenGL-related utilities GenericModel::GenericModel() { - init(Vec(0,0,0), Material()); + init(Vec(0,0,0), Material()); } GenericModel::GenericModel(Vec _pos, Material _mat) { - init(_pos, _mat); + init(_pos, _mat); } void GenericModel::init(Vec _pos, Material _mat) { - pos = _pos.clone(); - rot = new Vec(0,0,0); - scale = 1; - mat = _mat.clone(); - pt = gluNewQuadric(); - enabled = true; + pos = _pos.clone(); + rot = new Vec(0,0,0); + scale = 1; + mat = _mat.clone(); + pt = gluNewQuadric(); + visible = true; +} + +void GenericModel::hide ( ) { + visible = false; +} + +void GenericModel::show ( ) { + visible = true; } void GenericModel::setColor(Color* c) { @@ -36,50 +44,50 @@ } GluSphere::GluSphere() { - init(Vec(0,0,0), Material(Color(1,0,1)), 10); + init(Vec(0,0,0), Material(Color(1,0,1)), 10); } GluSphere::GluSphere(Vec _pos, Material _mat, float _rad) { - init(_pos, _mat, _rad); + init(_pos, _mat, _rad); } void GluSphere::init(Vec _pos, Material _mat, float _rad) { - GenericModel::init(_pos, _mat); - rad = _rad; + GenericModel::init(_pos, _mat); + rad = _rad; } void GluSphere::draw() { - glPushMatrix(); + if ( visible ) { + glPushMatrix(); - glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); - glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); - glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); + glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); - glTranslatef(pos->x(), pos->y(), pos->z()); + glTranslatef(pos->x(), pos->y(), pos->z()); - if (enabled) { - mat->draw(); - gluSphere(gluNewQuadric(), rad, 300, 300); - } - glPopMatrix(); + mat->draw(); + gluSphere(gluNewQuadric(), rad, 300, 300); + glPopMatrix(); + } } Fractal::Fractal() { - init(Vec(0,0,0), Material(), 4, 30); + init(Vec(0,0,0), Material(), 4, 30); } void Fractal::init(Vec _pos, Material _m, int _iters, float _len) { - GenericModel::init(_pos, _m); - iters = _iters; - pos = _pos.clone(); - len = _len; + GenericModel::init(_pos, _m); + iters = _iters; + pos = _pos.clone(); + len = _len; } SierpinskiPyramid::SierpinskiPyramid(Vec _pos, int _iters, float _len) { - Fractal::init(_pos, Material(), _iters, _len); - rot = new Vec(0,0,0); - buildList(); + Fractal::init(_pos, Material(), _iters, _len); + rot = new Vec(0,0,0); + buildList(); } void SierpinskiPyramid::buildList() { @@ -163,73 +171,77 @@ } void SierpinskiPyramid::draw() { - glPushMatrix(); + if ( visible ) { + glPushMatrix(); - /* rotate SierpinskiPyramid */ - glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); - glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); - glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); + /* rotate SierpinskiPyramid */ + glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); - /* move it to the desired position */ - glTranslatef(pos->x(), pos->y(), pos->z()); + /* move it to the desired position */ + glTranslatef(pos->x(), pos->y(), pos->z()); - /* scale it to the desired length, width and height */ - glScalef(len, len, len); + /* scale it to the desired length, width and height */ + glScalef(len, len, len); - /* set material */ - mat->draw(); + /* draw material */ + mat->draw(); - /* draw it */ - construct(iters); + /* draw it */ + construct(iters); - glPopMatrix(); + glPopMatrix(); + } } SierpinskiPyramid::~SierpinskiPyramid ( ) { } KochFractal::KochFractal(Vec _pos, int _iters, float _len) { - Fractal::init(_pos, Material(), _iters, _len / (_iters*_iters*_iters)); - multiplicity = 6; - mult_angle = 60; + Fractal::init(_pos, Material(), _iters, _len / (_iters*_iters*_iters)); + multiplicity = 6; + mult_angle = 60; } void KochFractal::draw() { - //TODO: move draw function to Fractal class, it's common for all - //inheriting fractals.... - glPushMatrix(); + if ( visible ) { + //TODO: move draw function to Fractal class, it's common for all + //inheriting fractals.... + glPushMatrix(); - /* rotate */ - glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); - glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); - glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); + /* rotate */ + glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); - /* move it to the desired position */ - glTranslatef(pos->x(), pos->y(), pos->z()); + /* move it to the desired position */ + glTranslatef(pos->x(), pos->y(), pos->z()); - for (int i = 0; i < multiplicity; i++) { - iter(iters); - glRotatef(mult_angle, 1,0,0); + for (int i = 0; i < multiplicity; i++) { + iter(iters); + glRotatef(mult_angle, 1,0,0); + } + + glPopMatrix(); } - - glPopMatrix(); } void KochFractal::iter(float nbit) { - float n = nbit - 1; + float n = nbit - 1; - if (n >= 0) { - iter(n); - glRotatef(60,1,0,0); - iter(n); - glRotatef(-120,1,0,0); - iter(n); - glRotatef(60,1,0,0); - iter(n); - } else { - glBegin(GL_LINES); - glVertex3f(0,0,0); - glVertex3f(0,len,0); - glEnd(); - glTranslatef(0,len,0); - } + if (n >= 0) { + iter(n); + glRotatef(60,1,0,0); + iter(n); + glRotatef(-120,1,0,0); + iter(n); + glRotatef(60,1,0,0); + iter(n); + } else { + glBegin(GL_LINES); + glVertex3f(0,0,0); + glVertex3f(0,len,0); + glEnd(); + glTranslatef(0,len,0); + } }