Advertisement
Guest User

myglwidget.cpp

a guest
May 4th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. // myglwidget.cpp
  2.  
  3. #include "myglwidget.h"
  4. #include "GL/glu.h"
  5. #include <QKeyEvent>
  6. #include <QMouseEvent>
  7. MyGLWidget::MyGLWidget(QWidget *parent) :
  8.     QGLWidget(parent), k_mysz(-0.25,-0.25,0.05,1,0,0),k_klawiatura(0.25,0.25,0.15,0,1,0)
  9. {
  10.  
  11.  
  12.     backgroundColor[0]=0.8;
  13.     backgroundColor[1]=0.8;
  14.     backgroundColor[2]=0.8;
  15.     backgroundColor[3]=1;
  16.  
  17.  
  18.  
  19. }
  20.  
  21.  
  22.  
  23.  
  24. void MyGLWidget::initializeGL()
  25. {
  26.  
  27.  
  28.  
  29.     glClearColor(backgroundColor[0],backgroundColor[1],backgroundColor[2],backgroundColor[3]);
  30.     glEnable(GL_DEPTH_TEST);
  31.  
  32. }
  33.  
  34. void MyGLWidget::paintGL()
  35. {
  36.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  37.     glLoadIdentity();
  38.  
  39.     k_klawiatura.rysuj();
  40.     k_mysz.rysuj();
  41.  
  42.  
  43.  
  44.  
  45.  
  46. }
  47.  
  48. void MyGLWidget::resizeGL(int width, int height)
  49. {
  50.  
  51.     double aspectRatio=double(width)/double(height);
  52.     glViewport(0,0,width,height);
  53.     glMatrixMode(GL_PROJECTION);
  54.     glLoadIdentity();
  55.     halfRangeX=0.5*aspectRatio;
  56.     halfRangeY=0.5;
  57.     glOrtho(-halfRangeX,halfRangeX,-halfRangeY,halfRangeY,-1,1);
  58.     glMatrixMode(GL_MODELVIEW);
  59. }
  60. double MyGLWidget::getHalfRangeY() const
  61. {
  62.     return halfRangeY;
  63. }
  64.  
  65. void MyGLWidget::setHalfRangeY(double value)
  66. {
  67.     halfRangeY = value;
  68. }
  69.  
  70. double MyGLWidget::getHalfRangeX() const
  71. {
  72.     return halfRangeX;
  73. }
  74.  
  75. void MyGLWidget::setHalfRangeX(double value)
  76. {
  77.     halfRangeX = value;
  78. }
  79.  
  80.  
  81.  
  82. void MyGLWidget::mousePressEvent(QMouseEvent *event)
  83. {
  84.     if(event->button()==Qt::LeftButton)
  85.     {
  86.         k_mysz.setX(2.*getHalfRangeX()*event->x()/width()-getHalfRangeX());
  87.         k_mysz.setY(-(2.*getHalfRangeY()*event->y()/height()-getHalfRangeY()));
  88.         updateGL();
  89.     }
  90. }
  91.  
  92. void MyGLWidget::keyPressEvent(QKeyEvent *event)
  93. {
  94.     switch(event->key())
  95.     {
  96.         case Qt::Key_Up:
  97.             k_klawiatura.changeY(0.01);
  98.             break;
  99.         case Qt::Key_Down:
  100.             k_klawiatura.changeY(-0.01);
  101.             break;
  102.         case Qt::Key_Right:
  103.             k_klawiatura.changeX(0.01);
  104.             break;
  105.         case Qt::Key_Left:
  106.             k_klawiatura.changeX(-0.01);
  107.             break;
  108.  
  109.     }
  110.     updateGL();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement