Commit 194f25be by Alisa Jung

shaderauswahl spinnt, rest funktioniert grade.

parent b06d9373
...@@ -10,6 +10,20 @@ Controller::~Controller(){ ...@@ -10,6 +10,20 @@ Controller::~Controller(){
} }
void Controller::initActions(QAction *cam, QAction *manipulate){
modeCameraAction = cam;
modeManipulateAction = manipulate;
}
void Controller::initWidget(MyGLWidget *w){
currentWidget = w;
}
void Controller::setCtrlPressed(bool isCtrlPressed){
ctrlPressed = isCtrlPressed;
}
void Controller::switchToModeCamera(){ void Controller::switchToModeCamera(){
qDebug("Bla"); qDebug("Bla");
modeCamera = true; modeCamera = true;
...@@ -22,6 +36,17 @@ void Controller::switchToModeManipulate(){ ...@@ -22,6 +36,17 @@ void Controller::switchToModeManipulate(){
void Controller::processMousePressEvent(QMouseEvent *event, MyGLWidget *widget){ void Controller::processMousePressEvent(QMouseEvent *event, MyGLWidget *widget){
rightButtonPressed = false; rightButtonPressed = false;
leftButtonPressed = false; leftButtonPressed = false;
if (ctrlPressed){
if (modeCamera){
modeCamera = false;
modeCameraAction->setChecked(false);
modeManipulateAction->setChecked(true);
} else{
modeCamera = true;
modeCameraAction->setChecked(true);
modeManipulateAction->setChecked(false);
}
}
if (event->button() == Qt::RightButton){ if (event->button() == Qt::RightButton){
rightButtonPressed = true; rightButtonPressed = true;
lastClickPosition = event->pos(); lastClickPosition = event->pos();
...@@ -30,8 +55,7 @@ void Controller::processMousePressEvent(QMouseEvent *event, MyGLWidget *widget){ ...@@ -30,8 +55,7 @@ void Controller::processMousePressEvent(QMouseEvent *event, MyGLWidget *widget){
leftButtonPressed = true; leftButtonPressed = true;
lastClickPosition = event->pos(); lastClickPosition = event->pos();
} }
currentWidget = widget;
//TODO if widget was not selected before, set active.
} }
void Controller::processMouseMoveEvent(QMouseEvent *event, MyGLWidget *widget){ void Controller::processMouseMoveEvent(QMouseEvent *event, MyGLWidget *widget){
...@@ -57,6 +81,12 @@ void Controller::processMouseMoveEvent(QMouseEvent *event, MyGLWidget *widget){ ...@@ -57,6 +81,12 @@ void Controller::processMouseMoveEvent(QMouseEvent *event, MyGLWidget *widget){
} }
} }
void Controller::resetCamera(){
if (currentWidget){
currentWidget->resetCamera();
}
}
void Controller::setScreenScenter(QPoint center){ void Controller::setScreenScenter(QPoint center){
screenCenter = center; screenCenter = center;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <QGLShader> #include <QGLShader>
#include <QDebug> #include <QDebug>
#include <QObject> #include <QObject>
#include <QAction>
class MyGLWidget; class MyGLWidget;
...@@ -23,6 +24,9 @@ public: ...@@ -23,6 +24,9 @@ public:
Controller(); Controller();
~Controller(); ~Controller();
void initActions(QAction* cam, QAction* manipulate);
void initWidget(MyGLWidget* w);
//processes input and might call follow up functions in widget //processes input and might call follow up functions in widget
void processMousePressEvent(QMouseEvent* event, MyGLWidget* widget); void processMousePressEvent(QMouseEvent* event, MyGLWidget* widget);
...@@ -31,9 +35,18 @@ public: ...@@ -31,9 +35,18 @@ public:
void processMouseMoveEvent(QMouseEvent* event, MyGLWidget* widget); void processMouseMoveEvent(QMouseEvent* event, MyGLWidget* widget);
void setScreenScenter(QPoint center); void setScreenScenter(QPoint center);
void setCtrlPressed(bool isCtrlPressed);
private: private:
QAction *modeCameraAction;
QAction *modeManipulateAction;
MyGLWidget* currentWidget;
bool modeCamera; bool modeCamera;
bool ctrlPressed = false;
bool rightButtonPressed; bool rightButtonPressed;
bool leftButtonPressed; bool leftButtonPressed;
QPoint screenCenter;//Center of screen in pixel coordinates QPoint screenCenter;//Center of screen in pixel coordinates
...@@ -45,6 +58,7 @@ private: ...@@ -45,6 +58,7 @@ private:
public slots: public slots:
void switchToModeCamera(); void switchToModeCamera();
void switchToModeManipulate(); void switchToModeManipulate();
void resetCamera();
}; };
......
...@@ -76,16 +76,15 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -76,16 +76,15 @@ MainWindow::MainWindow(QWidget *parent)
setStatusBar(statusBar); setStatusBar(statusBar);
//4.0 Widget //Assignment 2: Multiple Widgets. //4.0 Widget //Assignment 2: Multiple Widgets.
qDebug("widgets...");
controller = new Controller(); controller = new Controller();
myGLWidget = new MyGLWidget(controller, true, QQuaternion(),this); myGLWidget = new MyGLWidget(controller, true, QQuaternion(),this);
setCentralWidget(myGLWidget); setCentralWidget(myGLWidget);
controller->initWidget(myGLWidget);
viewFront = new MyGLWidget(controller, false, QQuaternion(0,1,0,0),this); viewFront = new MyGLWidget(controller, false, QQuaternion(),this);
double costhetahalf = cos(45); viewLeft = new MyGLWidget(controller, false, QQuaternion::fromAxisAndAngle(0,1,0,90),this);//TODO check rotations.
viewLeft = new MyGLWidget(controller, true, QQuaternion(costhetahalf,1,0,0),this);//TODO check rotations. viewTop = new MyGLWidget(controller, false, QQuaternion::fromAxisAndAngle(1,0,0,90),this);
viewTop = new MyGLWidget(controller, false, QQuaternion(costhetahalf,0,1,0),this);
viewLayout = new QGridLayout(this);
viewSplitter1 = new QSplitter; viewSplitter1 = new QSplitter;
viewSplitter1->addWidget(myGLWidget); viewSplitter1->addWidget(myGLWidget);
...@@ -95,24 +94,19 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -95,24 +94,19 @@ MainWindow::MainWindow(QWidget *parent)
viewSplitter2->addWidget(viewLeft); viewSplitter2->addWidget(viewLeft);
viewSplitter2->addWidget(viewTop); viewSplitter2->addWidget(viewTop);
viewSplitter3 = new QSplitter;
viewSplitter3->setOrientation(Qt::Vertical); viewSplitter3->setOrientation(Qt::Vertical);
viewSplitter3->addWidget(viewSplitter1); viewSplitter3->addWidget(viewSplitter1);
viewSplitter3->addWidget(viewSplitter2); viewSplitter3->addWidget(viewSplitter2);
viewSplitter3 = new QSplitter; viewLayout = new QGridLayout(this);
viewLayout->addWidget(viewSplitter3); viewLayout->addWidget(viewSplitter3);
QWidget* central_widget = new QWidget(this); QWidget* central_widget = new QWidget(this);
central_widget->setLayout(viewLayout); central_widget->setLayout(viewLayout);
setCentralWidget(central_widget); setCentralWidget(central_widget);
qDebug("tesselation...");
//4.1.1 connect shading actions to slot
connect(shadeWireframeAction,SIGNAL(triggered(bool)),myGLWidget,SLOT(shadeWireframeSlot()));
connect(shadeFlatAction,SIGNAL(triggered(bool)),myGLWidget,SLOT(shadeFlatSlot()));
connect(shadeGouraudAction,SIGNAL(triggered(bool)),myGLWidget,SLOT(shadeGouraudSlot()));
connect(shadePhongAction,SIGNAL(triggered(bool)),myGLWidget,SLOT(shadePhongSlot()));
//tesselation slider //tesselation slider
tesselationSlider = new QSlider(Qt::Horizontal,this); tesselationSlider = new QSlider(Qt::Horizontal,this);
...@@ -122,19 +116,29 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -122,19 +116,29 @@ MainWindow::MainWindow(QWidget *parent)
tesselationSlider->setMinimum(1); tesselationSlider->setMinimum(1);
tesselationSlider->setMaximum(16); tesselationSlider->setMaximum(16);
tesselationSlider->setValue(1); tesselationSlider->setValue(1);
connect(tesselationSlider,SIGNAL(valueChanged(int)),myGLWidget,SLOT(setTessellation(int)));
toolBar->addWidget(tesselationSlider); toolBar->addWidget(tesselationSlider);
//4.1.1 connect shading actions to slot
MyGLWidget* allWidgets[4] = {myGLWidget, viewFront, viewLeft, viewTop};
for (int i = 0; i < 4; i++){
connect(shadeWireframeAction,SIGNAL(triggered(bool)),allWidgets[i],SLOT(shadeWireframeSlot()));
connect(shadeFlatAction,SIGNAL(triggered(bool)),allWidgets[i],SLOT(shadeFlatSlot()));
connect(shadeGouraudAction,SIGNAL(triggered(bool)),allWidgets[i],SLOT(shadeGouraudSlot()));
connect(shadePhongAction,SIGNAL(triggered(bool)),allWidgets[i],SLOT(shadePhongSlot()));
connect(tesselationSlider,SIGNAL(valueChanged(int)),allWidgets[i],SLOT(setTessellation(int)));
}
//reset Camera button //reset Camera button
resetCameraAction = new QAction("Reset Camera",toolBar); resetCameraAction = new QAction("Reset Camera",toolBar);
resetCameraAction->setIcon(QIcon(":/img/cam_home.png")); resetCameraAction->setIcon(QIcon(":/img/cam_home.png"));
connect(resetCameraAction,SIGNAL(triggered(bool)),myGLWidget,SLOT(resetCamera())); connect(resetCameraAction,SIGNAL(triggered(bool)),controller,SLOT(resetCamera()));
toolBar->addAction(resetCameraAction); toolBar->addAction(resetCameraAction);
toolBar->insertSeparator(resetCameraAction); toolBar->insertSeparator(resetCameraAction);
/* Assignment 2*/ /* Assignment 2*/
qDebug("Camera / Object Manipulation...");
//2 Camera Mode, Object Manipulation Mode //2 Camera Mode, Object Manipulation Mode
modeCameraAction = new QAction("Camera Mode",toolBar); modeCameraAction = new QAction("Camera Mode",toolBar);
modeCameraAction->setIcon(QIcon(":/grapa-a2-iconset/camera.png")); modeCameraAction->setIcon(QIcon(":/grapa-a2-iconset/camera.png"));
...@@ -152,12 +156,14 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -152,12 +156,14 @@ MainWindow::MainWindow(QWidget *parent)
toolBar->addAction(modeManipulateAction); toolBar->addAction(modeManipulateAction);
connect(modeManipulateAction,SIGNAL(triggered(bool)),controller,SLOT(switchToModeManipulate())); connect(modeManipulateAction,SIGNAL(triggered(bool)),controller,SLOT(switchToModeManipulate()));
controller->initActions(modeCameraAction, modeManipulateAction);
modeSwitchGroup = new QActionGroup(toolBar); modeSwitchGroup = new QActionGroup(toolBar);
modeSwitchGroup->addAction(modeCameraAction); modeSwitchGroup->addAction(modeCameraAction);
modeSwitchGroup->addAction(modeManipulateAction); modeSwitchGroup->addAction(modeManipulateAction);
qDebug("View switch...");
//2 Switch view modes //2 Switch view modes
viewButton = new QToolButton(toolBar); viewButton = new QToolButton(toolBar);
...@@ -194,7 +200,7 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -194,7 +200,7 @@ MainWindow::MainWindow(QWidget *parent)
viewMenu->addAction(quadViewAction); viewMenu->addAction(quadViewAction);
viewGroup->addAction(quadViewAction); viewGroup->addAction(quadViewAction);
qDebug("Done initializing");
} }
......
#include "myglwidget.h" #include "myglwidget.h"
#include "controller.h" #include "controller.h"
//QGLShaderProgram *MyGLWidget::phongShader;
MyGLWidget::MyGLWidget(Controller* c, bool isPerspective, QQuaternion initialRotation, QWidget *parent) MyGLWidget::MyGLWidget(Controller* c, bool isPerspective, QQuaternion initialRotation, QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{ {
...@@ -8,6 +10,7 @@ MyGLWidget::MyGLWidget(Controller* c, bool isPerspective, QQuaternion initialRot ...@@ -8,6 +10,7 @@ MyGLWidget::MyGLWidget(Controller* c, bool isPerspective, QQuaternion initialRot
tesselation = 1; tesselation = 1;
qubeRotation = QQuaternion(); qubeRotation = QQuaternion();
cameraRotation = initialRotation; cameraRotation = initialRotation;
cameraStartRotation = initialRotation;
translatex = 0; translatex = 0;
translatey = 0; translatey = 0;
camRotCenter = QVector3D(0,0,0); camRotCenter = QVector3D(0,0,0);
...@@ -53,32 +56,13 @@ void MyGLWidget::initializeGL() ...@@ -53,32 +56,13 @@ void MyGLWidget::initializeGL()
{ {
glClearColor(0.0, 0.0, 0.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
qDebug("InitGPL");
//Phong Shader: // if (!phongShader){
QGLShader* vert = new QGLShader(QGLShader::Vertex); qDebug("Init shader stuff");
qDebug() << "new vert shader"; initShaderStuff();
QGLShader* frag = new QGLShader(QGLShader::Fragment); // }else {
qDebug() << "new frag shader"; // qDebug() << " phong not null: " << phongShader;
// }
bool e = QFile::exists(":/phong.vert");
qDebug() << "vert exists" << e;
bool successVertex = vert->compileSourceFile(":/phong.vert");
if (!successVertex)qDebug() << "Vertex compilation failed.";
else qDebug() << "Vertex compiled.";
bool successFragment = frag->compileSourceFile(":/phong.frag");
if (!successFragment) qDebug() << "Frag failed";
else qDebug() << "frag success";
phongShader = new QGLShaderProgram(this);
phongShader->addShader(vert);
phongShader->addShader(frag);
phongShader->link();
shadeFlatSlot();//set up flat shading
qDebug() << "flat done";
// glEnable(GL_CULL_FACE);//backface culling. // glEnable(GL_CULL_FACE);//backface culling.
...@@ -110,8 +94,36 @@ example, the background color. ...@@ -110,8 +94,36 @@ example, the background color.
glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);//set Position of light source. glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);//set Position of light source.
} }
void MyGLWidget::paintGL(){ void MyGLWidget::initShaderStuff(){
//Phong Shader:
QGLShader* vert = new QGLShader(QGLShader::Vertex);
qDebug() << "new vert shader";
QGLShader* frag = new QGLShader(QGLShader::Fragment);
qDebug() << "new frag shader";
bool e = QFile::exists(":/phong.vert");
qDebug() << "vert exists" << e;
bool successVertex = vert->compileSourceFile(":/phong.vert");
if (!successVertex)qDebug() << "Vertex compilation failed.";
else qDebug() << "Vertex compiled.";
bool successFragment = frag->compileSourceFile(":/phong.frag");
if (!successFragment) qDebug() << "Frag failed";
else qDebug() << "frag success";
phongShader = new QGLShaderProgram(this);
phongShader->addShader(vert);
phongShader->addShader(frag);
phongShader->link();
shadeFlatSlot();//set up flat shading
qDebug() << "FlatSlot setup.";
}
void MyGLWidget::paintGL(){
qDebug("paint gl");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); glLoadIdentity();
...@@ -280,6 +292,19 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){ ...@@ -280,6 +292,19 @@ void MyGLWidget:: mouseMoveEvent(QMouseEvent *event){
controller->processMouseMoveEvent(event,this); controller->processMouseMoveEvent(event,this);
} }
void MyGLWidget::keyPressEvent(QKeyEvent* event){
if (event->key() == Qt::Key_Control){
controller->setCtrlPressed(true);
}
}
void MyGLWidget::keyReleaseEvent(QKeyEvent *event){
if (event->key() == Qt::Key_Control){
controller->setCtrlPressed(false);
qDebug("ctrl released");
}
}
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.
...@@ -372,6 +397,6 @@ void MyGLWidget::resetCamera(){ ...@@ -372,6 +397,6 @@ void MyGLWidget::resetCamera(){
translatey = 0; translatey = 0;
camRotCenter = QVector3D(0,0,0); camRotCenter = QVector3D(0,0,0);
cameraZoom = cameraZoomDefault; cameraZoom = cameraZoomDefault;
cameraRotation = QQuaternion(); cameraRotation = cameraStartRotation;
updateGL(); updateGL();
} }
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
class Controller; class Controller;
class MyGLWidget : public QGLWidget class MyGLWidget : public QGLWidget
{ {
Q_OBJECT //from HelloGL Example Q_OBJECT //from HelloGL Example
...@@ -35,6 +36,8 @@ class MyGLWidget : public QGLWidget ...@@ -35,6 +36,8 @@ class MyGLWidget : public QGLWidget
void rotateCamera(QQuaternion diff); void rotateCamera(QQuaternion diff);
void rotateQube(QQuaternion diff); void rotateQube(QQuaternion diff);
void initShaderStuff();
protected: protected:
//4.1 Core Functionality //4.1 Core Functionality
void initializeGL(); void initializeGL();
...@@ -43,6 +46,8 @@ protected: ...@@ -43,6 +46,8 @@ 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);
void keyPressEvent(QKeyEvent * event);
void keyReleaseEvent(QKeyEvent *event);
Controller* controller; Controller* controller;
...@@ -55,6 +60,7 @@ private: ...@@ -55,6 +60,7 @@ private:
const double cameraZoomDefault = -5; const double cameraZoomDefault = -5;
double cameraZoom; double cameraZoom;
QQuaternion cameraRotation; QQuaternion cameraRotation;
QQuaternion cameraStartRotation;
QVector3D camRotCenter; QVector3D camRotCenter;
//translate camera //translate camera
......
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