portfolio/cpp/fumee/widget.cpp

321 lines
7.9 KiB
C++
Executable File

/* Copyright (C) 2013 Karl Phillip Buhr <karlphillip@gmail.com>
*
* 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 <GL/glu.h>
#include <QDebug>
#include <iostream>
#include <QKeyEvent>
#include <QTimer>
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent),Fumee_1(NULL)
{
_width = 0;
_height = 0;
_texture = new GLuint[2];
_fps = 30;
// Fumee_1 = NULL;
distance = -10.0;
x=0;x_rot = 0.;
y=0;y_rot = 0.;
z=0; z_rot = .0;
//
framedir = QString("../outils/Textures/fumee.png");
framedir2 = QString("../outils/Textures/cheminee.png");
}
GLWidget::~GLWidget()
{
glDeleteTextures(2, _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 = 180.;
y_rot = 0.;
z_rot = 0.;
Now.start();
// Set clear color as black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Select pixel storage mode used by glTexImage2D
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Create the texture
glGenTextures(2, _texture);
// Set clear color as black
qglClearColor(Qt::darkGray);
/* chargement des image de texture */
// Note: chargement de l'image qui servira de texture
qt_frame.load(framedir);
//qt_frame = qt_frame.convertToFormat(QImage::Format_ARGB6666_Premultiplied);
//qt_frame = qt_frame.rgbSwapped();
qt_frame2.load(framedir2);
// 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 = 30;
std::cerr << "GLWidget::initializeGL: " << _fps << " fps" << std::endl;
Fumee_1 = new Fumee(0.0,0.0,0.0,_fps/*10.5*/);// ne pas allez en deessous de 10ms en fonction du processeur utiliser
if (Fumee_1== NULL)
{
std::cout << "GLWidget::Fumee: !!! Failed to retrieve fumee" << std::endl;
return;
}
// Typical texture generation using data from the bitmap
_texture[0] = bindTexture ( qt_frame, GL_TEXTURE_2D, GL_RGBA );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
_texture[1] = bindTexture ( qt_frame2, GL_TEXTURE_2D, GL_RGBA );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glDisable(GL_TEXTURE_2D);
if (glGetError() != GL_NO_ERROR)
{
std::cout << "GLWidget::paintGL: !!! Failed glTexImage2D" << std::endl;
}
//QGLFormat::setDoubleBuffer(true);
/* Start the timer */
_tick();
}
void GLWidget::paintGL()
{
//animation
Fumee_1->anime(Now.currentTime());
// Clear the screen and depth buffer (with black)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//_view_ortho();
// Select the model view matrix and reset it
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);
repereScene3D(100.0f);
// _view_perspective();
//qglColor(Qt::gray) ;//
// _view_ortho();
qglColor(Qt::white) ;
Fumee_1->affiche(_texture);
//_view_perspective();
swapBuffers();
}
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., -1.0, 10000.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.;
}
update();
updateGL();// on modifie l'affichage
}
/*************************
*
* Sourie
*
* *********************/
void GLWidget::wheelEvent(QWheelEvent *event)
{
distance += (1.35 * event->delta()/120.0 );// 120.0);
update();
updateGL();
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
last_pos = event->pos();
update();
updateGL();
// qDebug() <<last_pos;
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->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.;
}
qDebug()<< "("<<x_rot <<" " << y_rot << " " << z_rot<<")";
last_pos = event->pos();
update();
updateGL();
}
static void qNormalizeAngle(int &angle)
{
while (angle < 0)
angle += 360 * 16;
while (angle > 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
{
double t = 50.;
glMatrixMode(GL_PROJECTION); // Select Projection
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
glOrtho( (-1.) * t, t, t, (-1.) *t, .0, 1000000 ); // 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)
{
glDisable(GL_LIGHTING);
glBegin (GL_LINES);
qglColor(Qt::darkRed) ; // red axe
glVertex3f (0., 0., 0.);
glVertex3f (l, 0., 0.);
qglColor(Qt::darkGreen) ;// green axe
glVertex3f (0., 0., 0.);
glVertex3f (0., l, 0.);
qglColor(Qt::darkBlue) ;//bleu axe
glVertex3f (0., 0., 0.);
glVertex3f (0., 0., l);
glEnd ();
glEnable(GL_LIGHTING);
}