/* Copyright (C) 2013 Karl Phillip Buhr * * This work is licensed under the Creative Commons Attribution-ShareAlike License. * To view a copy of this license, visit: * https://creativecommons.org/licenses/by-sa/2.5/legalcode * * Or to read the human-readable summary of the license: * https://creativecommons.org/licenses/by-sa/2.5/ */ #include "widget.h" #include #include #include #include #include GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) { _width = 0; _height = 0; _texture = 0; _fps = 30; distance = -5.0; x=0;x_rot = 0.; y=0;y_rot = 0.; z=0; z_rot = .0; now.start(); nb_obs = 1; aqua = new aquarium(20.); } GLWidget::~GLWidget() { glDeleteTextures(1, &_texture); } void GLWidget::_tick() { // triggers paintGL() update(); //updateGL(); // Set timer according to FPS QTimer::singleShot(1000/_fps, this, SLOT(_tick())); } void GLWidget::initializeGL() { distance = -10.0; x_rot = 0.; y_rot = 0.; z_rot = 0.; aqua->init(6); // Set clear color as black qglClearColor(Qt::darkGray); glEnable(GL_DEPTH_TEST); // Retrieve FPS from the camera //_fps = cv_capture.get(CV_CAP_PROP_FPS); if (!_fps || _fps <= 0) // if the function fails, fps is set to 15 _fps = 15; qDebug() << "GLWidget::initializeGL: " << _fps << " fps"; /* Start the timer */ _tick(); } void GLWidget::paintGL() { aqua->anime(now.elapsed()/1000.); now.start(); // Clear the screen and depth buffer (with black) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); // Model view matrix // glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, distance); glRotatef(x_rot / 16.0f, 1.0f, 0.0f, 0.0f); glRotatef(y_rot / 16.0f, 0.0f, 1.0f, 0.0f); glRotatef(z_rot / 16.0f, 0.0f, 0.0f, 1.0f); if (glGetError() != GL_NO_ERROR) { qDebug()<< "GLWidget::paintGL: !!! Failed glTexImage2D" ; } repereScene3D(100.); aqua->affiche(); } void GLWidget::resizeGL( int w, int h) { _width = w; _height = h; int side = qMin(_width, _height); glViewport((_width - side) / 2, (_height - side) / 2, side, side); glMatrixMode(GL_PROJECTION); glLoadIdentity(); #ifdef QT_OPENGL_ES_1 glOrthof(-2, +2, -2, +2, 1.0, 15.0); #else glOrtho(-20., +20., -20., +20., -20., 100000.0); #endif glMatrixMode(GL_MODELVIEW); } /************************* * * Clavier * * *********************/ void GLWidget::keyPressEvent(QKeyEvent *event) { if(event->key() == Qt::Key_Space){ distance = -10.0; x_rot = 0.; y_rot = 0.; z_rot = 0.; } updateGL();// on modifie l'affichage } /************************* * * Sourie * * *********************/ void GLWidget::wheelEvent(QWheelEvent *event) { distance += (2.0 * event->delta() / 120.0); updateGL(); } void GLWidget::mousePressEvent(QMouseEvent *event) { last_pos = event->pos(); // qDebug() <x() - last_pos.x(); int dy = event->y() - last_pos.y(); if (event->buttons() & Qt::LeftButton) { setXRotation(x_rot + 8 * dy); setYRotation(y_rot + 8 * dx); } else if (event->buttons() & Qt::RightButton) { setYRotation(x_rot + 8 * dy); setZRotation(z_rot + 8 * dx); } else if (event->buttons() & Qt::MiddleButton) { distance = -1.0; x_rot = 0.; y_rot = 0.; z_rot = 0.; } last_pos = event->pos(); // qDebug()< 360) angle -= 360 * 16; } void GLWidget::setXRotation(int angle) { qNormalizeAngle(angle); if (angle != x_rot) { x_rot = angle; //emit xRotationChanged(angle); updateGL(); } } void GLWidget::setYRotation(int angle) { qNormalizeAngle(angle); if (angle != y_rot) { y_rot = angle; //emit yRotationChanged(angle); updateGL(); } } void GLWidget::setZRotation(int angle) { qNormalizeAngle(angle); if (angle != z_rot) { z_rot = angle; //emit zRotationChanged(angle); updateGL(); } } /********************* * * * * Model view * * * *********************/ void GLWidget::_view_ortho() // Set Up An Ortho View { glMatrixMode(GL_PROJECTION); // Select Projection glPushMatrix(); // Push The Matrix glLoadIdentity(); // Reset The Matrix glOrtho( 0, _width , _height, 0, -1, 1 ); // Select Ortho Mode (640x480) glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix glPushMatrix(); // Push The Matrix glLoadIdentity(); // Reset The Matrix } void GLWidget::_view_perspective() // Set Up A Perspective View { glMatrixMode( GL_PROJECTION ); // Select Projection glPopMatrix(); // Pop The Matrix glMatrixMode( GL_MODELVIEW ); // Select Modelview glPopMatrix(); // Pop The Matrix } /************************* * * * * outil de dessin * * * * * ***********************/ // Repere orthonorme void GLWidget::repereScene3D(float l) { glBegin (GL_LINES); glColor3d(1.,0.,0.) ; // red axe glVertex3f (0., 0., 0.); glVertex3f (l, 0., 0.); glColor3d(0.,1.,0.) ;// green axe glVertex3f (0., 0., 0.); glVertex3f (0., l, 0.); glColor3d(0.,0.,1.) ;//bleu axe glVertex3f (0., 0., 0.); glVertex3f (0., 0., l); glEnd (); }