This shows you the differences between two versions of the page.
— |
wisar:fast-2d-widget [2014/08/11 13:25] (current) tmburdge created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | WonderWidget.h | ||
+ | <pre> | ||
+ | #include <QGLWidget> | ||
+ | class WonderWidget: public QGLWidget | ||
+ | { | ||
+ | Q_OBJECT | ||
+ | public: | ||
+ | WonderWidget(QWidget* parent, int sourcewidth, int sourceheight, int destwidth, int destheight): | ||
+ | QGLWidget(parent) | ||
+ | { | ||
+ | _sourcewidth = sourcewidth; | ||
+ | _sourceheight = sourceheight; | ||
+ | resize(destwidth, destheight); | ||
+ | } | ||
+ | |||
+ | void update(char* imgData); | ||
+ | |||
+ | protected: | ||
+ | void initializeGL(); | ||
+ | void paintGL(); | ||
+ | void resizeGL(int width, int height); | ||
+ | |||
+ | GLuint textureID; | ||
+ | int _sourcewidth; | ||
+ | int _sourceheight; | ||
+ | }; | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | WonderWidget.cpp | ||
+ | <pre> | ||
+ | #include "WonderWidget.h" | ||
+ | void WonderWidget::update(char* imgData) | ||
+ | { | ||
+ | if (imgData != 0) | ||
+ | { | ||
+ | glBindTexture(GL_TEXTURE_2D, textureID); | ||
+ | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _sourcewidth, _sourceheight, 0, GL_RGB, GL_UNSIGNED_BYTE, imgData); | ||
+ | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); | ||
+ | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); | ||
+ | |||
+ | } | ||
+ | |||
+ | updateGL(); | ||
+ | } | ||
+ | |||
+ | void WonderWidget::initializeGL() | ||
+ | { | ||
+ | glEnable(GL_DEPTH_TEST); | ||
+ | glEnable(GL_CULL_FACE); | ||
+ | glEnable(GL_TEXTURE_2D); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void WonderWidget::paintGL() | ||
+ | { | ||
+ | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
+ | glLoadIdentity(); | ||
+ | |||
+ | glBindTexture(GL_TEXTURE_2D, textureID); | ||
+ | glBegin(GL_QUADS); | ||
+ | glTexCoord2d(0,1); | ||
+ | glVertex3d(0, 0, -1); | ||
+ | |||
+ | glTexCoord2d(1,1); | ||
+ | glVertex3d(1, 0, -1); | ||
+ | |||
+ | glTexCoord2d(1,0); | ||
+ | glVertex3d(1, 1, -1); | ||
+ | |||
+ | glTexCoord2d(0,0); | ||
+ | glVertex3d(0, 1, -1); | ||
+ | glEnd(); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | void WonderWidget::resizeGL(int width, int height) | ||
+ | { | ||
+ | glViewport(0, 0, width, height); | ||
+ | |||
+ | glMatrixMode(GL_PROJECTION); | ||
+ | glLoadIdentity(); | ||
+ | glOrtho(0, 1, 0, 1, 0.1, 2.0); | ||
+ | glMatrixMode(GL_MODELVIEW); | ||
+ | |||
+ | } | ||
+ | |||
+ | </pre> |