Merge branch 'master' of github.com:fataku/LOG750-LAB2

This commit is contained in:
Dmitri K 2016-11-18 20:17:03 -05:00
commit d86f533f94
11 changed files with 216 additions and 29 deletions

View File

@ -174,8 +174,30 @@
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="menuInterpolation"/> <addaction name="menuInterpolation"/>
</widget> </widget>
<widget class="QMenu" name="menuFiltrage">
<property name="title">
<string>Filtrage</string>
</property>
<widget class="QMenu" name="menuMag_Filter">
<property name="title">
<string>Mag Filter</string>
</property>
<addaction name="actionMagNear"/>
<addaction name="actionMagLinear"/>
</widget>
<widget class="QMenu" name="menuMin_Filter">
<property name="title">
<string>Min Filter</string>
</property>
<addaction name="actionMinLin"/>
<addaction name="actionMinNear"/>
</widget>
<addaction name="menuMag_Filter"/>
<addaction name="menuMin_Filter"/>
</widget>
<addaction name="menu_File"/> <addaction name="menu_File"/>
<addaction name="menuAffichage"/> <addaction name="menuAffichage"/>
<addaction name="menuFiltrage"/>
</widget> </widget>
<widget class="QStatusBar" name="statusBar"> <widget class="QStatusBar" name="statusBar">
<property name="enabled"> <property name="enabled">
@ -220,6 +242,38 @@
<string>Gouraud</string> <string>Gouraud</string>
</property> </property>
</action> </action>
<action name="actionMagNear">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Nearest</string>
</property>
</action>
<action name="actionMagLinear">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Linear</string>
</property>
</action>
<action name="actionMinLin">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Nearest</string>
</property>
</action>
<action name="actionMinNear">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Linear MipMap Linear</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources/> <resources/>

View File

@ -4,10 +4,13 @@
#include "../interfaces/ivisitable.h" #include "../interfaces/ivisitable.h"
#include "../interfaces/visitor.h" #include "../interfaces/visitor.h"
class Viewer; class Viewer;
class SceneGroup;
class GlNode : public IVisitable { class GlNode : public IVisitable {
public: public:
QMatrix4x4 transform; QMatrix4x4 transform;
virtual SceneGroup* getParent() = 0;
virtual void setParent(SceneGroup* s) = 0;
}; };
#endif // GLNODE #endif // GLNODE

View File

@ -32,7 +32,7 @@ bool SceneGroup::hasNext()
} }
void SceneGroup::addChild(GlNode* child){ void SceneGroup::addChild(GlNode* child){
child->setParent(this);
children.push_back(child); children.push_back(child);
//children.push_back(std::shared_ptr<GlNode>(child)); //children.push_back(std::shared_ptr<GlNode>(child));
} }
@ -40,3 +40,11 @@ void SceneGroup::addChild(GlNode* child){
void SceneGroup::accept(Visitor &v) { void SceneGroup::accept(Visitor &v) {
v.visit(*this); v.visit(*this);
} }
SceneGroup* SceneGroup::getParent() {
return _parent;
}
void SceneGroup::setParent(SceneGroup* s) {
_parent = s;
}

View File

@ -10,6 +10,7 @@ class SceneGroup : public GlNode
private: private:
std::vector<GlNode*> children; std::vector<GlNode*> children;
int childIndex = 0; int childIndex = 0;
SceneGroup* _parent;
public: public:
void addChild(GlNode* c); void addChild(GlNode* c);
GlNode* getChild(); GlNode* getChild();
@ -19,6 +20,9 @@ public:
GlNode* childAt(int i); GlNode* childAt(int i);
SceneGroup* getParent();
void setParent(SceneGroup* s);
void accept(Visitor& v) override; void accept(Visitor& v) override;
SceneGroup(); SceneGroup();
}; };

View File

@ -17,6 +17,14 @@ void Cube::setColor(QColor& c) {
color = QColor(c); color = QColor(c);
} }
SceneGroup* Cube::getParent() {
return _parent;
}
void Cube::setParent(SceneGroup* c) {
_parent = c;
}
QColor Cube::getColor(){ QColor Cube::getColor(){
return color; return color;
}; };
@ -41,6 +49,14 @@ void Sphere::setType(int t) {
this->type = t; this->type = t;
} }
SceneGroup* Sphere::getParent() {
return _parent;
}
void Sphere::setParent(SceneGroup* c) {
_parent = c;
}
int Sphere::getType(){ int Sphere::getType(){
return this->type; return this->type;
}; };

View File

@ -4,6 +4,7 @@
#include <QVector4D> #include <QVector4D>
#include <QColor> #include <QColor>
#include "../interfaces/visitor.h" #include "../interfaces/visitor.h"
class SceneGroup;
class Shape : public GlNode class Shape : public GlNode
{ {
public: public:
@ -11,12 +12,15 @@ public:
virtual QColor getColor() = 0; virtual QColor getColor() = 0;
virtual void setType(int) = 0; virtual void setType(int) = 0;
virtual int getType() = 0; virtual int getType() = 0;
virtual SceneGroup* getParent() = 0;
virtual void setParent(SceneGroup* s) = 0;
}; };
class Cube : public Shape class Cube : public Shape
{ {
private: private:
int type = 0; int type = 0;
SceneGroup* _parent;
public: public:
Cube(){} Cube(){}
QColor color; QColor color;
@ -25,12 +29,15 @@ public:
QColor getColor(); QColor getColor();
void setType(int); void setType(int);
int getType(); int getType();
SceneGroup* getParent();
void setParent(SceneGroup* s);
}; };
class Sphere : public Shape class Sphere : public Shape
{ {
private: private:
int type = 0; int type = 0;
SceneGroup* _parent;
public: public:
Sphere(){} Sphere(){}
QColor color; QColor color;
@ -39,5 +46,7 @@ public:
QColor getColor(); QColor getColor();
void setType(int); void setType(int);
int getType(); int getType();
SceneGroup* getParent();
void setParent(SceneGroup* s);
}; };
#endif // SHAPES_H #endif // SHAPES_H

View File

@ -89,10 +89,10 @@ main()
vec3 nfNormal = normalize(fNormal); vec3 nfNormal = normalize(fNormal);
vec3 nviewDirection = normalize(fPosition); vec3 nviewDirection = normalize(fPosition);
fColor = calcDirLight(texColor, fPosition, fNormal) fColor = calcDirLight(texColor, fPosition, fNormal)
+ calcPointLight(texColor, fPosition, fNormal, 0)/4 + calcPointLight(texColor, fPosition, fNormal, 0)/4
+ calcPointLight(texColor, fPosition, fNormal, 1)/4 + calcPointLight(texColor, fPosition, fNormal, 1)/4
+ calcPointLight(texColor, fPosition, fNormal, 2)/4; + calcPointLight(texColor, fPosition, fNormal, 2)/4;
} else { } else {
fColor = texColor + ifColor; fColor = texColor + ifColor;
} }

View File

@ -61,6 +61,7 @@ namespace
QMatrix4x4 sunRotate; QMatrix4x4 sunRotate;
SceneGroup* selection; SceneGroup* selection;
PickedGeom selectedObj;
int currentPoint = 0; // VERY lazy way of tracking light balls int currentPoint = 0; // VERY lazy way of tracking light balls
} }
@ -187,17 +188,32 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl; std::cout << "--------------------------------------------------\nPicking shape at " << x << " (" << this->x() << " + " << e->x() << "), " << y << endl;
std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl; std::cout << "Window geom: " << this->window()->size().width() << "w, " << this->window()->size().height() << "h" << endl;
QMatrix4x4 selectedPosition = pickGeom(x, y); PickedGeom selectedGeom = pickGeom(x, y);
if(!selectedPosition.isIdentity() && e->modifiers().testFlag(Qt::ShiftModifier))
{ if(!selectedGeom.position.isIdentity() && e->modifiers().testFlag(Qt::ControlModifier))
Cube* c = new Cube; {
c->setType(TEX_WOODFLOOR); if(selectedObj.shape->getParent() != nullptr) {
SceneGroup* container = new SceneGroup; // Remove previous selection
//selectedPosition.copyDataTo(container->transform.data()); selectedObj.shape->getParent()->getChildren()->erase(
container->transform = selectedPosition; std::remove(selectedObj.shape->getParent()->getChildren()->begin(),
container->addChild(c); selectedObj.shape->getParent()->getChildren()->end(),
root.addChild(container); selection),
} else QGLViewer::mousePressEvent(e); selectedObj.shape->getParent()->getChildren()->end());
}
selectedObj.shape = selectedGeom.shape;
selectedObj.shape->getParent()->addChild(selection);
}
if(!selectedGeom.position.isIdentity() && e->modifiers().testFlag(Qt::ShiftModifier))
{
Cube* c = new Cube;
c->setType(TEX_WOODFLOOR);
SceneGroup* container = new SceneGroup;
//selectedPosition.copyDataTo(container->transform.data());
container->transform = selectedGeom.position;
container->addChild(c);
root.addChild(container);
} else QGLViewer::mousePressEvent(e);
} }
void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::mouseReleaseEvent(QMouseEvent* e) {
@ -236,6 +252,8 @@ void Viewer::init()
{ {
// Default non existant cube
selectedObj.shape = new Cube();
QColor* c1 = new QColor(255, 0, 255, 255); QColor* c1 = new QColor(255, 0, 255, 255);
QColor* c2 = new QColor(0, 255, 255, 255); QColor* c2 = new QColor(0, 255, 255, 255);
@ -252,8 +270,7 @@ void Viewer::init()
//SceneGroup *sc1 = new SceneGroup(); //SceneGroup *sc1 = new SceneGroup();
//sc1->addChild(selection); //sc1->addChild(selection);
//root.addChild(sc1); //root.addChild(sc1);
root.addChild(selection);
s1->transform.rotate(360 * 1/3,0,1,0); s1->transform.rotate(360 * 1/3,0,1,0);
s1->transform.translate(0.3,1,0); s1->transform.translate(0.3,1,0);
@ -268,7 +285,6 @@ void Viewer::init()
s3->transform.scale(0.05); s3->transform.scale(0.05);
s3->setColor(*c3); s3->setColor(*c3);
for(int i = 0; i < 10; i++){ for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){ for(int j = 0; j < 10; j++){
Shape* cube = new Cube(); Shape* cube = new Cube();
@ -284,6 +300,23 @@ void Viewer::init()
} }
} }
void Viewer::resetSphereColors(){
QColor* c1 = new QColor(255, 0, 255, 255);
QColor* c2 = new QColor(0, 255, 255, 255);
QColor* c3 = new QColor(255, 255, 0, 255);
int i = 0;/*
QColor* colors[3] = {c1, c2, c3};
while(selection.hasNext()) {
Shape currentSphere = dynamic_cast<Shape*>(selection->getChild());
currentSphere->setColor(colors[i++]);
}*/
}
void Viewer::initShaders() void Viewer::initShaders()
{ {
@ -716,13 +749,44 @@ void Viewer::visit(SceneGroup &s)
void Viewer::setPhong(bool on) { void Viewer::setPhong(bool on) {
m_program->bind(); m_program->bind();
m_program->setUniformValue(m_isPhongLoc, on); m_program->setUniformValue(m_isPhongLoc, on);
if(on) std::cout << "Phong ON\n"; if(on) std::cout << "Phong ON\n";
else std::cout << "Phong OFF\n"; else std::cout << "Phong OFF\n";
this->update(); this->update();
} }
void Viewer::setMagLinear(bool on) {
m_program->bind();
if(on) {
std::cout << "Linear ON" << endl;
s_texture->setMagnificationFilter(QOpenGLTexture::Linear);
} else {
std::cout << "Nearest ON" << endl;
s_texture->setMagnificationFilter(QOpenGLTexture::Nearest);
}
this->update();
}
void Viewer::setMinLinear(bool on) {
m_program->bind();
if(on) {
std::cout << "Linear MipMap Linear ON" << endl;
s_texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
} else {
std::cout << "Nearest ON" << endl;
s_texture->setMinificationFilter(QOpenGLTexture::Nearest);
}
this->update();
}
void Viewer::changeColor(QColor c){ void Viewer::changeColor(QColor c){
if(activeCell->getChildren()->size()){ if(activeCell->getChildren()->size()){
Shape* shape = dynamic_cast<Shape*> (activeCell->childAt(0)); Shape* shape = dynamic_cast<Shape*> (activeCell->childAt(0));
@ -732,7 +796,7 @@ void Viewer::changeColor(QColor c){
this->update(); this->update();
} }
QMatrix4x4 Viewer::pickGeom(int x, int y){ PickedGeom Viewer::pickGeom(int x, int y){
makeCurrent(); makeCurrent();
@ -744,6 +808,7 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){
isPickingActivated = true; isPickingActivated = true;
QMap<QRgb, QMatrix4x4> mapColorToPosition; QMap<QRgb, QMatrix4x4> mapColorToPosition;
QMap<QRgb, Shape*> mapColorToShape;
QColor c; QColor c;
QRgb startColor = 1; // alpha must be 100%, glReadPixels doesn't resturn alpha QRgb startColor = 1; // alpha must be 100%, glReadPixels doesn't resturn alpha
unsigned char pixelData[3]; unsigned char pixelData[3];
@ -774,6 +839,7 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){
//std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n"; //std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n";
currentTransform.translate(components[1], -components[2], -components[0]); currentTransform.translate(components[1], -components[2], -components[0]);
mapColorToPosition.insert(startColor, currentTransform); mapColorToPosition.insert(startColor, currentTransform);
mapColorToShape.insert(startColor, currentCube);
startColor++; startColor++;
} }
} }
@ -807,11 +873,17 @@ QMatrix4x4 Viewer::pickGeom(int x, int y){
//std::cout << "Picked " << pickedInt << endl; //std::cout << "Picked " << pickedInt << endl;
QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix); QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix);
Shape* pickedShape = mapColorToShape.value(pickedInt);
mapColorToPosition.clear(); mapColorToPosition.clear();
m_program->setUniformValue(m_isPickingModeLoc, false); m_program->setUniformValue(m_isPickingModeLoc, false);
isPickingActivated = false; isPickingActivated = false;
doneCurrent(); doneCurrent();
return pickedPosition; PickedGeom result = PickedGeom();
result.position = pickedPosition;
result.shape = pickedShape;
return result;
} }

View File

@ -37,6 +37,12 @@
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram) QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
struct PickedGeom {
Shape* shape;
QMatrix4x4 position;
};
class Viewer : public QGLViewer, class Viewer : public QGLViewer,
protected QOpenGLFunctions_4_0_Core, protected QOpenGLFunctions_4_0_Core,
public Visitor public Visitor
@ -54,6 +60,8 @@ public slots:
void cleanup(); void cleanup();
void changeColor(QColor); void changeColor(QColor);
void setPhong(bool); void setPhong(bool);
void setMagLinear(bool);
void setMinLinear(bool);
signals: signals:
int shapeSelected(int); int shapeSelected(int);
@ -71,11 +79,12 @@ protected :
std::stack<QMatrix4x4> modelStack; std::stack<QMatrix4x4> modelStack;
private: private:
void resetSphereColors();
void initShaders(); void initShaders();
void initGeometries(); void initGeometries();
void initBuffers(); void initBuffers();
void deselect(); void deselect();
QMatrix4x4 pickGeom(int, int); PickedGeom pickGeom(int, int);
// shader switching variables and constants; // shader switching variables and constants;
QOpenGLShaderProgram *colorPickerShaderProgram; QOpenGLShaderProgram *colorPickerShaderProgram;

View File

@ -19,6 +19,14 @@ MainWindow::MainWindow(QWidget *parent) :
lightTypeGroup->addAction(ui->actionGouraud); lightTypeGroup->addAction(ui->actionGouraud);
lightTypeGroup->addAction(ui->actionPhong); lightTypeGroup->addAction(ui->actionPhong);
minFilterGroup = new QActionGroup(this);
minFilterGroup->addAction(ui->actionMinLin);
minFilterGroup->addAction(ui->actionMinNear);
magFilterGroup = new QActionGroup(this);
magFilterGroup->addAction(ui->actionMagLinear);
magFilterGroup->addAction(ui->actionMagNear);
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(onColorPickerActivate())); connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(onColorPickerActivate()));
} }
@ -35,6 +43,8 @@ void MainWindow::addViewer(Viewer* viewer)
// actionGouraud shouldn't need one, since they are mutual // actionGouraud shouldn't need one, since they are mutual
connect(ui->actionPhong, SIGNAL(toggled(bool)), viewer, SLOT(setPhong(bool))); connect(ui->actionPhong, SIGNAL(toggled(bool)), viewer, SLOT(setPhong(bool)));
connect(ui->actionMagLinear, SIGNAL(toggled(bool)), viewer, SLOT(setMagLinear(bool)));
connect(ui->actionMinLin, SIGNAL(toggled(bool)), viewer, SLOT(setMinLinear(bool)));
} }
void MainWindow::onColorPickerActivate(){ void MainWindow::onColorPickerActivate(){

View File

@ -26,8 +26,10 @@ public slots:
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QColorDialog *colordialog; QColorDialog *colordialog;
QActionGroup *lightTypeGroup; QActionGroup *lightTypeGroup;
QActionGroup *minFilterGroup;
QActionGroup *magFilterGroup;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H