diff --git a/mainwindow.ui b/mainwindow.ui index 3283213..84fa443 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 588 - 499 + 649 + 560 @@ -15,7 +15,7 @@ - + @@ -29,12 +29,6 @@ 500 - - - 500 - 500 - - QFrame::StyledPanel @@ -45,6 +39,9 @@ + + 6 + @@ -131,7 +128,7 @@ 0 0 - 588 + 649 20 diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 4d4783b..2c15b12 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -100,86 +100,20 @@ void Viewer::draw() // Traverse the Scene in order to draw its components modelStack.push(modelViewMatrix); - //root.accept(*this); + root.accept(*this); + update(); } void Viewer::mouseMoveEvent(QMouseEvent* e) { - cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; + //cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; // Normal QGLViewer behavior. //QGLViewer::mouseMoveEvent(e); } void Viewer::mousePressEvent(QMouseEvent* e) { // Auto Return, but old code left in as reference - return; - - cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl; - - if(e->button() == 1){ // LMB - - int truY = 10 - (e->y()) / 500.0 * GRID_SIZE; - int truX = (e->x() / 500.0) * GRID_SIZE; - - cout << " -->Getting cell at " << truX << " : " << truY << endl; - - SceneGroup* row = dynamic_cast (root.childAt(truY)); - cout << " -->" << row << endl; - SceneGroup* cell = dynamic_cast (row->childAt(truX)); - cout << " -->" << cell << endl; - - if(e->modifiers() & Qt::ShiftModifier){ - // add a shape - if(!cell->getChildren()->size()){ - - // WARNING: CODE DEGEULASSE - - Shape* s = nullptr; - if(activeShape == 1){ - s = new Triangle(); - }else if(activeShape == 2){ - s = new Square(); - }else if(activeShape == 3){ - s = new Circle(); - } - - // WARNING: END OF CODE DEGEULASSE - - //activeCell->getChildren()->at(0) = s; - if(s != nullptr){ - s->setColor(*activeColor); - cell->addChild(s); - this->update(); - deselect(); - activeCell = cell; - } - } - } - - if(e->modifiers() & Qt::ControlModifier){ - // select a shape - deselect(); - activeCell = cell; - if(activeCell != nullptr && activeCell->getChildren()->size()){ - std::cout << "Cell has children..." << endl; - Shape* shape = dynamic_cast (activeCell->childAt(0)); - QColor newColor = shape->getColor(); - std::cout << newColor.Rgb << endl; - newColor.setAlpha(255); - shape->setColor(newColor); - //emit shapeSelected(getTypeIndex(typeof activeCell->childAt(0))); - int shapeId = 0; - if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle)) - shapeId = 1; - if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square)) - shapeId = 2; - if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle)) - shapeId = 3; - emit shapeSelected(shapeId); - }else{ - emit shapeSelected(0); - } - } - } + //std::cout << "Picking shape at " << e->pos().x() << ", " << e->pos().y() << endl; + Shape* selectedShape = pickGeom(e->pos().x()+20, e->pos().y()+20); } void Viewer::deselect(){ @@ -198,7 +132,7 @@ void Viewer::deselect(){ } void Viewer::mouseReleaseEvent(QMouseEvent* e) { - cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl; + //cout << "Viewer::mouseReleaseEvent(QMouseEvent* e)" << endl; //QGLViewer::mouseReleaseEvent(e); } @@ -224,12 +158,21 @@ void Viewer::init() initShaders(); initGeometries(); //initGrid(); + + /*QColor* color = new QColor(255, 128, 0, 255); + Circle* c = new Circle; + c->setColor(*color); + SceneGroup* cell = new SceneGroup; + cell->addChild(c); + root.addChild(cell);*/ + //update(); } void Viewer::initShaders() { // Load vertex and fragment shaders m_program = new QOpenGLShaderProgram; + colorPickerShaderProgram = m_program; if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/basicShader.vert")) { cerr << "Unable to load Shader" << endl << "Log file:" << endl; @@ -256,6 +199,34 @@ void Viewer::initShaders() if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) qDebug() << "Unable to find shader location for " << "projMatrix"; + + + /* + * Adding Texture Shader + * + */ + textureRenderShaderprogram = new QOpenGLShaderProgram; + if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Vertex, "src/shaders/textureShader.vert")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << textureRenderShaderprogram->log(); + } + if (!textureRenderShaderprogram->addShaderFromSourceFile(QOpenGLShader::Fragment, "src/shaders/textureShader.frag")) { + cerr << "Unable to load Shader" << endl + << "Log file:" << endl; + qDebug() << textureRenderShaderprogram->log(); + } + + textureRenderShaderprogram->link(); + + if ((m_vPositionLocation = m_program->attributeLocation("vPosition")) < 0) + qDebug() << "Unable to find shader location for " << "vPosition"; + + if ((m_mvMatrixLocation = m_program->uniformLocation("mvMatrix")) < 0) + qDebug() << "Unable to find shader location for " << "mvMatrix"; + + if ((m_projMatrixLocation = m_program->uniformLocation("projMatrix")) < 0) + qDebug() << "Unable to find shader location for " << "projMatrix"; } // Creates the basic shapes in memory. We only have 3, so we just prep them all in advance. @@ -384,3 +355,55 @@ void Viewer::changeColor(QColor c){ activeColor = new QColor(c); this->update(); } + +Shape* Viewer::pickGeom(int x, int y){ + QMap mapColorToShape; + QColor c; + QRgb startColor = 0xFF000033; // alpha must be 100%, glReadPixels doesn't resturn alpha + unsigned char pixelData[3]; + // Traverse tree + /* TODO: Make this recurse through SceneGroups, with like "populateMap(map, root, color)" + * Right now it's fine because we have simple Minecraft rules + * but could be important in the future + */ + QOpenGLShaderProgram *lastshader = m_program; + + //colorPickerShaderProgram->bind(); + + std::cout << "Iterating through " << root.getChildren()->size() << "items"<size(); i(root.childAt(i)); + Shape* currentCube; + + if(current->getChildren()->size()) + { + currentCube = dynamic_cast(current->childAt(0)); + } + + if(currentCube) + { + c.setRgba(startColor); + mapColorToShape.insert(c.rgba(), currentCube); + std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl; + currentCube->setColor(c); + startColor++; + } + } + root.accept(*this); + update(); + draw(); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + // TODO: figure out how to get this weird offset ↓↓ frae position maybe? + glReadPixels(x, camera()->screenHeight() - 1 - y + 62, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); + QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]); + Shape* pickedShape = mapColorToShape.value(pickedColor->rgba()); + std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl; + std::cout << "Picked Shape: " << pickedShape << endl; + + //lastshader->bind(); + return pickedShape; +} diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index dc4fe02..fbff682 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -36,7 +36,9 @@ QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram) -class Viewer : public QGLViewer, protected QOpenGLFunctions_4_0_Core, public Visitor +class Viewer : public QGLViewer, + protected QOpenGLFunctions_4_0_Core, + public Visitor { Q_OBJECT public: @@ -70,13 +72,18 @@ private: void initShaders(); void initGeometries(); void deselect(); + Shape* pickGeom(int, int); + // shader switching variables and constants; + QOpenGLShaderProgram *colorPickerShaderProgram; + QOpenGLShaderProgram *textureRenderShaderprogram; QOpenGLShaderProgram *m_program; int m_vPositionLocation; int m_colorLocation; int m_projMatrixLocation; int m_mvMatrixLocation; + SceneGroup* activeCell; QColor* activeColor; int activeShape; @@ -88,6 +95,7 @@ private: GLuint m_Buffers[NumBuffers]; Shape* generateShapeFromIndex(int); + }; #endif // SIMPLEVIEWER_H