4 #include "videoglwidget.h"
6 #ifndef GL_TEXTURE_RECTANGLE_EXT
7 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
10 VideoGLWidget::VideoGLWidget(QWidget *parent)
15 , m_display_ratio(4.0 / 3.0)
16 , m_backgroundColor(Qt::gray)
20 VideoGLWidget::~VideoGLWidget()
24 glDeleteTextures(1, &m_texture);
27 QSize VideoGLWidget::minimumSizeHint() const
32 QSize VideoGLWidget::sizeHint() const
34 return QSize(400, 300);
37 void VideoGLWidget::initializeGL()
39 qglClearColor(m_backgroundColor);
40 glShadeModel(GL_FLAT);
41 glDisable(GL_DEPTH_TEST);
42 glDisable(GL_CULL_FACE);
43 glDisable(GL_LIGHTING);
46 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
49 void VideoGLWidget::resizeGL(int width, int height)
51 double this_aspect = (double) width / height;
53 // Special case optimisation to negate odd effect of sample aspect ratio
54 // not corresponding exactly with image resolution.
55 if ((int)(this_aspect * 1000) == (int)(m_display_ratio * 1000)) {
59 // Use OpenGL to normalise sample aspect ratio
60 else if (height * m_display_ratio > width) {
62 h = width / m_display_ratio;
64 w = height * m_display_ratio;
70 glViewport(0, 0, width, height);
71 glMatrixMode(GL_PROJECTION);
73 gluOrtho2D(0, width, height, 0);
74 glMatrixMode(GL_MODELVIEW);
77 void VideoGLWidget::paintGL()
79 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81 glEnable(GL_TEXTURE_RECTANGLE_EXT);
85 glTexCoord2i(m_image_width, 0);
87 glTexCoord2i(m_image_width, m_image_height);
88 glVertex2i(x + w, y + h);
89 glTexCoord2i(0, m_image_height);
92 glDisable(GL_TEXTURE_RECTANGLE_EXT);
96 void VideoGLWidget::showImage(QImage image)
98 m_image_width = image.width();
99 m_image_height = image.height();
103 glDeleteTextures(1, &m_texture);
104 glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
105 glGenTextures(1, &m_texture);
106 glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
107 glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
108 glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
109 glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGBA,
110 GL_UNSIGNED_BYTE, image.bits());