diff --git a/src/glnodes/shapes.cpp b/src/glnodes/shapes.cpp index 22c57fd..e03cc4b 100644 --- a/src/glnodes/shapes.cpp +++ b/src/glnodes/shapes.cpp @@ -14,12 +14,19 @@ void Sphere::accept(Visitor &v) { // *** void Cube::setColor(QColor& c) { - std::cout << "Setting cubr color! " << c.rgb() << endl; - color = QColor(c); + color = QColor(c); } QColor Cube::getColor(){ - return color; + return color; +}; + +void Cube::setType(int t) { + this->type = t; +} + +int Cube::getType(){ + return this->type; }; void Sphere::setColor(QColor& c) { @@ -29,3 +36,11 @@ void Sphere::setColor(QColor& c) { QColor Sphere::getColor(){ return color; }; + +void Sphere::setType(int t) { + this->type = t; +} + +int Sphere::getType(){ + return this->type; +}; diff --git a/src/glnodes/shapes.h b/src/glnodes/shapes.h index 21765ec..5f53a31 100644 --- a/src/glnodes/shapes.h +++ b/src/glnodes/shapes.h @@ -9,25 +9,35 @@ class Shape : public GlNode public: virtual void setColor(QColor& c) = 0; virtual QColor getColor() = 0; + virtual void setType(int) = 0; + virtual int getType() = 0; }; class Cube : public Shape { +private: + int type = 0; public: Cube(){} QColor color; - void accept(Visitor& v) override; - void setColor(QColor& c); + void accept(Visitor& v) override; + void setColor(QColor& c); QColor getColor(); + void setType(int); + int getType(); }; class Sphere : public Shape { +private: + int type = 0; public: Sphere(){} QColor color; void accept(Visitor& v) override; void setColor(QColor& c); - QColor getColor(); + QColor getColor(); + void setType(int); + int getType(); }; #endif // SHAPES_H diff --git a/src/shaders/basicShader.frag b/src/shaders/basicShader.frag index 76e5388..d22ff4d 100644 --- a/src/shaders/basicShader.frag +++ b/src/shaders/basicShader.frag @@ -91,7 +91,7 @@ main() + calcPointLight(texColor, fPosition, fNormal, 1)/4 + calcPointLight(texColor, fPosition, fNormal, 2)/4; } else { - fColor = texColor * ifColor; + fColor = texColor + ifColor; } } } diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 2f2fb92..6411630 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -182,11 +182,21 @@ void Viewer::mousePressEvent(QMouseEvent* e) { // TODO: figure out how to get this weird offset ↓↓ frame position maybe? int x = this->x() + e->x() - 10; int y = this->parentWidget()->height() - e->y() - 23; - 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; - Shape* selectedShape = pickGeom(x, y); - // OpenGL coords start at the window's bottom-left, not the frame's. ugh. + 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; + + QMatrix4x4 selectedPosition = pickGeom(x, y); + if(!selectedPosition.isIdentity()) + { + Cube* c = new Cube; + c->setType(TEX_WOODFLOOR); + SceneGroup* container = new SceneGroup; + //selectedPosition.copyDataTo(container->transform.data()); + container->transform = selectedPosition; + container->addChild(c); + root.addChild(container); + } QGLViewer::mousePressEvent(e); } @@ -201,6 +211,7 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::init() { + identityMatrix.setToIdentity(); SkyboxCamera *_cam = new SkyboxCamera(); setCamera(_cam); //camera()->setType(qglviewer::Camera::PERSPECTIVE); @@ -229,55 +240,64 @@ void Viewer::init() { - QColor* c1 = new QColor(255, 0, 255, 255); - QColor* c2 = new QColor(0, 255, 255, 255); - QColor* c3 = new QColor(255, 255, 0, 255); + QColor* c1 = new QColor(255, 0, 255, 255); + QColor* c2 = new QColor(0, 255, 255, 255); + QColor* c3 = new QColor(255, 255, 0, 255); - Shape* s1 = new Sphere(); - Shape* s2 = new Sphere(); - Shape* s3 = new Sphere(); + Shape* s1 = new Sphere(); + Shape* s2 = new Sphere(); + Shape* s3 = new Sphere(); + selection = new SceneGroup(); + selection->addChild(s1); + selection->addChild(s2); + selection->addChild(s3); + + //SceneGroup *sc1 = new SceneGroup(); + //sc1->addChild(selection); + //root.addChild(sc1); + root.addChild(selection); + + s1->transform.rotate(360 * 1/3,0,1,0); + s1->transform.translate(1,0,0); + s1->transform.scale(0.05); + s1->setColor(*c1); + s2->transform.rotate(360 * 2/3,0,1,0); + s2->transform.translate(1,0,0); + s2->transform.scale(0.05); + s2->setColor(*c2); + s3->transform.rotate(360 * 3/3,0,1,0); + s3->transform.translate(1,0,0); + s3->transform.scale(0.05); + s3->setColor(*c3); + + + for(int i = 0; i < 10; i++){ + for(int j = 0; j < 10; j++){ + Shape* cube = new Cube(); + SceneGroup *sc = new SceneGroup(); + cube->setColor(*c1); + cube->setType(1); + sc->addChild(cube); + sc->transform.translate(-5+i, -5, -5+j); + root.addChild(sc); + } + } + /* Shape* cube1 = new Cube(); Shape* cube2 = new Cube(); Shape* cube3 = new Cube(); cube1->setColor(*c1); cube2->setColor(*c2); cube3->setColor(*c3); - cube2->transform.translate(1, 0, 0); - cube3->transform.translate(0, 0, 1); - - s1->transform.rotate(360 * 1/3,0,1,0); - s1->transform.translate(1,0,0); - s1->transform.scale(0.05); - s1->setColor(*c1); - s2->transform.rotate(360 * 2/3,0,1,0); - s2->transform.translate(1,0,0); - s2->transform.scale(0.05); - s2->setColor(*c2); - s3->transform.rotate(360 * 3/3,0,1,0); - s3->transform.translate(1,0,0); - s3->transform.scale(0.05); - s3->setColor(*c3); + cube2->transform.translate(3, 0, 0); + cube3->transform.translate(-3, 0, 0); + //*/ // uncomment this and the quaternion transformation in draw() // to see the cube rotate. This is how we can easily make the sun. // cube->transform.translate(3, 0, 0); - selection = new SceneGroup(); - selection->addChild(s1); - selection->addChild(s2); - selection->addChild(s3); - - SceneGroup *sc1 = new SceneGroup(); - SceneGroup *sc2 = new SceneGroup(); - SceneGroup *sc3 = new SceneGroup(); - sc1->addChild(cube1); - sc1->addChild(selection); - sc2->addChild(cube2); - sc3->addChild(cube3); - root.addChild(sc1); - root.addChild(sc2); - root.addChild(sc3); } } @@ -378,6 +398,7 @@ void Viewer::initShaders() // load remaining textures for(int i = 0; isetMinificationFilter(QOpenGLTexture::LinearMipMapLinear); TexturePrograms[i]->setMagnificationFilter(QOpenGLTexture::Linear); @@ -611,26 +632,26 @@ void Viewer::initBuffers(){ void Viewer::visit(Cube &s) { QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); + QColor* faceColor = new QColor; - int faces = floor(numVerticesCube/6); glBindVertexArray(m_VAOs[VAO_Cube]); + TexturePrograms[s.getType()]->bind(); + m_program->setUniformValue(m_isSkyLoc, false); + m_program->setUniformValue(m_drawTextLoc, true); + m_program->setUniformValue(m_isLightLoc, false); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); + + int faces = floor(numVerticesCube/6); for(int i =0; isetRgb(s.getColor().rgb() + i); - m_program->bind(); - m_program->setUniformValue(m_isSkyLoc, false); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - m_program->setUniformValue(m_normalMatrixLoc, modelViewMatrix.normalMatrix()); m_program->setUniformValue(m_colorLocation, *faceColor); - m_program->setUniformValue(m_drawTextLoc, true); - m_program->setUniformValue(m_isLightLoc, false); glDrawArrays(GL_TRIANGLES, i*6, 6); - glFinish(); - delete faceColor; } + delete faceColor; //glDrawArrays(GL_TRIANGLES, 0, 36); } @@ -705,32 +726,29 @@ void Viewer::changeColor(QColor c){ this->update(); } -Shape* Viewer::pickGeom(int x, int y){ +QMatrix4x4 Viewer::pickGeom(int x, int y){ makeCurrent(); m_program->bind(); m_program->setUniformValue(m_isPickingModeLoc, true); - //glReadBuffer(GL_COLOR_ATTACHMENT0); - //glBindFramebuffer(GL_FRAMEBUFFER, m_RenderBuffers[RenderBuffer_Main]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - isPickingActivated = true; - QMap mapColorToShape; + QMap mapColorToPosition; QColor c; - QRgb startColor = 0x1; // 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]; // Give each cube a color, let each cube manage their face-color assignment - std::cout << "Iterating through " << root.getChildren()->size() << " items"<size() << " items"<size(); i++) { - std::cout << " iterating... " << i << endl; - SceneGroup* current = dynamic_cast(root.getChild()); + //std::cout << " iterating... " << i << endl; + SceneGroup* current = dynamic_cast(root.childAt(i)); Shape* currentCube; if(current->getChildren()->size()) @@ -738,16 +756,23 @@ Shape* Viewer::pickGeom(int x, int y){ currentCube = dynamic_cast(current->childAt(0)); } + c.setRgb(startColor); currentCube->setColor(c); - if(currentCube) for (int j = 0; j<6; j++) + if(currentCube) for (int j = 0; j<3; j++) { - c.setRgb(startColor); -// QMatrix4x4 direction; direction.translate(0, 1, 0); - mapColorToShape.insert(c.rgb(), currentCube/*->transform * direction*/); - std::cout << "Setting " << currentCube << " to " << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha() << endl; - startColor++; + for(int i = -1; i<=1; i+=2) + { + QMatrix4x4 currentTransform = QMatrix4x4(current->transform); + int components[3] = {0, 0, 0}; + components[j] = i; + //std::cout << "Setting " << startColor << " to {" << components[0] << ", " << components[0] << ", " << components[1] << ", " << components[2] << "}\n"; + currentTransform.translate(components[1], -components[2], -components[0]); + mapColorToPosition.insert(startColor, currentTransform); + startColor++; + } } } + root.accept(*this); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); @@ -772,15 +797,15 @@ Shape* Viewer::pickGeom(int x, int y){ glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); QColor* pickedColor = new QColor(pixelData[0], pixelData[1], pixelData[2]); - unsigned int pickedInt = pickedColor->rgb(); - Shape* pickedShape = mapColorToShape.value(pickedInt - (pickedInt % 6) + 1); - std::cout << "Picked Color: " << pickedColor->red() << " " << pickedColor->green() << " " << pickedColor->blue() << " " << pickedColor->alpha() << endl; - std::cout << "Picked Shape: " << pickedShape << endl; + unsigned int pickedInt = pickedColor->rgb() % 0x1000000; // rgb returns ARGB. go figure. + //std::cout << "Picked " << pickedInt << endl; + QMatrix4x4 pickedPosition = mapColorToPosition.value(pickedInt, identityMatrix); + mapColorToPosition.clear(); m_program->setUniformValue(m_isPickingModeLoc, false); isPickingActivated = false; doneCurrent(); - return pickedShape; + return pickedPosition; } diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index e7129c1..fd58f40 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -75,7 +75,7 @@ private: void initGeometries(); void initBuffers(); void deselect(); - Shape* pickGeom(int, int); + QMatrix4x4 pickGeom(int, int); // shader switching variables and constants; QOpenGLShaderProgram *colorPickerShaderProgram; @@ -162,7 +162,7 @@ private: }; QOpenGLTexture *TexturePrograms[TEX_LENGTH]; - + QMatrix4x4 identityMatrix; QQuaternion rot; unsigned int frame;