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\
scenegraph.cpp \
primitive.cpp \
rigidbodytransformation.cpp \
controller.cpp \
box.cpp \
sphere.cpp
controller.cpp
HEADERS += mainwindow.h \
myglwidget.h \
scenegraph.h \
primitive.h \
rigidbodytransformation.h \
controller.h \
box.h \
sphere.h
controller.h
RESOURCES += \
helloqube.qrc
......
......@@ -18,8 +18,6 @@ MyGLWidget::MyGLWidget(int index, Controller* c, bool isPerspective, QQuaternion
this->isPerspective = isPerspective;
this->isFocused = isPerspective; //start with focus on perspective view
platzhalter = "bla";
primitive = new Box(0,platzhalter,1);
scene = controller->getSceneGraph();
scene->registerView(this);
}
......@@ -148,8 +146,51 @@ void MyGLWidget::paintGL(){
glMultMatrixf(camRot.data());
glTranslatef(camRotCenter.x(),camRotCenter.y(),camRotCenter.z());
scene->drawAll();
//rbt->drawChild();
// scene->drawAll();
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
if (isFocused) highlightViewport();
......@@ -215,6 +256,8 @@ void MyGLWidget::resetProjectionMatrix(){
}
/*User Input*/
void MyGLWidget::mousePressEvent(QMouseEvent *event){
controller->processMousePressEvent(event,this);
}
......@@ -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)
{
//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(){
}
void MyGLWidget::resetCamera(){
camRotCenter = QVector3D(0,0,0);
cameraZoom = cameraZoomDefault;
cameraRotation = cameraStartRotation;
updateGL();
}
/*Notification for changed model*/
void MyGLWidget::modelChanged(){
qDebug("update view");
updateGL();
}
//4.1.2 User Input: Zooming
void MyGLWidget::wheelEvent(QWheelEvent *event){
cameraZoom += 0.01*event->delta();//0.01 seemed to be a good factor
qDebug() << "Cam zoom delta " << event->delta() << " stored " << cameraZoom;
updateGL();
void MyGLWidget::drawSphere(int tesselation){
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();
}
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(){
updateGL();
}
void MyGLWidget::resetCamera(){
camRotCenter = QVector3D(0,0,0);
cameraZoom = cameraZoomDefault;
cameraRotation = cameraStartRotation;
updateGL();
}
......@@ -15,8 +15,6 @@
#include <QStylePainter>
#include <rigidbodytransformation.h>//vorläufig, bis szenengraph steht.
#include <box.h>
class Controller;
class SceneGraph;
......@@ -87,6 +85,12 @@ private:
bool isFocused;
SceneGraph* scene;
void drawSphere(int tesselation);
void drawBox(int tesselation);
void drawCylinder(int tesselation);
void drawCone(int tesselation);
void drawTorus(int tesselation);
public slots:
//4.1.1 slots for shading modes
void shadeWireframeSlot();
......
......@@ -13,12 +13,17 @@ QString Primitive::getName(){
return name;
}
Primitive::Primitive(int ID, QString name, int tesselation){
Primitive::Primitive(int ID, QString name, int tesselation, Type t){
this->ID = ID;
this->name = name;
this->tesselation = tesselation;
this->type = t;
}
void Primitive::drawPrimitive(){
int Primitive::getTesselation(){
return tesselation;
}
Primitive::Type Primitive::getType(){
return type;
}
......@@ -6,11 +6,15 @@
class Primitive
{
public:
enum Type {SPHERE, BOX, CYLINDER, CONE, TORUS};
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();
......@@ -18,6 +22,7 @@ protected:
int ID;
QString name;
int tesselation;
Type type;
};
#endif // PRIMITIVE_H
......@@ -2,27 +2,22 @@
RigidBodyTransformation::RigidBodyTransformation(Primitive* child)
{
tx = 0;
ty = 0;
tz = 0;
translation = QVector3D(0,0,0);
rotation = QQuaternion();
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){
tx += diff.x();
ty += diff.y();
tz += diff.z();
translation += diff;
}
const QVector3D RigidBodyTransformation::getTranslation(){
return translation;
}
QQuaternion RigidBodyTransformation::getRotation(){
const QQuaternion RigidBodyTransformation::getRotation(){
return rotation;
}
......@@ -35,26 +30,6 @@ void RigidBodyTransformation::setRotation(QQuaternion newRotation){
rotation = newRotation;
}
void RigidBodyTransformation::drawChild(){
//should be easily extendable to drawChildren
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();
Primitive* RigidBodyTransformation::getChild(){
return child;
}
......@@ -9,17 +9,19 @@ class RigidBodyTransformation
public:
RigidBodyTransformation(Primitive* child);
void setTranslation(double x, double y, double z);
void addTranslation(QVector3D diff);
void setRotation(QQuaternion newRotation);
void addRotation(QQuaternion diff);
QQuaternion getRotation();
const QQuaternion getRotation();
const QVector3D getTranslation();
Primitive* getChild();
void drawChild();
private :
//translation
double tx, ty, tz;
QVector3D translation;
//rotation
QQuaternion rotation;
......
......@@ -14,41 +14,34 @@ SceneGraph::SceneGraph()
}
//Scenegraph gets a list of all views it needs to notify on changes
void SceneGraph::registerView(MyGLWidget* view){
views.append(view);
}
//add primitives to sceneGraph
RigidBodyTransformation* SceneGraph::addSphere(int tesselation){
nextSphereName = "Sphere " + sphereIndex;
//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++;
sphereIndex++;
qDebug() << "New Sphere added in scenegraph." << nextSphereName;
RigidBodyTransformation* r = new RigidBodyTransformation(s);
nodes.append(r);
notifyViews();
return r;
return addPrimitive(p);
}
RigidBodyTransformation* SceneGraph::addBox(int tesselation){
nextBoxName = "Box " + boxIndex;
Box* b = new Box(idCounter,nextBoxName,tesselation);
Primitive* p = new Primitive(idCounter, nextBoxName, tesselation, Primitive::Type::BOX);
idCounter++;
boxIndex++;
qDebug("New box added in scenegraph");
RigidBodyTransformation* r = new RigidBodyTransformation(b);
nodes.append(r);
qDebug("Notify Views...");
notifyViews();
return r;
return addPrimitive(p);
}
RigidBodyTransformation* SceneGraph::addCylinder(int tesselation){
......@@ -66,14 +59,14 @@ RigidBodyTransformation* SceneGraph::addTorus(int tesselation){
return addBox(1);
}
void SceneGraph::drawAll(){
QList<RigidBodyTransformation*>::iterator i;
for (i = nodes.begin(); i != nodes.end(); ++i){
(*i)->drawChild();
}
RigidBodyTransformation* SceneGraph::addPrimitive(Primitive* p){
RigidBodyTransformation* r = new RigidBodyTransformation(p);
nodes.append(r);
notifyViews();
return r;
}
//transform something
void SceneGraph::addRotation(RigidBodyTransformation *r, QQuaternion diff){
r->addRotation(diff);
notifyViews();
......@@ -84,6 +77,12 @@ void SceneGraph::addTranslation(RigidBodyTransformation *r, QVector3D diff){
notifyViews();
}
//for drawing
const QList<RigidBodyTransformation*> SceneGraph::getGraph(){
return nodes;
}
//notifies views whenever something changes (is added or transformed)
void SceneGraph::notifyViews(){
QList<MyGLWidget*>::iterator i;
for (i = views.begin(); i != views.end(); i++){
......
......@@ -3,8 +3,6 @@
#include <QList>
#include <rigidbodytransformation.h>
#include <sphere.h>
#include <box.h>
class MyGLWidget;
......@@ -26,6 +24,8 @@ public:
void addTranslation(RigidBodyTransformation* r, QVector3D diff);
void addRotation(RigidBodyTransformation* r, QQuaternion diff);
const QList<RigidBodyTransformation*> getGraph();
//Hallo. Todos:
//Log selected name in status bar
//add remaining primitive types
......@@ -46,6 +46,7 @@ private:
QString nextConeName;
QString nextTorusName;
RigidBodyTransformation* addPrimitive(Primitive* p);
QList<RigidBodyTransformation*> nodes;
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