Newer
Older
cg / sources / poly.cpp
@glproj03 glproj03 on 29 Dec 2005 3 KB Changes:
#include <stdio.h>
#include <math.h>

#include <GL/gl.h>
#include <GL/glu.h>

#include "poly.h"
#include <vector>
#include <string>

using namespace std;

Poly::Poly() {
	position = new Vec(0,0,0);
	rotation = new Vec(0,0,0);
	color = new Color(1,0,0);
	setCenter(Vec(0,0,0));
}

Poly::Poly(std::vector<Vec*> & _points) {
	Poly(_points, Color(1,0,0));
}

Poly::Poly(std::vector<Vec*> & _points, Color _color) {
	position = new Vec(0,0,0);
	rotation = new Vec(0,0,0);
	color = _color.clone();

	for (std::vector<Vec*>::size_type i = 0 ; i < _points.size(); i++ ) {
		initpoints.push_back(_points.at(i));
	}

	setCenter(calcCenter());
}

Poly::~Poly() {
	delete position;
	delete rotation;
	delete color;
}

/*
* set the position where the (rotation-)center of
* the polygon is located
*/
void Poly::setCenter(Vec c) {
	position = c.clone();
}

/*
* initial rotation relative to rotation center
*/
Vec Poly::getInitRotation(Vec& v) {
	return Vec(atan2(v.y(), v.z()), atan2(v.x(), v.z()), atan2(v.y(), v.x()));
}

/*
* calculate the center relative to all vertices.
*/
Vec Poly::calcCenter() {
	Vec s (0,0,0);

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

	for ( std::vector<Vec*>::size_type i = 0; i < initpoints.size(); i++ ) {
		s = s + *(initpoints.at(i));
	}

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

/*
* reset the position of all vertices (ie. translate your
* polygon to an absolute position)
*/
void Poly::setPosition(Vec newcenter) {
	Vec diff = newcenter - (*position);
	position = newcenter.clone();

	for ( std::vector<Vec*>::size_type i = 0; i < initpoints.size(); i++ ) {
		initpoints.at(i)->add(diff);
	}
}

//additional rotation in arcs
void Poly::rotate(Vec rot) {
	rotation->add(rot);
}

Vec Poly::rotate_x(Vec& c) {
	Vec initrot = getInitRotation(c);
	Vec rot = initrot + *rotation;
	float r = Vec(0, c.y(), c.z()).length();
	return Vec (c.x(), r*cos(rot.x()), r*sin(rot.x()));
}

Vec Poly::rotate_y(Vec& c) {
	Vec initrot = getInitRotation(c);
	Vec rot = initrot + *rotation;
	float r = Vec(c.x(), 0, c.z()).length();
	return Vec (r*sin(rot.y()), c.y(), r*cos(rot.y()));
}

Vec Poly::rotate_z(Vec& c) {
	Vec initrot = getInitRotation(c);
	Vec rot = initrot + *rotation;
	float r = Vec(c.x(), c.y(), 0).length();
	return Vec (r*cos(rot.z()), r*sin(rot.z()), c.z());
}

void Poly::draw() {
	glBegin (GL_POLYGON);

	//Vec v1 = *((Vec*)initpoints->get(2)) - *((Vec*)initpoints->get(0));
	//Vec v2 = *((Vec*)initpoints->get(1)) - *((Vec*)initpoints->get(0));
	//glNormal3fv(  v1.cross(v2)+ *(Vec*)((*initpoints)[0]).c  );
	//glNormal3fv(  v1.cross(v2) + ((Vec*)initpoints->get(0))->c  );

	Vec* mv;

	for ( std::vector<Vec*>::size_type i = 0; i < initpoints.size(); i++ ) {
		color->drawColor();
		Vec out = *(initpoints.at(i)) - *position;
		out = rotate_x(out);
		out = rotate_y(out);
		out = rotate_z(out);
		mv = (*position + out).clone();
		glVertex3fv(mv->c);
	}
	glEnd();
}

int Poly::numPoints() {
	return (int)initpoints.size();
}

void Poly::vardump(std::string whitespace) {
	printf("%sPoly(%d points, center: [%f, %f, %f], rotation: [%f, %f, %f], color: [%f, %f, %f])\n",
		whitespace.c_str(), initpoints.size(), position->x(), position->y(), position->z(),
		rotation->x(), rotation->y(), rotation->z(), color->r, color->g, color->b);

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

void Poly::addPoint(Vec* p) {
	initpoints.push_back(p);
}

void Poly::setColor(Color c) {
	color = c.clone();
}

Color* Poly::getColor() {
	return color;
}