Commit 138ce86d by Kai Westerkamp

Added Camera & Controler

parent 9af11d94
/A1/build-hellocube-Desktop_Qt_5_4_2_MSVC2013_OpenGL_64bit-Debug
/A1/hellocube/*.user
/build-hellocube-Desktop_Qt_5_4_2_MSVC2013_OpenGL_64bit-Debug
/build-hellocube-Desktop_Qt_5_5_1_MinGW_32bit-Debug
/build-hellocube-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug
/A2/*.user
#include "camera.h"
Camera::Camera(bool persp)
{
this->persp = persp;
setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
}
void Camera::home()
{
this->rotation = homeRotation;
this->translation = homeTranslation;
}
void Camera::setHome(QQuaternion *rotation, QVector3D *translation)
{
//qDebug()<<*rotation<<" trans:"<<*translation;
this->homeRotation = rotation;
this->homeTranslation = translation;
home();
}
void Camera::rotate(QQuaternion *newPos )
{
// rotation *= *newPos;
}
void Camera::move(QVector3D *newPos)
{
// translation+=newPos;
}
void Camera::setupCamera(GLdouble aspect )
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(persp){
perspective(45.0,aspect,0.01,100.0);
} else {
int size = 1;
glOrtho(-size*aspect,size*aspect,-size,size,0.01,100.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Camera Tranforations
QMatrix4x4 mat = QMatrix4x4();
mat.translate(*translation);
glMultMatrixf(mat.data());
mat = QMatrix4x4();
mat.rotate(*rotation);
glMultMatrixf(mat.data());
}
void Camera::perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
}
#ifndef CAMERA_H
#define CAMERA_H
#include <QtGui>
#include <QtOpenGL>
#include <QQuaternion>
#include <QVector3D>
class Camera: public QObject
{
Q_OBJECT
private:
bool persp;
QQuaternion *rotation;
QVector3D *translation;
QQuaternion *homeRotation;
QVector3D *homeTranslation;
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
public slots:
void home();
public:
Camera(bool persp);
void setHome(QQuaternion *rotation, QVector3D *translation);
void rotate(QQuaternion *newPos );
void move(QVector3D *newPos );
void setupCamera(GLdouble aspect);
};
#endif // CAMERA_H
#include "controler.h"
#include "camera.h"
#include "glview.h"
#include "scene.h"
Controler::Controler()
{
}
#ifndef CONTROLER_H
#define CONTROLER_H
#include <QObject>
class camera;
class GLView;
class Scene;
class Controler: public QObject
{
Q_OBJECT
public:
Controler();
};
#endif // CONTROLER_H
#include "glview.h"
GLView::GLView(Scene *scene,bool perspective)
GLView::GLView(Scene *scene,Camera * camera,Controler *controler )
{
this->persp = perspective;
this->camera = camera;
this->scene = scene;
setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
this->controler = controler;
}
QSize GLView::minimumSizeHint() const
......@@ -26,7 +26,7 @@ void GLView::initializeGL ( ) {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
static GLfloat lightPosition[4] = { 0.5, 0.0, 2.0, 1.0 };
static GLfloat lightPosition[4] = { 0.0, 0.0, 2.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
//Shader Setup
......@@ -52,73 +52,63 @@ void GLView::initShader()
}
void GLView::setHome(QQuaternion *rotation, QVector3D *translation)
{
//qDebug()<<*rotation<<" trans:"<<*translation;
this->homeRotation = rotation;
this->homeTranslation = translation;
home();
}
void GLView::home()
{
this->rotation = homeRotation;
this->translation = homeTranslation;
}
void GLView::paintGL ()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//set Projection
resizeGL(0,0);
glLoadIdentity();
//Camera Tranforations
QMatrix4x4 mat = QMatrix4x4();
mat.translate(*translation);
glMultMatrixf(mat.data());
glViewport(0,0,this->width(),this->height());
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if(isActive){
shader->setUniformValue("shaded",false);
glLineWidth(10);
GLfloat color[] = {1.0,1.0,0.0};
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,color);
glMaterialf(GL_FRONT,GL_SHININESS,128);
glBegin (GL_LINE_LOOP);
glVertex3i (-1, -1, 0);
glVertex3i (1, -1, 0);
glVertex3i (1, 1, 0);
glVertex3i (-1, 1, 0);
glEnd ();
}
// glRotated(45,0,1,0);
shader->setUniformValue("shaded",true);
mat = QMatrix4x4();
mat.rotate(*rotation);
glMultMatrixf(mat.data());
//set Projection and Camera Rotation
camera->setupCamera(aspect);
//draw Scene
scene->draw();
}
void GLView::resizeGL(int width , int height )
{
glViewport(0, 0, this->width(), this->height());
GLdouble aspect = 1.0*this->width()/this->height();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(persp){
perspective(45.0,aspect,0.01,100.0);
} else {
int size = 1;
glOrtho(-size*aspect,size*aspect,-size,size,0.01,100.0);
}
glMatrixMode(GL_MODELVIEW);
aspect = 1.0*width/height;
}
void GLView::perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
GLdouble xmin, xmax, ymin, ymax;
ymax = zNear * tan( fovy * M_PI / 360.0 );
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
void GLView::setAcive(bool active)
{
this->isActive = active;
}
Camera *GLView::getCamera()
{
return this->camera;
}
void GLView::mousePressEvent(QMouseEvent *event ) {}
......
......@@ -3,8 +3,10 @@
#include <QtGui>
#include <QtOpenGL>
#include <scene.h>
#include <controler.h>
#include <camera.h>
#include <scene.h>
class GLView : public QGLWidget
{
......@@ -17,33 +19,29 @@ protected :
void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ;
public:
GLView(Scene *scene,bool perspective = true);
GLView(Scene *scene,Camera * camera,Controler *controler );
void setHome(QQuaternion *rotation, QVector3D *translation);
QSize minimumSizeHint() const;
QSize sizeHint() const;
void rotate(QVector3D *newPos );
void move(QPointF *newPos );
public slots:
void home();
void setAcive(bool active);
Camera *getCamera();
private:
bool persp;
QQuaternion *rotation;
QVector3D *translation;
QGLShaderProgram *shader;
Scene *scene;
QQuaternion *homeRotation;
QVector3D *homeTranslation;
Controler *controler;
Camera *camera;
bool isActive;
QGLShaderProgram *shader;
Scene *scene;
GLdouble aspect;
void initShader();
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
};
#endif // GLVIEW_H
......@@ -15,12 +15,16 @@ SOURCES += main.cpp\
mainwindow.cpp \
cubewidget.cpp \
glview.cpp \
scene.cpp
scene.cpp \
controler.cpp \
camera.cpp
HEADERS += mainwindow.h \
cubewidget.h \
glview.h \
scene.h
scene.h \
controler.h \
camera.h
RESOURCES += \
hellocube.qrc
......
......@@ -12,22 +12,33 @@ MainWindow::MainWindow(QWidget *parent)
toolBar = new QToolBar("Shading",this);
statusBar = new QStatusBar(this);
controler = new Controler;
//Views
scene = new Scene();
perspectiveView = new GLView(scene,true);
frontView = new GLView(scene,false);
frontView->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
leftView = new GLView(scene,false);
leftView->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(0.0,1.0,0.0,90.0).toVector4D()), new QVector3D(0.0,0.0,-3.0));
topView = new GLView(scene,false);
topView->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1.0,0.0,0.0,90.0).toVector4D()), new QVector3D(0.0,0.0,-3.0));
Camera *perspectiveCam = new Camera(true);
perspectiveView = new GLView(scene,perspectiveCam,controler);
Camera *frontCam = new Camera(false);
frontCam->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0));
frontView = new GLView(scene,frontCam,controler);
Camera *leftCam = new Camera(false);
leftCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(0.0,1.0,0.0,90.0).toVector4D()),
new QVector3D(0.0,0.0,-3.0));
leftView = new GLView(scene,leftCam,controler);
Camera *topCam = new Camera(false);
topCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1.0,0.0,0.0,90.0).toVector4D()),
new QVector3D(0.0,0.0,-3.0));
topView = new GLView(scene,topCam,controler);
topSplit = new QSplitter(Qt::Horizontal,this);
bottomSplit = new QSplitter(Qt::Horizontal,this);
verticalSplit = new QSplitter(Qt::Vertical,this);
activeView = perspectiveView;
setActiveView(perspectiveView);
topSplit->addWidget(perspectiveView);
topSplit->addWidget(frontView);
......@@ -107,7 +118,7 @@ MainWindow::MainWindow(QWidget *parent)
connect(aboutAction,SIGNAL(triggered()),this,SLOT(showAboutBox()));
camHome = new QAction(QIcon(":/img/cam_home.png"),"Cam Home", toolBar);
connect(camHome, SIGNAL(triggered(bool)),activeView,SLOT(home()));
// Assemble Menus
......@@ -164,6 +175,19 @@ void MainWindow::showQuad(){
leftView->show();
topView->show();
}
void MainWindow::setActiveView(GLView * active)
{
activeView = active;
perspectiveView->setAcive(false);
frontView->setAcive(false);
leftView->setAcive(false);
topView->setAcive(false);
activeView->setAcive(true);
Camera* cam = activeView->getCamera();
connect(camHome, SIGNAL(triggered(bool)),cam,SLOT(home()));
}
......
......@@ -14,7 +14,9 @@
#include <QSplitter>
#include <glview.h>
#include <scene.h>
#include <camera.h>
#include <controler.h>
class MainWindow : public QMainWindow
{
......@@ -45,6 +47,7 @@ private:
QStatusBar *statusBar;
Scene* scene;
Controler *controler;
QSplitter *topSplit;
QSplitter *bottomSplit;
......@@ -57,7 +60,7 @@ private:
GLView *topView;
void setActiveView();
void setActiveView(GLView * active);
public:
MainWindow(QWidget *parent = 0);
......
uniform bool shaded;
varying vec3 position;
varying vec3 normal;
......@@ -27,6 +29,8 @@ void main(void)
+ Ispec
);
gl_FragColor = vec4(specular);
if(shaded)
gl_FragColor = color;
else
gl_FragColor = vec4(1,1,0,1);
}
#ifndef SCENE_H
#define SCENE_H
#include <QObject>
#include <QtOpenGL>
#include <QGLFunctions>
#include <QOpenGLFunctions>
class Scene
class Scene: public QObject
{
Q_OBJECT
public:
Scene();
void draw();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment