Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////
  2. // DISPLAY.H
  3. //////////////////
  4.  
  5. #ifndef DISPLAY_H
  6. #define DISPLAY_H
  7.  
  8. #include <QOpenGLWidget>
  9. #include <QOpenGLFunctions>
  10.  
  11. class Display : public QOpenGLWidget
  12. {
  13. public:
  14.     Display(QWidget* parent);
  15.     ~Display();
  16.  
  17.     void render();
  18.     static QSize displaySize() { return QSize(displayWidth, displayHeight); }
  19.  
  20.     void doTest();
  21.  
  22. protected:
  23.     void initializeGL() override;
  24.     void paintGL() override;
  25.     void resizeGL(int width, int height) override;
  26.  
  27. private:
  28.     static const quint32 displayWidth = 256;
  29.     static const quint32 displayHeight = 240;
  30.  
  31.     void setPixelColor(quint32 x, quint32 y, quint32 color);
  32.  
  33.     QColor clearColor;
  34.  
  35.     quint32* textureBuffer;
  36.     GLuint texture;
  37. };
  38.  
  39. #endif // DISPLAY_H
  40.  
  41.  
  42. //////////////////
  43. // DISPLAY.CPP
  44. //////////////////
  45.  
  46. #include "display.h"
  47.  
  48. Display::Display(QWidget* parent)
  49.     : QOpenGLWidget(parent), clearColor(Qt::black)
  50. {
  51.     textureBuffer = new quint32[displayWidth*displayHeight];
  52.  
  53.     setMinimumSize(displayWidth, displayHeight);
  54. }
  55.  
  56. Display::~Display()
  57. {
  58.     delete[] textureBuffer;
  59. }
  60.  
  61. void Display::initializeGL()
  62. {
  63.     glDisable(GL_DEPTH_TEST);
  64.     glDisable(GL_ALPHA_TEST);
  65.     glDisable(GL_STENCIL_TEST);
  66.  
  67.     glEnable(GL_TEXTURE_2D);
  68.     glEnable(GL_DITHER);
  69.  
  70.     glGenTextures(1, &texture);
  71.     glBindTexture(GL_TEXTURE_2D, texture);
  72.     glPixelStorei(GL_UNPACK_ROW_LENGTH, displayWidth);
  73.  
  74.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  75.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  76.  
  77.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, displayWidth, displayHeight, 0,
  78.                  GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureBuffer);
  79. }
  80.  
  81. void Display::paintGL()
  82. {
  83.     glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), 1.0f);
  84.  
  85.     glViewport(0, 0, width(), height());
  86.  
  87.     glMatrixMode(GL_PROJECTION);
  88.     glLoadIdentity();
  89.     glOrtho(0, width(), 0, height(), -1, 1);
  90.  
  91.     glMatrixMode(GL_MODELVIEW);
  92.     glLoadIdentity();
  93.  
  94.  
  95.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  96.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  97.     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, displayWidth, displayHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, textureBuffer);
  98.  
  99.  
  100.     glBindTexture(GL_TEXTURE_2D, texture);
  101.     glBegin(GL_TRIANGLE_STRIP);
  102.  
  103.     glTexCoord2f(0.0, 1.0);
  104.     glVertex3i(0, 0, 0);
  105.  
  106.     glTexCoord2f(1.0, 1.0);
  107.     glVertex3i(width(), 0, 0);
  108.  
  109.     glTexCoord2f(0.0, 0.0);
  110.     glVertex3i(0, height(), 0);
  111.  
  112.     glTexCoord2f(1.0, 0.0);
  113.     glVertex3i(width(), height(), 0);
  114.  
  115.     glEnd();
  116. }
  117.  
  118. void Display::resizeGL(int width, int height)
  119. {
  120.     Q_UNUSED(width);
  121.     Q_UNUSED(height);
  122.  
  123.     // Do some aspect ratio stuff here eventually...
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement