mirror of
https://github.com/ConjureETS/LOG750-LAB2.git
synced 2026-03-24 11:31:20 +00:00
Fixing geometry bug and adding backface culling.
This commit is contained in:
parent
b94626fb5d
commit
df30603767
@ -19,7 +19,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// Instantiate and layout the viewer.
|
// Instantiate and layout the viewer.
|
||||||
Viewer *v = new Viewer();
|
Viewer *v = new Viewer();
|
||||||
w.addViewer(v);
|
w.addViewer(v);
|
||||||
w.show();
|
w.show();
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
|
|||||||
@ -41,17 +41,17 @@ using namespace std;
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
const int numVerticesCube = 12*3;
|
const int numVerticesCube = 12*3;
|
||||||
const double frame_limit = 5;
|
const double frame_limit = 5;
|
||||||
const double inc_mult = 5;
|
const double inc_mult = 5;
|
||||||
const double inc_offset = 1.05;
|
const double inc_offset = 1.05;
|
||||||
}
|
}
|
||||||
|
|
||||||
Viewer::Viewer()
|
Viewer::Viewer()
|
||||||
{
|
{
|
||||||
activeColor = new QColor(255, 255, 255, 255);
|
activeColor = new QColor(255, 255, 255, 255);
|
||||||
activeCell = nullptr;
|
activeCell = nullptr;
|
||||||
activeShape = 0;
|
activeShape = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Viewer::~Viewer()
|
Viewer::~Viewer()
|
||||||
@ -76,48 +76,49 @@ void Viewer::cleanup()
|
|||||||
|
|
||||||
void Viewer::draw()
|
void Viewer::draw()
|
||||||
{
|
{
|
||||||
m_program->bind();
|
m_program->bind();
|
||||||
|
|
||||||
// Get projection and camera transformations
|
// Get projection and camera transformations
|
||||||
QMatrix4x4 projectionMatrix;
|
QMatrix4x4 projectionMatrix;
|
||||||
QMatrix4x4 modelViewMatrix;
|
QMatrix4x4 modelViewMatrix;
|
||||||
camera()->getProjectionMatrix(projectionMatrix);
|
camera()->getProjectionMatrix(projectionMatrix);
|
||||||
camera()->getModelViewMatrix(modelViewMatrix);
|
camera()->getModelViewMatrix(modelViewMatrix);
|
||||||
|
|
||||||
// Prepare a transformation stack
|
// Prepare a transformation stack
|
||||||
|
|
||||||
// stack<QMatrix4x4> modelStack;
|
// stack<QMatrix4x4> modelStack;
|
||||||
modelViewMatrix.scale(0.95);
|
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()
|
// uncomment this and the cube translation in init()
|
||||||
// to see the cube spin
|
// to see the cube spin
|
||||||
modelViewMatrix.rotate(rot);
|
/*rot = QQuaternion::fromAxisAndAngle(1, 0, 1, 90/60 * frame);
|
||||||
projectionMatrix.translate(0, -5, 0);
|
modelViewMatrix.rotate(rot);
|
||||||
projectionMatrix.rotate(15, 1, 0, 0);
|
projectionMatrix.translate(0, -5, 0);
|
||||||
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
|
projectionMatrix.rotate(15, 1, 0, 0);//*/
|
||||||
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
|
||||||
|
|
||||||
// Traverse the Scene in order to draw its components
|
m_program->setUniformValue(m_projMatrixLocation, projectionMatrix);
|
||||||
|
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
||||||
|
|
||||||
modelStack.push(modelViewMatrix);
|
// Traverse the Scene in order to draw its components
|
||||||
root.accept(*this);
|
|
||||||
frame++;
|
modelStack.push(modelViewMatrix);
|
||||||
update();
|
root.accept(*this);
|
||||||
|
frame++;
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::mouseMoveEvent(QMouseEvent* e) {
|
void Viewer::mouseMoveEvent(QMouseEvent* e) {
|
||||||
cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
|
cout << "Viewer::mouseMoveEvent(QMouseEvent* e)" << endl;
|
||||||
// Normal QGLViewer behavior.
|
// Normal QGLViewer behavior.
|
||||||
//QGLViewer::mouseMoveEvent(e);
|
//QGLViewer::mouseMoveEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::mousePressEvent(QMouseEvent* e) {
|
void Viewer::mousePressEvent(QMouseEvent* e) {
|
||||||
// Auto Return, but old code left in as reference
|
// Auto Return, but old code left in as reference
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/*cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl;
|
/*cout << "Viewer::mouseMoveEvent(QMouseEvent* e) : " << e->button() << endl;
|
||||||
|
|
||||||
if(e->button() == 1){ // LMB
|
if(e->button() == 1){ // LMB
|
||||||
|
|
||||||
@ -139,23 +140,23 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
|
|||||||
|
|
||||||
Shape* s = nullptr;
|
Shape* s = nullptr;
|
||||||
if(activeShape == 1){
|
if(activeShape == 1){
|
||||||
s = new Triangle();
|
s = new Triangle();
|
||||||
}else if(activeShape == 2){
|
}else if(activeShape == 2){
|
||||||
s = new Square();
|
s = new Square();
|
||||||
}else if(activeShape == 3){
|
}else if(activeShape == 3){
|
||||||
s = new Circle();
|
s = new Circle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARNING: END OF CODE DEGEULASSE
|
// WARNING: END OF CODE DEGEULASSE
|
||||||
|
|
||||||
//activeCell->getChildren()->at(0) = s;
|
//activeCell->getChildren()->at(0) = s;
|
||||||
if(s != nullptr){
|
if(s != nullptr){
|
||||||
s->setColor(*activeColor);
|
s->setColor(*activeColor);
|
||||||
cell->addChild(s);
|
cell->addChild(s);
|
||||||
this->update();
|
this->update();
|
||||||
deselect();
|
deselect();
|
||||||
activeCell = cell;
|
activeCell = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +175,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
|
|||||||
int shapeId = 0;
|
int shapeId = 0;
|
||||||
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle))
|
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Triangle))
|
||||||
shapeId = 1;
|
shapeId = 1;
|
||||||
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square))
|
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Square))
|
||||||
shapeId = 2;
|
shapeId = 2;
|
||||||
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle))
|
if(typeid(*(activeCell->getChildren()->at(0))) == typeid(Circle))
|
||||||
shapeId = 3;
|
shapeId = 3;
|
||||||
@ -183,7 +184,7 @@ void Viewer::mousePressEvent(QMouseEvent* e) {
|
|||||||
emit shapeSelected(0);
|
emit shapeSelected(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}//*/
|
}//*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::deselect(){
|
void Viewer::deselect(){
|
||||||
@ -208,13 +209,10 @@ void Viewer::mouseReleaseEvent(QMouseEvent* e) {
|
|||||||
|
|
||||||
void Viewer::init()
|
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);
|
//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::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).
|
// Our scene will be from -5 to 5 in X and Y (the grid will be 10x10).
|
||||||
setSceneRadius(5);
|
setSceneRadius(5);
|
||||||
@ -226,16 +224,20 @@ void Viewer::init()
|
|||||||
|
|
||||||
// Init shaders & geometry
|
// Init shaders & geometry
|
||||||
initShaders();
|
initShaders();
|
||||||
initGeometries();
|
initGeometries();
|
||||||
|
|
||||||
Shape* cube = new Cube();
|
{
|
||||||
cube->setColor(*activeColor);
|
Shape* cube = new Cube();
|
||||||
// uncomment this and the quaternion transformation in draw()
|
cube->setColor(*activeColor);
|
||||||
// to see the cube rotate.
|
|
||||||
// cube->transform.translate(3, 0, 0);
|
// uncomment this and the quaternion transformation in draw()
|
||||||
SceneGroup *c = new SceneGroup();
|
// to see the cube rotate. This is how we can easily make the sun.
|
||||||
c->addChild(cube);
|
// cube->transform.translate(3, 0, 0);
|
||||||
root.addChild(c);
|
|
||||||
|
SceneGroup *c = new SceneGroup();
|
||||||
|
c->addChild(cube);
|
||||||
|
root.addChild(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::initShaders()
|
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.
|
// Creates the basic shapes in memory. We only have 3, so we just prep them all in advance.
|
||||||
void Viewer::initGeometries()
|
void Viewer::initGeometries()
|
||||||
{
|
{
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glEnable( GL_BLEND );
|
glEnable( GL_BLEND );
|
||||||
glClearColor(0.0,0.0,0.0,0.0);
|
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
|
// Create our VertexArrays Objects and VertexBuffer Objects
|
||||||
glGenVertexArrays(NumVAOs, m_VAOs);
|
glGenVertexArrays(NumVAOs, m_VAOs);
|
||||||
@ -283,30 +288,30 @@ void Viewer::initGeometries()
|
|||||||
|
|
||||||
// Create our pentagone object, store its vertices on the graphic card, and
|
// Create our pentagone object, store its vertices on the graphic card, and
|
||||||
// bind the data to the vPosition attribute of the shader
|
// bind the data to the vPosition attribute of the shader
|
||||||
GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares
|
GLfloat verticesCube[numVerticesCube][3] = { // 12 triangles == 6 squares
|
||||||
//Front, if Z is towards us
|
//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 },
|
||||||
{ 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
|
//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 },
|
||||||
{ 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
|
//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 },
|
||||||
{ -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
|
//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 },
|
||||||
{ 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
|
//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 },
|
||||||
{ 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
|
//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]);
|
glBindVertexArray(m_VAOs[VAO_Cube]);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
|
glBindBuffer(GL_ARRAY_BUFFER, m_Buffers[VBO_Cube]);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesCube), verticesCube, GL_STATIC_DRAW);
|
||||||
|
|
||||||
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
|
glVertexAttribPointer(m_vPositionLocation, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
|
||||||
glEnableVertexAttribArray(m_vPositionLocation);
|
glEnableVertexAttribArray(m_vPositionLocation);
|
||||||
@ -316,14 +321,14 @@ void Viewer::visit(Cube &s)
|
|||||||
{
|
{
|
||||||
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
|
QMatrix4x4 modelViewMatrix = modelStack.top() * QMatrix4x4(s.transform);
|
||||||
|
|
||||||
int faces = floor(numVerticesCube/6);
|
int faces = floor(numVerticesCube/6);
|
||||||
for(int i = 0; i < faces; i++){ // 6 vertexes par face
|
for(int i = 0; i < faces; i++){ // 6 vertexes par face
|
||||||
glBindVertexArray(m_VAOs[VAO_Cube]);
|
glBindVertexArray(m_VAOs[VAO_Cube]);
|
||||||
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
m_program->setUniformValue(m_mvMatrixLocation, modelViewMatrix);
|
||||||
m_program->setUniformValue(m_colorLocation, QColor::fromHsv(360/6*i, 255, 255, 200));
|
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)
|
void Viewer::visit(SceneGroup &s)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user