Newer
Older
cg / sources / glumodel.cpp
@glproj03 glproj03 on 3 Feb 2006 1 KB added koch fractal
#include "glumodel.h"

#include <string>
#include <GL/gl.h>      // This file import the OpenGL interface
#include <GL/glu.h>     // This file offers some OpenGL-related utilities

GenericModel::GenericModel() {
  init(Vec(0,0,0), Material());
}

GenericModel::GenericModel(Vec _pos, Material _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;
}

GluSphere::GluSphere() {
  init(Vec(0,0,0), Material(Color(1,0,1)), 10);
}

GluSphere::GluSphere(Vec _pos, Material _mat, float _rad) {
  init(_pos, _mat, _rad);
}

void GluSphere::init(Vec _pos, Material _mat, float _rad) {
  GenericModel::init(_pos, _mat);
  rad = _rad;
}

void GluSphere::draw() {
  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);

  glTranslatef(pos->x(), pos->y(), pos->z());

  if (enabled) {
	mat->draw();
	gluSphere(gluNewQuadric(), rad, 300, 300);	
  }
  glPopMatrix();

}

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);
  }
}