Fixing geometry bug and adding backface culling.

This commit is contained in:
Dmitri K 2016-10-31 16:54:30 -04:00
parent b94626fb5d
commit df30603767
2 changed files with 88 additions and 83 deletions

View File

@ -88,14 +88,15 @@ void Viewer::draw()
// stack<QMatrix4x4> modelStack;
modelViewMatrix.scale(0.95);
modelViewMatrix.rotate(45, 0, 1, 0);
rot = QQuaternion::fromAxisAndAngle(1, 0, 1, 90/60 * frame);
//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);
projectionMatrix.rotate(15, 1, 0, 0);//*/
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
@ -210,11 +211,8 @@ 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::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);
@ -228,15 +226,19 @@ void Viewer::init()
initShaders();
initGeometries();
{
Shape* cube = new Cube();
cube->setColor(*activeColor);
// uncomment this and the quaternion transformation in draw()
// to see the cube rotate.
// 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()
{
@ -275,6 +277,9 @@ void Viewer::initGeometries()
{
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
@ -300,8 +305,8 @@ void Viewer::initGeometries()
{ -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 }
{ -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]);