Commit 49ce7bd6 by Kai Westerkamp

basic scene graph

parent 9a9bd4f3
......@@ -64,9 +64,8 @@ void GLView::paintGL ()
// format.setSamples();
// format.setAttachment(QGLFramebufferObject::);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glViewport(0,0,this->width(),this->height());
......@@ -75,7 +74,22 @@ void GLView::paintGL ()
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
shader->setUniformValue("shaded",true);
//set Projection and Camera Rotation
camera->setupCamera(aspect);
//draw Scene
scene->draw();
if(isActive){
glDisable(GL_DEPTH_TEST);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
shader->setUniformValue("shaded",false);
glLineWidth(10);
GLfloat color[] = {1.0,1.0,0.0};
......@@ -92,16 +106,6 @@ void GLView::paintGL ()
glEnd ();
}
shader->setUniformValue("shaded",true);
//set Projection and Camera Rotation
camera->setupCamera(aspect);
//draw Scene
scene->draw();
}
void GLView::resizeGL(int width , int height )
......
......@@ -17,5 +17,6 @@
<file>img/view-dual.png</file>
<file>img/viewports.png</file>
<file>img/view-single.png</file>
<file>img/delete.png</file>
</qresource>
</RCC>
......@@ -6,22 +6,19 @@ MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("Hello Cube");
menuBar = new QMenuBar();
toolBar = new QToolBar("Shading",this);
statusBar = new QStatusBar(this);
//Views
scene = new Scene();
scene->setTesselation(1);
connect(scene,SIGNAL(activChanged()),this,SLOT(updateStatusBar()));
connect(scene,SIGNAL(activChanged()),this,SLOT(updateGL()));
controler = new Controler(this,scene);
Camera *perspectiveCam = new Camera(true);
......@@ -31,6 +28,7 @@ MainWindow::MainWindow(QWidget *parent)
frontCam->setHome(new QQuaternion(), new QVector3D(0.0,0.0,-4.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,-4.0));
......@@ -45,8 +43,10 @@ MainWindow::MainWindow(QWidget *parent)
bottomSplit = new QSplitter(Qt::Horizontal,this);
verticalSplit = new QSplitter(Qt::Vertical,this);
camHome = new QAction(QIcon(":/img/cam_home.png"),"Cam Home", toolBar);
setActiveView(perspectiveView);
topSplit->addWidget(perspectiveView);
topSplit->addWidget(frontView);
......@@ -55,6 +55,8 @@ MainWindow::MainWindow(QWidget *parent)
verticalSplit->addWidget(topSplit);
verticalSplit->addWidget(bottomSplit);
setCentralWidget(verticalSplit);
showQuad();
......@@ -125,18 +127,17 @@ MainWindow::MainWindow(QWidget *parent)
aboutAction = new QAction("About",menuBar);
connect(aboutAction,SIGNAL(triggered()),this,SLOT(showAboutBox()));
camHome = new QAction(QIcon(":/img/cam_home.png"),"Cam Home", toolBar);
slider = new QSlider(Qt::Horizontal,toolBar);
slider->setMinimum( 0 );
slider->setMaximum( 4 );
connect(slider,SIGNAL(valueChanged(int)),scene,SLOT(setTessellation(int)));
connect(slider,SIGNAL(valueChanged(int)),scene,SLOT(setTesselation(int)));
slider->setValue( 1 );
// Add Primitives
spheresAdd = new QAction("Sphere",this);
spheresAdd->setIcon(QIcon(":/img/sphere.png"));
connect(spheresAdd, SIGNAL(triggered()), scene, SLOT(addSphere()));
......@@ -157,12 +158,23 @@ MainWindow::MainWindow(QWidget *parent)
torusAdd->setIcon(QIcon(":/img/torus.png"));
connect(torusAdd, SIGNAL(triggered()), scene, SLOT(addTorus()));
qDebug()<<"Test";
groupAdd = new QAction("Group",this);
groupAdd->setIcon(QIcon(":/img/wireframe.png"));
connect(groupAdd, SIGNAL(triggered()), scene, SLOT(addNode()));
activeDelete = new QAction("Delete",this);
activeDelete->setIcon(QIcon(":/img/delete.png"));
connect(activeDelete, SIGNAL(triggered()), scene, SLOT(deletActive()));
primitivesMenu->addAction(spheresAdd);
primitivesMenu->addAction(boxesAdd);
primitivesMenu->addAction(cylindersAdd);
primitivesMenu->addAction(coneAdd);
primitivesMenu->addAction(torusAdd);
primitivesMenu->addAction(groupAdd);
primitivesMenu->addAction(activeDelete);
// Assemble Menus
......@@ -214,20 +226,13 @@ void MainWindow::initDoc()
QDockWidget *dock = new QDockWidget("Scene", this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
sceneView = new QTreeView();
sceneView = new QTreeView(this);
sceneView->setSelectionBehavior(QAbstractItemView::SelectRows);
// sceneView->setModel(scene);
sceneView->expandAll();
sceneView->setModel(scene);
connect(sceneView,SIGNAL(clicked(QModelIndex)),scene, SLOT(objectSelected(QModelIndex)));
QListWidget *customerList = new QListWidget(dock);
customerList->addItems(QStringList()
<< "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton"
<< "Jane Doe, Memorabilia, 23 Watersedge, Beaton"
<< "Tammy Shea, Tiblanka, 38 Sea Views, Carlton"
<< "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal"
<< "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston"
<< "Sally Hobart, Tiroli Tea, 67 Long River, Fedula");
dock->setWidget(customerList);
dock->setWidget(sceneView);
addDockWidget(Qt::RightDockWidgetArea, dock);
......@@ -276,10 +281,10 @@ void MainWindow::updateGL()
void MainWindow::setActiveView(GLView * active)
{
perspectiveView->setAcive((perspectiveView == active));
frontView->setAcive((frontView == active));
leftView->setAcive((leftView == active));
topView->setAcive((topView == active));
disconnect(camHome, SIGNAL(triggered(bool)),perspectiveView,SLOT(home()));
disconnect(camHome, SIGNAL(triggered(bool)),frontView,SLOT(home()));
disconnect(camHome, SIGNAL(triggered(bool)),leftView,SLOT(home()));
......
......@@ -49,6 +49,8 @@ private:
QAction *cylindersAdd;
QAction *coneAdd;
QAction *torusAdd;
QAction *groupAdd;
QAction *activeDelete;
QToolBar *toolBar;
......
......@@ -4,7 +4,15 @@
Scene::Scene( )
{
root = new SceneNode(this);
root = new SceneNode();
root->setName("ROOT");
active = root;
activeIndex = QModelIndex();
}
Scene::~Scene( )
{
delete root;
}
int Scene::simpleScene()
......@@ -18,6 +26,8 @@ int Scene::simpleScene()
addCylinder();
moveActive(QVector3D(0,1.5,-1.5));
addNode();
addTorus();
moveActive(QVector3D(0,0,-3));
......@@ -28,61 +38,97 @@ int Scene::simpleScene()
}
ScenePrimitive *Scene::addCube()
void Scene::addSceneObjectTaActive(SceneObject *obj){
SceneNode* parent;
QModelIndex parentIndex;
if(active->isNode()){
// qDebug()<<"Active is Node "<<active->getName();
parent = static_cast<SceneNode*>(active);
parentIndex = activeIndex;
} else {
parent = static_cast<SceneNode*>(active->getParent());
parentIndex = activeIndex.parent();
// qDebug()<<"Active is Primitve "<<active->getName()<<"Parent"<<parent->getName();
}
beginInsertRows(parentIndex, parent->childCount(), parent->childCount());
parent->add(obj);
obj->setParent(parent);
endInsertRows();
activeIndex = index(parent->childCount()-1,0,parentIndex);
active = obj;
qDebug()<<"Adding"<<obj->getName()<<" to "<<getItem(parentIndex)->getName()<<" Active"<<getItem(activeIndex)->getName();
emit activChanged();
}
void Scene::addCube()
{
float color[] = {0.0,0.0,1.0};
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Quader,tesselation,root);
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Quader,tesselation);
primitive->setMaterial(color);
root->add(primitive);
active = primitive;
emit activChanged();
return primitive;
addSceneObjectTaActive(primitive);
}
ScenePrimitive* Scene::addSphere()
void Scene::addSphere()
{
float color[] = {0.0,1.0,0.0};
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Sphere,tesselation,root);
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Sphere,tesselation);
primitive->setMaterial(color);
root->add(primitive);
active = primitive;
emit activChanged();
return primitive;
addSceneObjectTaActive(primitive);
}
ScenePrimitive* Scene::addCylinder()
void Scene::addCylinder()
{
float color[] = {1.0,0.0,0.0};
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Cylinder,tesselation,root);
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Cylinder,tesselation);
primitive->setMaterial(color);
root->add(primitive);
active = primitive;
emit activChanged();
return primitive;
addSceneObjectTaActive(primitive);
}
ScenePrimitive* Scene::addTorus()
void Scene::addTorus()
{
float color[] = {1.0,0.0,1.0};
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Torus,tesselation,root);
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Torus,tesselation);
primitive->setMaterial(color);
root->add(primitive);
active = primitive;
emit activChanged();
return primitive;
addSceneObjectTaActive(primitive);
}
ScenePrimitive* Scene::addCone()
void Scene::addCone()
{
float color[] = {0.0,1.0,1.0};
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Cone,tesselation,root);
ScenePrimitive *primitive = new ScenePrimitive(PrimitiveType::Cone,tesselation);
primitive->setMaterial(color);
root->add(primitive);
active = primitive;
emit activChanged();
return primitive;
addSceneObjectTaActive(primitive);
}
void Scene::addNode()
{
SceneNode *node = new SceneNode();
node->setName("Graph Node");
addSceneObjectTaActive(node);
}
void Scene::deletActive()
{
QModelIndex parentIndex = activeIndex.parent();
SceneNode *parent = static_cast<SceneNode*>(active->getParent());
active->childNumber();
int number = active->childNumber();
beginRemoveColumns(parentIndex, number, number);
parent->remove(number);
endRemoveColumns();
}
void Scene::setTesselation(int tesselation)
{
......@@ -122,4 +168,99 @@ void Scene::rotateActive(QQuaternion rot)
active->rotate(rot);
}
//Model funktions
SceneObject *Scene::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
SceneObject *item = static_cast<SceneObject*>(index.internalPointer());
if (item)
return item;
}
return root;
}
int Scene::rowCount(const QModelIndex &parent) const
{
SceneObject *parentItem = getItem(parent);
// qDebug()<<"ChildCount:"<<parentItem->childCount();
return parentItem->childCount();
}
int Scene::columnCount(const QModelIndex & parent ) const
{
SceneObject *item = getItem(parent);
// qDebug()<<"ColumnCount:"<<item->columnCount();
return item->columnCount();
}
Qt::ItemFlags Scene::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
QModelIndex Scene::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0)
return QModelIndex();
SceneObject *parentItem = getItem(parent);
SceneObject *childItem = parentItem->children(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex Scene::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
SceneObject *childItem = getItem(index);
SceneObject *parentItem = childItem->getParent();
if (parentItem == root)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
}
QVariant Scene::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
SceneObject *item = getItem(index);
return item->data(index.column());
}
QVariant Scene::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return root->data(section);
return QVariant();
}
void Scene::objectSelected(QModelIndex index)
{
activeIndex = index;
active = getItem(index);
emit activChanged();
}
......@@ -10,11 +10,12 @@
#include <sceneobject.h>
#include <sceneprimitive.h>
class Scene: public QObject // QAbstractItemModel
class Scene: public QAbstractItemModel // QAbstractItemModel
{
Q_OBJECT
public:
Scene();
~Scene();
void draw();
int simpleScene();
......@@ -23,28 +24,36 @@ public:
void moveActive(QVector3D dir);
void rotateActive(QQuaternion rot);
void addSceneObjectTaActive(SceneObject *obj);
SceneObject *getActive();
SceneObject *setActive(int id);
// QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
// Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
// QVariant headerData(int section, Qt::Orientation orientation,
// int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
// QModelIndex index(int row, int column,
// const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
// QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
// int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
// int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
int tesselation;
public slots:
void setTesselation(int tesselation);
ScenePrimitive *addCube();
ScenePrimitive *addSphere();
ScenePrimitive *addCylinder();
ScenePrimitive *addTorus();
ScenePrimitive *addCone();
void objectSelected(QModelIndex index);
void setTesselation(int tesselation);
void addCube();
void addSphere();
void addCylinder();
void addTorus();
void addCone();
void addNode();
void deletActive();
signals:
void activChanged();
......@@ -52,6 +61,8 @@ signals:
private:
SceneNode *root;
SceneObject *active;
QModelIndex activeIndex;
SceneObject *getItem(const QModelIndex &index) const;
SceneNode *getRoot();
......
#include "scenenode.h"
SceneNode::SceneNode(QObject *parent)
:SceneObject(parent)
SceneNode::SceneNode()
:SceneObject()
{
childs = QList<SceneObject*>();
}
SceneNode::~SceneNode()
{
qDeleteAll(childs);
}
SceneObject *SceneNode::children(int number)
{
return childs.value(number);
}
int SceneNode::childCount() const
{
return childs.count();
}
void SceneNode::add(SceneObject *child)
{
childs.append(child);
}
void SceneNode::remove(int number)
{
delete childs.takeAt(number);
}
SceneObject *SceneNode::find(int id)
{
foreach (SceneObject *obj, childs) {
......@@ -30,7 +52,6 @@ void SceneNode::draw()
{
glPushMatrix();
applyTransformation();
foreach (SceneObject *obj, childs) {
obj->draw();
}
......
......@@ -11,10 +11,19 @@ private:
QList<SceneObject*> childs;
public:
SceneNode(QObject *parent);
SceneNode();
~SceneNode();
SceneObject *children(int number);
int childCount() const;
QList<SceneObject*> getChildren(){return childs;}
virtual bool isNode(){return true;qDebug()<<"isNode";}
SceneObject *find(int id);
void draw();
void add(SceneObject *child);
void remove(int number);
};
#endif // SCENENODE_H
#include "sceneobject.h"
#include "scenenode.h"
SceneObject::SceneObject(QObject *parent)
:QObject(parent)
SceneObject::SceneObject()
{
id = 3;
name = QString("Scene Object "+id);
......@@ -10,6 +10,57 @@ SceneObject::SceneObject(QObject *parent)
}
SceneObject *SceneObject::getParent()
{
return parent;
}
SceneObject *SceneObject::children(int /* number*/)
{
return NULL;
}
int SceneObject::childCount() const
{
return 0;
}
int SceneObject::childNumber() const
{
if (parent && parent->isNode()){
SceneNode* node = static_cast<SceneNode*>(parent);
node->getChildren().indexOf(const_cast<SceneObject*>(this));
}
return 0;
}
int SceneObject::columnCount() const
{
return 1+itemData.count();
}
QVariant SceneObject::data(int column) const
{
//qDebug()<<"Data "<<column<<"="<<name;
if(column == 0) return name;
return itemData.value(column);
}
bool SceneObject::setData(int column, const QVariant &value)
{
if (column < 0 || column >= itemData.size())
return false;
itemData[column] = value;
return true;
}
int SceneObject::getID(){return id;}
QString SceneObject::getName(){return name;}
......
......@@ -4,6 +4,7 @@
#include <QObject>
#include <QQuaternion>
#include <QVector3D>
#include <QVector>
#include <QtOpenGL>
#include <gl/GLU.h>
......@@ -17,13 +18,30 @@ protected:
QString name;
void applyTransformation();
QVector<QVariant> itemData;
SceneObject *parent;
public:
SceneObject(QObject *parent = 0);
SceneObject();
void setParent(SceneObject *parent){this->parent = parent;}
SceneObject *getParent();
virtual SceneObject *children(int number);
virtual bool isNode(){return false;}
int childNumber() const;
virtual int childCount() const;
int columnCount() const;
QVariant data(int column) const;
bool setData(int column, const QVariant &value);
virtual void draw();
virtual SceneObject *find(int id);
int getID();
QString getName();
void setName(QString name){this->name = name;}
void move(QVector3D dir);
void rotate(QQuaternion rot);
......
......@@ -9,8 +9,8 @@ int ScenePrimitive::torusCount = 1;
int ScenePrimitive::coneCount = 1;
ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation, QObject *parent)
:SceneObject(parent)
ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation)
:SceneObject()
{
this->type = type;
this->tesselation = tesselation;
......@@ -36,7 +36,6 @@ ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation, QObject *par
break;
case Cylinder:
name = QString("Cylinder %1").arg(ScenePrimitive::cylinderCount);
qDebug()<<ScenePrimitive::cylinderCount;
ScenePrimitive::cylinderCount++;
this->tesselation = 5*pow(2,tesselation);
break;
......@@ -54,13 +53,13 @@ ScenePrimitive::ScenePrimitive(PrimitiveType type, int tesselation, QObject *par
qDebug()<<"Enum Error";
break;
}
// qDebug()<<"Tesselation"<<this->tesselation<<getName();
// qDebug()<<"Tesselation"<<this->tesselation<<getName();
}
void ScenePrimitive::draw(){
glPushMatrix();
applyTransformation();
// qDebug()<<this->getName()<<"Matrial"<<*color<<"Trans"<<translation<<"Rot"<<rotation;
glMaterialfv(GL_FRONT,GL_AMBIENT,color);
GLfloat white[] = {1.0,1.0,1.0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,color);
......
......@@ -28,8 +28,9 @@ public:
void draw();
void setMaterial(float *color);
ScenePrimitive(PrimitiveType type, int tesselation, QObject *parent);
ScenePrimitive(PrimitiveType type, int tesselation);
};
#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