Newer
Older
cg / sources / model.cpp
@glproj03 glproj03 on 31 Jan 2006 2 KB Changes:
#include "model.h"

#include <string>

Model::Model() {
	rotation = new Vec(0,0,0);
	position = new Vec(0,0,0);
	drawMe = true;
}

Model::Model(vector<Poly*> & _faces) {
	for (std::vector<Poly*>::size_type i = 0 ; i < _faces.size(); i++ ) {
		faces.push_back(_faces.at(i));
	}

	Vec c = calcCenter();
	setCenter(c);
	rotation = new Vec(0,0,0);
	drawMe = true;
}

Model::~Model() {
	delete position;
	delete rotation;
}

void Model::setCenter(Vec* c) {
	position = c->clone();

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->setCenter(position);
	}
}

void Model::setCenter(Vec& c) {  
	position = c.clone();

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->setCenter(c);
	}
}

Vec Model::calcCenter() {
	Vec s(0,0,0);

	if ( faces.size() == 0 ) {
		return s;
	}

	for ( std::vector<Poly*>::size_type i = 0; i < faces.size(); i++ ) {
		s = s + faces.at(i)->calcCenter();
	}

	return s / (float)faces.size();
}

void Model::rotate(Vec rot) {
	rotation->add((Vec)rot);

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->rotate(rot);
	}
}

void Model::draw() {
	if ( drawMe ) {
		for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
			faces.at(i)->draw();
		}
	}
}

void Model::addFace(Poly* face) {
	faces.push_back(face);
}

int Model::numFaces() {
	return faces.size();
}

void Model::vardump(std::string whitespace) {
	printf("%sModel(%d faces, center: [%f, %f, %f], rotation: [%f, %f, %f])\n",
		whitespace.c_str(), faces.size(), position->x(), position->y(), position->z(),
		rotation->x(), rotation->y(), rotation->z());

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->vardump(whitespace+"	");
	}
}

void Model::setDelta(float d) {
	delta = d;

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->setDelta(d);
	}
}

float Model::getDelta() {
	return delta;
}

void Model::showNormals() {
	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->showNormals();
	}
}

void Model::hideNormals() {
	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->hideNormals();
	}
}

Model* Model::clone() {
	Model* nm = new Model();

	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		nm->addFace(faces.at(i)->clone());
	}

	nm->setCenter(position);
	nm->setDelta(delta);

	return nm;
}

void Model::setColor(Color* c) {
	for (std::vector<Poly*>::size_type i = 0 ; i < faces.size(); i++ ) {
		faces.at(i)->setColor(c);
	}
}

void Model::show() {
	drawMe = true;
}

void Model::hide() {
	drawMe = false;
}