Advertisement
Guest User

blablabluberbluber

a guest
Oct 5th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. //test.hpp
  2. #ifndef TEST_H_
  3. #define TEST_H_
  4. #include <QWindow>
  5.  
  6. class QBackingStore;
  7. class QTimer;
  8. class QResizeEvent;
  9. class QOpenGLContext;
  10. class QOpenGLPaintDevice;
  11.  
  12. class Test
  13.     : public QWindow
  14. {
  15.     Q_OBJECT
  16. public:
  17.     Test();
  18. private slots:
  19.     void mainLoop();
  20. private:
  21.     void resizeEvent(QResizeEvent *);
  22.     void init();
  23.     void render();
  24.  
  25.     QTimer *timer;
  26.  
  27.     QOpenGLContext *glContext;
  28. };
  29.  
  30. #endif
  31.  
  32.  
  33. //test.cpp:
  34. #include "test.hpp"
  35. #include <QtGui>
  36. #include <iostream>
  37. #include <stdlib.h>
  38. #include <QOpenGLFunctions_4_3_Compatibility>
  39.  
  40. Test::Test()
  41.     :QWindow()
  42.     ,timer(new QTimer())
  43.     ,glContext(new QOpenGLContext)
  44. {
  45.     setSurfaceType(QWindow::OpenGLSurface);
  46.     setGeometry(100, 100, 600, 500);
  47.  
  48.     glContext->setFormat(requestedFormat());
  49.     glContext->create();
  50.  
  51.     connect(timer, SIGNAL(timeout()),
  52.         this, SLOT(mainLoop()));
  53.     timer->start(1000/60);
  54. }
  55.  
  56. void Test::init()
  57. {
  58.     glMatrixMode(GL_PROJECTION);
  59.     glLoadIdentity();
  60.  
  61.     glViewport(0, 0, width(), height());
  62.     glOrtho(0, width(), 0, height(), -1, 1);
  63.  
  64.     glMatrixMode(GL_MODELVIEW);
  65.     glLoadIdentity();
  66.  
  67. }
  68.  
  69. void Test::resizeEvent(QResizeEvent *e)
  70. {
  71.     init();
  72. }
  73.  
  74. void Test::mainLoop()
  75. {
  76.     if(isExposed())
  77.         render();
  78. }
  79.  
  80. void Test::render()
  81. {
  82.     //init
  83.     glContext->makeCurrent(this);
  84.  
  85.     static bool inited = false;
  86.     if(!inited)
  87.     {
  88.         inited = true;
  89.         init();
  90.     }
  91.  
  92.     //reder
  93.     static int x=0;
  94.     static int y=0;
  95.  
  96.     x++; y++;
  97.  
  98.     glClearColor(0, 0, 0, 0);
  99.     glClear(GL_COLOR_BUFFER_BIT);
  100.  
  101.     glColor3d(1, 1, 1);
  102.  
  103.     glBegin(GL_LINE_LOOP);
  104.     glVertex2d(x, y);
  105.     glVertex2d(x+50, y);
  106.     glVertex2d(x+50, y+50);
  107.     glVertex2d(x, y+50);
  108.     glEnd();
  109.  
  110.     glContext->swapBuffers(this);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement