2009-01-21 01:00:31 +01:00
|
|
|
|
/* libsegl - Sebas Extended GL Library
|
|
|
|
|
* Collection of Opengl/3D-Math helpers
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
|
|
|
|
|
* http://www.seba-geek.de
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-03-08 23:03:26 +01:00
|
|
|
|
#include "punkt3d.h"
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
namespace segl {
|
|
|
|
|
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D::Punkt3D() {
|
|
|
|
|
x = y = z = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Punkt3D::Punkt3D(float _x, float _y, float _z) {
|
|
|
|
|
set(_x, _y, _z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Punkt3D::set(float _x, float _y, float _z) {
|
|
|
|
|
x = _x;
|
|
|
|
|
y = _y;
|
|
|
|
|
z = _z;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
float Punkt3D::abs() const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return sqrt(x*x+y*y+z*z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D erg;
|
|
|
|
|
erg.x = y*b.z - z*b.y;
|
|
|
|
|
erg.y = z*b.x - x*b.z;
|
|
|
|
|
erg.z = x*b.y - y*b.x;
|
|
|
|
|
|
|
|
|
|
return erg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Punkt3D::normalize() {
|
|
|
|
|
float a = abs();
|
|
|
|
|
x /= a;
|
|
|
|
|
y /= a;
|
|
|
|
|
z /= a;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::getNormalized() const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D ret(*this);
|
|
|
|
|
ret.normalize();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
bool Punkt3D::isNormalized() const {
|
|
|
|
|
return (abs()==1.0f);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
float Punkt3D::calcAngle(Punkt3D b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
if(abs()*b.abs()==0.0f)
|
|
|
|
|
return 0.0f;
|
|
|
|
|
return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::getOrtographic() const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D erg;
|
|
|
|
|
if(!x) {
|
|
|
|
|
erg.x = 0.0f;
|
|
|
|
|
erg.y = z;
|
|
|
|
|
erg.z = -y;
|
|
|
|
|
} else if(!y) {
|
|
|
|
|
erg.x = z;
|
|
|
|
|
erg.y = 0;
|
|
|
|
|
erg.z = -x;
|
|
|
|
|
} else {
|
|
|
|
|
// z, oder genereller fall
|
|
|
|
|
erg.x = y;
|
|
|
|
|
erg.y = -x;
|
|
|
|
|
erg.z = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
return erg;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::getOrtographic2() const {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
Punkt3D nullvec;
|
|
|
|
|
Punkt3D erg;
|
|
|
|
|
erg.set(y, -x, 0.0f);
|
|
|
|
|
if(erg!=nullvec)
|
|
|
|
|
return erg;
|
|
|
|
|
erg.set(z, 0.0f, -x);
|
|
|
|
|
if(erg!=nullvec)
|
|
|
|
|
return erg;
|
|
|
|
|
erg.set(0.0f, z, -y);
|
|
|
|
|
return erg;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void Punkt3D::print(std::string coordname) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
if(coordname!="")
|
|
|
|
|
coordname.append(" ");
|
|
|
|
|
std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator+(const Punkt3D &b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D c;
|
|
|
|
|
c.x = x + b.x;
|
|
|
|
|
c.y = y + b.y;
|
|
|
|
|
c.z = z + b.z;
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator-(const Punkt3D &b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
Punkt3D c;
|
|
|
|
|
c.x = x - b.x;
|
|
|
|
|
c.y = y - b.y;
|
|
|
|
|
c.z = z - b.z;
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Punkt3D& Punkt3D::operator+=(const Punkt3D &b) {
|
|
|
|
|
x += b.x;
|
|
|
|
|
y += b.y;
|
|
|
|
|
z += b.z;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Punkt3D& Punkt3D::operator-=(const Punkt3D &b) {
|
|
|
|
|
x -= b.x;
|
|
|
|
|
y -= b.y;
|
|
|
|
|
z -= b.z;
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator+(const float &_m) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return Punkt3D(x+_m, y+_m, z+_m);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator-(const float &_m) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return Punkt3D(x-_m, y-_m, z-_m);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator*(const float &_m) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return Punkt3D(x*_m, y*_m, z*_m);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator/(const float &_m) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return Punkt3D(x/_m, y/_m, z/_m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Punkt3D& Punkt3D::operator+=(const float &_m) {
|
|
|
|
|
x += _m;
|
|
|
|
|
y += _m;
|
|
|
|
|
z += _m;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
Punkt3D& Punkt3D::operator-=(const float &_m) {
|
|
|
|
|
x -= _m;
|
|
|
|
|
y -= _m;
|
|
|
|
|
z -= _m;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
Punkt3D& Punkt3D::operator*=(const float &_m) {
|
|
|
|
|
x *= _m;
|
|
|
|
|
y *= _m;
|
|
|
|
|
z *= _m;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
Punkt3D& Punkt3D::operator/=(const float &_m) {
|
|
|
|
|
x /= _m;
|
|
|
|
|
y /= _m;
|
|
|
|
|
z /= _m;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
float Punkt3D::operator*(const Punkt3D& _m) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return x * _m.x + y * _m.y + z * _m.z;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
Punkt3D Punkt3D::operator-() const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return Punkt3D(-x, -y, -z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
bool Punkt3D::operator==(const Punkt3D& b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return ( x==b.x && y==b.y && z==b.z);
|
|
|
|
|
}
|
2008-09-19 17:34:01 +02:00
|
|
|
|
bool Punkt3D::operator!=(const Punkt3D& b) const {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return !(*this==b);
|
|
|
|
|
}
|
2008-08-10 17:14:54 +02:00
|
|
|
|
|
2008-03-08 23:03:26 +01:00
|
|
|
|
// Freunde
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
segl::Punkt3D operator+(const float& _m, const segl::Punkt3D& b) {
|
|
|
|
|
return segl::Punkt3D(b.x+_m, b.y+_m, b.z+_m);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
segl::Punkt3D operator-(const float& _m, const segl::Punkt3D& b) {
|
|
|
|
|
return segl::Punkt3D(b.x-_m, b.y-_m, b.z-_m);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
segl::Punkt3D operator*(const float& _m, const segl::Punkt3D& b) {
|
|
|
|
|
return segl::Punkt3D(b.x*_m, b.y*_m, b.z*_m);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
segl::Punkt3D operator/(const float& _m, const segl::Punkt3D& b) {
|
|
|
|
|
return segl::Punkt3D(b.x/_m, b.y/_m, b.z/_m);
|
2008-03-08 23:03:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
std::ostream &operator<<(std::ostream &ostr, const segl::Punkt3D &r) {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
return ostr << "(" << r.x << ", " << r.y << ", " << r.z << ")";
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-10 17:14:54 +02:00
|
|
|
|
} // namespace segl
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
float abs(const segl::Punkt3D &p) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
return p.abs();
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-17 02:46:39 +02:00
|
|
|
|
// OpenGL Funktionen f<>r Punkt3D
|
2008-03-08 23:03:26 +01:00
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glVertex3f(const segl::Punkt3D &p) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
glVertex3f(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glTranslatef(const segl::Punkt3D &p) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
glTranslatef(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glNormal3f(const segl::Punkt3D &p) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
glNormal3f(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glRotatef(const float °, const segl::Punkt3D &vec) {
|
2008-03-08 23:03:26 +01:00
|
|
|
|
glRotatef(deg, vec.x, vec.y, vec.z);
|
|
|
|
|
}
|
2008-06-08 14:09:56 +02:00
|
|
|
|
|
|
|
|
|
// Funktionen mit richtgen bezeichnern
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glVertexP3D(const segl::Punkt3D &p) {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
glVertex3f(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glTranslateP3D(const segl::Punkt3D &p) {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
glTranslatef(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glNormalP3D(const segl::Punkt3D &p) {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
glNormal3f(p.x, p.y, p.z);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void glRotateP3D(const float °, const segl::Punkt3D &vec) {
|
2008-06-08 14:09:56 +02:00
|
|
|
|
glRotatef(deg, vec.x, vec.y, vec.z);
|
|
|
|
|
}
|
2008-06-17 02:46:39 +02:00
|
|
|
|
|
2008-09-19 17:34:01 +02:00
|
|
|
|
void gluLookAt(const segl::Punkt3D &pos, const segl::Punkt3D &viewport, const segl::Punkt3D &normal) {
|
2008-06-17 02:46:39 +02:00
|
|
|
|
gluLookAt( pos.x, pos.y, pos.z,
|
|
|
|
|
viewport.x, viewport.y, viewport.z,
|
|
|
|
|
normal.x, normal.y, normal.z );
|
|
|
|
|
}
|