211 lines
4.5 KiB
C++
211 lines
4.5 KiB
C++
/* 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.
|
||
*/
|
||
|
||
#include "glcamera.h"
|
||
|
||
namespace segl {
|
||
|
||
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);
|
||
}
|
||
|
||
void GLCamera::setPosDirNorm(Punkt3D p, Punkt3D d, Punkt3D n) {
|
||
pos = p;
|
||
dir = d.getNormalized();
|
||
norm = n.getNormalized();
|
||
}
|
||
|
||
// 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<72>" << 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();
|
||
}
|
||
|
||
void GLCamera::handleKeys(float sec) {
|
||
Uint8 *keys = SDL_GetKeyState(NULL);
|
||
|
||
if(keys[SDLK_w])
|
||
moveForward(sec);
|
||
if(keys[SDLK_s])
|
||
moveBackward(sec);
|
||
if(keys[SDLK_UP])
|
||
rotateUp(sec);
|
||
if(keys[SDLK_DOWN])
|
||
rotateDown(sec);
|
||
if(keys[SDLK_LEFT])
|
||
rotateLeft(sec);
|
||
if(keys[SDLK_RIGHT])
|
||
rotateRight(sec);
|
||
if(keys[SDLK_a])
|
||
moveLeft(sec);
|
||
if(keys[SDLK_d])
|
||
moveRight(sec);
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
Punkt3D GLCamera::getPos() {
|
||
return pos;
|
||
}
|
||
|
||
GLCamera::~GLCamera() {
|
||
|
||
}
|
||
|
||
} // namespace segl
|