From 12d677afdb0d56241ffce7df3f7d2e90666ede56 Mon Sep 17 00:00:00 2001 From: seba Date: Fri, 19 Sep 2008 17:34:01 +0200 Subject: [PATCH] Versionskram Neue Geotypen --- Makefile | 8 +-- fpsmanager.cpp | 2 +- geotypes.cpp | 131 +++++++++++++++++++++++++++++++++++++++------- geotypes.h | 47 +++++++++++------ glgui/button.h | 4 +- glgui/object.h | 4 +- glgui/textlabel.h | 4 +- glgui/window.h | 4 +- model/loadobj.cpp | 2 +- punkt2d.cpp | 12 +++++ punkt2d.h | 3 ++ punkt3d.cpp | 58 ++++++++++---------- punkt3d.h | 56 ++++++++++---------- 13 files changed, 228 insertions(+), 107 deletions(-) diff --git a/Makefile b/Makefile index 0534258..bc0344d 100755 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CC = g++ AR = ar -OBJECTS = punkt3d.o punkt2d.o emath.o emath_opengl.o color.o gldrawhelper.o glfontengine.o rect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o fpsmanager.o glcamera.o catmullromspline.o extstring.o quader.o -OBJOPT = -Wall -c -DVERSION=$(VERSION) -O2 `sdl-config --cflags` -WINOPT = -Wall -c -DVERSION=$(VERSION) -O2 -I/usr/i586-mingw32msvc/include -I/usr/i586-mingw32msvc/include/SDL +OBJECTS = segl.o punkt3d.o punkt2d.o emath.o emath_opengl.o color.o gldrawhelper.o glfontengine.o rect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o fpsmanager.o glcamera.o catmullromspline.o extstring.o quader.o geotypes.o +OBJOPT = -Wall -c -DVERSION=\"$(VERSION)\" -O2 `sdl-config --cflags` +WINOPT = -Wall -c -DVERSION=\"$(VERSION)\" -O2 -I/usr/i586-mingw32msvc/include -I/usr/i586-mingw32msvc/include/SDL LIBLINK = -lc -shared -Wl,-soname,$(LIBNAME).so.$(LIBVERSION) WINLINK = -L/usr/i586-mingw32msvc/lib /usr/i586-mingw32msvc/lib/SDL_image.lib -lmingw32 -shared -mwindows -lGL -lGLU -lSDL -lSDL_image /usr/i586-mingw32msvc/lib/SDL_image.lib /usr/i586-mingw32msvc/lib/SDL_image.dll SUBDIRS = glgui glmenu model @@ -14,6 +14,8 @@ EXT = so.$(LIBVERSION) .PHONY: windows +all: seglar lib + seglar: $(OBJECTS) subdirs rm -f $(LIBNAME).a $(AR) rcs $(LIBNAME).a $(OBJECTS) $(SUBDIROBJECTS) diff --git a/fpsmanager.cpp b/fpsmanager.cpp index 11b53df..08b89dd 100644 --- a/fpsmanager.cpp +++ b/fpsmanager.cpp @@ -11,7 +11,7 @@ FPSManager::FPSManager(int _fps) { void FPSManager::setFPS(int _fps) { if(_fps<1 || _fps > 500) - return; + return; tickrate = 1000.0f/(float)_fps; } diff --git a/geotypes.cpp b/geotypes.cpp index 20d2f1c..f26e289 100644 --- a/geotypes.cpp +++ b/geotypes.cpp @@ -11,20 +11,28 @@ Sphere::Sphere() { radius = 1.0f; } -bool Sphere::collision(const Sphere &s) { - +bool Sphere::collision(const Sphere &s) const { + return ((pos-s.pos)*(pos-s.pos))<((radius+s.radius)*(radius+s.radius)); } -bool Sphere::collision(const Ray &r) { - +bool Sphere::collision(const Ray &r) const { + return true; } -bool Sphere::collision(const Box & b) { +bool Sphere::collision(const Box & b) const { + return true; +} +bool Sphere::collision(const Plane &p) const { + return true; } -bool Sphere::collision(const Plane &p) { +bool Sphere::inSphere(Punkt3D p) const { + return abs(pos-p)<=radius; +} +Punkt3D Sphere::getPos() const { + return pos; } Ray::Ray() { @@ -32,17 +40,96 @@ Ray::Ray() { } Ray::Ray(Punkt3D _pos, Punkt3D _dir) { + set(_pos, _dir); +} + +void Ray::set(Punkt3D _pos, Punkt3D _dir) { pos = _pos; dir = _dir; } -bool Ray::collision(const Sphere &s) { +Punkt3D Ray::get(float x) { + return pos + dir*x; +} + +// TODO: Heavy Testing +bool Ray::onRay(Punkt3D p, int rnd) { + float r1 = 0.0f, r2 = 0.0f, r3 = 0.0f; + short fcount = 0; + bool g1=true, g2=true, g3=true; + Punkt3D f = p-pos; + + if(dir.x==0.0f) { + if(f.x!=0.0f) + return false; + g1 = false; + fcount++; + } else + r1 = f.x / dir.x; + + if(dir.y==0.0f) { + if(f.y!=0.0f) + return false; + g2 = false; + fcount++; + } else + r2 = f.y / dir.y; + + if(dir.z==0.0f) { + if(f.z!=0.0f) + return false; + g2 = false; + fcount++; + } else + r2 = f.z / dir.z; + + if(fcount>=2) + return true; + + if(rnd>=0) { + // TODO:Implement rounding + // r1 = round(r1, rnd); + // r2 = round(r2, rnd); + // r3 = round(r3, rnd); + } + + if(g1) + return (r2 == r3); + else if(g2) + return (r1 == r3); + else if(g3) + return (r1 == r2); + else + return (r1 == r2 == r3); +} + +float Ray::dist(Punkt3D p) { + return abs(p - get( getParam(p) )); +} + +float Ray::getParam(Punkt3D p, bool onray) { + if(onray) { + if(!onRay(p)) + return 0.0f; + } + return -((pos-p)*dir) / (dir*dir); +} + +bool Ray::collision(const Sphere &s) const { return s.collision(*this); } -bool Ray::collision(const Ray &r); -bool Ray::collision(const Box & b); -bool Ray::collision(const Plane &p); +bool Ray::collision(const Ray &r) const { + return true; +} + +bool Ray::collision(const Box & b) const { + return true; +} + +bool Ray::collision(const Plane &p) const { + return true; +} Box::Box() { max.set(1.0f, 1.0f, 1.0f); @@ -53,20 +140,20 @@ Box::Box(Punkt3D _min, Punkt3D _max) { max = _max; } -bool Box::collision(const Sphere &s) { +bool Box::collision(const Sphere &s) const { return s.collision(*this); } -bool Box::collision(const Ray &r) { +bool Box::collision(const Ray &r) const { return r.collision(*this); } -bool Box::collision(const Box & b) { - +bool Box::collision(const Box & b) const { + return true; } -bool Box::collision(const Plane &p) { - +bool Box::collision(const Plane &p) const { + return true; } Plane::Plane() { @@ -84,20 +171,24 @@ Plane::Plane(float x, float y, float z, float a) { norm.normalize(); } -bool Plane::collision(const Sphere &s) { +bool Plane::collision(const Sphere &s) const { return s.collision(*this); } -bool Plane::collision(const Ray &r) { +bool Plane::collision(const Ray &r) const { return r.collision(*this); } -bool Plane::collision(const Box & b) { +bool Plane::collision(const Box & b) const { return b.collision(*this); } -bool Plane::collision(const Plane &p) { +bool Plane::collision(const Plane &p) const { + return true; +} +float Plane::dist(Punkt3D p) const { + } } // namespace segl diff --git a/geotypes.h b/geotypes.h index 15d8460..c4be645 100644 --- a/geotypes.h +++ b/geotypes.h @@ -6,7 +6,7 @@ namespace segl { class Ray; -class Box +class Box; class Plane; @@ -18,10 +18,13 @@ class Sphere { Sphere(Punkt3D _pos, float radius); Sphere(); - bool collision(const Sphere &s); - bool collision(const Ray &r); - bool collision(const Box & b); - bool collision(const Plane &p); + bool collision(const Sphere &s) const; + bool collision(const Ray &r) const; + bool collision(const Box & b) const; + bool collision(const Plane &p) const; + + bool inSphere(Punkt3D p) const; + Punkt3D getPos() const; }; class Ray { @@ -32,10 +35,16 @@ class Ray { Ray(); Ray(Punkt3D _pos, Punkt3D _dir); - bool collision(const Sphere &s); - bool collision(const Ray &r); - bool collision(const Box & b); - bool collision(const Plane &p); + void set(Punkt3D _pos, Punkt3D _dir); + Punkt3D get(float x); + bool onRay(Punkt3D p, int rnd=-1); + float dist(Punkt3D p); + float getParam(Punkt3D p, bool onray=false); + + bool collision(const Sphere &s) const; + bool collision(const Ray &r) const; + bool collision(const Box & b) const; + bool collision(const Plane &p) const; }; class Box { @@ -46,10 +55,10 @@ class Box { Box(); Box(Punkt3D _min, Punkt3D _max); - bool collision(const Sphere &s); - bool collision(const Ray &r); - bool collision(const Box & b); - bool collision(const Plane &p); + bool collision(const Sphere &s) const; + bool collision(const Ray &r) const; + bool collision(const Box & b) const; + bool collision(const Plane &p) const; }; class Plane { @@ -61,10 +70,14 @@ class Plane { Plane(Punkt3D _pos, Punkt3D _norm); Plane(float x, float y, float z, float a); - bool collision(const Sphere &s); - bool collision(const Ray &r); - bool collision(const Box & b); - bool collision(const Plane &p); + bool collision(const Sphere &s) const; + bool collision(const Ray &r) const; + bool collision(const Box & b) const; + bool collision(const Plane &p) const; + + float dist(Punkt3D p) const; }; } // namespace segl + +#endif diff --git a/glgui/button.h b/glgui/button.h index a46d42b..e0d6945 100644 --- a/glgui/button.h +++ b/glgui/button.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include "textlabel.h" diff --git a/glgui/object.h b/glgui/object.h index 13890cb..69434ac 100644 --- a/glgui/object.h +++ b/glgui/object.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include class GLGuiObject { protected: diff --git a/glgui/textlabel.h b/glgui/textlabel.h index bb54def..b9a9a18 100644 --- a/glgui/textlabel.h +++ b/glgui/textlabel.h @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include "object.h" #include "../color.h" diff --git a/glgui/window.h b/glgui/window.h index 996fc6e..ba249f8 100644 --- a/glgui/window.h +++ b/glgui/window.h @@ -4,8 +4,8 @@ #include #include #include -#include -#include +#include +#include #include "../color.h" #include "../gldrawhelper.h" diff --git a/model/loadobj.cpp b/model/loadobj.cpp index 9e758e5..2a91068 100644 --- a/model/loadobj.cpp +++ b/model/loadobj.cpp @@ -216,7 +216,7 @@ bool LoadOBJ::load(Model *m) { minz = std::min(minz, m->meshdata[i].z); maxz = std::max(maxz, m->meshdata[i].z); } - std::cout << "(" << minx << ", " << maxx << ") " << "(" << miny << ", " << maxy << ") " << "(" << minz << ", " << maxz << ") " << std::endl; +// std::cout << "(" << minx << ", " << maxx << ") " << "(" << miny << ", " << maxy << ") " << "(" << minz << ", " << maxz << ") " << std::endl; m->boundingbox.d.set(minx, miny, minz); m->boundingbox.a.set(minx, miny, maxz); m->boundingbox.b.set(maxx, miny, maxz); diff --git a/punkt2d.cpp b/punkt2d.cpp index 29b7d70..c426fb6 100644 --- a/punkt2d.cpp +++ b/punkt2d.cpp @@ -25,6 +25,18 @@ float Punkt2D::abs() { return sqrt(x*x + y*y); } +void Punkt2D::normalize() { + float a = abs(); + x /= a; + y /= a; +} + +Punkt2D Punkt2D::getNormalized() const { + Punkt2D ret(*this); + ret.normalize(); + return ret; +} + Punkt2D Punkt2D::operator+(const Punkt2D &b) { Punkt2D c; c.x = x + b.x; diff --git a/punkt2d.h b/punkt2d.h index 0bc8fd6..52e68a9 100644 --- a/punkt2d.h +++ b/punkt2d.h @@ -18,6 +18,9 @@ class Punkt2D { void print(std::string=""); float abs(); + void normalize(); + Punkt2D getNormalized() const; + // Operatoren Punkt2D operator+(const Punkt2D&); Punkt2D operator-(const Punkt2D&); diff --git a/punkt3d.cpp b/punkt3d.cpp index dbf7ac6..2c75129 100644 --- a/punkt3d.cpp +++ b/punkt3d.cpp @@ -16,11 +16,11 @@ void Punkt3D::set(float _x, float _y, float _z) { z = _z; } -float Punkt3D::abs() { +float Punkt3D::abs() const { return sqrt(x*x+y*y+z*z); } -Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) { +Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) const { Punkt3D erg; erg.x = y*b.z - z*b.y; erg.y = z*b.x - x*b.z; @@ -36,23 +36,23 @@ void Punkt3D::normalize() { z /= a; } -Punkt3D Punkt3D::getNormalized() { +Punkt3D Punkt3D::getNormalized() const { Punkt3D ret(*this); ret.normalize(); return ret; } -bool Punkt3D::isNormalized() { - return (abs()==1); +bool Punkt3D::isNormalized() const { + return (abs()==1.0f); } -float Punkt3D::calcAngle(Punkt3D b) { +float Punkt3D::calcAngle(Punkt3D b) const { if(abs()*b.abs()==0.0f) return 0.0f; return rad2deg(std::acos(((*this)*b)/(abs()*b.abs()))); } -Punkt3D Punkt3D::getOrtographic() { +Punkt3D Punkt3D::getOrtographic() const { Punkt3D erg; if(!x) { erg.x = 0.0f; @@ -71,7 +71,7 @@ Punkt3D Punkt3D::getOrtographic() { return erg; } -Punkt3D Punkt3D::getOrtographic2() { +Punkt3D Punkt3D::getOrtographic2() const { Punkt3D nullvec; Punkt3D erg; erg.set(y, -x, 0.0f); @@ -84,13 +84,13 @@ Punkt3D Punkt3D::getOrtographic2() { return erg; } -void Punkt3D::print(std::string coordname) { +void Punkt3D::print(std::string coordname) const { if(coordname!="") coordname.append(" "); std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl; } -Punkt3D Punkt3D::operator+(const Punkt3D &b) { +Punkt3D Punkt3D::operator+(const Punkt3D &b) const { Punkt3D c; c.x = x + b.x; c.y = y + b.y; @@ -99,7 +99,7 @@ Punkt3D Punkt3D::operator+(const Punkt3D &b) { return c; } -Punkt3D Punkt3D::operator-(const Punkt3D &b) { +Punkt3D Punkt3D::operator-(const Punkt3D &b) const { Punkt3D c; c.x = x - b.x; c.y = y - b.y; @@ -124,19 +124,19 @@ Punkt3D& Punkt3D::operator-=(const Punkt3D &b) { return *this; } -Punkt3D Punkt3D::operator+(const float &_m) { +Punkt3D Punkt3D::operator+(const float &_m) const { return Punkt3D(x+_m, y+_m, z+_m); } -Punkt3D Punkt3D::operator-(const float &_m) { +Punkt3D Punkt3D::operator-(const float &_m) const { return Punkt3D(x-_m, y-_m, z-_m); } -Punkt3D Punkt3D::operator*(const float &_m) { +Punkt3D Punkt3D::operator*(const float &_m) const { return Punkt3D(x*_m, y*_m, z*_m); } -Punkt3D Punkt3D::operator/(const float &_m) { +Punkt3D Punkt3D::operator/(const float &_m) const { return Punkt3D(x/_m, y/_m, z/_m); } @@ -165,18 +165,18 @@ Punkt3D& Punkt3D::operator/=(const float &_m) { return *this; } -float Punkt3D::operator*(const Punkt3D& _m) { +float Punkt3D::operator*(const Punkt3D& _m) const { return x * _m.x + y * _m.y + z * _m.z; } -Punkt3D Punkt3D::operator-() { +Punkt3D Punkt3D::operator-() const { return Punkt3D(-x, -y, -z); } -bool Punkt3D::operator==(const Punkt3D& b) { +bool Punkt3D::operator==(const Punkt3D& b) const { return ( x==b.x && y==b.y && z==b.z); } -bool Punkt3D::operator!=(const Punkt3D& b) { +bool Punkt3D::operator!=(const Punkt3D& b) const { return !(*this==b); } @@ -204,46 +204,46 @@ std::ostream &operator<<(std::ostream &ostr, const segl::Punkt3D &r) { } // namespace segl -float abs(segl::Punkt3D p) { +float abs(const segl::Punkt3D &p) { return p.abs(); } // OpenGL Funktionen f�r Punkt3D -void glVertex3f(segl::Punkt3D p) { +void glVertex3f(const segl::Punkt3D &p) { glVertex3f(p.x, p.y, p.z); } -void glTranslatef(segl::Punkt3D p) { +void glTranslatef(const segl::Punkt3D &p) { glTranslatef(p.x, p.y, p.z); } -void glNormal3f(segl::Punkt3D p) { +void glNormal3f(const segl::Punkt3D &p) { glNormal3f(p.x, p.y, p.z); } -void glRotatef(float deg, segl::Punkt3D vec) { +void glRotatef(const float °, const segl::Punkt3D &vec) { glRotatef(deg, vec.x, vec.y, vec.z); } // Funktionen mit richtgen bezeichnern -void glVertexP3D(segl::Punkt3D p) { +void glVertexP3D(const segl::Punkt3D &p) { glVertex3f(p.x, p.y, p.z); } -void glTranslateP3D(segl::Punkt3D p) { +void glTranslateP3D(const segl::Punkt3D &p) { glTranslatef(p.x, p.y, p.z); } -void glNormalP3D(segl::Punkt3D p) { +void glNormalP3D(const segl::Punkt3D &p) { glNormal3f(p.x, p.y, p.z); } -void glRotateP3D(float deg, segl::Punkt3D vec) { +void glRotateP3D(const float °, const segl::Punkt3D &vec) { glRotatef(deg, vec.x, vec.y, vec.z); } -void gluLookAt(segl::Punkt3D pos, segl::Punkt3D viewport, segl::Punkt3D normal) { +void gluLookAt(const segl::Punkt3D &pos, const segl::Punkt3D &viewport, const segl::Punkt3D &normal) { gluLookAt( pos.x, pos.y, pos.z, viewport.x, viewport.y, viewport.z, normal.x, normal.y, normal.z ); diff --git a/punkt3d.h b/punkt3d.h index 93de090..6363c89 100644 --- a/punkt3d.h +++ b/punkt3d.h @@ -16,39 +16,39 @@ class Punkt3D { float x, y, z; void set(float, float, float); - float abs(); - Punkt3D kreuzprodukt(const Punkt3D&); + float abs() const; + Punkt3D kreuzprodukt(const Punkt3D&) const; void normalize(); - Punkt3D getNormalized(); - bool isNormalized(); - float calcAngle(Punkt3D); - Punkt3D getOrtographic(); - Punkt3D getOrtographic2(); + Punkt3D getNormalized() const; + bool isNormalized() const; + float calcAngle(Punkt3D b) const; + Punkt3D getOrtographic() const; + Punkt3D getOrtographic2() const; - void print(std::string=""); + void print(std::string="") const; // Operatoren - Punkt3D operator+(const Punkt3D&); - Punkt3D operator-(const Punkt3D&); + Punkt3D operator+(const Punkt3D&) const; + Punkt3D operator-(const Punkt3D&) const; Punkt3D& operator+=(const Punkt3D&); Punkt3D& operator-=(const Punkt3D&); - Punkt3D operator+(const float&); - Punkt3D operator-(const float&); - Punkt3D operator*(const float&); - Punkt3D operator/(const float&); + Punkt3D operator+(const float&) const; + Punkt3D operator-(const float&) const; + Punkt3D operator*(const float&) const; + Punkt3D operator/(const float&) const; Punkt3D& operator+=(const float&); Punkt3D& operator-=(const float&); Punkt3D& operator*=(const float&); Punkt3D& operator/=(const float&); - float operator*(const Punkt3D&); + float operator*(const Punkt3D&) const; - Punkt3D operator-(); + Punkt3D operator-() const; - bool operator==(const Punkt3D&); - bool operator!=(const Punkt3D&); + bool operator==(const Punkt3D&) const; + bool operator!=(const Punkt3D&) const; friend Punkt3D operator+(const float&, const Punkt3D&); friend Punkt3D operator-(const float&, const Punkt3D&); @@ -59,22 +59,22 @@ class Punkt3D { } // namespace segl -float abs(segl::Punkt3D); +float abs(const segl::Punkt3D&); // OpenGL-Functions for Punkt3D -void glVertex3f(segl::Punkt3D); -void glTranslatef(segl::Punkt3D); -void glNormal3f(segl::Punkt3D); +void glVertex3f(const segl::Punkt3D&); +void glTranslatef(const segl::Punkt3D&); +void glNormal3f(const segl::Punkt3D&); -void glRotatef(float, segl::Punkt3D); +void glRotatef(const float&, const segl::Punkt3D&); // Funktionen mit richtgen bezeichnern -void glVertexP3D(segl::Punkt3D); -void glTranslateP3D(segl::Punkt3D); -void glNormalP3D(segl::Punkt3D); +void glVertexP3D(const segl::Punkt3D&); +void glTranslateP3D(const segl::Punkt3D&); +void glNormalP3D(const segl::Punkt3D&); -void glRotateP3D(float, segl::Punkt3D); -void gluLookAt(segl::Punkt3D pos, segl::Punkt3D viewport, segl::Punkt3D normal); +void glRotateP3D(const float&, const segl::Punkt3D&); +void gluLookAt(const segl::Punkt3D &pos, const segl::Punkt3D &viewport, const segl::Punkt3D &normal); #endif