Commit e41ab5d8 by Alisa Jung

refactored: no primitive subclasses, enum instead. Drawing code in Widget (=view)

parent 154e4e6c
#include "box.h"
Box::Box(int ID, QString name, int tesselation) : Primitive(ID, name, tesselation)
{
qDebug() << "adding box " << ID;
}
Box::~Box(){
}
void Box::drawPrimitive(){
//tesselation
double qubeWidth = 1.0;
double dist = qubeWidth / (double)tesselation;
//4.1.1 Unit Qube + Tesselation + Diffuse Material
GLfloat specularColor[] = { 1, 1, 1 };
GLfloat shininess[] = { 128 };//specular exponent
glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
//vorne // z-Achse
// glColor3f(0,0,1);//blau //alt und doof
GLfloat blue[] = { 0, 0, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);
glNormal3f(0, 0, 1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, 0.5 - dist*j, 0.5);
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*j, 0.5);
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*(j + 1), 0.5);
glVertex3f(0.5 - dist*i, 0.5 - dist*(j + 1), 0.5);
}
}
glEnd();
//oben // y-Achse
GLfloat green[] = { 0, 1, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*(i + 1), 0.5, 0.5 - dist*(j + 1));
glVertex3f(0.5 - dist*(i + 1), 0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*i, 0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*i, 0.5, 0.5 - dist*(j + 1));
}
}
glEnd();
//rechts //x-Achse
GLfloat red[] = { 1, 0, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
glNormal3f(1, 0, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5, 0.5 - dist*i, 0.5 - dist*j);
glVertex3f(0.5, 0.5 - dist*(i + 1), 0.5 - dist*j);
glVertex3f(0.5, 0.5 - dist*(i + 1), 0.5 - dist*(j + 1));
glVertex3f(0.5, 0.5 - dist*i, 0.5 - dist*(j + 1));
}
}
glEnd();
//hinten // z-Achse
GLfloat yellow[] = { 1, 0.9f, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
glNormal3f(0, 0, -1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, 0.5 - dist*(j + 1), -0.5);//4
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*(j + 1), -0.5);//3
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*j, -0.5);//2
glVertex3f(0.5 - dist*i, 0.5 - dist*j, -0.5);//1
}
}
glEnd();
//links
GLfloat cyan[] = { 0, 1, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
glNormal3f(-1, 0, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(-0.5, 0.5 - dist*i, 0.5 - dist*(j + 1));
glVertex3f(-0.5, 0.5 - dist*(i + 1), 0.5 - dist*(j + 1));
glVertex3f(-0.5, 0.5 - dist*(i + 1), 0.5 - dist*j);
glVertex3f(-0.5, 0.5 - dist*i, 0.5 - dist*j);
}
}
glEnd();
//unten //-y
GLfloat magenta[] = { 1, 0, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, magenta);
glNormal3f(0, -1, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, -0.5, 0.5 - dist*(j + 1));
glVertex3f(0.5 - dist*i, -0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*(i + 1), -0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*(i + 1), -0.5, 0.5 - dist*(j + 1));
}
}
glEnd();
}
#ifndef BOX_H
#define BOX_H
#include <primitive.h>
#include <QDebug>
class Box : public Primitive
{
public:
Box(int ID, QString name, int tesselation);
~Box();
void drawPrimitive() override;
};
#endif // BOX_H
...@@ -18,18 +18,14 @@ SOURCES += main.cpp\ ...@@ -18,18 +18,14 @@ SOURCES += main.cpp\
scenegraph.cpp \ scenegraph.cpp \
primitive.cpp \ primitive.cpp \
rigidbodytransformation.cpp \ rigidbodytransformation.cpp \
controller.cpp \ controller.cpp
box.cpp \
sphere.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
myglwidget.h \ myglwidget.h \
scenegraph.h \ scenegraph.h \
primitive.h \ primitive.h \
rigidbodytransformation.h \ rigidbodytransformation.h \
controller.h \ controller.h
box.h \
sphere.h
RESOURCES += \ RESOURCES += \
helloqube.qrc helloqube.qrc
......
...@@ -18,8 +18,6 @@ MyGLWidget::MyGLWidget(int index, Controller* c, bool isPerspective, QQuaternion ...@@ -18,8 +18,6 @@ MyGLWidget::MyGLWidget(int index, Controller* c, bool isPerspective, QQuaternion
this->isPerspective = isPerspective; this->isPerspective = isPerspective;
this->isFocused = isPerspective; //start with focus on perspective view this->isFocused = isPerspective; //start with focus on perspective view
platzhalter = "bla";
primitive = new Box(0,platzhalter,1);
scene = controller->getSceneGraph(); scene = controller->getSceneGraph();
scene->registerView(this); scene->registerView(this);
} }
...@@ -148,8 +146,51 @@ void MyGLWidget::paintGL(){ ...@@ -148,8 +146,51 @@ void MyGLWidget::paintGL(){
glMultMatrixf(camRot.data()); glMultMatrixf(camRot.data());
glTranslatef(camRotCenter.x(),camRotCenter.y(),camRotCenter.z()); glTranslatef(camRotCenter.x(),camRotCenter.y(),camRotCenter.z());
scene->drawAll(); // scene->drawAll();
//rbt->drawChild();
const QList<RigidBodyTransformation*> graph = scene->getGraph();
QList<RigidBodyTransformation*>::const_iterator i;
for (i = graph.begin(); i != graph.end(); ++i){
//Set up Transformation:
//rotation
const QQuaternion rot = (*i)->getRotation();
QMatrix4x4 m = QMatrix4x4();
m.rotate(rot);
//translation
const QVector3D pos = (*i)->getTranslation();
QMatrix4x4 translateRot1 = QMatrix4x4(1,0,0,pos.x(),0,1,0,pos.y(),0,0,1,pos.z(),0,0,0,1);
//stack matrices
glPushMatrix();
glMultMatrixf(translateRot1.data());//Punkte zurück schieben damit rotation um qube zentrum ist
glMultMatrixf(m.data());
//draw primitive
Primitive* p = (*i)->getChild();
int tesselation = p->getTesselation();
Primitive::Type type = p->getType();
if (type == Primitive::Type::SPHERE){
drawSphere(tesselation);
}
else if (type == Primitive::Type::BOX){
drawBox(tesselation);
}else if (type == Primitive::Type::CYLINDER){
drawCylinder(tesselation);
}else if (type == Primitive::Type::CONE){
drawCone(tesselation);
} else if (type == Primitive::Type::TORUS){
drawTorus(tesselation);
}else {
qDebug() << "Weird Type." << type;
}
//reset transformation for next primitive
glPopMatrix();
}
//has to be down here or will be overpainted by above code //has to be down here or will be overpainted by above code
if (isFocused) highlightViewport(); if (isFocused) highlightViewport();
...@@ -215,6 +256,8 @@ void MyGLWidget::resetProjectionMatrix(){ ...@@ -215,6 +256,8 @@ void MyGLWidget::resetProjectionMatrix(){
} }
/*User Input*/
void MyGLWidget::mousePressEvent(QMouseEvent *event){ void MyGLWidget::mousePressEvent(QMouseEvent *event){
controller->processMousePressEvent(event,this); controller->processMousePressEvent(event,this);
} }
...@@ -236,6 +279,15 @@ void MyGLWidget::keyReleaseEvent(QKeyEvent *event){ ...@@ -236,6 +279,15 @@ void MyGLWidget::keyReleaseEvent(QKeyEvent *event){
} }
} }
//4.1.2 User Input: Zooming
void MyGLWidget::wheelEvent(QWheelEvent *event){//I don't want to extract this little code to the controller...
cameraZoom += 0.01*event->delta();//0.01 seemed to be a good factor
qDebug() << "Cam zoom delta " << event->delta() << " stored " << cameraZoom;
updateGL();
}
/* View Modification: Camera*/
void MyGLWidget::translateCamera(double xDiff, double yDiff, double zDiff) void MyGLWidget::translateCamera(double xDiff, double yDiff, double zDiff)
{ {
//in order to "move parallel to image plane", we have to rotate the image coordinates xDiff and yDiff by our current camera rotation. //in order to "move parallel to image plane", we have to rotate the image coordinates xDiff and yDiff by our current camera rotation.
...@@ -256,16 +308,156 @@ QQuaternion MyGLWidget::getCameraRotation(){ ...@@ -256,16 +308,156 @@ QQuaternion MyGLWidget::getCameraRotation(){
} }
void MyGLWidget::resetCamera(){
camRotCenter = QVector3D(0,0,0);
cameraZoom = cameraZoomDefault;
cameraRotation = cameraStartRotation;
updateGL();
}
/*Notification for changed model*/
void MyGLWidget::modelChanged(){ void MyGLWidget::modelChanged(){
qDebug("update view"); qDebug("update view");
updateGL(); updateGL();
} }
//4.1.2 User Input: Zooming void MyGLWidget::drawSphere(int tesselation){
void MyGLWidget::wheelEvent(QWheelEvent *event){ GLfloat specularColor[] = { 0.3, 0.5, 0.5 };
cameraZoom += 0.01*event->delta();//0.01 seemed to be a good factor GLfloat shininess[] = { 120 };//specular exponent
qDebug() << "Cam zoom delta " << event->delta() << " stored " << cameraZoom; glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
updateGL(); glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
GLfloat sphereColor[] = { 0, 0.8, 0.8 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, sphereColor);
glNormal3f(0, 0, 1);
glBegin(GL_LINE_LOOP);
GLUquadric* q = gluNewQuadric();
// gluQuadricDrawStyle(q, GLU_FILL);
gluSphere(q, 0.5, tesselation, tesselation);
glEnd();
}
void MyGLWidget::drawBox(int tesselation){
//tesselation
double qubeWidth = 1.0;
double dist = qubeWidth / (double)tesselation;
//4.1.1 Unit Qube + Tesselation + Diffuse Material
GLfloat specularColor[] = { 1, 1, 1 };
GLfloat shininess[] = { 128 };//specular exponent
glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
//vorne // z-Achse
// glColor3f(0,0,1);//blau //alt und doof
GLfloat blue[] = { 0, 0, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);
glNormal3f(0, 0, 1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, 0.5 - dist*j, 0.5);
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*j, 0.5);
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*(j + 1), 0.5);
glVertex3f(0.5 - dist*i, 0.5 - dist*(j + 1), 0.5);
}
}
glEnd();
//oben // y-Achse
GLfloat green[] = { 0, 1, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
glNormal3f(0, 1, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*(i + 1), 0.5, 0.5 - dist*(j + 1));
glVertex3f(0.5 - dist*(i + 1), 0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*i, 0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*i, 0.5, 0.5 - dist*(j + 1));
}
}
glEnd();
//rechts //x-Achse
GLfloat red[] = { 1, 0, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
glNormal3f(1, 0, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5, 0.5 - dist*i, 0.5 - dist*j);
glVertex3f(0.5, 0.5 - dist*(i + 1), 0.5 - dist*j);
glVertex3f(0.5, 0.5 - dist*(i + 1), 0.5 - dist*(j + 1));
glVertex3f(0.5, 0.5 - dist*i, 0.5 - dist*(j + 1));
}
}
glEnd();
//hinten // z-Achse
GLfloat yellow[] = { 1, 0.9f, 0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
glNormal3f(0, 0, -1);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, 0.5 - dist*(j + 1), -0.5);//4
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*(j + 1), -0.5);//3
glVertex3f(0.5 - dist*(i + 1), 0.5 - dist*j, -0.5);//2
glVertex3f(0.5 - dist*i, 0.5 - dist*j, -0.5);//1
}
}
glEnd();
//links
GLfloat cyan[] = { 0, 1, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
glNormal3f(-1, 0, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(-0.5, 0.5 - dist*i, 0.5 - dist*(j + 1));
glVertex3f(-0.5, 0.5 - dist*(i + 1), 0.5 - dist*(j + 1));
glVertex3f(-0.5, 0.5 - dist*(i + 1), 0.5 - dist*j);
glVertex3f(-0.5, 0.5 - dist*i, 0.5 - dist*j);
}
}
glEnd();
//unten //-y
GLfloat magenta[] = { 1, 0, 1 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, magenta);
glNormal3f(0, -1, 0);
glBegin(GL_QUADS);
for (int i = 0; i < tesselation; i++){
for (int j = 0; j < tesselation; j++){
glVertex3f(0.5 - dist*i, -0.5, 0.5 - dist*(j + 1));
glVertex3f(0.5 - dist*i, -0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*(i + 1), -0.5, 0.5 - dist*j);
glVertex3f(0.5 - dist*(i + 1), -0.5, 0.5 - dist*(j + 1));
}
}
glEnd();
}
void MyGLWidget::drawCylinder(int tesselation){
//Todo
}
void MyGLWidget::drawCone(int tesselation){
}
void MyGLWidget::drawTorus(int tesselation){
} }
...@@ -302,9 +494,4 @@ void MyGLWidget::shadePhongSlot(){ ...@@ -302,9 +494,4 @@ void MyGLWidget::shadePhongSlot(){
updateGL(); updateGL();
} }
void MyGLWidget::resetCamera(){
camRotCenter = QVector3D(0,0,0);
cameraZoom = cameraZoomDefault;
cameraRotation = cameraStartRotation;
updateGL();
}
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
#include <QStylePainter> #include <QStylePainter>
#include <rigidbodytransformation.h>//vorläufig, bis szenengraph steht. #include <rigidbodytransformation.h>//vorläufig, bis szenengraph steht.
#include <box.h>
class Controller; class Controller;
class SceneGraph; class SceneGraph;
...@@ -87,6 +85,12 @@ private: ...@@ -87,6 +85,12 @@ private:
bool isFocused; bool isFocused;
SceneGraph* scene; SceneGraph* scene;
void drawSphere(int tesselation);
void drawBox(int tesselation);
void drawCylinder(int tesselation);
void drawCone(int tesselation);
void drawTorus(int tesselation);
public slots: public slots:
//4.1.1 slots for shading modes //4.1.1 slots for shading modes
void shadeWireframeSlot(); void shadeWireframeSlot();
......
...@@ -13,12 +13,17 @@ QString Primitive::getName(){ ...@@ -13,12 +13,17 @@ QString Primitive::getName(){
return name; return name;
} }
Primitive::Primitive(int ID, QString name, int tesselation){ Primitive::Primitive(int ID, QString name, int tesselation, Type t){
this->ID = ID; this->ID = ID;
this->name = name; this->name = name;
this->tesselation = tesselation; this->tesselation = tesselation;
this->type = t;
} }
void Primitive::drawPrimitive(){ int Primitive::getTesselation(){
return tesselation;
}
Primitive::Type Primitive::getType(){
return type;
} }
...@@ -6,11 +6,15 @@ ...@@ -6,11 +6,15 @@
class Primitive class Primitive
{ {
public: public:
enum Type {SPHERE, BOX, CYLINDER, CONE, TORUS};
Primitive(); Primitive();
~Primitive(); ~Primitive();
Primitive(int ID, QString name, int tesselation); Primitive(int ID, QString name, int tesselation, Type t);
virtual void drawPrimitive(); int getTesselation();
Primitive::Type getType();
QString getName(); QString getName();
...@@ -18,6 +22,7 @@ protected: ...@@ -18,6 +22,7 @@ protected:
int ID; int ID;
QString name; QString name;
int tesselation; int tesselation;
Type type;
}; };
#endif // PRIMITIVE_H #endif // PRIMITIVE_H
...@@ -2,27 +2,22 @@ ...@@ -2,27 +2,22 @@
RigidBodyTransformation::RigidBodyTransformation(Primitive* child) RigidBodyTransformation::RigidBodyTransformation(Primitive* child)
{ {
tx = 0; translation = QVector3D(0,0,0);
ty = 0;
tz = 0;
rotation = QQuaternion(); rotation = QQuaternion();
this->child = child; this->child = child;
qDebug() << "new rbt " << tx << "," << ty << "," << tz << ", rot " << rotation; // qDebug() << "new rbt " << tx << "," << ty << "," << tz << ", rot " << rotation;
} }
void RigidBodyTransformation::setTranslation(double x, double y, double z){
tx = x;
ty = y;
tz = z;
}
void RigidBodyTransformation::addTranslation(QVector3D diff){ void RigidBodyTransformation::addTranslation(QVector3D diff){
tx += diff.x(); translation += diff;
ty += diff.y(); }
tz += diff.z();
const QVector3D RigidBodyTransformation::getTranslation(){
return translation;
} }
QQuaternion RigidBodyTransformation::getRotation(){ const QQuaternion RigidBodyTransformation::getRotation(){
return rotation; return rotation;
} }
...@@ -35,26 +30,6 @@ void RigidBodyTransformation::setRotation(QQuaternion newRotation){ ...@@ -35,26 +30,6 @@ void RigidBodyTransformation::setRotation(QQuaternion newRotation){
rotation = newRotation; rotation = newRotation;
} }
void RigidBodyTransformation::drawChild(){ Primitive* RigidBodyTransformation::getChild(){
//should be easily extendable to drawChildren return child;
qDebug() << "Draw " << child->getName() << " at " << tx << "," << ty << "," << tz << ", rot " << rotation;
//set up transform
//for cube rotation
QMatrix4x4 m = QMatrix4x4();
m.rotate(rotation);
//for cube translation
QMatrix4x4 translateRot1 = QMatrix4x4(1,0,0,tx,0,1,0,ty,0,0,1,tz,0,0,0,1);
//stack matrices
glPushMatrix();
glMultMatrixf(translateRot1.data());//Punkte zurück schieben damit rotation um qube zentrum ist
glMultMatrixf(m.data());
//maybe a foreach later on?
child->drawPrimitive();
glPopMatrix();
} }
...@@ -9,17 +9,19 @@ class RigidBodyTransformation ...@@ -9,17 +9,19 @@ class RigidBodyTransformation
public: public:
RigidBodyTransformation(Primitive* child); RigidBodyTransformation(Primitive* child);
void setTranslation(double x, double y, double z);
void addTranslation(QVector3D diff); void addTranslation(QVector3D diff);
void setRotation(QQuaternion newRotation); void setRotation(QQuaternion newRotation);
void addRotation(QQuaternion diff); void addRotation(QQuaternion diff);
QQuaternion getRotation();
const QQuaternion getRotation();
const QVector3D getTranslation();
Primitive* getChild();
void drawChild(); void drawChild();
private : private :
//translation //translation
double tx, ty, tz; QVector3D translation;
//rotation //rotation
QQuaternion rotation; QQuaternion rotation;
......
...@@ -14,41 +14,34 @@ SceneGraph::SceneGraph() ...@@ -14,41 +14,34 @@ SceneGraph::SceneGraph()
} }
//Scenegraph gets a list of all views it needs to notify on changes
void SceneGraph::registerView(MyGLWidget* view){ void SceneGraph::registerView(MyGLWidget* view){
views.append(view); views.append(view);
} }
//add primitives to sceneGraph
RigidBodyTransformation* SceneGraph::addSphere(int tesselation){ RigidBodyTransformation* SceneGraph::addSphere(int tesselation){
nextSphereName = "Sphere " + sphereIndex; nextSphereName = "Sphere " + sphereIndex;
//tesselation < 3 makes no sense. //tesselation < 3 makes no sense.
Sphere* s = new Sphere(idCounter, nextSphereName, tesselation+2); Primitive* p = new Primitive(idCounter, nextSphereName, tesselation+2, Primitive::Type::SPHERE);
idCounter++; idCounter++;
sphereIndex++; sphereIndex++;
qDebug() << "New Sphere added in scenegraph." << nextSphereName; qDebug() << "New Sphere added in scenegraph." << nextSphereName;
RigidBodyTransformation* r = new RigidBodyTransformation(s); return addPrimitive(p);
nodes.append(r);
notifyViews();
return r;
} }
RigidBodyTransformation* SceneGraph::addBox(int tesselation){ RigidBodyTransformation* SceneGraph::addBox(int tesselation){
nextBoxName = "Box " + boxIndex; nextBoxName = "Box " + boxIndex;
Box* b = new Box(idCounter,nextBoxName,tesselation); Primitive* p = new Primitive(idCounter, nextBoxName, tesselation, Primitive::Type::BOX);
idCounter++; idCounter++;
boxIndex++; boxIndex++;
qDebug("New box added in scenegraph"); qDebug("New box added in scenegraph");
RigidBodyTransformation* r = new RigidBodyTransformation(b); return addPrimitive(p);
nodes.append(r);
qDebug("Notify Views...");
notifyViews();
return r;
} }
RigidBodyTransformation* SceneGraph::addCylinder(int tesselation){ RigidBodyTransformation* SceneGraph::addCylinder(int tesselation){
...@@ -66,14 +59,14 @@ RigidBodyTransformation* SceneGraph::addTorus(int tesselation){ ...@@ -66,14 +59,14 @@ RigidBodyTransformation* SceneGraph::addTorus(int tesselation){
return addBox(1); return addBox(1);
} }
void SceneGraph::drawAll(){ RigidBodyTransformation* SceneGraph::addPrimitive(Primitive* p){
RigidBodyTransformation* r = new RigidBodyTransformation(p);
QList<RigidBodyTransformation*>::iterator i; nodes.append(r);
for (i = nodes.begin(); i != nodes.end(); ++i){ notifyViews();
(*i)->drawChild(); return r;
}
} }
//transform something
void SceneGraph::addRotation(RigidBodyTransformation *r, QQuaternion diff){ void SceneGraph::addRotation(RigidBodyTransformation *r, QQuaternion diff){
r->addRotation(diff); r->addRotation(diff);
notifyViews(); notifyViews();
...@@ -84,6 +77,12 @@ void SceneGraph::addTranslation(RigidBodyTransformation *r, QVector3D diff){ ...@@ -84,6 +77,12 @@ void SceneGraph::addTranslation(RigidBodyTransformation *r, QVector3D diff){
notifyViews(); notifyViews();
} }
//for drawing
const QList<RigidBodyTransformation*> SceneGraph::getGraph(){
return nodes;
}
//notifies views whenever something changes (is added or transformed)
void SceneGraph::notifyViews(){ void SceneGraph::notifyViews(){
QList<MyGLWidget*>::iterator i; QList<MyGLWidget*>::iterator i;
for (i = views.begin(); i != views.end(); i++){ for (i = views.begin(); i != views.end(); i++){
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#include <QList> #include <QList>
#include <rigidbodytransformation.h> #include <rigidbodytransformation.h>
#include <sphere.h>
#include <box.h>
class MyGLWidget; class MyGLWidget;
...@@ -26,6 +24,8 @@ public: ...@@ -26,6 +24,8 @@ public:
void addTranslation(RigidBodyTransformation* r, QVector3D diff); void addTranslation(RigidBodyTransformation* r, QVector3D diff);
void addRotation(RigidBodyTransformation* r, QQuaternion diff); void addRotation(RigidBodyTransformation* r, QQuaternion diff);
const QList<RigidBodyTransformation*> getGraph();
//Hallo. Todos: //Hallo. Todos:
//Log selected name in status bar //Log selected name in status bar
//add remaining primitive types //add remaining primitive types
...@@ -46,6 +46,7 @@ private: ...@@ -46,6 +46,7 @@ private:
QString nextConeName; QString nextConeName;
QString nextTorusName; QString nextTorusName;
RigidBodyTransformation* addPrimitive(Primitive* p);
QList<RigidBodyTransformation*> nodes; QList<RigidBodyTransformation*> nodes;
QList<MyGLWidget*> views; QList<MyGLWidget*> views;
......
#include "sphere.h"
#include <gl/GLU.h>
Sphere::Sphere(int ID, QString name, int tesselation) : Primitive(ID, name, tesselation)
{
qDebug() << "adding sphere " << ID;
}
Sphere::~Sphere(){
}
void Sphere::drawPrimitive(){
GLfloat specularColor[] = { 0.3, 0.5, 0.5 };
GLfloat shininess[] = { 120 };//specular exponent
glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
GLfloat sphereColor[] = { 0, 0.8, 0.8 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, sphereColor);
glNormal3f(0, 0, 1);
glBegin(GL_LINE_LOOP);
GLUquadric* q = gluNewQuadric();
// gluQuadricDrawStyle(q, GLU_FILL);
gluSphere(q, 0.5, tesselation, tesselation);
glEnd();
}
#ifndef SPHERE_H
#define SPHERE_H
#include <primitive.h>
#include <QDebug>
class Sphere : public Primitive
{
public:
Sphere(int ID, QString name, int tesselation);
~Sphere();
void drawPrimitive() override;
};
#endif // SPHERE_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