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
| #include <iostream>
#include "GLScene.h"
using namespace std;
GLScene::GLScene(QMainWindow *parent, QGLFormat & glformat)
: QGLWidget(glformat, parent),
m_shader("./Shaders/couleurs.vert", "./Shaders/couleurs.frag")
{
// Initialisation des matrices
projection.loadIdentity();
modelview.loadIdentity();
QTimer* updateGL = new QTimer(this);
connect (updateGL, SIGNAL(timeout), this, SLOT(updateGL()));
updateGL->start(1);
}
GLScene::~GLScene()
{
}
void GLScene::initializeGL()
{
GLenum initialisationGLEW(glewInit());
if(initialisationGLEW != GLEW_OK)
std::cout << "Erreur d'initialisation de GLEW : " << glewGetErrorString(initialisationGLEW) << std::endl;
// Set up the rendering context, etc.:
glClearColor(0.0, 0.0, 0.0, 0.0);
m_shader.initialiser();
projection.perspective(70, float(width()/height()), 1, 100);
glViewport(0, 0, this->width(), this->height());
}
void GLScene::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
float eye[3] = {0,0,0};
float focus[3] = {0,0,5};
float up[3] = {0,1,0};
modelview.loadIdentity();
modelview.lookAt(eye, focus, up);
float vertices[] = {-0.5, -0.5, 50.0, 0.0, 0.5, 50.0, 0.5, -0.5, 50.0};
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0, vertices);
glEnableVertexAttribArray(0);
glUseProgram(m_shader.getProgramID());
// On envoie les matrices au shader
glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "modelview"), 1, GL_TRUE, modelview.getValeurs());
glUniformMatrix4fv(glGetUniformLocation(m_shader.getProgramID(), "projection"), 1, GL_TRUE, projection.getValeurs());
glDrawArrays(GL_TRIANGLES,0,3);
glUseProgram(0);
glDisableVertexAttribArray(0);
swapBuffers();
}
void GLScene::resizeGL(int w, int h)
{
glViewport( 0, 0, w, qMax( h, 1 ) );
}
void GLScene::keyPressEvent(QKeyEvent* e)
{
if(e->key() == Qt::Key_Escape)
QCoreApplication::instance()->quit();
} |