Makefileupdate: subdirs werden benutzt

glmenu: spielemenu erstellt
Punkt3D: aus emath.h raus, eigene header bekommen
Punkt2D: endlich erstellt
This commit is contained in:
seba 2008-03-08 23:03:26 +01:00
parent 996c52b22e
commit a233128818
22 changed files with 811 additions and 278 deletions

View File

@ -1,15 +1,19 @@
COMPILER = g++ COMPILER = g++
OBJECTS = emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o glgui/glgui.a OBJECTS = punkt3d.o punkt2d.o emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glsdlscreen.o sdlfuncs.o
SUBDIRS = glgui glmenu
SUBDIROBJECTS = glgui/*.o glmenu/*.o
VERSION = 0.0.1 VERSION = 0.0.1
LIBNAME = libsegl LIBNAME = libsegl
seglar: $(OBJECTS) subdirs
segllib: $(OBJECTS)
seglar: $(OBJECTS)
rm -f $(LIBNAME).a rm -f $(LIBNAME).a
ar rcs $(LIBNAME).a $(OBJECTS) ar rcs $(LIBNAME).a $(OBJECTS) $(SUBDIROBJECTS)
# ranlib $(LIBNAME).a # ranlib $(LIBNAME).a
segllib: $(OBJECTS) subdirs
subdirs:
@for i in $(SUBDIRS); do $(MAKE) -C $$i; done
%.o: %.cpp %.h %.o: %.cpp %.h
$(COMPILER) -c `sdl-config --cflags` $< $(COMPILER) -c `sdl-config --cflags` $<
@ -18,10 +22,14 @@ seglar: $(OBJECTS)
glgui/glgui.a: glgui/glgui.a:
cd glgui; $(MAKE); cd glgui; $(MAKE);
glmenu/glmenu.a:
cd glmenu; $(MAKE);
testprog: seglar testprog.o testprog: seglar testprog.o
g++ `sdl-config --libs` -lSDL_image -lGL -lGLU testprog.o -o testprog $(LIBNAME).a g++ `sdl-config --libs` -lSDL_image -lGL -lGLU testprog.o -o testprog $(LIBNAME).a
clean: clean:
rm -f $(OBJECTS) rm -f $(OBJECTS)
cd glgui; $(MAKE) clean cd glgui; $(MAKE) clean
cd glmenu; $(MAKE) clean
@echo Done cleaning... @echo Done cleaning...

206
emath.cpp
View File

@ -1,211 +1,5 @@
#include "emath.h" #include "emath.h"
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;
}
float Punkt3D::abs() {
return sqrt(x*x+y*y+z*z);
}
Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) {
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;
}
Punkt3D Punkt3D::getNormalized() {
Punkt3D ret(*this);
ret.normalize();
return ret;
}
bool Punkt3D::isNormalized() {
return (abs()==1);
}
float Punkt3D::calcAngle(Punkt3D b) {
if(abs()*b.abs()==0.0f)
return 0.0f;
return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
}
Punkt3D Punkt3D::getOrtographic() {
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;
}
void Punkt3D::print(std::string coordname) {
if(coordname!="")
coordname.append(" ");
std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
}
Punkt3D Punkt3D::operator+(const Punkt3D &b) {
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) {
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;
}
Punkt3D Punkt3D::operator+(const float &_m) {
return Punkt3D(x+_m, y+_m, z+_m);
}
Punkt3D Punkt3D::operator-(const float &_m) {
return Punkt3D(x-_m, y-_m, z-_m);
}
Punkt3D Punkt3D::operator*(const float &_m) {
return Punkt3D(x*_m, y*_m, z*_m);
}
Punkt3D Punkt3D::operator/(const float &_m) {
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;
}
float Punkt3D::operator*(const Punkt3D& _m) {
return x * _m.x + y * _m.y + z * _m.z;
}
Punkt3D Punkt3D::operator-() {
return Punkt3D(-x, -y, -z);
}
bool Punkt3D::operator==(const Punkt3D& b) {
return ( x==b.x && y==b.y && z==b.z);
}
bool Punkt3D::operator!=(const Punkt3D& b) {
return !(*this==b);
}
// Freunde
Punkt3D operator+(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x+_m, b.y+_m, b.z+_m);
}
Punkt3D operator-(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x-_m, b.y-_m, b.z-_m);
}
Punkt3D operator*(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x*_m, b.y*_m, b.z*_m);
}
Punkt3D operator/(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x/_m, b.y/_m, b.z/_m);
}
float abs(Punkt3D p) {
return p.abs();
}
// OpenGL Funktionen für Punkt3D
void glVertex3f(Punkt3D p) {
glVertex3f(p.x, p.y, p.z);
}
void glTranslatef(Punkt3D p) {
glTranslatef(p.x, p.y, p.z);
}
void glNormal(Punkt3D p) {
glNormal3f(p.x, p.y, p.z);
}
void glRotatef(float deg, Punkt3D vec) {
glRotatef(deg, vec.x, vec.y, vec.z);
}
// Trigonometrische Funktionen // Trigonometrische Funktionen
float deg2rad(float deg) { float deg2rad(float deg) {

61
emath.h
View File

@ -5,69 +5,14 @@
#include <cmath> #include <cmath>
#include <SDL_opengl.h> #include <SDL_opengl.h>
class Punkt3D { #include "punkt3d.h" /* aus kompatibilitätsgründen */
public:
Punkt3D();
Punkt3D(float, float, float);
float x, y, z;
void set(float, float, float);
float abs();
Punkt3D kreuzprodukt(const Punkt3D&);
void normalize();
Punkt3D getNormalized();
bool isNormalized();
float calcAngle(Punkt3D);
Punkt3D getOrtographic();
void print(std::string="");
// Operatoren
Punkt3D operator+(const Punkt3D&);
Punkt3D operator-(const Punkt3D&);
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&);
Punkt3D& operator-=(const float&);
Punkt3D& operator*=(const float&);
Punkt3D& operator/=(const float&);
float operator*(const Punkt3D&);
Punkt3D operator-();
bool operator==(const Punkt3D&);
bool operator!=(const Punkt3D&);
friend Punkt3D operator+(const float&, const Punkt3D&);
friend Punkt3D operator-(const float&, const Punkt3D&);
friend Punkt3D operator*(const float&, const Punkt3D&);
friend Punkt3D operator/(const float&, const Punkt3D&);
};
float abs(Punkt3D);
// OpenGL-Funktionen für Punkt3D
void glVertex3f(Punkt3D);
void glTranslatef(Punkt3D);
void glNormal3f(Punkt3D);
void glRotatef(float, Punkt3D);
float deg2rad(float deg); float deg2rad(float deg);
float rad2deg(float rad); float rad2deg(float rad);
// *grml* scheiss cmath-sinus // *grml* scheiss cmath-sinus
// diese beiden sinusfunktionenrechnen erst deg2rad
// wenn es sich nicht um 0, 90, 180, 270 oder ein vielfaches handelt
float ssin(float); float ssin(float);
float scos(float); float scos(float);

View File

@ -94,7 +94,8 @@ bool GLFontEngine::fontSelect(std::string fontstr) {
font = fontpool[fontstr]; font = fontpool[fontstr];
else { else {
//fallbackfont - ersten aus der liste //fallbackfont - ersten aus der liste
font = (fontpool.begin())->second; font = (++fontpool.begin())->second;
std::cout << "font: " << font << std::endl;
} }
return true; return true;
@ -106,7 +107,7 @@ void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
void GLFontEngine::renderLine(std::string text, SDL_Rect pos) { void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
// glColor4f(r, g, b, a); // glColor4f(r, g, b, a);
glMatrixMode(GL_TEXTURE); glMatrixMode(GL_TEXTURE);
glLoadIdentity(); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
@ -123,7 +124,7 @@ void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
for(unsigned int i=0; i<text.length(); i++) { for(unsigned int i=0; i<text.length(); i++) {
int ch = text[i]; int ch = text[i];
if(ch<0) { if(ch<0) {
ch += 256; // char geht wohl in machen fällen nur von -128 bis +127 ch += 256; // char geht wohl in machen fällen nur von -128 bis +127 *hust*
} }
// std::cout << (int)text[i] << " "; // std::cout << (int)text[i] << " ";
tex_x = ((ch-1)%16 / 16.0f); tex_x = ((ch-1)%16 / 16.0f);
@ -156,10 +157,11 @@ void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
} }
void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) { void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
SDL_Rect m = { x, y, getTextWidth(str), fsize}; SDL_Rect m = { x, y, getTextWidth(str), fsize};
if(center) if(center)
m.x = m.x - m.w/2; m.x = m.x - m.w/2;
renderLine(str, m); renderLine(str, m);
if(rendered_to) if(rendered_to)
@ -231,7 +233,10 @@ void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
} }
int GLFontEngine::getTextWidth(const std::string &moep) { int GLFontEngine::getTextWidth(const std::string &moep) {
return (int)(moep.length()*font->charwidth*fsize); if(fontloaded)
return (int)(moep.length()*(font->charwidth*fsize));
else
return 1;
} }
std::map<std::string, GLFont*> GLFontEngine::fontpool; std::map<std::string, GLFont*> GLFontEngine::fontpool;

0
glgui/glgui.h Normal file
View File

View File

@ -33,7 +33,8 @@ SDL_Rect GLGuiWindow::getPos() {
} }
GLGuiWindow::~GLGuiWindow() { GLGuiWindow::~GLGuiWindow() {
for(unsigned int i=0; i<items.size(); i++) { // der der allokiert soll sich gefälligst drum kümmern
delete(items[i]); // for(unsigned int i=0; i<items.size(); i++) {
} // delete(items[i]);
// }
} }

12
glmenu/Makefile Executable file
View File

@ -0,0 +1,12 @@
COMPILER = g++
OBJECTS = menumanager.o menumenu.o menuitem.o menuitems.o
glguilib: $(OBJECTS)
# rm glgui.a -f
ar crus glmenu.a $(OBJECTS)
%.o: %.cpp %.h
$(COMPILER) -c `sdl-config --cflags` $<
clean:
rm -f $(OBJECTS)

8
glmenu/glmenu.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __GLMENU_H
#include "menumanager.h"
#include "menumenu.h"
#include "menuitem.h"
#include "menuitems.h"
#endif

39
glmenu/menuitem.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "menuitem.h"
MenuItem::MenuItem(std::string c) {
caption = c;
fontsizeadd = 0;
fontname = "menufont";
usevalue = false;
fontengine.fontSelect(fontname);
}
void MenuItem::render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight) {
fontengine.setSize(basefontsize+fontsizeadd);
if(highlight)
fontengine.setColor(1.0f, 0.0f, 0.0f);
else
fontengine.setColor(1.0f, 1.0f, 1.0f);
if(!usevalue) {
fontengine.renderLine(caption, pos.x, pos.y, center);
} else {
// center und position ggf. überarbeiten..
Punkt2D tmp;
tmp = pos - valuewidth/2 - fontengine.getTextWidth(caption)/2;
fontengine.renderLine(caption, tmp.x, tmp.y, center);
tmp = pos + valuewidth/2;
fontengine.renderLine(value, tmp.x, tmp.y, false);
}
}
int MenuItem::getFontSizeAdd() {
return fontsizeadd;
}
MenuItem::~MenuItem() {
}

30
glmenu/menuitem.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __MENUITEM_H
#define __MENUITEM_H
#include <string>
#include "../glfontengine.h"
#include "../punkt2d.h"
class MenuItem {
protected:
std::string caption;
std::string value;
GLFontEngine fontengine;
std::string fontname;
int fontsizeadd;
bool usevalue;
public:
MenuItem(std::string);
virtual ~MenuItem();
virtual void left() { };
virtual void right() { };
virtual void select() { };
int getFontSizeAdd();
virtual void render(Punkt2D pos, bool center, int basefontsize, int maxwidth, int valuewidth, bool highlight);
};
#endif

9
glmenu/menuitems.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "menuitems.h"
MISendSDLEvent::MISendSDLEvent(std::string str, SDL_Event event) : MenuItem(str) {
sendevent = event;
}
void MISendSDLEvent::select() {
SDL_PushEvent(&sendevent);
}

17
glmenu/menuitems.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef __MENUITEMS_H
#define __MENUITEMS_H
#include <SDL.h>
#include "menuitem.h"
class MISendSDLEvent : public MenuItem {
private:
SDL_Event sendevent;
public:
MISendSDLEvent(std::string str, SDL_Event event);
void select();
};
#endif

54
glmenu/menumanager.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "menumanager.h"
MenuManager::MenuManager() {
aktuell = 0;
}
void MenuManager::addMenu(MenuMenu *mm) {
menus.push_back(mm);
if(menus.size()==1)
aktuell = mm;
}
bool MenuManager::changeMenu(MenuMenu *mm) {
for(unsigned int i=0; i<menus.size(); i++) {
if(menus[i]==mm) {
aktuell = mm;
return true;
}
}
return false;
}
void MenuManager::render() {
glEnable(GL_TEXTURE_2D);
GLFontEngine::prepare2DbyPushingMatrix();
if(aktuell)
aktuell->render();
GLFontEngine::regain3DbyPoppingMatrix();
}
void MenuManager::up() {
if(aktuell)
aktuell->up();
}
void MenuManager::down() {
if(aktuell)
aktuell->down();
}
void MenuManager::left() {
if(aktuell)
aktuell->left();
}
void MenuManager::right() {
if(aktuell)
aktuell->right();
}
void MenuManager::select() {
if(aktuell)
aktuell->select();
}

29
glmenu/menumanager.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef __MENUMANAGER_H
#define __MENUMANAGER_H
#include <vector>
#include "../glfontengine.h"
#include "menumenu.h"
class MenuManager {
private:
MenuMenu *aktuell;
std::vector<MenuMenu*> menus;
public:
MenuManager();
void addMenu(MenuMenu*);
bool changeMenu(MenuMenu*);
void render();
void up();
void down();
void left();
void right();
void select();
};
#endif

76
glmenu/menumenu.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "menumenu.h"
MenuMenu::MenuMenu() {
itempos = 0;
centermenu = false;
centerScreenX = false;
basefontsize = 20;
maxwidth = 0;
valuewidth = 100;
offset = 20;
}
void MenuMenu::setPos(const Punkt2D &p) {
menupos = p;
}
void MenuMenu::setCenter(bool _c) {
centermenu = _c;
}
void MenuMenu::setCenterScreenX(bool _c) {
centerScreenX = _c;
}
void MenuMenu::setBaseFontSize(int _bfs) {
if(_bfs>0)
basefontsize = _bfs;
}
void MenuMenu::setMaxWidth(int _mw) {
if(_mw>0)
maxwidth = _mw;
}
void MenuMenu::addMenuItem(MenuItem* mi) {
menuitems.push_back(mi);
}
void MenuMenu::up() {
if(itempos==0)
itempos = menuitems.size()-1;
else
itempos--;
}
void MenuMenu::down() {
if(itempos==menuitems.size()-1)
itempos = 0;
else
itempos++;
}
void MenuMenu::left() {
menuitems[itempos]->left();
}
void MenuMenu::right() {
menuitems[itempos]->right();
}
void MenuMenu::select() {
menuitems[itempos]->select();
}
void MenuMenu::render() {
Punkt2D pos = menupos;
if(centerScreenX) {
SDL_Surface *screen = SDL_GetVideoSurface();
pos.x = screen->w/2;
}
for(unsigned int i=0; i<menuitems.size(); i++) {
menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, itempos==i);
pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
}
}

41
glmenu/menumenu.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef __MENUMENU_H
#define __MENUMENU_H
#include <vector>
#include <SDL.h>
#include "../punkt2d.h"
#include "menuitem.h"
class MenuMenu {
private:
std::vector<MenuItem*> menuitems;
int itempos;
Punkt2D menupos;
bool centermenu;
bool centerScreenX;
int basefontsize;
int offset;
int maxwidth;
int valuewidth;
public:
MenuMenu();
void setPos(const Punkt2D&);
void setCenter(bool);
void setCenterScreenX(bool);
void setBaseFontSize(int);
void setMaxWidth(int);
void up();
void down();
void left();
void right();
void select();
void addMenuItem(MenuItem*);
void render();
};
#endif

View File

@ -119,3 +119,11 @@ bool GLSDLScreen::apply() {
return true; return true;
} }
int GLSDLScreen::getWidth() {
return width;
}
int GLSDLScreen::getHeight() {
return height;
}

View File

@ -34,6 +34,9 @@ class GLSDLScreen {
bool isOK(); bool isOK();
bool apply(); bool apply();
int getWidth();
int getHeight();
}; };
#endif #endif

132
punkt2d.cpp Normal file
View File

@ -0,0 +1,132 @@
#include "punkt2d.h"
Punkt2D::Punkt2D() {
x = y = 0.0f;
}
Punkt2D::Punkt2D(float _x, float _y) {
set(_x, _y);
}
void Punkt2D::set(float _x, float _y) {
x = _x;
y = _y;
}
void Punkt2D::print(std::string coordname) {
if(coordname!="")
coordname.append(" ");
std::cout << coordname << "Coord: (" << x << ", " << y << ")" << std::endl;
}
Punkt2D Punkt2D::operator+(const Punkt2D &b) {
Punkt2D c;
c.x = x + b.x;
c.y = y + b.y;
// c.z = z + b.z;
return c;
}
Punkt2D Punkt2D::operator-(const Punkt2D &b) {
Punkt2D c;
c.x = x - b.x;
c.y = y - b.y;
// c.z = z - b.z;
return c;
}
Punkt2D& Punkt2D::operator+=(const Punkt2D &b) {
x += b.x;
y += b.y;
// z += b.z;
return *this;
}
Punkt2D& Punkt2D::operator-=(const Punkt2D &b) {
x -= b.x;
y -= b.y;
// z -= b.z;
return *this;
}
Punkt2D Punkt2D::operator+(const float &_m) {
return Punkt2D(x+_m, y+_m);
}
Punkt2D Punkt2D::operator-(const float &_m) {
return Punkt2D(x-_m, y-_m);
}
Punkt2D Punkt2D::operator*(const float &_m) {
return Punkt2D(x*_m, y*_m);
}
Punkt2D Punkt2D::operator/(const float &_m) {
return Punkt2D(x/_m, y/_m);
}
Punkt2D& Punkt2D::operator+=(const float &_m) {
x += _m;
y += _m;
// z += _m;
return *this;
}
Punkt2D& Punkt2D::operator-=(const float &_m) {
x -= _m;
y -= _m;
// z -= _m;
return *this;
}
Punkt2D& Punkt2D::operator*=(const float &_m) {
x *= _m;
y *= _m;
// z *= _m;
return *this;
}
Punkt2D& Punkt2D::operator/=(const float &_m) {
x /= _m;
y /= _m;
// z /= _m;
return *this;
}
float Punkt2D::operator*(const Punkt2D& _m) {
return x * _m.x + y * _m.y;
}
Punkt2D Punkt2D::operator-() {
return Punkt2D(-x, -y);
}
bool Punkt2D::operator==(const Punkt2D& b) {
return ( x==b.x && y==b.y);
}
bool Punkt2D::operator!=(const Punkt2D& b) {
return !(*this==b);
}
// Freunde
Punkt2D operator+(const float& _m, const Punkt2D& b) {
return Punkt2D(b.x+_m, b.y+_m);
}
Punkt2D operator-(const float& _m, const Punkt2D& b) {
return Punkt2D(b.x-_m, b.y-_m);
}
Punkt2D operator*(const float& _m, const Punkt2D& b) {
return Punkt2D(b.x*_m, b.y*_m);
}
Punkt2D operator/(const float& _m, const Punkt2D& b) {
return Punkt2D(b.x/_m, b.y/_m);
}
// float abs(Punkt2D p) {
// return p.abs();
// }

49
punkt2d.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef __PUNKT3D_H
#define __PUNKT3D_H
#include <iostream>
#include <cmath>
#include "emath.h"
class Punkt2D {
public:
Punkt2D();
Punkt2D(float, float);
float x, y;
void set(float, float);
void print(std::string="");
// Operatoren
Punkt2D operator+(const Punkt2D&);
Punkt2D operator-(const Punkt2D&);
Punkt2D& operator+=(const Punkt2D&);
Punkt2D& operator-=(const Punkt2D&);
Punkt2D operator+(const float&);
Punkt2D operator-(const float&);
Punkt2D operator*(const float&);
Punkt2D operator/(const float&);
Punkt2D& operator+=(const float&);
Punkt2D& operator-=(const float&);
Punkt2D& operator*=(const float&);
Punkt2D& operator/=(const float&);
float operator*(const Punkt2D&);
Punkt2D operator-();
bool operator==(const Punkt2D&);
bool operator!=(const Punkt2D&);
friend Punkt2D operator+(const float&, const Punkt2D&);
friend Punkt2D operator-(const float&, const Punkt2D&);
friend Punkt2D operator*(const float&, const Punkt2D&);
friend Punkt2D operator/(const float&, const Punkt2D&);
};
// float abs(Punkt3D);
#endif

207
punkt3d.cpp Normal file
View File

@ -0,0 +1,207 @@
#include "punkt3d.h"
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;
}
float Punkt3D::abs() {
return sqrt(x*x+y*y+z*z);
}
Punkt3D Punkt3D::kreuzprodukt(const Punkt3D &b) {
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;
}
Punkt3D Punkt3D::getNormalized() {
Punkt3D ret(*this);
ret.normalize();
return ret;
}
bool Punkt3D::isNormalized() {
return (abs()==1);
}
float Punkt3D::calcAngle(Punkt3D b) {
if(abs()*b.abs()==0.0f)
return 0.0f;
return rad2deg(std::acos(((*this)*b)/(abs()*b.abs())));
}
Punkt3D Punkt3D::getOrtographic() {
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;
}
void Punkt3D::print(std::string coordname) {
if(coordname!="")
coordname.append(" ");
std::cout << coordname << "Coord: (" << x << ", " << y << ", " << z << ")" << std::endl;
}
Punkt3D Punkt3D::operator+(const Punkt3D &b) {
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) {
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;
}
Punkt3D Punkt3D::operator+(const float &_m) {
return Punkt3D(x+_m, y+_m, z+_m);
}
Punkt3D Punkt3D::operator-(const float &_m) {
return Punkt3D(x-_m, y-_m, z-_m);
}
Punkt3D Punkt3D::operator*(const float &_m) {
return Punkt3D(x*_m, y*_m, z*_m);
}
Punkt3D Punkt3D::operator/(const float &_m) {
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;
}
float Punkt3D::operator*(const Punkt3D& _m) {
return x * _m.x + y * _m.y + z * _m.z;
}
Punkt3D Punkt3D::operator-() {
return Punkt3D(-x, -y, -z);
}
bool Punkt3D::operator==(const Punkt3D& b) {
return ( x==b.x && y==b.y && z==b.z);
}
bool Punkt3D::operator!=(const Punkt3D& b) {
return !(*this==b);
}
// Freunde
Punkt3D operator+(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x+_m, b.y+_m, b.z+_m);
}
Punkt3D operator-(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x-_m, b.y-_m, b.z-_m);
}
Punkt3D operator*(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x*_m, b.y*_m, b.z*_m);
}
Punkt3D operator/(const float& _m, const Punkt3D& b) {
return Punkt3D(b.x/_m, b.y/_m, b.z/_m);
}
float abs(Punkt3D p) {
return p.abs();
}
// OpenGL Funktionen für Punkt3D
void glVertex3f(Punkt3D p) {
glVertex3f(p.x, p.y, p.z);
}
void glTranslatef(Punkt3D p) {
glTranslatef(p.x, p.y, p.z);
}
void glNormal(Punkt3D p) {
glNormal3f(p.x, p.y, p.z);
}
void glRotatef(float deg, Punkt3D vec) {
glRotatef(deg, vec.x, vec.y, vec.z);
}

66
punkt3d.h Normal file
View File

@ -0,0 +1,66 @@
#ifndef __PUNKT3D_h
#define __PUNKT3D_h
#include <iostream>
#include <cmath>
#include "emath.h"
class Punkt3D {
public:
Punkt3D();
Punkt3D(float, float, float);
float x, y, z;
void set(float, float, float);
float abs();
Punkt3D kreuzprodukt(const Punkt3D&);
void normalize();
Punkt3D getNormalized();
bool isNormalized();
float calcAngle(Punkt3D);
Punkt3D getOrtographic();
void print(std::string="");
// Operatoren
Punkt3D operator+(const Punkt3D&);
Punkt3D operator-(const Punkt3D&);
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&);
Punkt3D& operator-=(const float&);
Punkt3D& operator*=(const float&);
Punkt3D& operator/=(const float&);
float operator*(const Punkt3D&);
Punkt3D operator-();
bool operator==(const Punkt3D&);
bool operator!=(const Punkt3D&);
friend Punkt3D operator+(const float&, const Punkt3D&);
friend Punkt3D operator-(const float&, const Punkt3D&);
friend Punkt3D operator*(const float&, const Punkt3D&);
friend Punkt3D operator/(const float&, const Punkt3D&);
};
float abs(Punkt3D);
// OpenGL-Funktionen für Punkt3D
void glVertex3f(Punkt3D);
void glTranslatef(Punkt3D);
void glNormal3f(Punkt3D);
void glRotatef(float, Punkt3D);
#endif