]> git.sesse.net Git - kdenlive/blob - src/videoglwidget.cpp
Add Mac OS X compatibility through new MLT sdl_audio consumer and a QTGLWidget!
[kdenlive] / src / videoglwidget.cpp
1
2 #include <QtGui>
3 #include <QtOpenGL>
4 #include "videoglwidget.h"
5
6 #ifndef GL_TEXTURE_RECTANGLE_EXT
7 #define GL_TEXTURE_RECTANGLE_EXT GL_TEXTURE_RECTANGLE_NV
8 #endif
9
10 VideoGLWidget::VideoGLWidget(QWidget *parent)
11  : QGLWidget(parent)
12  , m_image_width(0)
13  , m_image_height(0)
14  , m_texture(0)
15  , m_display_ratio(4.0/3.0)
16  , m_backgroundColor(Qt::gray)
17 {
18 }
19
20 VideoGLWidget::~VideoGLWidget()
21 {
22     makeCurrent();
23     if (m_texture)
24         glDeleteTextures(1, &m_texture);
25 }
26
27 QSize VideoGLWidget::minimumSizeHint() const
28 {
29     return QSize(40, 30);
30 }
31
32 QSize VideoGLWidget::sizeHint() const
33 {
34     return QSize(400, 300);
35 }
36
37 void VideoGLWidget::initializeGL()
38 {
39     qglClearColor(m_backgroundColor);
40     glShadeModel(GL_FLAT);
41     glDisable(GL_DEPTH_TEST);
42     glDisable(GL_CULL_FACE);
43     glDisable(GL_LIGHTING);
44     glDisable(GL_DITHER);
45     glDisable(GL_BLEND);
46     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
47 }
48
49 void VideoGLWidget::resizeGL(int width, int height)
50 {
51     double this_aspect = (double) width / height;
52
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))
56     {
57         w = width;
58         h = height;
59     }
60     // Use OpenGL to normalise sample aspect ratio
61     else if (height * m_display_ratio > width)
62     {
63         w = width;
64         h = width / m_display_ratio;
65     }
66     else
67     {
68         w = height * m_display_ratio;
69         h = height;
70     }
71     x = (width - w) / 2;
72     y = (height - h) / 2;
73
74     glViewport(0, 0, width, height);
75     glMatrixMode(GL_PROJECTION);
76     glLoadIdentity();
77     gluOrtho2D(0, width, height, 0);
78     glMatrixMode(GL_MODELVIEW);
79 }
80
81 void VideoGLWidget::paintGL()
82 {
83     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84     if (m_texture)
85     {
86         glEnable(GL_TEXTURE_RECTANGLE_EXT);
87         glBegin(GL_QUADS);
88             glTexCoord2i(0, 0);
89             glVertex2i(x, y);
90             glTexCoord2i(m_image_width, 0);
91             glVertex2i(x + w, y);
92             glTexCoord2i(m_image_width, m_image_height);
93             glVertex2i(x + w, y + h);
94             glTexCoord2i(0, m_image_height);
95             glVertex2i(x, y + h);
96         glEnd();
97         glDisable(GL_TEXTURE_RECTANGLE_EXT);
98     }
99 }
100
101 void VideoGLWidget::showImage(QImage image)
102 {
103     m_image_width = image.width();
104     m_image_height = image.height();
105
106     makeCurrent();
107     if (m_texture)
108         glDeleteTextures(1, &m_texture);
109     glPixelStorei(GL_UNPACK_ROW_LENGTH, m_image_width);
110     glGenTextures(1, &m_texture);
111     glBindTexture(GL_TEXTURE_RECTANGLE_EXT, m_texture);
112     glTexParameteri(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
113     glTexParameterf(GL_TEXTURE_RECTANGLE_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114     glTexImage2D(GL_TEXTURE_RECTANGLE_EXT, 0, GL_RGBA8, m_image_width, m_image_height, 0, GL_RGBA,
115         GL_UNSIGNED_BYTE, image.bits());
116     updateGL();
117 }