From b94626fb5ddd856c79a6008ec6ca8b2f47b4e496 Mon Sep 17 00:00:00 2001 From: Dmitri K Date: Sun, 30 Oct 2016 23:39:53 -0400 Subject: [PATCH 1/2] Added basic cube and removed unnecessary shapes. to add more cubes, create a GlNode and add a Cube child to it. Then add that GlNode to the SceneGroup. --- src/glnodes/shapes.cpp | 33 +------ src/glnodes/shapes.h | 22 +---- src/interfaces/visitor.h | 8 +- src/main.cpp | 5 +- src/viewer/simpleViewer.cpp | 186 +++++++++++++++--------------------- src/viewer/simpleViewer.h | 11 ++- 6 files changed, 91 insertions(+), 174 deletions(-) diff --git a/src/glnodes/shapes.cpp b/src/glnodes/shapes.cpp index 55de8f2..5ea289c 100644 --- a/src/glnodes/shapes.cpp +++ b/src/glnodes/shapes.cpp @@ -2,43 +2,16 @@ #include #include -void Square::accept(Visitor &v) { - v.visit(*this); -} - -void Circle::accept(Visitor &v) { - v.visit(*this); -} - -void Triangle::accept(Visitor &v) { +void Cube::accept(Visitor &v) { v.visit(*this); } // *** -void Square::setColor(QColor& c) { +void Cube::setColor(QColor& c) { color = QColor(c); } -void Circle::setColor(QColor& c) { - color = QColor(c); -} - -void Triangle::setColor(QColor& c) { - color = QColor(c); -} - - -QColor Triangle::getColor(){ - return color; -}; - - -QColor Circle::getColor(){ - return color; -}; - - -QColor Square::getColor(){ +QColor Cube::getColor(){ return color; }; diff --git a/src/glnodes/shapes.h b/src/glnodes/shapes.h index 2f92c5b..5d058ae 100644 --- a/src/glnodes/shapes.h +++ b/src/glnodes/shapes.h @@ -11,28 +11,10 @@ public: virtual QColor getColor() = 0; }; -class Circle : public Shape +class Cube : public Shape { public: - Circle(){} - QColor color; - void accept(Visitor& v) override; - void setColor(QColor& c); - QColor getColor(); -}; -class Triangle : public Shape -{ -public: - Triangle(){} - QColor color; - void accept(Visitor& v) override; - void setColor(QColor& c); - QColor getColor(); -}; -class Square : public Shape -{ -public: - Square(){} + Cube(){} QColor color; void accept(Visitor& v) override; void setColor(QColor& c); diff --git a/src/interfaces/visitor.h b/src/interfaces/visitor.h index 9786fb9..999f5c7 100644 --- a/src/interfaces/visitor.h +++ b/src/interfaces/visitor.h @@ -1,15 +1,11 @@ #ifndef VISITOR_H #define VISITOR_H class SceneGroup; -class Square; -class Circle; -class Triangle; +class Cube; class Visitor { public: virtual void visit(SceneGroup &n) = 0; - virtual void visit(Square &s) = 0; - virtual void visit(Circle &s) = 0; - virtual void visit(Triangle &s) = 0; + virtual void visit(Cube &s) = 0; }; #endif // VISITOR_H diff --git a/src/main.cpp b/src/main.cpp index 2c5842c..91f87dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,10 +19,7 @@ int main(int argc, char *argv[]) // Instantiate and layout the viewer. Viewer *v = new Viewer(); - w.addViewer(v); - - //w.setFixedSize(dw.width() * 0.7, dw.height() * 0.7); - + w.addViewer(v); w.show(); return a.exec(); diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index 4d4783b..b3b34d3 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -41,9 +41,7 @@ using namespace std; namespace { -const int numVerticesSquare = 5; -const int numVerticesTriangle = 3; -const int numVerticesCircle = 26; +const int numVerticesCube = 12*3; const double frame_limit = 5; const double inc_mult = 5; const double inc_offset = 1.05; @@ -53,7 +51,7 @@ Viewer::Viewer() { activeColor = new QColor(255, 255, 255, 255); activeCell = nullptr; - activeShape = 0; + activeShape = 0; } Viewer::~Viewer() @@ -78,42 +76,48 @@ void Viewer::cleanup() void Viewer::draw() { - // Bind our vertex/fragment shaders - m_program->bind(); + m_program->bind(); // Get projection and camera transformations - QMatrix4x4 projectionMatrix; + QMatrix4x4 projectionMatrix; QMatrix4x4 modelViewMatrix; camera()->getProjectionMatrix(projectionMatrix); camera()->getModelViewMatrix(modelViewMatrix); // Prepare a transformation stack - // stack modelStack; - - modelViewMatrix.translate(-4.5, -4.5); + // stack modelStack; modelViewMatrix.scale(0.95); + modelViewMatrix.rotate(45, 0, 1, 0); - m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + rot = QQuaternion::fromAxisAndAngle(1, 0, 1, 90/60 * frame); + // uncomment this and the cube translation in init() + // to see the cube spin + modelViewMatrix.rotate(rot); + projectionMatrix.translate(0, -5, 0); + projectionMatrix.rotate(15, 1, 0, 0); + m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - // Traverse the Scene in order to draw its components + // Traverse the Scene in order to draw its components - modelStack.push(modelViewMatrix); - //root.accept(*this); + modelStack.push(modelViewMatrix); + root.accept(*this); + frame++; + update(); } void Viewer::mouseMoveEvent(QMouseEvent* e) { cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; // Normal QGLViewer behavior. - //QGLViewer::mouseMoveEvent(e); + //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; + /*cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl; if(e->button() == 1){ // LMB @@ -135,23 +139,23 @@ void Viewer::mousePressEvent(QMouseEvent* e) { Shape* s = nullptr; if(activeShape == 1){ - s = new Triangle(); + s = new Triangle(); }else if(activeShape == 2){ - s = new Square(); + s = new Square(); }else if(activeShape == 3){ - s = new Circle(); + s = new Circle(); } // WARNING: END OF CODE DEGEULASSE - //activeCell->getChildren()->at(0) = s; + //activeCell->getChildren()->at(0) = s; if(s != nullptr){ s->setColor(*activeColor); cell->addChild(s); this->update(); deselect(); - activeCell = cell; - } + activeCell = cell; + } } } @@ -170,7 +174,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { int shapeId = 0; if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle)) shapeId = 1; - if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square)) + if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square)) shapeId = 2; if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle)) shapeId = 3; @@ -179,7 +183,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { emit shapeSelected(0); } } - } + }//*/ } void Viewer::deselect(){ @@ -204,13 +208,13 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::init() { - // We want to restrict ourselves to a 2D viewer. + // We want to restrict ourselves to a 2D viewer. //camera()->setType(qglviewer::Camera::ORTHOGRAPHIC); - /*setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); - setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION);*/ -// setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); -// setMouseBinding(Qt::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); -// setMouseBinding(Qt::ShiftModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); + //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION);//*/ + //setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + //setMouseBinding(Qt::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + //setMouseBinding(Qt::ShiftModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); // Our scene will be from -5 to 5 in X and Y (the grid will be 10x10). setSceneRadius(5); @@ -223,7 +227,15 @@ void Viewer::init() // Init shaders & geometry initShaders(); initGeometries(); - //initGrid(); + + Shape* cube = new Cube(); + cube->setColor(*activeColor); + // uncomment this and the quaternion transformation in draw() + // to see the cube rotate. + // cube->transform.translate(3, 0, 0); + SceneGroup *c = new SceneGroup(); + c->addChild(cube); + root.addChild(c); } void Viewer::initShaders() @@ -261,8 +273,9 @@ void Viewer::initShaders() // Creates the basic shapes in memory. We only have 3, so we just prep them all in advance. void Viewer::initGeometries() { - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_BLEND ); glClearColor(0.0,0.0,0.0,0.0); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable( GL_BLEND ); + glClearColor(0.0,0.0,0.0,0.0); // Create our VertexArrays Objects and VertexBuffer Objects glGenVertexArrays(NumVAOs, m_VAOs); @@ -270,92 +283,47 @@ void Viewer::initGeometries() // Create our pentagone object, store its vertices on the graphic card, and // bind the data to the vPosition attribute of the shader - GLfloat verticesSquare[numVerticesSquare][3] = { - { -0.5, 0.5, 0 }, - { -0.5, -0.5, 0 }, - { 0.5, -0.5, 0 }, - { 0.5, 0.5, 0 }, - { -0.5, 0.5, 0 } + GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares + //Front, if Z is towards us + { -0.5, 0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, + { 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 }, + //Back + { -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }, + { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }, + //Right + { -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5 }, + { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }, + //Left + { 0.5, 0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 }, + { 0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }, + //Top + { -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 }, + { 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }, + //Bottom + { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, + { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 } }; - glBindVertexArray(m_VAOs[VAO_Square]); - glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Square]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesSquare), verticesSquare, GL_STATIC_DRAW); - - glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); - glEnableVertexAttribArray(m_vPositionLocation); - - // Create our triangle object, store its vertices on the graphic card, and - // bind the data to the vPosition attribute of the shader - GLfloat verticesTriangle[numVerticesTriangle][3] = { - { -0.5, -0.5, 0.0 }, - { 0.5, -0.5, 0.0 }, - { 0.0, 0.5, 0.0 } - }; - - glBindVertexArray(m_VAOs[VAO_Triangle]); - glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Triangle]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesTriangle), verticesTriangle, GL_STATIC_DRAW); - - glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); - glEnableVertexAttribArray(m_vPositionLocation); - - // Create our Circle Object, and store its vertices with its bindings - - GLfloat verticesCircle[numVerticesCircle][3]; - - const float PI = 3.1415926f; - double increment = 2.0f * PI / (numVerticesCircle - 2); - - verticesCircle[0][0] = 0; - verticesCircle[0][1] = 0; - verticesCircle[0][2] = 0; - - for(int i = 1; i < numVerticesCircle; i++) { - double angle = increment * (i); - - verticesCircle[i][0] = 0.5 * cos(angle); - verticesCircle[i][1] = 0.5 * sin(angle); - verticesCircle[i][2] = 0; - } - - glBindVertexArray(m_VAOs[VAO_Circle]); - glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Circle]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCircle), verticesCircle, GL_STATIC_DRAW); + glBindVertexArray(m_VAOs[VAO_Cube]); + glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW); glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(m_vPositionLocation); } -void Viewer::visit(Square &s) +void Viewer::visit(Cube &s) { QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); - glBindVertexArray(m_VAOs[VAO_Square]); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - m_program->setUniformValue(m_colorLocation, s.color); + int faces = floor(numVerticesCube/6); + for(int i = 0; i < faces; i++){ // 6 vertexes par face + glBindVertexArray(m_VAOs[VAO_Cube]); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_colorLocation, QColor::fromHsv(360/6*i, 255, 255, 200)); - glDrawArrays(GL_TRIANGLE_FAN, 0, numVerticesSquare); -} -void Viewer::visit(Circle &s) -{ - QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); - - glBindVertexArray(m_VAOs[VAO_Circle]); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - m_program->setUniformValue(m_colorLocation, s.color); - - glDrawArrays(GL_TRIANGLE_FAN, 0, numVerticesCircle); -} -void Viewer::visit(Triangle &s) -{ - QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); - - glBindVertexArray(m_VAOs[VAO_Triangle]); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - m_program->setUniformValue(m_colorLocation, s.color); - - glDrawArrays(GL_TRIANGLES, 0, numVerticesTriangle); + glDrawArrays(GL_TRIANGLES, i*6, 6); + } } void Viewer::visit(SceneGroup &s) diff --git a/src/viewer/simpleViewer.h b/src/viewer/simpleViewer.h index dc4fe02..ad2611c 100644 --- a/src/viewer/simpleViewer.h +++ b/src/viewer/simpleViewer.h @@ -44,9 +44,7 @@ public: ~Viewer(); virtual void visit(SceneGroup &s); - virtual void visit(Square &s); - virtual void visit(Circle &s); - virtual void visit(Triangle &s); + virtual void visit(Cube &s); public slots: void cleanup(); @@ -81,13 +79,16 @@ private: QColor* activeColor; int activeShape; - enum VAO_IDs { VAO_Square, VAO_Triangle, VAO_Circle, NumVAOs }; - enum Buffer_IDs { VBO_Square, VBO_Triangle, VBO_Circle, NumBuffers }; + enum VAO_IDs { VAO_Cube, NumVAOs }; + enum Buffer_IDs { VBO_Cube, NumBuffers }; GLuint m_VAOs[NumVAOs]; GLuint m_Buffers[NumBuffers]; Shape* generateShapeFromIndex(int); + + QQuaternion rot; + unsigned int frame; }; #endif // SIMPLEVIEWER_H From df3060376703d609bf38fe37bf3d0fcde7e59651 Mon Sep 17 00:00:00 2001 From: Dmitri K Date: Mon, 31 Oct 2016 16:54:30 -0400 Subject: [PATCH 2/2] Fixing geometry bug and adding backface culling. --- src/main.cpp | 2 +- src/viewer/simpleViewer.cpp | 169 +++++++++++++++++++----------------- 2 files changed, 88 insertions(+), 83 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 91f87dc..ccfec73 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) // Instantiate and layout the viewer. Viewer *v = new Viewer(); - w.addViewer(v); + w.addViewer(v); w.show(); return a.exec(); diff --git a/src/viewer/simpleViewer.cpp b/src/viewer/simpleViewer.cpp index b3b34d3..e76dff9 100644 --- a/src/viewer/simpleViewer.cpp +++ b/src/viewer/simpleViewer.cpp @@ -41,17 +41,17 @@ using namespace std; namespace { -const int numVerticesCube = 12*3; -const double frame_limit = 5; -const double inc_mult = 5; -const double inc_offset = 1.05; + const int numVerticesCube = 12*3; + const double frame_limit = 5; + const double inc_mult = 5; + const double inc_offset = 1.05; } Viewer::Viewer() { activeColor = new QColor(255, 255, 255, 255); activeCell = nullptr; - activeShape = 0; + activeShape = 0; } Viewer::~Viewer() @@ -76,48 +76,49 @@ void Viewer::cleanup() void Viewer::draw() { - m_program->bind(); + m_program->bind(); // Get projection and camera transformations - QMatrix4x4 projectionMatrix; + QMatrix4x4 projectionMatrix; QMatrix4x4 modelViewMatrix; camera()->getProjectionMatrix(projectionMatrix); camera()->getModelViewMatrix(modelViewMatrix); // Prepare a transformation stack - // stack modelStack; + // stack modelStack; modelViewMatrix.scale(0.95); - modelViewMatrix.rotate(45, 0, 1, 0); - rot = QQuaternion::fromAxisAndAngle(1, 0, 1, 90/60 * frame); - // uncomment this and the cube translation in init() - // to see the cube spin - modelViewMatrix.rotate(rot); - projectionMatrix.translate(0, -5, 0); - projectionMatrix.rotate(15, 1, 0, 0); - m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + //modelViewMatrix.rotate(45, 0, 1, 0); + // uncomment this and the cube translation in init() + // to see the cube spin + /*rot = QQuaternion::fromAxisAndAngle(1, 0, 1, 90/60 * frame); + modelViewMatrix.rotate(rot); + projectionMatrix.translate(0, -5, 0); + projectionMatrix.rotate(15, 1, 0, 0);//*/ - // Traverse the Scene in order to draw its components + m_program->setUniformValue(m_projMatrixLocation, projectionMatrix); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - modelStack.push(modelViewMatrix); - root.accept(*this); - frame++; - update(); + // Traverse the Scene in order to draw its components + + modelStack.push(modelViewMatrix); + root.accept(*this); + frame++; + update(); } void Viewer::mouseMoveEvent(QMouseEvent* e) { cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl; // Normal QGLViewer behavior. - //QGLViewer::mouseMoveEvent(e); + //QGLViewer::mouseMoveEvent(e); } void Viewer::mousePressEvent(QMouseEvent* e) { - // Auto Return, but old code left in as reference - return; + // Auto Return, but old code left in as reference + return; - /*cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl; + /*cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl; if(e->button() == 1){ // LMB @@ -139,23 +140,23 @@ void Viewer::mousePressEvent(QMouseEvent* e) { Shape* s = nullptr; if(activeShape == 1){ - s = new Triangle(); + s = new Triangle(); }else if(activeShape == 2){ - s = new Square(); + s = new Square(); }else if(activeShape == 3){ - s = new Circle(); + s = new Circle(); } // WARNING: END OF CODE DEGEULASSE - //activeCell->getChildren()->at(0) = s; + //activeCell->getChildren()->at(0) = s; if(s != nullptr){ s->setColor(*activeColor); cell->addChild(s); this->update(); deselect(); - activeCell = cell; - } + activeCell = cell; + } } } @@ -174,7 +175,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { int shapeId = 0; if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle)) shapeId = 1; - if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square)) + if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square)) shapeId = 2; if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle)) shapeId = 3; @@ -183,7 +184,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) { emit shapeSelected(0); } } - }//*/ + }//*/ } void Viewer::deselect(){ @@ -208,13 +209,10 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) { void Viewer::init() { - // We want to restrict ourselves to a 2D viewer. - //camera()->setType(qglviewer::Camera::ORTHOGRAPHIC); - setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); - //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION);//*/ - //setMouseBinding(Qt::NoModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); - //setMouseBinding(Qt::ControlModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); - //setMouseBinding(Qt::ShiftModifier, Qt::MouseButton(Qt::LeftButton + Qt::MidButton), CAMERA, NO_MOUSE_ACTION); + // We want to restrict ourselves to a 2D viewer. + //camera()->setType(qglviewer::Camera::ORTHOGRAPHIC); + //setMouseBinding(Qt::NoModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); + //setMouseBinding(Qt::AltModifier, Qt::LeftButton, CAMERA, NO_MOUSE_ACTION);//*/ // Our scene will be from -5 to 5 in X and Y (the grid will be 10x10). setSceneRadius(5); @@ -226,16 +224,20 @@ void Viewer::init() // Init shaders & geometry initShaders(); - initGeometries(); + initGeometries(); - Shape* cube = new Cube(); - cube->setColor(*activeColor); - // uncomment this and the quaternion transformation in draw() - // to see the cube rotate. - // cube->transform.translate(3, 0, 0); - SceneGroup *c = new SceneGroup(); - c->addChild(cube); - root.addChild(c); + { + Shape* cube = new Cube(); + cube->setColor(*activeColor); + + // 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); + + SceneGroup *c = new SceneGroup(); + c->addChild(cube); + root.addChild(c); + } } void Viewer::initShaders() @@ -273,9 +275,12 @@ void Viewer::initShaders() // Creates the basic shapes in memory. We only have 3, so we just prep them all in advance. void Viewer::initGeometries() { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable( GL_BLEND ); - glClearColor(0.0,0.0,0.0,0.0); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable( GL_BLEND ); + glEnable( GL_CULL_FACE ); + glFrontFace( GL_CCW ); + glCullFace( GL_BACK ); + glClearColor(0.0,0.0,0.0,0.0); // Create our VertexArrays Objects and VertexBuffer Objects glGenVertexArrays(NumVAOs, m_VAOs); @@ -283,30 +288,30 @@ void Viewer::initGeometries() // Create our pentagone object, store its vertices on the graphic card, and // bind the data to the vPosition attribute of the shader - GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares - //Front, if Z is towards us - { -0.5, 0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, - { 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 }, - //Back - { -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }, - { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }, - //Right - { -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5 }, - { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }, - //Left - { 0.5, 0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 }, - { 0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }, - //Top - { -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 }, - { 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }, - //Bottom - { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, - { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 } + GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares + //Front, if Z is towards us + { -0.5, 0.5, 0.5 }, { -0.5, -0.5, 0.5 }, { 0.5, -0.5, 0.5 }, + { 0.5, -0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { -0.5, 0.5, 0.5 }, + //Back + { -0.5, 0.5, -0.5 }, { 0.5, 0.5, -0.5 }, { 0.5, -0.5, -0.5 }, + { 0.5, -0.5, -0.5 }, { -0.5, -0.5, -0.5 }, { -0.5, 0.5, -0.5 }, + //Right + { -0.5, 0.5, 0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, -0.5, -0.5 }, + { -0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, 0.5, 0.5 }, + //Left + { 0.5, 0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, 0.5, -0.5 }, + { 0.5, -0.5, -0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, -0.5, 0.5 }, + //Top + { -0.5, 0.5, 0.5 }, { 0.5, 0.5, 0.5 }, { 0.5, 0.5, -0.5 }, + { 0.5, 0.5, -0.5 }, { -0.5, 0.5, -0.5 }, { -0.5, 0.5, 0.5 }, + //Bottom + { -0.5, -0.5, 0.5 }, { 0.5, -0.5, -0.5 }, { 0.5, -0.5, 0.5 }, + { 0.5, -0.5, -0.5 }, { -0.5, -0.5, 0.5 }, { -0.5, -0.5, -0.5 } }; - glBindVertexArray(m_VAOs[VAO_Cube]); - glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); - glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW); + glBindVertexArray(m_VAOs[VAO_Cube]); + glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW); glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); glEnableVertexAttribArray(m_vPositionLocation); @@ -316,14 +321,14 @@ void Viewer::visit(Cube &s) { QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform); - int faces = floor(numVerticesCube/6); - for(int i = 0; i < faces; i++){ // 6 vertexes par face - glBindVertexArray(m_VAOs[VAO_Cube]); - m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); - m_program->setUniformValue(m_colorLocation, QColor::fromHsv(360/6*i, 255, 255, 200)); + int faces = floor(numVerticesCube/6); + for(int i = 0; i < faces; i++){ // 6 vertexes par face + glBindVertexArray(m_VAOs[VAO_Cube]); + m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix); + m_program->setUniformValue(m_colorLocation, QColor::fromHsv(360/6*i, 255, 255, 200)); - glDrawArrays(GL_TRIANGLES, i*6, 6); - } + glDrawArrays(GL_TRIANGLES, i*6, 6); + } } void Viewer::visit(SceneGroup &s)