portfolio/color-space/GLwindow.cpp
2026-08-29 23:20:33 +02:00

262 lines
5.8 KiB
C++

#include "GLwindow.h"
#include <GL/glu.h>
#include <iostream>
#include <QKeyEvent>
#include <QTimer>
#include <QDebug>
GLwindow::GLwindow(QWidget *parent)
: QGLWidget(parent)
{
_width = 0;
_height = 0;
distance = -5.0;
x=0;x_rot = 0.;
y=0;y_rot = 0.;
z=0; z_rot = .0;
// QWidget::setMouseTracking(true);
}
GLwindow::~GLwindow()
{
// glDeleteTextures(1, &_blur_texture);
}
void GLwindow::_tick()
{
/* redisplay */
update(); // triggers paintGL()
QTimer::singleShot(33, this, SLOT(_tick()));
}
void GLwindow::initializeGL()
{
distance = -10.0;
x_rot = 0.;
y_rot = 0.;
z_rot = 0.;
qglClearColor(Qt::darkGray);
glEnable(GL_DEPTH_TEST);
_tick();
}
void GLwindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_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);
// Projection matrix
/* glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, 1.0*width()/height(), 0.1f, 100.0f);
*/ //_view_ortho();
_draw();
//_view_perspective();
}
void GLwindow::resizeGL( int width, int height)
{
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(-2, +2, -2, +2, 1.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
}
/*************************
*
* Clavier
*
* *********************/
void GLwindow::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 GLwindow::wheelEvent(QWheelEvent *event)
{
distance *= 1.0 + (1.0 * event->delta() / 1200.0);
updateGL();
}
void GLwindow::mousePressEvent(QMouseEvent *event)
{
last_pos = event->pos();
// qDebug() <<last_pos;
}
void GLwindow::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) {
setXRotation(x_rot + 8 * dy);
setZRotation(z_rot + 8 * dx);
} else if (event->buttons() & Qt::MiddleButton) {
distance = -10.0;
x_rot = 0.;
y_rot = 0.;
z_rot = 0.;
}
last_pos = event->pos();
// qDebug()<<last_pos;
}
static void qNormalizeAngle(int &angle)
{
while (angle < 0)
angle += 360 * 16;
while (angle > 360)
angle -= 360 * 16;
}
void GLwindow::setXRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != x_rot) {
x_rot = angle;
//emit xRotationChanged(angle);
updateGL();
}
}
void GLwindow::setYRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != y_rot) {
y_rot = angle;
//emit yRotationChanged(angle);
updateGL();
}
}
void GLwindow::setZRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != z_rot) {
z_rot = angle;
//emit zRotationChanged(angle);
updateGL();
}
}
/*********************
* *
* * Model view
* *
* *********************/
void GLwindow::_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 GLwindow::_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
static void 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 ();
}
void GLwindow::_draw(){
qglColor(Qt::white);
renderText(200, 200, QString("Scene RGB"));
repereScene3D(200.0);
if(_image)
_draw_rgb();
}
/***********************************************************************************
** Project drawer
**
**
**
** *****************************************************************************/
void GLwindow::_setImg(QImage* Img){
_image = Img;
updateGL();
}
void GLwindow::_draw_Point(QRgb pixel){
// glColor4f(1.0f,1.0f,1.0f, 0.8); // Set The Alpha Value (Starts At 0.2)
qglColor(pixel);
glVertex3d(qRed(pixel)* 200 /255., qGreen(pixel) * 200 /255., qBlue(pixel) * 200 /255.);
}
void GLwindow::_draw_rgb(){
glPointSize(3.);
glBegin(GL_POINTS);
for(int i=0; i< _image->size().height();i++)
for(int j=0; j< _image->size().width();j++)
_draw_Point(_image->pixel(i,j));
glEnd();
}