Initialisierung des GIT-Repositorys

This commit is contained in:
seba 2008-02-09 13:43:23 +01:00
commit 9de0171fc5
34 changed files with 2009 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
*.a

22
Makefile Executable file
View File

@ -0,0 +1,22 @@
COMPILER = g++
OBJECTS = emath.o emath_opengl.o glcolor.o gldrawhelper.o glfontengine.o glrect.o gltexture.o matrix.o quaternion.o rotationsmatrix.o glgui/glgui.a
VERSION = 0.0.1
sgllib: $(OBJECTS)
sglar: $(OBJECTS)
rm -f sgllib.a
ar rfc sgllib.a $(OBJECTS)
%.o: %.cpp %.h
$(COMPILER) -c `sdl-config --cflags` $<
glgui/glgui.a:
cd glgui; $(MAKE);
clean:
rm -f $(OBJECTS)
cd glgui; $(MAKE) clean
@echo Done cleaning...

243
emath.cpp Normal file
View File

@ -0,0 +1,243 @@
#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
float deg2rad(float deg) {
return (deg/180.0f)*3.1415926535897932384626433f;
}
float rad2deg(float rad) {
return (rad/3.1415926535897932384626433f)*180.0f;
}
float ssin(float c) {
int t=(int)c;
if(t!=c)
return sin(deg2rad(c));
t = t % 360;
if(t<0)
t = 360 + t;
switch(t) {
case 0:
return 0.0f;
case 90:
return 1.0f;
case 180:
return 0.0f;
case 270:
return -1.0f;
default:
// std::cout << "ssin sollte keinen defaultwert zurückliefern. c=" << c << ", t=" << t << std::endl;
return sin(deg2rad(c));
}
}
float scos(float c) {
return ssin(c+90.0f);
}

74
emath.h Normal file
View File

@ -0,0 +1,74 @@
#ifndef __EMATH_H
#define __EMATH_H
#include <iostream>
#include <cmath>
#include <SDL_opengl.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);
float deg2rad(float deg);
float rad2deg(float rad);
// *grml* scheiss cmath-sinus
float ssin(float);
float scos(float);
#endif

46
emath_opengl.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "emath_opengl.h"
void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *rvec, float *rvecdeg, Punkt3D *rvec2, float *rvecdeg2) {
Punkt3D rotvec, rotvec2, nullvec;
float rotvecdeg = 0.0f, rotvecdeg2 = 0.0f;
rotvec = a.kreuzprodukt(b);
rotvecdeg = a.calcAngle(b);
if(std::abs(rotvecdeg)==180.0f) {
rotvec = a.getOrtographic();
}
if(rotvec==nullvec) {
rotvec.set(1.0f, 0.0f, 0.0f);
rotvecdeg = 0.0f;
}
Rotationsmatrix rotnobj(rotvec, -rotvecdeg);
c = Rotationsmatrix(rotvec, rotvecdeg) * c;
rotvec2 = c.kreuzprodukt(d);
rotvecdeg2 = c.calcAngle(d);
if(std::abs(rotvecdeg2)==180.0f) {
rotvec2 = b;
}
if(rotvec2==nullvec) {
rotvec2.set(1.0f, 0.0f, 0.0f);
rotvecdeg2 = 0.0f;
}
rotvec2 = rotnobj * rotvec2;
*rvec = rotvec;
*rvecdeg = rotvecdeg;
*rvec2 = rotvec2;
*rvecdeg2 = rotvecdeg2;
}
void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d) {
Punkt3D rvec, rvec2;
float rvecdeg, rvecdeg2;
rotFrom2VecTo2Vec(a, b, c, d, &rvec, &rvecdeg, &rvec2, &rvecdeg2);
glRotatef(rvecdeg, rvec);
glRotatef(rvecdeg2, rvec2);
}

11
emath_opengl.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef __EMATH_OPENGL
#define __EMATH_OPENGL
#include <cmath>
#include "emath.h"
#include "rotationsmatrix.h"
#include <SDL_opengl.h>
void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d, Punkt3D *, float *, Punkt3D *, float *);
void rotFrom2VecTo2Vec(Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d);
#endif

30
glcolor.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "glcolor.h"
GLColor::GLColor() {
set(0.0f, 0.0f, 0.0f, 1.0f);
setalpha = true;
}
GLColor::GLColor(const SDL_Color &c) {
set(c.r/255.0f, c.g/255.0f, c.b/255.0f);
setalpha = true;
}
void GLColor::set(float _r, float _g, float _b, float _a) {
r = _r;
g = _g;
b = _b;
a = _a;
}
SDL_Color GLColor::getSDLColor() {
SDL_Color c = {(Uint8)(r*255.0f), (Uint8)(g*255), (Uint8)(b*255), (Uint8)(a*255)};
}
void glColorGLC(GLColor c) {
if(c.setalpha) {
glColor4f(c.r, c.g, c.b, c.a);
} else {
glColor3f(c.r, c.g, c.b);
}
}

24
glcolor.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __GLCOLOR_H
#define __GLCOLOR_H
#include <SDL.h>
#include <SDL_opengl.h>
class GLColor {
public:
float r, g, b, a;
bool setalpha;
GLColor();
GLColor(const SDL_Color&);
void set(float _r, float _g, float _b, float _a=1.0f);
SDL_Color getSDLColor();
};
void glColorGLC(GLColor c);
// a) setalpha entscheiden lassen
// b) zwei funktionen, eine setzt alpha mit in richtung 3f, 4f..
#endif

24
gldrawhelper.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "gldrawhelper.h"
void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex) {
GLRect tmptex;
if(tex) {
tmptex = *tex;
} else {
tmptex.set(0.0f, 0.0f, 1.0f, 1.0f);
}
glBegin(GL_QUADS);
glTexCoord2f(tmptex.x, tmptex.y);
glVertex2i(rect->x, rect->y+rect->h);
glTexCoord2f(tmptex.x+tmptex.w, tmptex.y);
glVertex2i(rect->x+rect->w, rect->y+rect->h);
glTexCoord2f(tmptex.x+tmptex.w, tmptex.y+tmptex.h);
glVertex2i(rect->x+rect->w, rect->y);
glTexCoord2f(tmptex.x, tmptex.y+tmptex.h);
glVertex2i(rect->x, rect->y);
glEnd();
}

12
gldrawhelper.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __GLDRAWHELPER_H
#define __GLDRAWHELPER_H
#include <SDL.h>
#include <SDL_opengl.h>
#include "glrect.h"
void GLDrawSDLRect(SDL_Rect *rect, GLRect *tex=0);
#endif

231
glfontengine.cpp Normal file
View File

@ -0,0 +1,231 @@
#include "glfontengine.h"
bool GLFontEngine::addFont(std::string fontfile, std::string fontname) {
GLFont *tmp = new GLFont(fontfile, 0.685f);
if(!tmp->font->isLoaded()) {
delete(tmp);
std::cerr << fontfile << " konnte als Font nicht geladen werden" << std::endl;
return false;
}
fontpool[fontname] = tmp;
return false;
}
void GLFontEngine::quit() {
for(std::map<std::string, GLFont*>::iterator iter = fontpool.begin(); iter != fontpool.end(); iter++) {
delete(iter->second); // unloaded texture.. der dtor
fontpool.erase(iter);
}
}
void GLFontEngine::prepare2DbyPushingMatrix() {
const SDL_Surface *screen = SDL_GetVideoSurface();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, screen->w, screen->h, 0, -1, 1);
// glOrtho(0, 640, 480, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
}
void GLFontEngine::regain3DbyPoppingMatrix() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void GLFontEngine::paintSDLRect(SDL_Rect r) {
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(r.x, r.y);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(r.x, r.y+r.h);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(r.x+r.w, r.y+r.h);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(r.x+r.w, r.y);
glEnd();
}
GLFontEngine::GLFontEngine() {
init();
fontloaded = fontSelect("");
}
GLFontEngine::GLFontEngine(std::string fontstr) {
init();
fontloaded = fontSelect(fontstr);
}
void GLFontEngine::init() {
// r = g = b = a = 1.0f;
fsize = 16;
}
void GLFontEngine::setColor(float _r, float _g, float _b, float _a) {
// r = _r;
// g = _g;
// b = _b;
// a = _a;
col.set(_r, _g, _b, _a);
}
void GLFontEngine::setColor(GLColor c) {
col = c;
}
bool GLFontEngine::fontSelect(std::string fontstr) {
if(fontpool.size()==0) {
fontloaded = false;
return false;
}
fontloaded = true; // fontstr oder fallbackfont
if(fontpool[fontstr])
font = fontpool[fontstr];
else {
//fallbackfont - ersten aus der liste
font = (fontpool.begin())->second;
}
return true;
}
void GLFontEngine::renderText(std::string text, SDL_Rect pos) {
renderLine(text, pos);
}
void GLFontEngine::renderLine(std::string text, SDL_Rect pos) {
// glColor4f(r, g, b, a);
glColorGLC(col);
if(fontloaded) {
font->font->selectTexture();
float step = pos.w / (float)text.length();
float tex_x, tex_y;
float tex_len = (1.0f/16.0f);
for(unsigned int i=0; i<text.length(); i++) {
int ch = text[i];
if(ch<0) {
ch += 256; // char geht wohl in machen fällen nur von -128 bis +127
}
// std::cout << (int)text[i] << " ";
tex_x = ((ch-1)%16 / 16.0f);
tex_y = 1.0f-(floor((ch-1)/16) / 16.0f)-tex_len;
// std::cout << text[i] << " == " << tex_x << ", " << tex_y << std::endl;
glBegin(GL_QUADS);
glTexCoord2f(tex_x, tex_y+tex_len);
glVertex2f(pos.x+i*step, pos.y);
glTexCoord2f(tex_x, tex_y);
glVertex2f(pos.x+i*step, pos.y+pos.h);
glTexCoord2f(tex_x+tex_len, tex_y);
glVertex2f(pos.x+(i+1)*step, pos.y+pos.h);
glTexCoord2f(tex_x+tex_len, tex_y+tex_len);
glVertex2f(pos.x+(i+1)*step, pos.y);
glEnd();
}
// std::cout << std::endl;
} else {
glBegin(GL_QUADS);
glVertex2f(pos.x, pos.y);
glVertex2f(pos.x+pos.w, pos.y);
glVertex2f(pos.x+pos.w, pos.y+pos.h);
glVertex2f(pos.x, pos.y+pos.h);
glEnd();
}
}
void GLFontEngine::renderLine(std::string str, int x, int y, bool center, SDL_Rect *rendered_to) {
SDL_Rect m = { x, y, getTextWidth(str), fsize};
if(center)
m.x = m.x - m.w/2;
renderLine(str, m);
if(rendered_to)
*rendered_to = m;
return;
}
void GLFontEngine::renderLines(std::string str, int x, int y, bool center, SDL_Rect *rendered_to, int wrap, int paintbackground) {
if(wrap) {
// \n einfügen, wenns zu groß ist
for(unsigned int i=0, a=0; i<str.length(); i++, a++) {
if(str[i]!='\n') {
if(a*font->charwidth*fsize>wrap) {
str.insert(i, "\n");
}
} else {
a=0;
}
}
}
if(paintbackground) {
std::cout << "Paint Background implementieren.. ;) " << std::endl;
}
SDL_Rect m;
int strlpos;
int max_width = 0;
int linecount = 0;
std::string rstr;
while(str!="") {
if((strlpos = str.find('\n'))!=std::string::npos) {
rstr = str.substr(0, strlpos);
str = str.substr(strlpos+1);
} else {
rstr = str;
str = "";
}
renderLine(rstr, x, y+(int)(1.1*linecount*fsize), center, &m);
max_width = std::max(max_width, (int)m.w);
linecount++;
}
if(rendered_to) {
m.w = max_width;
m.y = y;
*rendered_to = m;
}
return;
}
void GLFontEngine::setSize(int s) {
fsize = s;
}
int GLFontEngine::getSize() {
return fsize;
}
void GLFontEngine::getSDLRect(const std::string &str, SDL_Rect *r) {
r->w = getTextWidth(str);
r->h = getSize();
}
int GLFontEngine::getTextWidth(const std::string &moep) {
return (int)(moep.length()*font->charwidth*fsize);
}
std::map<std::string, GLFont*> GLFontEngine::fontpool;

70
glfontengine.h Normal file
View File

@ -0,0 +1,70 @@
#ifndef __GLFONTENGINE_H
#define __GLFONTENGINE_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <SDL.h>
#include <SDL_opengl.h>
#include "gltexture.h"
#include "glcolor.h"
class GLFont {
public:
GLFont(std::string fontfile, float cwdth) {
charwidth = cwdth;
font = new GLTexture(fontfile);
}
~GLFont() {
delete(font);
}
GLTexture* font;
float charwidth;
};
class GLFontEngine {
private:
static std::map<std::string, GLFont*> fontpool;
GLFont *font;
bool fontloaded;
GLColor col;
// float r, g, b, a;
int fsize;
void init();
public:
static bool addFont(std::string fontfile, std::string fontname);
static void quit();
static void prepare2DbyPushingMatrix();
static void regain3DbyPoppingMatrix();
static void paintSDLRect(SDL_Rect);
GLFontEngine();
GLFontEngine(std::string);
bool fontSelect(std::string);
void setColor(float, float, float, float=1.0f);
void setColor(GLColor);
void renderText(std::string, SDL_Rect); // wrapper
// void renderText(std::string, int x, int y, *SDL_Rect=0, bool center=false);
// void renderLines(std::string, int x, int y,
void renderLine(std::string, SDL_Rect);
void renderLine(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0);
void renderLines(std::string, int x, int y, bool center=false, SDL_Rect *rendered_to=0, int wrap=0, int paintbackground=0);
void setSize(int);
int getSize();
void getSDLRect(const std::string&, SDL_Rect*);
int getTextWidth(const std::string&);
};
#endif

12
glgui/Makefile Executable file
View File

@ -0,0 +1,12 @@
# COMPILER = g++
OBJECTS = button.o object.o textlabel.o window.o
glguilib: $(OBJECTS)
rm glgui.a -f
ar rfc glgui.a $(OBJECTS)
%.o: %.cpp %.h
$(COMPILER) -c `sdl-config --cflags` $<
clean:
rm -f $(OBJECTS)

18
glgui/button.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "button.h"
GLGuiButton::GLGuiButton(int _eventid, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(_x, _y, _center, _wrap) {
highlightable = true;
clickable = true;
eventid = _eventid;
highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f);
}
GLGuiButton::GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center, int _wrap) : GLGuiTextLabel(str, _x, _y, _center, _wrap){
highlightable = true;
clickable = true;
eventid = _eventid;
highlightcol.set(1.0f, 0.0f, 0.0f, 1.0f);
}
void GLGuiButton::onMouseOver(int m_x, int m_y) {
// Längste stelle finden, dann collision
}

28
glgui/button.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __GLGUIBUTTON_H
#define __GLGUIBUTTON_H
#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "textlabel.h"
#define GLGUI_BUTTONDOWN SDL_USEREVENT+10
class GLGuiButton : public GLGuiTextLabel {
protected:
int eventid;
GLColor highlightcol;
public:
GLGuiButton(int _eventid, int _x, int _y, bool _center=false, int _wrap=0);
GLGuiButton(int _eventid, std::string str, int _x, int _y, bool _center=false, int _wrap=0);
void setHighlightColor();
void onMouseOver(int m_x, int m_y);
void onMouseClick(int m_x, int m_y);
};
#endif

7
glgui/object.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "object.h"
GLGuiObject::GLGuiObject() {
highlightable = false;
clickable = false;
keyboardable = false;
}

29
glgui/object.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef __GLGUIOBJECT_H
#define __GLGUIOBJECT_H
#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
class GLGuiObject {
protected:
SDL_Rect pos;
bool highlightable, clickable, keyboardable;
public:
GLGuiObject();
virtual ~GLGuiObject() { };
virtual void render()=0;
// Optionen
bool isHighlightable() { return highlightable; };
bool isClickable() { return clickable; };
bool isKeyboardable() { return keyboardable; };
virtual void onMouseOver(int m_x, int m_y) { };
virtual void onMouseClick(int m_x, int m_y, int m_button) { };
virtual void onKeyboardInput(char c) { };
};
#endif

37
glgui/textlabel.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "textlabel.h"
GLGuiTextLabel::GLGuiTextLabel(int _x, int _y, bool _center, int _wrap) {
setPos(_x, _y);
center = _center;
wrap = _wrap;
col.set(1.0f, 1.0f, 1.0f, 1.0f);
}
GLGuiTextLabel::GLGuiTextLabel(std::string str, int _x, int _y, bool _center, int _wrap) {
setPos(_x, _y);
center = _center;
wrap = _wrap;
setText(str);
col.set(1.0f, 1.0f, 1.0f, 1.0f);
}
void GLGuiTextLabel::setText(std::string str) {
text = str;
}
void GLGuiTextLabel::setColor(GLColor c) {
col = c;
}
void GLGuiTextLabel::setPos(int _x, int _y) {
x = _x;
y = _y;
}
void GLGuiTextLabel::render() {
glColorGLC(col);
GLFontEngine::prepare2DbyPushingMatrix();
fontengine.renderLines(text, x, y, center, 0, wrap, 0);
GLFontEngine::regain3DbyPoppingMatrix();
}

32
glgui/textlabel.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef __GLGUITEXTLABEL_H
#define __GLGUITEXTLABEL_H
#include <iostream>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "object.h"
#include "../glcolor.h"
#include "../glfontengine.h"
class GLGuiTextLabel : public GLGuiObject{
protected:
GLFontEngine fontengine;
std::string text;
GLColor col;
int x, y;
bool center;
int wrap;
public:
GLGuiTextLabel(int _x, int _y, bool _center=false, int _wrap=0);
GLGuiTextLabel(std::string, int _x, int _y, bool _center=false, int _wrap=0);
void setText(std::string str);
void setColor(GLColor);
void setPos(int _x, int _y);
virtual void render();
};
#endif

39
glgui/window.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "window.h"
GLGuiWindow::GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor) {
pos = _pos;
bgcolor = _bgcolor;
}
void GLGuiWindow::addItem(GLGuiObject *gobj) {
// Objekt wird in delete gelöscht
items.push_back(gobj);
if(gobj->isHighlightable())
highlightable.push_back(gobj);
if(gobj->isClickable())
clickable.push_back(gobj);
if(gobj->isKeyboardable())
keyboardable.push_back(gobj);
}
void GLGuiWindow::render() {
if(renderbackground) {
glColorGLC(bgcolor);
GLDrawSDLRect(&pos);
}
for(unsigned int i=0; i<items.size(); i++) {
items[i]->render();
}
}
SDL_Rect GLGuiWindow::getPos() {
return pos;
}
GLGuiWindow::~GLGuiWindow() {
for(unsigned int i=0; i<items.size(); i++) {
delete(items[i]);
}
}

36
glgui/window.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef __GLGUIWINDOW_H
#define __GLGUIWINDOW_H
#include <iostream>
#include <string>
#include <vector>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "../glcolor.h"
#include "../gldrawhelper.h"
#include "object.h"
class GLGuiWindow : public GLGuiObject {
private:
bool renderbackground;
SDL_Rect pos;
GLColor bgcolor;
std::vector<GLGuiObject*> items;
std::vector<GLGuiObject*> highlightable;
std::vector<GLGuiObject*> clickable;
std::vector<GLGuiObject*> keyboardable;
public:
GLGuiWindow(SDL_Rect _pos, GLColor _bgcolor);
~GLGuiWindow();
void addItem(GLGuiObject*);
SDL_Rect getPos();
void render();
};
#endif

35
glrect.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "glrect.h"
GLRect::GLRect() {
set(0.0f, 0.0f, 0.0f, 0.0f);
}
GLRect::GLRect(float _x, float _y, float _w, float _h) {
set(_x, _y, _w, _h);
}
GLRect::GLRect(const SDL_Rect &r) {
set(r.x, r.y, r.w, r.h);
}
void GLRect::set(float _x, float _y, float _w, float _h) {
x = _x;
y = _y;
w = _w;
h = _h;
}
void GLRect::setPoint(float _x, float _y) {
x = _x;
y = _y;
}
void GLRect::setDim(float _w, float _h) {
w = _w;
h = _h;
}
SDL_Rect GLRect::getSDLRect() {
SDL_Rect tmp = {(Uint8)x, (Uint8)y, (Uint8)w, (Uint8)h};
return tmp;
}

22
glrect.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __GLRECT_H
#define __GLRECT_H
#include <SDL.h>
class GLRect {
public:
float x, y;
float w, h;
GLRect();
GLRect(float, float, float, float);
GLRect(const SDL_Rect&);
void set(float, float, float, float);
void setPoint(float, float);
void setDim(float, float);
SDL_Rect getSDLRect();
};
#endif

172
gltexture.cpp Normal file
View File

@ -0,0 +1,172 @@
#include "gltexture.h"
GLTexture::GLTexture() {
init();
alltextures.push_back(this);
}
GLTexture::GLTexture(std::string fname, GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
init();
setParameter(_minfilter, _magfilter, _wraps, _wrapt);
loadImage(fname);
alltextures.push_back(this);
}
GLTexture::GLTexture(SDL_Surface *srfc) {
init();
loadSurface(srfc);
}
void GLTexture::init() {
loaded = false;
texconverted = false;
keepsurface = false;
donotreload = false;
minfilter = GL_LINEAR;
magfilter = GL_LINEAR;
wraps = GL_REPEAT;
wrapt = GL_REPEAT;
tex = 0;
width = height = 0;
}
bool GLTexture::loadLocalSurface() {
if(!texconverted)
convertLocalSurface();
width = tex->w;
height = tex->h;
glGenTextures(1, &texint);
glBindTexture(GL_TEXTURE_2D, texint);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wraps);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapt);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, width, height, GL_BGRA, GL_UNSIGNED_BYTE, tex->pixels);
loaded = true;
return true;
}
void GLTexture::convertLocalSurface() {
tex = SDL_DisplayFormatAlpha(tex);
mirrorSurfaceMiddleX(tex);
texconverted = true;
}
bool GLTexture::loadImage(std::string fname) {
if(isLoaded())
unloadTexture();
filename = fname;
tex = IMG_Load(filename.c_str());
if(!tex) {
std::cerr << "Textur konnte nicht geladen werden: " << filename << std::endl;
return false;
}
bool ret = loadLocalSurface();
keepsurface = false;
SDL_FreeSurface(tex);
tex = 0;
return ret;
}
bool GLTexture::loadSurface(SDL_Surface *srfc, bool noreloading) {
if(isLoaded())
unloadTexture();
if(filename!="")
filename = "";
tex = srfc;
keepsurface = true;
donotreload = noreloading;
return loadLocalSurface();
}
bool GLTexture::selectTexture() {
if(!loaded) {
std::cerr << "Textur wurde nicht geladen: " << filename << std::endl;
return false;
}
glBindTexture(GL_TEXTURE_2D, texint);
return true;
}
void GLTexture::unloadTexture() {
glDeleteTextures(1, &texint);
loaded = false;
// Entweder tex == image, wurde schon gelöscht oder wir müssen es nich löschen
}
bool GLTexture::setParameter(GLint _minfilter, GLint _magfilter, GLint _wraps, GLint _wrapt) {
if(_minfilter!=0)
minfilter = _minfilter;
if(_magfilter!=0)
magfilter = _magfilter;
if(_wraps!=0)
wraps = _wraps;
if(_wrapt!=0)
wrapt = _wrapt;
if(!loaded)
return true;
if(keepsurface&&tex!=0) {
unloadTexture();
return loadLocalSurface();
} else if(filename!="") {
return loadImage("filename");
} else {
// keine datei, kein surface
unloadTexture();
std::cerr << "Couldn't reload GLTexture " << this << "- No surface, no file!" << std::endl;
return false;
}
}
int GLTexture::getW() {
return width;
}
int GLTexture::getH() {
return height;
}
bool GLTexture::isLoaded() {
return loaded;
}
void GLTexture::reloadAll() {
for(unsigned int i=0; i<alltextures.size(); i++)
alltextures[i]->setParameter();
}
GLTexture::~GLTexture() {
if(isLoaded())
unloadTexture();
for(unsigned int i=0; i<alltextures.size(); i++) {
if(alltextures[i]==this) {
alltextures.erase(alltextures.begin()+i);
}
}
}
std::vector<GLTexture*> GLTexture::alltextures;

63
gltexture.h Normal file
View File

@ -0,0 +1,63 @@
#ifndef __GLTEXTUR_H
#define __GLTEXTUR_H
#include <iostream>
#include <string>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_opengl.h>
#include "sdlfuncs.h"
class GLTexture {
private:
GLuint texint;
SDL_Surface *tex;
int width, height;
std::string filename; // wenn von datei geladen
// Parameter
GLint minfilter;
GLint magfilter;
GLint wraps;
GLint wrapt;
bool loaded;
bool texconverted;
bool keepsurface;
bool donotreload;
void init();
bool loadLocalSurface();
void convertLocalSurface();
static std::vector<GLTexture*> alltextures;
protected:
GLTexture(SDL_Surface*);
bool loadSurface(SDL_Surface*, bool = false);
public:
static void reloadAll();
GLTexture();
GLTexture(std::string, GLint=GL_LINEAR, GLint=GL_LINEAR, GLint=GL_REPEAT, GLint=GL_REPEAT);
~GLTexture();
//load
bool loadImage(std::string);
bool selectTexture();
void unloadTexture();
bool setParameter(GLint=0, GLint=0, GLint=0, GLint=0);
inline bool select() { return selectTexture(); }
int getW();
int getH();
bool isLoaded();
};
#endif

177
main.cpp Normal file
View File

@ -0,0 +1,177 @@
#define VERSION "0.2.2 DEV"
#include <iostream>
#include <string>
#include <SDL.h>
#include <SDL_opengl.h>
#include "gltexture.h"
#include "glfontengine.h"
// #define GL_DEBUG_MOVEMENT false
void setupGL();
void resizeOpenGLWindow(int, int, float=1.0f, float=100.0f);
int main(int argc, char **argv) {
std::cout << "ARGV: " << argv[0] << std::endl;
//Vars
SDL_Surface *screen;
bool quit = false;
SDL_Event event;
int w_width = 640;
int w_height = 480;
int w_bbp = 32;
float znear = 1.0f;
float zfar = 100.0f;
if(SDL_Init(SDL_INIT_VIDEO)<0) {
std::cerr << "Konnte SDL nicht initialisieren" << SDL_GetError() << std::endl;
return 1;
}
// Setting video mode
int videoFlags;
const SDL_VideoInfo *videoInfo;
videoInfo = SDL_GetVideoInfo();
if(!videoInfo) {
std::cerr << "Video query failed: " << SDL_GetError() << std::endl;
return 1;
}
videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;
if(videoInfo->hw_available)
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;
if(videoInfo->blit_hw)
videoFlags |= SDL_HWACCEL;
// videoFlags |=SDL_FULLSCREEN;
// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if(!(screen = SDL_SetVideoMode(w_width, w_height, w_bbp, videoFlags))) {
std::cerr << "Kein SDL-Screen verfügbar: " << SDL_GetError() << std::endl;
return 1;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_WM_SetCaption("GLGui Test, 0);
// blank screen
// SDL_FillRect(screen, 0, SDL_MapRGBA(screen->format, 0, 0, 0, 0));
// GL Optionen
setupGL();
resizeOpenGLWindow(w_width, w_height, znear, zfar);
// Keyrepeat, falls benötigt auskommentieren
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
// Fontengine initliaisieren, für alle die Fonts brauchen
GLFontEngine::addFont("font_big_better_alpha.png", "default");
// Programmvariablen
Snake3D snake3d;
//movement & keyboardcontrol
Uint8 *keys = SDL_GetKeyState(NULL);;
SDLKey tkey;
typedef enum { move_up=0, move_down, move_left, move_right} nummovekeys;
SDLKey movekeys[4];
bool pause = true;
bool blend = false;
bool extralines = false;
bool fullscreen = false;
// splash screen
bool splashscreen = true;
snake3d.pause(true);
SDL_Rect splashpos = { 40, 40, screen->w -80, 310 };
GLFontEngine fontengine("default");
#ifdef GL_DEBUG_MOVEMENT
movekeys[move_up] = SDLK_i;
movekeys[move_down] = SDLK_k;
movekeys[move_left] = SDLK_j;
movekeys[move_right] = SDLK_l;
#else
movekeys[move_up] = SDLK_UP;
movekeys[move_down] = SDLK_DOWN;
movekeys[move_left] = SDLK_LEFT;
movekeys[move_right] = SDLK_RIGHT;
#endif
while(!quit) {
if(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
keys = SDL_GetKeyState(NULL);
tkey = event.key.keysym.sym;
break;
case SDL_KEYUP:
keys = SDL_GetKeyState(NULL);
break;
}
} else {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
SDL_Delay(10);
}
}
GLFontEngine::quit();
SDL_Quit();
return 0;
}
void setupGL() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
// glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
// glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);
}
void resizeOpenGLWindow(int width, int height, float znear, float zfar) {
if(height==0)
height = 1;
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

149
matrix.cpp Normal file
View File

@ -0,0 +1,149 @@
#include "matrix.h"
Matrix::Matrix(int _m, int _n) {
m = _m;
n = _n;
allocate();
}
Matrix::Matrix(int _m, int _n, const float **src) {
m = _m;
n = _n;
allocate();
copyFromSource(src);
}
Matrix::Matrix(Punkt3D d) {
m = 3;
n = 1;
allocate();
c[0][0] = d.x;
c[1][0] = d.y;
c[2][0] = d.z;
}
Matrix::Matrix(const Matrix& mat) {
m = mat.m;
n = mat.n;
allocate();
copyFromSource(mat.getMatrix());
}
void Matrix::allocate() {
c = new float*[m];
for(int i=0; i<m; i++) {
c[i] = new float[n];
for(int t=0; t<n; t++)
c[i][t] = 0;
}
}
void Matrix::copyFromSource(const float** src) {
for(int i=0; i<m; i++) {
for(int t=0; t<n; t++)
c[i][t] = src[i][t];
}
}
const float **Matrix::getMatrix() const {
return (const float**)c;
}
bool Matrix::set(float d, int _m, int _n) {
if(_m>=m||_n>=n)
return false;
c[_m][_n] = d;
return true;
}
Matrix Matrix::operator*(const Matrix &d) {
if(n!=d.m)
return Matrix(1,1);
Matrix erg(m, d.n);
for(int a=0; a<m; a++) {
for(int b=0; b<d.n; b++) {
float zerg = 0.0f;
for(int _c=0; _c<n; _c++) {
zerg += c[a][_c] * d.c[_c][b];
}
erg.c[a][b] = zerg;
}
}
return erg;
}
Matrix& Matrix::operator=(const Matrix& mat) {
if(mat.m<=m&&mat.n<=n) {
copyFromSource(mat.getMatrix());
}
return *this;
}
// Matrix Matrix::operator+(const float &f) {
// Matrix tmp(*this);
// for(int i=0; i<m; i++) {
// for(int j=0; j<n; j++) {
// tmp.c[i][j] += f;
// }
// }
// return tmp;
// }
//
// Matrix Matrix::operator-(const float &f) {
// Matrix tmp(*this);
// for(int i=0; i<m; i++) {
// for(int j=0; j<n; j++) {
// tmp.c[i][j] -= f;
// }
// }
// return tmp;
// }
//
// Matrix Matrix::operator*(const float &f) {
// Matrix tmp(*this);
// for(int i=0; i<m; i++) {
// for(int j=0; j<n; j++) {
// tmp.c[i][j] *= f;
// }
// }
// return tmp;
// }
//
// Matrix Matrix::operator/(const float &f) {
// Matrix tmp(*this);
// for(int i=0; i<m; i++) {
// for(int j=0; j<n; j++) {
// tmp.c[i][j] /= f;
// }
// }
// return tmp;
// }
int Matrix::getM() const {
return m;
}
int Matrix::getN() const {
return n;
}
void Matrix::print(std::string s) const {
std::cout << "Matrix " << s << "(" << m << "," << n << ")" << std::endl;
for(int a=0; a<m; a++) {
for(int b=0; b<n; b++) {
std::cout << std::setw(10) << c[a][b];
}
std::cout << std::endl;
}
}
Matrix::~Matrix() {
for(int i=0; i<m; i++) {
delete[](c[i]);
}
delete[](c);
}

42
matrix.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef __MATRIX_H
#define __MATRIX_H
#include <iostream>
#include <iomanip>
#include "emath.h"
class Matrix {
protected:
float **c;
int m, n;
void allocate();
void copyFromSource(const float**);
public:
Matrix(int, int);
Matrix(int, int, const float**);
Matrix(Punkt3D);
Matrix(const Matrix&);
~Matrix();
const float **getMatrix() const;
bool set(float, int, int);
int getM() const;
int getN() const;
Matrix operator*(const Matrix&);
Matrix& operator=(const Matrix&);
// Matrix operator+(const float&);
// Matrix operator-(const float&);
// Matrix operator*(const float&);
// Matrix operator/(const float&);
void print(std::string="") const;
};
#endif

77
quaternion.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "quaternion.h"
Quaternion::Quaternion() {
w = 1.0f;
x = y = z = 0.0f;
}
void Quaternion::createFromRotation(float deg, float _x, float _y, float _z) {
float tmpres = sin(deg2rad(deg) / 2.0f);
w = cos(deg2rad(deg) / 2.0f);
x = _x * tmpres;
y = _y * tmpres;
z = _z * tmpres;
}
Quaternion Quaternion::operator*(const Quaternion &q) {
Quaternion ret;
ret.w = w*q.w - x*q.x - y*q.y - z*q.z;
ret.x = w*q.x + x*q.w + y*q.z - z*q.y;
ret.y = w*q.y + y*q.w + z*q.x - x*q.z;
ret.z = w*q.z + z*q.w + x*q.y - y*q.x;
return ret;
}
void Quaternion::createGlMatrix(GLfloat *matrix) {
if(!matrix)
return;
matrix[0] = 1.0f - 2.0f * ( y*y + z*z);
matrix[1] = 2.0f * ( x*y + z*w);
matrix[2] = 2.0f * ( x*z - y*w);
matrix[3] = 0.0f;
matrix[4] = 2.0f * ( x*y - z*w);
matrix[5] = 1.0f - 2.0f * ( x*x + z*z);
matrix[6] = 2.0f * ( z*y + x*w);
matrix[7] = 0.0f;
matrix[8] = 2.0f * ( x*z + y*w);
matrix[9] = 2.0f * ( y*z - x*w);
matrix[10] = 1.0f - 2.0f * ( x*x + y*y);
matrix[11] = 0.0f;
matrix[12] = 0.0f;
matrix[13] = 0.0f;
matrix[14] = 0.0f;
matrix[15] = 1.0f;
}
Punkt3D Quaternion::getDirectionVector() {
GLfloat matrix[16];
createGlMatrix(matrix);
return getDirectionVector(matrix);
}
Punkt3D Quaternion::getDirectionVector(GLfloat *matrix) {
if(!matrix)
return Punkt3D();
Punkt3D tmp;
tmp.x = matrix[8];
tmp.y = matrix[9];
tmp.z = matrix[10];
return tmp;
}
void glMultMatrixf(Quaternion q) {
GLfloat matrix[16];
q.createGlMatrix(matrix);
glMultMatrixf(matrix);
}

30
quaternion.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __QUATERNION_H
#define __QUATERNION_H
#include <iostream>
#include <cmath>
#include <SDL_opengl.h>
#include "emath.h"
class Quaternion {
private:
float w;
float x,y,z;
public:
Quaternion();
void createFromRotation(float, float, float, float);
void createGlMatrix(GLfloat*);
Punkt3D getDirectionVector();
Punkt3D getDirectionVector(GLfloat*);
Quaternion operator*(const Quaternion&);
};
void glMultMatrixf(Quaternion);
#endif

91
rotationsmatrix.cpp Normal file
View File

@ -0,0 +1,91 @@
#include "rotationsmatrix.h"
Rotationsmatrix::Rotationsmatrix() : Matrix(3,3) {
set(x_axis, 0.0f);
}
Rotationsmatrix::Rotationsmatrix(axis _ax, float _deg) : Matrix(3,3) {
set(_ax, _deg);
}
Rotationsmatrix::Rotationsmatrix(Punkt3D _rotvec, float _deg) : Matrix(3,3) {
set(_rotvec, _deg);
}
void Rotationsmatrix::set(Punkt3D _rotvec, float _deg) {
rotvec = _rotvec;
rotvec.normalize();
deg = _deg;
initRot();
}
void Rotationsmatrix::set(axis _ax, float _deg) {
deg = _deg;
switch(_ax) {
case x_axis:
rotvec.set(1.0f, 0.0f, 0.0f);
break;
case y_axis:
rotvec.set(0.0f, 1.0f, 0.0f);
break;
case z_axis:
rotvec.set(0.0f, 0.0f, 1.0f);
break;
}
initRot();
}
void Rotationsmatrix::set(float _deg) {
deg = _deg;
initRot();
}
void Rotationsmatrix::initRot() {
c[0][0] = scos(deg) + pow(rotvec.x,2.0f) * (1 - scos(deg));
c[0][1] = rotvec.x * rotvec.y * (1 - scos(deg)) - rotvec.z * ssin(deg);
c[0][2] = rotvec.x * rotvec.z * (1 - scos(deg)) + rotvec.y * ssin(deg);
c[1][0] = rotvec.y * rotvec.x * (1 - scos(deg)) + rotvec.z * ssin(deg);
c[1][1] = scos(deg) + pow(rotvec.y,2.0f) * (1 - scos(deg));
c[1][2] = rotvec.y * rotvec.z * (1 - scos(deg)) - rotvec.x * ssin(deg);
c[2][0] = rotvec.z * rotvec.x * (1 - scos(deg)) - rotvec.y * ssin(deg);
c[2][1] = rotvec.z * rotvec.y * (1 - scos(deg)) + rotvec.x * ssin(deg);
c[2][2] = scos(deg) + pow(rotvec.z,2.0f) * (1 - scos(deg));
}
Punkt3D Rotationsmatrix::operator*(const Punkt3D &p) {
Matrix erg(3,1);
Punkt3D erg2;
erg = (((Matrix*)(this))->operator*(Matrix(p)));
const float **mat = erg.getMatrix();
erg2.set(mat[0][0], mat[1][0], mat[2][0]);
return erg2;
}
Rotationsmatrix Rotationsmatrix::operator*(const Rotationsmatrix &p) {
Matrix m(3, 3);
Rotationsmatrix rotmat;
m = ((Matrix)(*this) * (Matrix)p);
const float **mat = m.getMatrix();
rotmat.c[0][0] = mat[0][0];
rotmat.c[0][1] = mat[0][1];
rotmat.c[0][2] = mat[0][2];
rotmat.c[1][0] = mat[1][0];
rotmat.c[1][1] = mat[1][1];
rotmat.c[1][2] = mat[1][2];
rotmat.c[2][0] = mat[2][0];
rotmat.c[2][1] = mat[2][1];
rotmat.c[2][2] = mat[2][2];
return rotmat;
}
Rotationsmatrix::~Rotationsmatrix() {
}

30
rotationsmatrix.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __ROTATIONSMATRIX_H
#define __ROTATIONSMATRIX_H
#include <cmath>
#include "emath.h"
#include "matrix.h"
enum axis { x_axis=0, y_axis, z_axis };
class Rotationsmatrix : public Matrix {
private:
float deg;
Punkt3D rotvec;
void initRot();
public:
Rotationsmatrix();
Rotationsmatrix(axis, float);
Rotationsmatrix(Punkt3D, float);
~Rotationsmatrix();
void set(float);
void set(axis, float);
void set(Punkt3D, float);
Punkt3D operator*(const Punkt3D&);
Rotationsmatrix operator*(const Rotationsmatrix&);
};
#endif

82
sdlfuncs.cpp Normal file
View File

@ -0,0 +1,82 @@
#include "sdlfuncs.h"
Uint32 getPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) {
Uint32 a,b;
a = getPixel(surface, x1, y1);
b = getPixel(surface, x2, y2);
setPixel(surface, x1, y1, b);
setPixel(surface, x2, y2, a);
}
void mirrorSurfaceMiddleX(SDL_Surface *surface) {
SDL_LockSurface(surface);
int upto = surface->h / 2;
for(int i=0; i<upto; i++) {
for(int x=0; x<surface->w; x++) {
swapPixel(surface, x, i, x, surface->h-1-i);
}
}
SDL_UnlockSurface(surface);
}

12
sdlfuncs.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __SDLFUNCS_H
#define __SDLFUNCS_H
#include <iostream>
#include <SDL.h>
Uint32 getPixel(SDL_Surface *surface, int x, int y);
void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel);
void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2);
void mirrorSurfaceMiddleX(SDL_Surface *surface);
#endif