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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
| #include "glwidget.h"
#include <math.h>
#include <QDebug>
///////////////////////////////////////////////////////////////////////////
//// Constructeur
///////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Nom : GLWidget
// Role : Constructeur
//---------------------------------------------------------------------------
GLWidget::GLWidget(const QGLFormat& format, QWidget* parent ):
QGLWidget( format, parent ),
zoomSensibility(1.2f),
TouchePresse(-1),
Wafer_offsetX(0.0f),
Wafer_offsetY(0.0f),
Wafer_nbDivision(128.0f),/* avec 128, une ligne fantôme se produit, comme si GL_LINE_LOOP possédait
une limite et refermait le cycle automatiquement */
wafer_angle(45.0f),
Wafer_rayon(150.0f),
waferDisplayList(0),
PositionCameraInitiale(0.0f, 0.0f, 5.0f),
CibleCameraInitiale(0.0f, 0.0f, -1.0f),
liste(NULL)
{
lastPos = QPoint(0,0);
//Background_color.setRgbF(1.0,0.97647,0.917647,1.0);
Background_color.setRgbF(1.0,0.97647,0.917647,0.0);
//Background_color = Qt::GlobalColor::black;
// Wafer_colorCentre.setRgbF(1.00f, 249.0f / 255.0f , 234.0f / 255.0f);
Wafer_colorCentre.setRgbF(250.0f/ 255.0f, 250.0f / 255.0f ,255.0f / 255.0f);
//Wafer_colorExterieur.setRgbF(174.0f / 255.0f, 255.0f / 255.0f, 243.0f/ 255.0f);
Wafer_colorExterieur.setRgbF(239.0f / 255.0f, 239.0f / 255.0f, 239.0f/ 255.0f);
//Wafer_contour.setRgbF(0.0f / 255.0f, 255.0f / 255.0f, 108.0f / 255.0f);
Wafer_contour.setRgbF(206.0f / 255.0f, 206.0f / 255.0f, 206.0f / 255.0f);
Repere_axeX.setRgbF(0.90f, 0.70f, 0.70f);
Repere_axeY.setRgbF(0.70f, 0.70f,0.90f);
initializeGL();
reinitCam();
/* m_timer.setInterval(1000/600);
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
m_timer.start();*/
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
m_timer.start(16);
frame_count = 0;
last_count = 0;
last_time = QTime::currentTime();
}
//---------------------------------------------------------------------------
// Nom : initializeGL
// Role : Cette fonction devrait mettre en place tous les drapeaux de rendu de contexte OpenGL, définir les listes d'affichage, etc.
//---------------------------------------------------------------------------
void GLWidget::initializeGL()
{
QGLFormat glFormat = QGLWidget::format();
if ( !glFormat.sampleBuffers() )
qWarning() << "Could not enable sample buffers";
glShadeModel(GL_SMOOTH);
// changement de la couleur de fond
glClearColor(Background_color.redF(),Background_color.greenF(), Background_color.blueF(), Background_color.alphaF());
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// Prepare a complete shader program...
if ( !prepareShaderProgram( ":/shaders/vertex_shader.glsl", ":/shaders/fragment_shader.glsl" ) )
return;
}
//---------------------------------------------------------------------------
// Nom : prepareShaderProgram
// Role : Préparation des shaders
//---------------------------------------------------------------------------
bool GLWidget::prepareShaderProgram( const QString& vertexShaderPath,
const QString& fragmentShaderPath )
{
const GLubyte * test = glGetString(GL_VERSION);
// First we load and compile the vertex shader...
bool result = m_program.addShaderFromSourceFile( QGLShader::Vertex, vertexShaderPath );
if ( !result )
qWarning() << m_program.log();
// ...now the fragment shader...
result = m_program.addShaderFromSourceFile( QGLShader::Fragment, fragmentShaderPath );
if ( !result )
qWarning() << m_program.log();
// ...and finally we link them to resolve any references.
result = m_program.link();
if ( !result )
qWarning() << "Could not link shader program:" << m_program.log();
return result;
}
///////////////////////////////////////////////////////////////////////////
//// Gestion de l'affichage
///////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Nom : resizeGL
// Role : fonction appellée lors d'un changement de taille du composant.
//---------------------------------------------------------------------------
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0f, (GLdouble)width/(GLdouble)height, 0.00001, 1000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//---------------------------------------------------------------------------
// Nom : paintGL
// Role : fonction appelée à chaque fois que le widget doit être redessiné
//---------------------------------------------------------------------------
void GLWidget::paintGL()
{
++frame_count;
QTime new_time = QTime::currentTime();
if (last_time.msecsTo(new_time) >= 1000)
{
// on sauvegarde le FPS dans last_count et on réinitialise
last_count = frame_count;
frame_count = 0;
last_time = QTime::currentTime();
fps = last_count;
}
//qDebug() << QString("FPS:%1").arg(last_count);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// Caméra
gluLookAt(PositionCamera.x(), PositionCamera.y(), PositionCamera.z(), CibleCamera.x(), CibleCamera.y(), CibleCamera.z(), 0, 1,0);
// Enregistrement de la matrice de base (transformée pour point de vue)
//glPushMatrix();
//Affichage du Wafer
if ( waferDisplayList <=0)
InitDisplayListWafer();
if ( waferDisplayList>0)
glCallList(waferDisplayList);
//glPopMatrix();
if ( liste != NULL)
liste->AfficheItem();
}
//---------------------------------------------------------------------------
// Nom : InitDisplayListWafer
// Role : fonction d'initialisation de la displayList de la scene ( wafer + repère)
//---------------------------------------------------------------------------
void GLWidget::InitDisplayListWafer()
{
if (waferDisplayList <=0)
{
glDeleteLists(waferDisplayList,1);
waferDisplayList = 0;
}
/* on cree un objet et on recupere son identifiant */
waferDisplayList = glGenLists(1);
if(waferDisplayList == 0)
{
qDebug() << "erreur lors de la creation de la liste\n" ;
return;
}
/* remplissage de notre liste */
glNewList(waferDisplayList, GL_COMPILE); /* start */
// Affichage du wafer
glBegin(GL_TRIANGLE_FAN);
glColor3f(Wafer_colorCentre.redF(),Wafer_colorCentre.greenF(), Wafer_colorCentre.blueF());
// point central; la coordonnée y est négative pour ne pas masquer les items
glVertex3f(Wafer_offsetX, Wafer_offsetY, -1.1 );
glColor3f(Wafer_colorExterieur.redF(),Wafer_colorExterieur.greenF(), Wafer_colorExterieur.blueF());
double parcAngle = 360.0f - wafer_angle;
for (int i=0; i <= Wafer_nbDivision+1; i++)
{
double pos = (wafer_angle / 2.0f) + (i % (int) (Wafer_nbDivision+1) * parcAngle / Wafer_nbDivision);
double rad_pos = pos * M_PI / 180.0f + M_PI/2;
glVertex3f(Wafer_offsetX + cos(rad_pos) * Wafer_rayon, Wafer_offsetY + sin(rad_pos) * Wafer_rayon, -0.9f);
}
glEnd();// fin glBegin(GL_TRIANGLE_FAN);
// et maintenant le contour
glLineWidth(1.0);
glBegin(GL_LINE_STRIP); // on n'utilise pas GL_LINE_LOOP: bugs à partir de 128 points sur certaines cartes graphiques (dont la mienne!)
glColor3f(Wafer_contour.redF(),Wafer_contour.greenF(), Wafer_contour.blueF());
for (int i=0; i <= Wafer_nbDivision+1; i++)
{
double pos = (wafer_angle / 2.0f) + (i % (int) (Wafer_nbDivision+1) * parcAngle / Wafer_nbDivision);
double rad_pos = pos * M_PI / 180.0f + M_PI/2;
glVertex3f(Wafer_offsetX + cos(rad_pos) * Wafer_rayon, Wafer_offsetY + sin(rad_pos) * Wafer_rayon, -1.1f);
}
glEnd();// fin glBegin(GL_LINE_STRIP);
//affichage du repère
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(Repere_axeX.redF(),Repere_axeX.greenF(), Repere_axeX.blueF());
glVertex3f(0.0f,0.0f, -0.01f);
glVertex3f(1.0f, 0.0f, -0.01f);
glColor3f(Repere_axeX.redF(),Repere_axeX.greenF(), Repere_axeX.blueF());
glVertex3f(0.0f, 0.0f, -0.01f);
glVertex3f(0.0f, 1.0f, -0.01f);
glEnd(); // fin glBegin(GL_LINES);
glEndList(); // fin de la displayList
}
////////////////////////////////////////////////////////////////////////////////////
//// Entrée / Sortie
////////////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Nom : wheelEvent
// Role : Action induit par un mouvement de molette
//---------------------------------------------------------------------------
void GLWidget::wheelEvent(QWheelEvent* event)
{
operateZoom(event->delta() > 0);
}
//---------------------------------------------------------------------------
// Nom : mousePressEvent
// Role : Action induit par un clic sur la souris
//---------------------------------------------------------------------------
void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos= event->globalPos();
}
//---------------------------------------------------------------------------
// Nom : mouseMoveEvent
// Role : Action induit par un mouvement de la souris
//---------------------------------------------------------------------------
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
//if ( TouchePresse == Qt::Key_Control)
{
float m_moveSensibility = 63;
// Déplacement de la scène parallèlement au plan xy
QVector3D relativeCam = PositionCamera - CibleCamera; // Coordonnées avec la cible comme origine
// L'utilisation de relativeCam.length permet d'augmenter l'ampleur du déplacement en dézoom, divisée par m_sensibility pour que cette variation soit aussi assujetie à la sensibilité
float moveX = (3.0 * relativeCam.length()/m_moveSensibility) * static_cast<float>(event->globalX() - lastPos.x())/m_moveSensibility;
float moveY = (3.0 * relativeCam.length()/m_moveSensibility) * static_cast<float>(event->globalY() - lastPos.y())/m_moveSensibility;
PositionCamera-= QVector3D(moveX, -moveY,0.0);
CibleCamera-= QVector3D(moveX, -moveY,0.0);
//qDebug() << "Camera X : "<<QString::number(PositionCamera.x(),'f') << " ; Camera Y : "<< QString::number(PositionCamera.y(),'f') ;
}
lastPos= event->globalPos();
}
//---------------------------------------------------------------------------
// Nom : keyPressEvent
// Role : Action induit par un appuie de touche sur le clavier
//---------------------------------------------------------------------------
void GLWidget::keyPressEvent ( QKeyEvent * event )
{
TouchePresse = event->key();
qDebug() << "Appui sur une touche";
}
//---------------------------------------------------------------------------
// Nom : keyPressEvent
// Role : Action induit par un relachement de touche sur le clavier
//---------------------------------------------------------------------------
void GLWidget::keyReleaseEvent ( QKeyEvent * event )
{
TouchePresse = 0;
qDebug() << "Relachement d'une touche";
}
///////////////////////////////////////////////////////////////////////////////
//// Caméra
///////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Nom : reinitCam
// Role : Reinitialise la caméra
//---------------------------------------------------------------------------
void GLWidget::reinitCam()
{
PositionCamera = PositionCameraInitiale;
CibleCamera = CibleCameraInitiale;
}
//---------------------------------------------------------------------------
// Nom : operateZoom
// Role : Modifi le zoom
//---------------------------------------------------------------------------
void GLWidget::operateZoom(bool toNear)
{
QVector3D relativeCam = PositionCamera - CibleCamera;
if(toNear)
relativeCam /= zoomSensibility;
else
relativeCam *= zoomSensibility;
if(relativeCam.length() < 0.1)
relativeCam = relativeCam.normalized();
else if(relativeCam.length() > 999)
relativeCam = relativeCam.normalized() * 999;
else
PositionCamera = relativeCam + CibleCamera;
} |
Partager