1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include "myopenglwidget.h"
#include <GL/glu.h>
#include <QGLWidget>
#include <QThread>
#include <QOpenGLFunctions>
MyOpenGLWidget::MyOpenGLWidget(QWidget *parent) : QOpenGLWidget (parent)
{
}
MyOpenGLWidget::~MyOpenGLWidget()
{
delete pointShaderProgram;
}
void MyOpenGLWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0.0f,0.0f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pointShaderProgram = new QOpenGLShaderProgram(this);
pointShaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "/home/rv/Bureau/Sniffer-master/src/fragmentshader.fsh");
pointShaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "/home/rv/Bureau/Sniffer-master/src/pointshader.vsh");
pointShaderProgram->link();
pointShaderProgram->bind();
//Initialize Cube3D
cubeAlpha = 25;
cubeBeta = -25;
cubeDistance = 2.5;
cubeVertices << QVector3D(-0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) // Front
<< QVector3D( 0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5)
<< QVector3D( 0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) // Back
<< QVector3D(-0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5)
<< QVector3D(-0.5, -0.5, -0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, 0.5, 0.5) // Left
<< QVector3D(-0.5, 0.5, 0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, -0.5, -0.5)
<< QVector3D( 0.5, -0.5, 0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, 0.5, -0.5) // Right
<< QVector3D( 0.5, 0.5, -0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, -0.5, 0.5)
<< QVector3D(-0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, 0.5) << QVector3D( 0.5, 0.5, -0.5) // Top
<< QVector3D( 0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, -0.5) << QVector3D(-0.5, 0.5, 0.5)
<< QVector3D(-0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, -0.5) << QVector3D( 0.5, -0.5, 0.5) // Bottom
<< QVector3D( 0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, 0.5) << QVector3D(-0.5, -0.5, -0.5);
glGenBuffers(1, &cubeVBO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, cubeVertices.size() * sizeof(QVector3D), cubeVertices.constData(), GL_STATIC_DRAW);
}
void MyOpenGLWidget::resizeGL(int width, int height)
{
glViewport(0,0,width,height);
cubeProjectionMatrix.setToIdentity();
cubeProjectionMatrix.perspective(60.0, (float)width/(float)height, 0.001, 1000);
}
void MyOpenGLWidget::paintGL()
{
//Part where we are supposed to display our 3D Cube
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
pointShaderProgram->bind();
pointShaderProgram->setUniformValue("isCube",true);
cubeModelMatrix.setToIdentity();
cubeViewMatrix.setToIdentity();
cubeViewMatrix.lookAt(QVector3D(0,0,cubeDistance), QVector3D(0,0,0), QVector3D(0,1,0));
cubeModelMatrix.rotate(cubeBeta,1,0,0);
cubeModelMatrix.rotate(cubeAlpha,0,1,0);
pointShaderProgram->setUniformValue("qt_ModelViewProjectionMatrix",cubeProjectionMatrix * cubeViewMatrix * cubeModelMatrix);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,nullptr);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0,cubeVertices.size());
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,0);
pointShaderProgram->release();
} |
Partager