Commit 640cd99a by Kai Westerkamp

Basic Scengraph

parent 138ce86d
...@@ -3,13 +3,14 @@ ...@@ -3,13 +3,14 @@
Camera::Camera(bool persp) Camera::Camera(bool persp)
{ {
this->persp = persp; this->persp = persp;
setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0)); setHome(new QQuaternion(), new QVector3D(0.0,0.0,-4.0));
} }
void Camera::home() void Camera::home()
{ {
this->rotation = homeRotation; this->rotation = homeRotation;
this->translation = homeTranslation; this->translation = homeTranslation;
} }
void Camera::setHome(QQuaternion *rotation, QVector3D *translation) void Camera::setHome(QQuaternion *rotation, QVector3D *translation)
...@@ -20,14 +21,19 @@ void Camera::setHome(QQuaternion *rotation, QVector3D *translation) ...@@ -20,14 +21,19 @@ void Camera::setHome(QQuaternion *rotation, QVector3D *translation)
home(); home();
} }
void Camera::rotate(QQuaternion *newPos ) void Camera::rotate(QQuaternion newPos )
{ {
// rotation *= *newPos; if(persp){
QQuaternion newRot = newPos * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
}
} }
void Camera::move(QVector3D *newPos) void Camera::move(QVector3D newPos)
{ {
// translation+=newPos;
QVector3D newTrans = newPos + *translation;
translation = new QVector3D(newTrans);
} }
......
...@@ -13,8 +13,8 @@ class Camera: public QObject ...@@ -13,8 +13,8 @@ class Camera: public QObject
Q_OBJECT Q_OBJECT
private: private:
bool persp; bool persp;
QQuaternion *rotation; // QQuaternion *rotation;
QVector3D *translation; // QVector3D *translation;
QQuaternion *homeRotation; QQuaternion *homeRotation;
QVector3D *homeTranslation; QVector3D *homeTranslation;
...@@ -25,10 +25,13 @@ public slots: ...@@ -25,10 +25,13 @@ public slots:
void home(); void home();
public: public:
QQuaternion *rotation;
QVector3D *translation;
Camera(bool persp); Camera(bool persp);
void setHome(QQuaternion *rotation, QVector3D *translation); void setHome(QQuaternion *rotation, QVector3D *translation);
void rotate(QQuaternion *newPos ); void rotate(QQuaternion newPos );
void move(QVector3D *newPos ); void move(QVector3D newPos );
void setupCamera(GLdouble aspect); void setupCamera(GLdouble aspect);
}; };
......
...@@ -2,9 +2,121 @@ ...@@ -2,9 +2,121 @@
#include "camera.h" #include "camera.h"
#include "glview.h" #include "glview.h"
#include "scene.h" #include "scene.h"
#include "mainwindow.h"
Controler::Controler() Controler::Controler(MainWindow *mainwindow,Scene *scene)
{ {
this->mainwindow = mainwindow;
this->scene = scene;
viewMode = CAMERA;
}
void Controler::mousePressed(GLView* view, QMouseEvent *event)
{
activeView = view;
mainwindow->setActiveView(view);
lastSpeherePos = trackballPoint(event->pos().x(),event->pos().y());
lastScreenPos = new QPointF(event->screenPos());
event->accept();
}
void Controler::mouseMoveEvent(GLView* view, QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
rotate(trackballPoint(event->pos().x(),event->pos().y()));
event->accept();
} else if (event->buttons() & Qt::RightButton) {
move(new QPointF(event->screenPos()));
event->accept();
}
}
void Controler::wheelEvent(GLView* view, QWheelEvent *event ) {
activeView = view;
mainwindow->setActiveView(view);
bool ctrlPressed = QApplication::keyboardModifiers() & Qt::ControlModifier;
if((viewMode == CAMERA) ^ ctrlPressed){
if(event->delta()<0)
activeView->getCamera()->move(QVector3D(0.0,0.0,1.0));
else
activeView->getCamera()->move(QVector3D(0.0,0.0,-1.0));
} else if((viewMode == EDIT) ^ ctrlPressed){
} else{
qDebug()<<"möp"<<viewMode<<" "<<CAMERA<<" "<<EDIT;
}
mainwindow->updateGL();
}
QVector3D* Controler::trackballPoint(int x, int y){
float xo,yo,zo;
// qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-activeView->width())/ activeView->height();
yo = (activeView->height()-(2.0*y))/activeView->width();
float d = sqrt(xo*xo+yo*yo);
zo = qMax(qCos(M_PI_2*d),qreal(0.0)); //qMin(d,1.0f)
QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize();
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos;
}
void Controler::rotate(QVector3D *newPos )
{
QVector3D axis = QVector3D::crossProduct(*lastSpeherePos,*newPos);
float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis)));
axis.normalize();
//axis = rotation->conjugate().rotatedVector(axis);
bool ctrlPressed = QApplication::keyboardModifiers() & Qt::ControlModifier;
if((viewMode == CAMERA) ^ ctrlPressed){
activeView->getCamera()->rotate(QQuaternion::fromAxisAndAngle(axis, angle));
} else if((viewMode == EDIT) ^ ctrlPressed){
} else{
qDebug()<<"möp"<<viewMode<<" "<<CAMERA<<" "<<EDIT;
}
lastSpeherePos = newPos;
mainwindow->updateGL();
} }
void Controler::move(QPointF * newPos){
QPointF dt = *newPos;
dt -= *lastScreenPos;
dt *= 0.01f;
float dx = dt.x();
float dy = dt.y()*-1.0;
bool ctrlPressed = QApplication::keyboardModifiers() & Qt::ControlModifier;
if((viewMode == CAMERA) ^ ctrlPressed){
activeView->getCamera()->move(QVector3D(dx,dy,0.0));
} else if((viewMode == EDIT) ^ ctrlPressed){
scene->move(0,activeView->getCamera()->rotation->rotatedVector(QVector3D(dx,dy,0.0)));
} else{
qDebug()<<"möp"<<viewMode<<" "<<CAMERA<<" "<<EDIT;
}
lastScreenPos = newPos;
mainwindow->updateGL();
}
void Controler::setCamMode(){
viewMode = CAMERA;
}
void Controler::setEditMode(){
viewMode = EDIT;
}
...@@ -2,16 +2,44 @@ ...@@ -2,16 +2,44 @@
#define CONTROLER_H #define CONTROLER_H
#include <QObject> #include <QObject>
#include <QtGui>
class camera; class camera;
class GLView; class GLView;
class Scene; class Scene;
class MainWindow;
enum Mode{CAMERA, EDIT};
class Controler: public QObject class Controler: public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Controler(); Controler(MainWindow *mainwindow,Scene *scene);
void mousePressed(GLView* view, QMouseEvent *event);
void mouseMoveEvent(GLView* view, QMouseEvent *event);
void wheelEvent(GLView* view, QWheelEvent *event );
public slots:
void setCamMode();
void setEditMode();
private:
MainWindow *mainwindow;
Scene *scene;
GLView *activeView;
QVector3D *lastSpeherePos;
QPointF * lastScreenPos;
Mode viewMode;
QVector3D* trackballPoint(int x, int y);
void rotate(QVector3D *newPos );
void move(QPointF * newPos);
}; };
#endif // CONTROLER_H #endif // CONTROLER_H
...@@ -26,7 +26,7 @@ void GLView::initializeGL ( ) { ...@@ -26,7 +26,7 @@ void GLView::initializeGL ( ) {
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0); glEnable(GL_LIGHT0);
static GLfloat lightPosition[4] = { 0.0, 0.0, 2.0, 1.0 }; static GLfloat lightPosition[4] = { 0.0, 0.0, 4.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
//Shader Setup //Shader Setup
...@@ -51,6 +51,11 @@ void GLView::initShader() ...@@ -51,6 +51,11 @@ void GLView::initShader()
qCritical()<< "Linking failed"<<shader->log(); qCritical()<< "Linking failed"<<shader->log();
} }
void GLView::home(){
camera->home();
updateGL();
}
void GLView::paintGL () void GLView::paintGL ()
...@@ -103,6 +108,7 @@ void GLView::resizeGL(int width , int height ) ...@@ -103,6 +108,7 @@ void GLView::resizeGL(int width , int height )
void GLView::setAcive(bool active) void GLView::setAcive(bool active)
{ {
this->isActive = active; this->isActive = active;
updateGL();
} }
Camera *GLView::getCamera() Camera *GLView::getCamera()
...@@ -111,7 +117,18 @@ Camera *GLView::getCamera() ...@@ -111,7 +117,18 @@ Camera *GLView::getCamera()
} }
void GLView::mousePressEvent(QMouseEvent *event ) {} void GLView::mousePressEvent(QMouseEvent *event )
void GLView::mouseMoveEvent(QMouseEvent *event ){} {
void GLView::wheelEvent(QWheelEvent *event ) {} controler->mousePressed(this,event);
}
void GLView::mouseMoveEvent(QMouseEvent *event )
{
controler->mouseMoveEvent(this,event);
}
void GLView::wheelEvent(QWheelEvent *event )
{
controler->wheelEvent(this,event);
}
...@@ -18,6 +18,9 @@ protected : ...@@ -18,6 +18,9 @@ protected :
void mousePressEvent(QMouseEvent *event ) ; void mousePressEvent(QMouseEvent *event ) ;
void mouseMoveEvent(QMouseEvent *event ) ; void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ; void wheelEvent(QWheelEvent *event ) ;
public slots:
void home();
public: public:
GLView(Scene *scene,Camera * camera,Controler *controler ); GLView(Scene *scene,Camera * camera,Controler *controler );
void setHome(QQuaternion *rotation, QVector3D *translation); void setHome(QQuaternion *rotation, QVector3D *translation);
......
...@@ -17,14 +17,20 @@ SOURCES += main.cpp\ ...@@ -17,14 +17,20 @@ SOURCES += main.cpp\
glview.cpp \ glview.cpp \
scene.cpp \ scene.cpp \
controler.cpp \ controler.cpp \
camera.cpp camera.cpp \
sceneprimitive.cpp \
sceneobject.cpp \
scenenode.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
cubewidget.h \ cubewidget.h \
glview.h \ glview.h \
scene.h \ scene.h \
controler.h \ controler.h \
camera.h camera.h \
sceneprimitive.h \
sceneobject.h \
scenenode.h
RESOURCES += \ RESOURCES += \
hellocube.qrc hellocube.qrc
......
...@@ -12,26 +12,28 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -12,26 +12,28 @@ MainWindow::MainWindow(QWidget *parent)
toolBar = new QToolBar("Shading",this); toolBar = new QToolBar("Shading",this);
statusBar = new QStatusBar(this); statusBar = new QStatusBar(this);
controler = new Controler;
//Views //Views
scene = new Scene(); scene = new Scene();
scene->simpleScene();
controler = new Controler(this,scene);
Camera *perspectiveCam = new Camera(true); Camera *perspectiveCam = new Camera(true);
perspectiveView = new GLView(scene,perspectiveCam,controler); perspectiveView = new GLView(scene,perspectiveCam,controler);
Camera *frontCam = new Camera(false); Camera *frontCam = new Camera(false);
frontCam->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-3.0)); frontCam->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-4.0));
frontView = new GLView(scene,frontCam,controler); frontView = new GLView(scene,frontCam,controler);
Camera *leftCam = new Camera(false); Camera *leftCam = new Camera(false);
leftCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(0.0,1.0,0.0,90.0).toVector4D()), leftCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(0.0,1.0,0.0,90.0).toVector4D()),
new QVector3D(0.0,0.0,-3.0)); new QVector3D(0.0,0.0,-4.0));
leftView = new GLView(scene,leftCam,controler); leftView = new GLView(scene,leftCam,controler);
Camera *topCam = new Camera(false); Camera *topCam = new Camera(false);
topCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1.0,0.0,0.0,90.0).toVector4D()), topCam->setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1.0,0.0,0.0,90.0).toVector4D()),
new QVector3D(0.0,0.0,-3.0)); new QVector3D(0.0,0.0,-4.0));
topView = new GLView(scene,topCam,controler); topView = new GLView(scene,topCam,controler);
topSplit = new QSplitter(Qt::Horizontal,this); topSplit = new QSplitter(Qt::Horizontal,this);
...@@ -74,13 +76,13 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -74,13 +76,13 @@ MainWindow::MainWindow(QWidget *parent)
cameraMode->setCheckable(true); cameraMode->setCheckable(true);
cameraMode->setIcon(QIcon(":/img/camera.png")); cameraMode->setIcon(QIcon(":/img/camera.png"));
cameraMode->setChecked(true); cameraMode->setChecked(true);
// connect(cameraMode, SIGNAL(triggered()), mainWidget, SLOT(showWireframe())); connect(cameraMode, SIGNAL(triggered()), controler, SLOT(setCamMode()));
interactionGroup->addAction(cameraMode); interactionGroup->addAction(cameraMode);
editMode = new QAction("Edit",this); editMode = new QAction("Edit",this);
editMode->setCheckable(true); editMode->setCheckable(true);
editMode->setIcon(QIcon(":/img/select.png")); editMode->setIcon(QIcon(":/img/select.png"));
// connect(editMode, SIGNAL(triggered()), mainWidget, SLOT(showWireframe())); connect(editMode, SIGNAL(triggered()), controler, SLOT(setEditMode()));
interactionGroup->addAction(editMode); interactionGroup->addAction(editMode);
//View Actions //View Actions
...@@ -155,10 +157,10 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -155,10 +157,10 @@ MainWindow::MainWindow(QWidget *parent)
} }
void MainWindow::showSingle(){ void MainWindow::showSingle(){
perspectiveView->show(); perspectiveView->show();
frontView->hide(); frontView->hide();
leftView->hide(); leftView->hide();
topView->hide(); topView->hide();
} }
void MainWindow::showDual(){ void MainWindow::showDual(){
...@@ -177,18 +179,30 @@ void MainWindow::showQuad(){ ...@@ -177,18 +179,30 @@ void MainWindow::showQuad(){
} }
void MainWindow::updateGL()
{
perspectiveView->updateGL();
frontView->updateGL();
leftView->updateGL();
topView->updateGL();
}
void MainWindow::setActiveView(GLView * active) void MainWindow::setActiveView(GLView * active)
{ {
activeView = active; perspectiveView->setAcive((perspectiveView == active));
perspectiveView->setAcive(false); frontView->setAcive((frontView == active));
frontView->setAcive(false); leftView->setAcive((leftView == active));
leftView->setAcive(false); topView->setAcive((topView == active));
topView->setAcive(false);
activeView->setAcive(true);
Camera* cam = activeView->getCamera(); disconnect(camHome, SIGNAL(triggered(bool)),perspectiveView,SLOT(home()));
connect(camHome, SIGNAL(triggered(bool)),cam,SLOT(home())); disconnect(camHome, SIGNAL(triggered(bool)),frontView,SLOT(home()));
disconnect(camHome, SIGNAL(triggered(bool)),leftView,SLOT(home()));
disconnect(camHome, SIGNAL(triggered(bool)),topView,SLOT(home()));
connect(camHome, SIGNAL(triggered(bool)),active,SLOT(home()));
} }
......
...@@ -53,18 +53,16 @@ private: ...@@ -53,18 +53,16 @@ private:
QSplitter *bottomSplit; QSplitter *bottomSplit;
QSplitter *verticalSplit; QSplitter *verticalSplit;
GLView *activeView;
GLView *perspectiveView; GLView *perspectiveView;
GLView *frontView; GLView *frontView;
GLView *leftView; GLView *leftView;
GLView *topView; GLView *topView;
void setActiveView(GLView * active);
public: public:
MainWindow(QWidget *parent = 0); MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
void setActiveView(GLView * active);
void updateGL();
public slots: public slots:
void showAboutBox(); void showAboutBox();
......
...@@ -2,115 +2,62 @@ ...@@ -2,115 +2,62 @@
Scene::Scene() Scene::Scene()
{ {
root = new SceneNode(this);
} }
void Scene::simpleScene()
{
float red[] = {1.0,0.0,0.0};
float green[] = {0.0,1.0,0.0};
float blue[] = {0.0,0.0,1.0};
float cyan[] = {0.0,1.0,1.0};
float magenta[] = {1.0,0.0,1.0};
float yellow[] = {1.0,1.0,0.0};
ScenePrimitive *box = new ScenePrimitive(PrimitiveType::Quader,2,root);
box->setMaterial(blue);
root->add(box);
ScenePrimitive *sphere = new ScenePrimitive(PrimitiveType::Sphere,10,root);
sphere->move(QVector3D(1.5,0,0));
sphere->setMaterial(green);
root->add(sphere);
ScenePrimitive *cylinder = new ScenePrimitive(PrimitiveType::Cylinder,10,root);
cylinder->move(QVector3D(0,1.5,0));
cylinder->setMaterial(red);
root->add(cylinder);
ScenePrimitive *torus = new ScenePrimitive(PrimitiveType::Torus,10,root);
torus->move(QVector3D(0,0,1.5));
torus->setMaterial(magenta);
root->add(torus);
}
void Scene::draw() void Scene::draw()
{ {
drawCube(); root->draw();
} }
void Scene::drawCube() SceneObject *Scene::getRoot()
{ {
GLfloat red[] = {1.0,0.0,0.0}; return root;
GLfloat green[] = {0.0,1.0,0.0};
GLfloat blue[] = {0.0,0.0,1.0};
GLfloat cyan[] = {0.0,1.0,1.0};
GLfloat magenta[] = {1.0,0.0,1.0};
GLfloat yellow[] = {1.0,1.0,0.0};
float increment = 1.0/(pow(2,4));
setMaterial(red);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, 0.5f,y);
glVertex3f(x, 0.5f,y);
glVertex3f(x, 0.5f, y+increment);
glVertex3f( x+increment, 0.5f, y+increment);
}
}
glEnd();
setMaterial(magenta);
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, -0.5f,y+increment);
glVertex3f(x, -0.5f,y+increment);
glVertex3f(x, -0.5f, y);
glVertex3f( x+increment, -0.5f, y);
}
}
glEnd();
setMaterial(green);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y+increment, 0.5f);
glVertex3f(x, y+increment, 0.5f);
glVertex3f(x, y, 0.5f);
glVertex3f( x+increment, y, 0.5f);
}
}
glEnd();
setMaterial(yellow);
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y, -0.5f);
glVertex3f(x, y,-0.5f);
glVertex3f(x, y+increment,-0.5f);
glVertex3f( x+increment,y+increment, -0.5f);
}
}
glEnd();
setMaterial(blue);
glBegin(GL_QUADS);
glNormal3f(-1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( -0.5f, x+increment,y+increment);
glVertex3f( -0.5f,x+increment,y);
glVertex3f( -0.5f,x, y);
glVertex3f( -0.5f,x, y+increment);
}
}
glEnd();
setMaterial(cyan);
glBegin(GL_QUADS);
glNormal3f(1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( 0.5f, x+increment,y);
glVertex3f( 0.5f,x+increment,y+increment);
glVertex3f( 0.5f,x, y+increment);
glVertex3f( 0.5f,x, y);
}
}
glEnd();
} }
void Scene::setMaterial(GLfloat *color ) void Scene::move(int id, QVector3D dir)
{ {
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
GLfloat white[] = {1.0,1.0,1.0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,white);
GLfloat mShininess[] = {128};
glMaterialfv(GL_FRONT,GL_SHININESS,mShininess);
} }
void Scene::rotate(int id, QQuaternion rot)
{
}
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
#include <QGLFunctions> #include <QGLFunctions>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <scenenode.h>
#include <sceneobject.h>
#include <sceneprimitive.h>
class Scene: public QObject class Scene: public QObject
{ {
Q_OBJECT Q_OBJECT
...@@ -13,9 +17,18 @@ public: ...@@ -13,9 +17,18 @@ public:
Scene(); Scene();
void draw(); void draw();
void simpleScene();
void move(int id, QVector3D dir);
void rotate(int id, QQuaternion rot);
private: private:
void drawCube(); SceneNode *root;
void setMaterial(GLfloat *color );
SceneObject *getRoot();
}; };
#endif // SCENE_H #endif // SCENE_H
#include "scenenode.h"
SceneNode::SceneNode(QObject *parent)
:SceneObject(parent)
{
childs = QList<SceneObject*>();
}
void SceneNode::add(SceneObject *child)
{
childs.append(child);
}
void SceneNode::draw()
{
glPushMatrix();
applyTransformation();
foreach (SceneObject *obj, childs) {
obj->draw();
}
glPopMatrix();
}
#ifndef SCENENODE_H
#define SCENENODE_H
#include <sceneobject.h>
#include <QList>
class SceneNode : public SceneObject
{
Q_OBJECT
private:
QList<SceneObject*> childs;
public:
SceneNode(QObject *parent);
void draw();
void add(SceneObject *child);
};
#endif // SCENENODE_H
#include "sceneobject.h"
SceneObject::SceneObject(QObject *parent)
:QObject(parent)
{
id = 3;
rotation = QQuaternion();
translation = QVector3D();
}
int SceneObject::getID()
{
return id;
}
void SceneObject::move(QVector3D dir)
{
translation+=dir;
}
void SceneObject::rotate(QQuaternion rot)
{
rotation *= QQuaternion(rotation*rot);
}
void SceneObject::draw(){qDebug()<<"Drawing abstract Scene Element";}
void SceneObject::applyTransformation()
{
QMatrix4x4 mat = QMatrix4x4();
mat.translate(translation);
glMultMatrixf(mat.data());
mat = QMatrix4x4();
mat.rotate(rotation);
glMultMatrixf(mat.data());
}
#ifndef SCENEOBJECT_H
#define SCENEOBJECT_H
#include <QObject>
#include <QQuaternion>
#include <QVector3D>
#include <QtOpenGL>
#include <gl/GLU.h>
class SceneObject : public QObject
{
Q_OBJECT
protected:
QQuaternion rotation;
QVector3D translation;
int id;
QString name;
void applyTransformation();
public:
SceneObject(QObject *parent = 0);
virtual void draw();
int getID();
void move(QVector3D dir);
void rotate(QQuaternion rot);
};
#endif // SCENEOBJECT_H
#include "sceneprimitive.h"
#include "algorithm"
ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation, QObject *parent)
:SceneObject(parent)
{
this->type = type;
this->tesselation = tesselation;
qobj = gluNewQuadric();
gluQuadricNormals(qobj, GLU_SMOOTH);
this->color = new float[3];
this->color[0] = 0.0;
this->color[1] = 0.0;
this->color[2] = 0.0;
}
void ScenePrimitive::draw(){
glPushMatrix();
applyTransformation();
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
GLfloat white[] = {1.0,1.0,1.0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
glMaterialfv(GL_FRONT,GL_SPECULAR,white);
GLfloat mShininess[] = {128};
glMaterialfv(GL_FRONT,GL_SHININESS,mShininess);
int radius = 1;
switch (this->type) {
case Quader:
drawCube(tesselation);
break;
case Sphere:
gluSphere(qobj,1,tesselation,tesselation);
break;
case Cylinder:
gluCylinder(qobj,radius,radius,3,tesselation,tesselation);
gluQuadricOrientation(qobj,GLU_INSIDE);
gluDisk( qobj, 0.0, radius, tesselation, 1);
glTranslatef(0,0,3);
gluQuadricOrientation(qobj,GLU_OUTSIDE);
gluDisk( qobj, 0.0 , radius, tesselation, 1);
break;
case Torus:
// glutSolidTorus(5, 15, 8, 128);
break;
default:
qDebug()<<"Enum Error";
break;
}
glPopMatrix();
}
void ScenePrimitive::setMaterial(float *color )
{
this->color = new float[3];
this->color[0] = color[0];
this->color[1] = color[1];
this->color[2] = color[2];
// qDebug()<< *this->color<<" "<<this->color[0]<<" "<<this->color[1]<<" "<<this->color[2];
}
void ScenePrimitive::drawCube(int tesselation)
{
float increment = 1.0/(pow(2,tesselation));
glBegin(GL_QUADS);
glNormal3f(0,1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, 0.5f,y);
glVertex3f(x, 0.5f,y);
glVertex3f(x, 0.5f, y+increment);
glVertex3f( x+increment, 0.5f, y+increment);
}
}
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment, -0.5f,y+increment);
glVertex3f(x, -0.5f,y+increment);
glVertex3f(x, -0.5f, y);
glVertex3f( x+increment, -0.5f, y);
}
}
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,0,1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y+increment, 0.5f);
glVertex3f(x, y+increment, 0.5f);
glVertex3f(x, y, 0.5f);
glVertex3f( x+increment, y, 0.5f);
}
}
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,0,-1);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( x+increment,y, -0.5f);
glVertex3f(x, y,-0.5f);
glVertex3f(x, y+increment,-0.5f);
glVertex3f( x+increment,y+increment, -0.5f);
}
}
glEnd();
glBegin(GL_QUADS);
glNormal3f(-1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( -0.5f, x+increment,y+increment);
glVertex3f( -0.5f,x+increment,y);
glVertex3f( -0.5f,x, y);
glVertex3f( -0.5f,x, y+increment);
}
}
glEnd();
glBegin(GL_QUADS);
glNormal3f(1,0,0);
for (float x = -0.5; x < 0.5 ; x+= increment) {
for (float y = -0.5; y < 0.5 ; y+= increment ) {
glVertex3f( 0.5f, x+increment,y);
glVertex3f( 0.5f,x+increment,y+increment);
glVertex3f( 0.5f,x, y+increment);
glVertex3f( 0.5f,x, y);
}
}
glEnd();
}
#ifndef SCENEPRIMITIVE_H
#define SCENEPRIMITIVE_H
#include <sceneobject.h>
enum PrimitiveType{Quader=0,Sphere,Cylinder,Torus};
class ScenePrimitive : public SceneObject
{
Q_OBJECT
private:
PrimitiveType type;
int tesselation;
GLUquadric *qobj;
float *color;
void drawCube(int tesselation);
public:
void draw();
void setMaterial(float *color);
ScenePrimitive(PrimitiveType type, int tesselation, QObject *parent);
};
#endif // SCENEPRIMITIVE_H
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