Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // myglwidget.cpp
- #include "myglwidget.h"
- #include "GL/glu.h"
- #include <QKeyEvent>
- #include <QMouseEvent>
- MyGLWidget::MyGLWidget(QWidget *parent) :
- QGLWidget(parent), k_mysz(-0.25,-0.25,0.05,1,0,0),k_klawiatura(0.25,0.25,0.15,0,1,0)
- {
- backgroundColor[0]=0.8;
- backgroundColor[1]=0.8;
- backgroundColor[2]=0.8;
- backgroundColor[3]=1;
- }
- void MyGLWidget::initializeGL()
- {
- glClearColor(backgroundColor[0],backgroundColor[1],backgroundColor[2],backgroundColor[3]);
- glEnable(GL_DEPTH_TEST);
- }
- void MyGLWidget::paintGL()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- k_klawiatura.rysuj();
- k_mysz.rysuj();
- }
- void MyGLWidget::resizeGL(int width, int height)
- {
- double aspectRatio=double(width)/double(height);
- glViewport(0,0,width,height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- halfRangeX=0.5*aspectRatio;
- halfRangeY=0.5;
- glOrtho(-halfRangeX,halfRangeX,-halfRangeY,halfRangeY,-1,1);
- glMatrixMode(GL_MODELVIEW);
- }
- double MyGLWidget::getHalfRangeY() const
- {
- return halfRangeY;
- }
- void MyGLWidget::setHalfRangeY(double value)
- {
- halfRangeY = value;
- }
- double MyGLWidget::getHalfRangeX() const
- {
- return halfRangeX;
- }
- void MyGLWidget::setHalfRangeX(double value)
- {
- halfRangeX = value;
- }
- void MyGLWidget::mousePressEvent(QMouseEvent *event)
- {
- if(event->button()==Qt::LeftButton)
- {
- k_mysz.setX(2.*getHalfRangeX()*event->x()/width()-getHalfRangeX());
- k_mysz.setY(-(2.*getHalfRangeY()*event->y()/height()-getHalfRangeY()));
- updateGL();
- }
- }
- void MyGLWidget::keyPressEvent(QKeyEvent *event)
- {
- switch(event->key())
- {
- case Qt::Key_Up:
- k_klawiatura.changeY(0.01);
- break;
- case Qt::Key_Down:
- k_klawiatura.changeY(-0.01);
- break;
- case Qt::Key_Right:
- k_klawiatura.changeX(0.01);
- break;
- case Qt::Key_Left:
- k_klawiatura.changeX(-0.01);
- break;
- }
- updateGL();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement