parent
fc21a5ca38
commit
026961a7a6
@ -0,0 +1,101 @@
|
|||||||
|
#include "catmullromspline.h"
|
||||||
|
|
||||||
|
CatmullRomSpline::CatmullRomSpline(float _s) {
|
||||||
|
cr = new Matrix(4, 4);
|
||||||
|
setS(_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CatmullRomSpline::makeMatrix() {
|
||||||
|
cr->set(-s , 0, 0);
|
||||||
|
cr->set( 2-s , 0, 1);
|
||||||
|
cr->set( s-2 , 0, 2);
|
||||||
|
cr->set( s , 0, 3);
|
||||||
|
|
||||||
|
cr->set( 2*s , 1, 0);
|
||||||
|
cr->set( s-3 , 1, 1);
|
||||||
|
cr->set( 3-2*s, 1, 2);
|
||||||
|
cr->set(-s , 1, 3);
|
||||||
|
|
||||||
|
cr->set(-s, 2, 0);
|
||||||
|
cr->set( 0, 2, 1);
|
||||||
|
cr->set( s, 2, 2);
|
||||||
|
cr->set( 0, 2, 3);
|
||||||
|
|
||||||
|
cr->set( 0, 3, 0);
|
||||||
|
cr->set( 1, 3, 1);
|
||||||
|
cr->set( 0, 3, 2);
|
||||||
|
cr->set( 0, 3, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix CatmullRomSpline::getPosition(float u, Matrix &controlpoints) {
|
||||||
|
// Matrix controlpoints(4, a.getN());
|
||||||
|
// for(int i=0; i<a.getN(); i++) {
|
||||||
|
// controlpoints.set(a.get(0, i), 0, i);
|
||||||
|
// controlpoints.set(b.get(0, i), 1, i);
|
||||||
|
// controlpoints.set(c.get(0, i), 2, i);
|
||||||
|
// controlpoints.set(d.get(0, i), 3, i);
|
||||||
|
// }
|
||||||
|
Matrix param(1, 4);
|
||||||
|
param.set(u*u*u, 0, 0);
|
||||||
|
param.set(u*u, 0, 1);
|
||||||
|
param.set(u, 0, 2);
|
||||||
|
param.set(1, 0, 3);
|
||||||
|
|
||||||
|
return param * (*cr) * controlpoints;
|
||||||
|
}
|
||||||
|
|
||||||
|
Punkt3D CatmullRomSpline::getPosition(float u, Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d) {
|
||||||
|
Matrix erg(1, 3);
|
||||||
|
Matrix controlpoints(4, 3);
|
||||||
|
|
||||||
|
controlpoints.set(a.x, 0, 0);
|
||||||
|
controlpoints.set(a.y, 0, 1);
|
||||||
|
controlpoints.set(a.z, 0, 2);
|
||||||
|
|
||||||
|
controlpoints.set(b.x, 1, 0);
|
||||||
|
controlpoints.set(b.y, 1, 1);
|
||||||
|
controlpoints.set(b.z, 1, 2);
|
||||||
|
|
||||||
|
controlpoints.set(c.x, 2, 0);
|
||||||
|
controlpoints.set(c.y, 2, 1);
|
||||||
|
controlpoints.set(c.z, 2, 2);
|
||||||
|
|
||||||
|
controlpoints.set(d.x, 3, 0);
|
||||||
|
controlpoints.set(d.y, 3, 1);
|
||||||
|
controlpoints.set(d.z, 3, 2);
|
||||||
|
|
||||||
|
erg = getPosition(u, controlpoints);
|
||||||
|
|
||||||
|
return Punkt3D(erg.get(0,0), erg.get(0,1), erg.get(0,2));
|
||||||
|
}
|
||||||
|
|
||||||
|
Punkt2D CatmullRomSpline::getPosition(float u, Punkt2D a, Punkt2D b, Punkt2D c, Punkt2D d) {
|
||||||
|
Matrix erg(1, 2);
|
||||||
|
Matrix controlpoints(4, 2);
|
||||||
|
|
||||||
|
controlpoints.set(a.x, 0, 0);
|
||||||
|
controlpoints.set(a.y, 0, 1);
|
||||||
|
|
||||||
|
controlpoints.set(b.x, 1, 0);
|
||||||
|
controlpoints.set(b.y, 1, 1);
|
||||||
|
|
||||||
|
controlpoints.set(c.x, 2, 0);
|
||||||
|
controlpoints.set(c.y, 2, 1);
|
||||||
|
|
||||||
|
controlpoints.set(d.x, 3, 0);
|
||||||
|
controlpoints.set(d.y, 3, 1);
|
||||||
|
|
||||||
|
erg = getPosition(u, controlpoints);
|
||||||
|
|
||||||
|
return Punkt2D(erg.get(0,0), erg.get(0,1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CatmullRomSpline::setS(float _s) {
|
||||||
|
s = _s;
|
||||||
|
makeMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
CatmullRomSpline::~CatmullRomSpline() {
|
||||||
|
delete(cr);
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef __CATMULLROMSPLINE_H
|
||||||
|
#define __CATMULLROMSPLINE_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_opengl.h>
|
||||||
|
#include "matrix.h"
|
||||||
|
#include "punkt2d.h"
|
||||||
|
#include "punkt3d.h"
|
||||||
|
|
||||||
|
|
||||||
|
class CatmullRomSpline {
|
||||||
|
private:
|
||||||
|
Matrix *cr;
|
||||||
|
float s;
|
||||||
|
|
||||||
|
void makeMatrix();
|
||||||
|
public:
|
||||||
|
CatmullRomSpline(float _s=0.5f);
|
||||||
|
|
||||||
|
Matrix getPosition(float u, Matrix &controlpoints);
|
||||||
|
Punkt3D getPosition(float u, Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d);
|
||||||
|
Punkt2D getPosition(float u, Punkt2D a, Punkt2D b, Punkt2D c, Punkt2D d);
|
||||||
|
|
||||||
|
|
||||||
|
void setS(float _s);
|
||||||
|
~CatmullRomSpline();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,154 @@
|
|||||||
|
#include "glcamera.h"
|
||||||
|
|
||||||
|
GLCamera::GLCamera() {
|
||||||
|
std_norm.set(0.0f, 1.0f, 0.0f);
|
||||||
|
std_dir.set(0.0f, 0.0f, -1.0f);
|
||||||
|
mpersec = 10.0f;
|
||||||
|
apersec = 90.0f;
|
||||||
|
doupdate = true;
|
||||||
|
|
||||||
|
norm.set(0.0f, 1.0f, 0.0f);
|
||||||
|
dir.set(0.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
|
rotx = roty = 0.0f;
|
||||||
|
|
||||||
|
rotmatX.set(x_axis, 0.0f);
|
||||||
|
rotmatY.set(y_axis, 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::updateVectors() {
|
||||||
|
dir = rotmatY * rotmatX * Punkt3D(0.0f, 0.0f, -1.0f);
|
||||||
|
norm = rotmatY * rotmatX * std_norm;
|
||||||
|
dir.normalize();
|
||||||
|
norm.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::setCamera() {
|
||||||
|
|
||||||
|
if(doupdate) {
|
||||||
|
updateVectors();
|
||||||
|
}
|
||||||
|
Punkt3D port = pos + dir;
|
||||||
|
gluLookAt( pos.x, pos.y, pos.z,
|
||||||
|
port.x, port.y, port.z,
|
||||||
|
norm.x, norm.y, norm.z);
|
||||||
|
// rotFrom2VecTo2Vec(std_dir, dir, std_norm, norm);
|
||||||
|
// glTranslateP3D(-pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Move-Funktionen
|
||||||
|
|
||||||
|
void GLCamera::moveForward(float sec) {
|
||||||
|
if(doupdate) {
|
||||||
|
updateVectors();
|
||||||
|
}
|
||||||
|
pos += (mpersec*sec) * dir;
|
||||||
|
// std::cout << "move..";
|
||||||
|
// pos.print("pos");
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::moveBackward(float sec) {
|
||||||
|
if(doupdate) {
|
||||||
|
updateVectors();
|
||||||
|
}
|
||||||
|
pos -= (mpersec*sec) * dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::moveLeft(float sec) {
|
||||||
|
if(doupdate) {
|
||||||
|
updateVectors();
|
||||||
|
}
|
||||||
|
Punkt3D right = (dir.kreuzprodukt(norm));
|
||||||
|
right.normalize();
|
||||||
|
pos -= right * sec * mpersec;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::moveRight(float sec) {
|
||||||
|
if(doupdate) {
|
||||||
|
updateVectors();
|
||||||
|
}
|
||||||
|
Punkt3D right = (dir.kreuzprodukt(norm));
|
||||||
|
right.normalize();
|
||||||
|
pos += right * sec * mpersec;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::rotateLeft(float sec) {
|
||||||
|
roty += apersec * sec;
|
||||||
|
rotmatY.set(roty);
|
||||||
|
doupdate = true;
|
||||||
|
// rotmat.set(Punkt3D(0.0f, 1.0f, 0.0f), sec*apersec);
|
||||||
|
// dir = rotmat * dir;
|
||||||
|
// dir.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::rotateRight(float sec) {
|
||||||
|
roty -= apersec * sec;
|
||||||
|
rotmatY.set(roty);
|
||||||
|
doupdate = true;
|
||||||
|
// statt norm
|
||||||
|
// rotmat.set(Punkt3D(0.0f, 1.0f, 0.0f), -apersec*sec);
|
||||||
|
// dir = rotmat * dir;
|
||||||
|
// dir.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::rotateUp(float sec) {
|
||||||
|
if(rotx+sec*apersec<=90.0f && rotx+sec*apersec>=-90.0f) {
|
||||||
|
rotx += sec*apersec;
|
||||||
|
rotmatX.set(rotx);
|
||||||
|
doupdate = true;
|
||||||
|
} else {
|
||||||
|
//std::cout << "zu groß" << std::endl;
|
||||||
|
}
|
||||||
|
// rotmat.set(dir.kreuzprodukt(norm), -apersec*sec);
|
||||||
|
// norm = rotmat * norm;
|
||||||
|
// dir = rotmat * dir;
|
||||||
|
// norm.normalize();
|
||||||
|
// dir.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::rotateDown(float sec) {
|
||||||
|
if( (rotx-sec*apersec) >= -90.0f) {
|
||||||
|
rotx -= sec*apersec;
|
||||||
|
rotmatX.set(rotx);
|
||||||
|
doupdate = true;
|
||||||
|
}
|
||||||
|
// rotmat.set(dir.kreuzprodukt(norm), apersec*sec);
|
||||||
|
// norm = rotmat * norm;
|
||||||
|
// dir = rotmat * dir;
|
||||||
|
// norm.normalize();
|
||||||
|
// dir.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
float GLCamera::getXrot() {
|
||||||
|
return rotx;
|
||||||
|
}
|
||||||
|
|
||||||
|
float GLCamera::getYrot() {
|
||||||
|
return roty;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::print() {
|
||||||
|
std::cout << " --- GL Camera --- " << std::endl;
|
||||||
|
pos.print("Position");
|
||||||
|
norm.print("Normale");
|
||||||
|
dir.print("Direction");
|
||||||
|
dir.kreuzprodukt(norm).print("Kreuz");
|
||||||
|
std::cout << "Rotation X: " << rotx << " Y: " << roty << std::endl;
|
||||||
|
rotmatY.print();
|
||||||
|
std::cout << " --- End Camera --- " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GLCamera::renderCoord() {
|
||||||
|
glBegin(GL_LINES);
|
||||||
|
glColor3f(0.0f, 1.0f, 0.0f);
|
||||||
|
glVertex3f(0.0f, -1.0f, 0.0f);
|
||||||
|
glVertex3f(0.0f, -1.0f, -5.0f);
|
||||||
|
glEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
GLCamera::~GLCamera() {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
#ifndef __GLCAMERA_H
|
||||||
|
#define __GLCAMERA_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_opengl.h>
|
||||||
|
#include "rotationsmatrix.h"
|
||||||
|
#include "emath_opengl.h"
|
||||||
|
#include "punkt3d.h"
|
||||||
|
|
||||||
|
class GLCamera {
|
||||||
|
private:
|
||||||
|
Punkt3D std_dir;
|
||||||
|
Punkt3D std_norm;
|
||||||
|
|
||||||
|
Punkt3D pos;
|
||||||
|
Punkt3D norm;
|
||||||
|
Punkt3D dir; // normalisiert
|
||||||
|
|
||||||
|
float rotx;
|
||||||
|
float roty;
|
||||||
|
bool doupdate;
|
||||||
|
|
||||||
|
float mpersec;
|
||||||
|
float apersec;
|
||||||
|
|
||||||
|
Rotationsmatrix rotmatX;
|
||||||
|
Rotationsmatrix rotmatY;
|
||||||
|
|
||||||
|
void updateVectors();
|
||||||
|
public:
|
||||||
|
GLCamera();
|
||||||
|
// GLCamera(Punkt3d
|
||||||
|
|
||||||
|
void setCamera();
|
||||||
|
|
||||||
|
// Std Move
|
||||||
|
void moveForward(float sec);
|
||||||
|
void moveBackward(float sec);
|
||||||
|
void moveLeft(float sec);
|
||||||
|
void moveRight(float sec);
|
||||||
|
void rotateLeft(float sec);
|
||||||
|
void rotateRight(float sec);
|
||||||
|
void rotateUp(float sec);
|
||||||
|
void rotateDown(float sec);
|
||||||
|
|
||||||
|
float getXrot();
|
||||||
|
float getYrot();
|
||||||
|
|
||||||
|
void print();
|
||||||
|
void renderCoord();
|
||||||
|
|
||||||
|
~GLCamera();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in new issue