diff --git a/include/thing.h b/include/thing.h index cebaf09..8a78da2 100644 --- a/include/thing.h +++ b/include/thing.h @@ -22,6 +22,7 @@ virtual void translate(Vec d); virtual void scale(float f); virtual void trace(bool s); + virtual void setIterations(int i); }; class View : public Thing { @@ -40,4 +41,15 @@ void setColor(Color* c); }; +class ThingGroup : public Thing { + private: + std::vector things; + public: + ThingGroup(); + void addThing(Thing* t); + void show(); + void hide(); + void draw(); +}; + #endif diff --git a/sources/animation.cpp b/sources/animation.cpp index 4d1bfe8..196b286 100644 --- a/sources/animation.cpp +++ b/sources/animation.cpp @@ -47,6 +47,7 @@ trace = false; showNormals = false; showMe = true; + iterations = 0; } void UserAnimation::tick() { @@ -101,7 +102,9 @@ } #ifdef DEBUG - if ( current != oldcurrent ) { printf("Switched to object %d\n", current); } + if ( current != oldcurrent ) { + printf("Switched to object %d\n", current); + } #endif /* speed */ @@ -197,6 +200,18 @@ kbdlock = 20; } + /* fractal iterations */ + if ( kbd[SDLK_i] ) { + + iterations = iterations % 5; + + iterations++; + + things.at(current)->setIterations(iterations); + + kbdlock = 20; + } + /* trace */ if ( kbd[SDLK_t] ) { things.at(current)->trace(trace); trace = !trace; kbdlock = 20; } } diff --git a/sources/light.cpp b/sources/light.cpp index 2f23ed4..dc3ba69 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -1,41 +1,41 @@ #include "light.h" Light::Light() { - printf("light not initialized\n"); + printf("light not initialized\n"); } Light::Light(GLenum _num, Vec _pos, Material _mat) { - init(_num, _pos, _mat); + init(_num, _pos, _mat); } void Light::init(GLenum _num, Vec _pos, Material _mat) { - num = _num; - pos = _pos.clone(); - mat = _mat.clone(); - rot = new Vec(0,0,0); - showtrace = true; - sphere = gluNewQuadric(); + num = _num; + pos = _pos.clone(); + mat = _mat.clone(); + rot = new Vec(0,0,0); + showtrace = true; + sphere = gluNewQuadric(); - show(); + show(); } void Light::trace(bool _s) { - showtrace = _s; + showtrace = _s; } void Light::show() { - enabled = true; - glEnable(num); + enabled = true; + glEnable(num); } void Light::hide() { - enabled = false; - glDisable(num); + enabled = false; + glDisable(num); } void Light::setPosition(Vec newpos) { - delete pos; - pos = newpos.clone(); + delete pos; + pos = newpos.clone(); } void Light::translate(Vec d) { @@ -43,7 +43,7 @@ } void Light::rotate(Vec a) { - rot->add((Vec)a); + rot->add((Vec)a); } void Light::setColor(Color* c) { @@ -53,37 +53,41 @@ } void Light::draw() { - //save camera position - glPushMatrix(); + if ( enabled ) { + //save camera position + glPushMatrix(); - //glLightfv(num, GL_AMBIENT, mat->ambient->v()); - glLightfv(num, GL_DIFFUSE, mat->ambient->v()); - glLightfv(num, GL_SPECULAR, mat->specular->v()); - //TODO - //glLightf(num,GL_QUADRATIC_ATTENUATION,.001f); + glTranslatef(pos->x(), pos->y(), pos->z()); - glTranslatef(pos->x(), pos->y(), pos->z()); + float p[] = {0.0f, 0.0f, 0.0f, 1.0f}; + glLightfv(num, GL_POSITION, p); - float p[] = {0.0f, 0.0f, 0.0f, 1.0f}; - glLightfv(num, GL_POSITION, p); - - if (showtrace) { - //sphere - float c[] = {1,1,1,1}; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c); - gluSphere(sphere, 1, 10, 10); - } - - glPopMatrix(); + if (showtrace) { + //sphere + float c[] = {1,1,1,1}; + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c); + gluSphere(sphere, 1, 10, 10); + } + + //glLightfv(num, GL_AMBIENT, mat->ambient->v()); + glLightfv(num, GL_DIFFUSE, mat->ambient->v()); + glLightfv(num, GL_SPECULAR, mat->specular->v()); + //TODO + //glLightf(num,GL_QUADRATIC_ATTENUATION,.001f); + + glTranslatef(pos->x(), pos->y(), pos->z()); + + glPopMatrix(); + } } SpotLight::SpotLight(GLenum _num, Vec _pos, Material _mat, float _angle) { - Light::init(_num, _pos, _mat); + Light::init(_num, _pos, _mat); - cutoff_angle = _angle; - cone = gluNewQuadric(); + cutoff_angle = _angle; + cone = gluNewQuadric(); - show(); + show(); } void SpotLight::draw() { diff --git a/sources/main.cpp b/sources/main.cpp index c9c7402..c19cbf1 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -230,8 +230,6 @@ userAnim->addColor(&cyan); userAnim->addColor(&grey); - std::vector models; - std::vector anims; anims.push_back((Animation*)userAnim); @@ -328,8 +326,6 @@ /* end cube */ - //models.push_back(cube); - /* begin import */ char* modelfile = "obj/sample2.obj"; //char* modelfile = "obj/sample.obj"; @@ -351,8 +347,6 @@ /* end import */ - //models.push_back(myModel); - // Prepare configuration: int bitsPerColor = 8; if (WINDOW_COLORDEPTH == 16) { @@ -442,7 +436,7 @@ float viewerMovingSpeed = 1.0f; - std::vector::size_type modelCounter = 0; + ThingGroup* rotatingCubes = new ThingGroup(); Model* t; Color* tc; @@ -453,7 +447,7 @@ t->scale(pow(1.02f, i)); tc = new Color(i*0.02f, 1.0f, 1.0f-i*0.02f); t->setColor(tc); - models.push_back(t); + rotatingCubes->addThing((Thing*)t); } for ( int i = 0; i < 50; i++ ) { t = cube->clone(); @@ -462,8 +456,10 @@ t->setPosition(new Vec(200.0f*cos(((float)-i)*PI/50.0f), 0.0f, 200.0f*sin(((float)-i)*PI/50.0f))); tc = new Color(i*0.02f, 1.0f-i*0.02f, 1.0f-i*0.02f); t->setColor(tc); - models.push_back(t); + rotatingCubes->addThing((Thing*)t); } + + userAnim->addThing((Thing*)rotatingCubes); cube->showNormals(); float delta = 0; @@ -522,11 +518,7 @@ ((Animation*)anims.at(anim_ct))->tick(); } - /* draw some models */ - for ( modelCounter = 0; modelCounter < models.size(); modelCounter++ ) { - //models.at(modelCounter)->draw(); - //models.at(modelCounter)->rotate(Vec(1.5f, 0.0f, 0.0f)); - } + rotatingCubes->draw(); // //xy plane // glBegin(GL_TRIANGLES); @@ -554,10 +546,10 @@ //mysphere.draw(); myfrac->draw(); - //kochfrac->draw(); + kochfrac->draw(); //draw cube - cube->draw(); + //cube->draw(); //draw imported model //myModel->draw(); diff --git a/sources/thing.cpp b/sources/thing.cpp index 13de17c..ccfd27e 100644 --- a/sources/thing.cpp +++ b/sources/thing.cpp @@ -17,6 +17,7 @@ void Thing::trace(bool s) { } void Thing::show() { } void Thing::hide() { } +void Thing::setIterations(int i) { } View::View() { rotation = new Vec(0.0f, 0.0f, 0.0f); @@ -59,3 +60,28 @@ glRotatef(rotation->z(), 0.0f, 0.0f, 1.0f); } + +ThingGroup::ThingGroup ( ) { +} + +void ThingGroup::addThing ( Thing* t ) { + things.push_back(t); +} + +void ThingGroup::show ( ) { + for ( std::vector::size_type i = 0; i < things.size(); i++ ) { + things.at(i)->show(); + } +} + +void ThingGroup::hide ( ) { + for ( std::vector::size_type i = 0; i < things.size(); i++ ) { + things.at(i)->hide(); + } +} + +void ThingGroup::draw ( ) { + for ( std::vector::size_type i = 0; i < things.size(); i++ ) { + things.at(i)->draw(); + } +}