diff --git a/include/glumodel.h b/include/glumodel.h index 32ea3e8..205a8b2 100644 --- a/include/glumodel.h +++ b/include/glumodel.h @@ -6,7 +6,7 @@ #include "poly.h" #include -class GluModel : public Thing { +class GenericModel : public Thing { public: GLUquadricObj* pt; Vec* pos; @@ -14,15 +14,15 @@ Material* mat; float scale; bool enabled; - GluModel(); - GluModel(Vec _pos, Material _mat); + GenericModel(); + GenericModel(Vec _pos, Material _mat); void init(Vec _pos, Material _mat); void setMaterial(Color _amb); void setMaterial(Color _amb, Color _spec); }; -class GluSphere : public GluModel { +class GluSphere : public GenericModel { public: float rad; GluSphere(); @@ -31,4 +31,13 @@ void draw(); }; +class KochFractal : public GenericModel { + public: + float iters; + float len; + KochFractal(); + void draw(); + void iter(float nbit); +}; + #endif diff --git a/sources/glumodel.cpp b/sources/glumodel.cpp index 1b9a06b..784da2a 100644 --- a/sources/glumodel.cpp +++ b/sources/glumodel.cpp @@ -1,16 +1,18 @@ #include "glumodel.h" #include +#include // This file import the OpenGL interface +#include // This file offers some OpenGL-related utilities -GluModel::GluModel() { +GenericModel::GenericModel() { init(Vec(0,0,0), Material()); } -GluModel::GluModel(Vec _pos, Material _mat) { +GenericModel::GenericModel(Vec _pos, Material _mat) { init(_pos, _mat); } -void GluModel::init(Vec _pos, Material _mat) { +void GenericModel::init(Vec _pos, Material _mat) { pos = _pos.clone(); rot = new Vec(0,0,0); scale = 1; @@ -28,7 +30,7 @@ } void GluSphere::init(Vec _pos, Material _mat, float _rad) { - GluModel::init(_pos, _mat); + GenericModel::init(_pos, _mat); rad = _rad; } @@ -49,3 +51,38 @@ } +KochFractal::KochFractal() { + iters = 4; + len = 30 / (iters*iters*iters); +} + +void KochFractal::draw() { + glPushMatrix(); + for (int i = 0; i < 6; i++) { + iter(iters); + glRotatef(60, 1,0,0); + } + glPopMatrix(); +} + +void KochFractal::iter(float nbit) { + 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); + //glRotatef(30,1,0,0); + + } else { + glBegin(GL_LINES); + glVertex3f(0,0,0); + glVertex3f(0,len,0); + glEnd(); + glTranslatef(0,len,0); + } +} diff --git a/sources/main.cpp b/sources/main.cpp index e6a3b76..050449a 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -244,6 +244,8 @@ Material(Color(1,1,0)), 50); + KochFractal myfrac = KochFractal(); + /* begin cube */ Vec* ulh = new Vec(0 ,0 ,0 ); Vec* ulv = new Vec(0 ,0 ,10); @@ -488,50 +490,42 @@ /* draw some models */ for ( modelCounter = 0; modelCounter < models.size(); modelCounter++ ) { - models.at(modelCounter)->draw(); + //models.at(modelCounter)->draw(); models.at(modelCounter)->rotate(Vec(1.5f, 0.0f, 0.0f)); } - //xy plane - glBegin(GL_TRIANGLES); - glNormal3f(0,0,1); - glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); - glVertex3f(0,0,0); - //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); - glVertex3f(10,0,0); - //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red.v()); - glVertex3f(0,10,0); - glEnd(); +// //xy plane +// glBegin(GL_TRIANGLES); +// glNormal3f(0,0,1); +// glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); +// glVertex3f(0,0,0); +// //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); +// glVertex3f(10,0,0); +// //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red.v()); +// glVertex3f(0,10,0); +// glEnd(); - //xz plane - glBegin(GL_TRIANGLES); - glNormal3f(0,1,0); - glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); - glVertex3f(0,0,0); - //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); - glVertex3f(30,0,0); - //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green.v()); - glVertex3f(0,0,30); - glEnd(); +// //xz plane +// glBegin(GL_TRIANGLES); +// glNormal3f(0,1,0); +// glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); +// glVertex3f(0,0,0); +// //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); +// glVertex3f(30,0,0); +// //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green.v()); +// glVertex3f(0,0,30); +// glEnd(); - //sphere -// glPushMatrix(); -// Material m = Material(Color(0.5,0.5,0.5),Color(0.5,0.5,0.5)); -// m.draw(); -// //float mat[] = {0.5,0.5,0.5,1.0}; -// //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat); -// //glMaterialfv(GL_FRONT, GL_SPECULAR, mat); -// //glMateriali(GL_FRONT, GL_SHININESS, 96); -// glTranslatef(0,-60,0); -// gluSphere(gluNewQuadric(), 50, 300, 300); -// glPopMatrix(); + //draw sphere mysphere.draw(); + myfrac.draw(); + //draw cube - cube->draw(); + //cube->draw(); //draw imported model - myModel->draw(); + //myModel->draw();