window.cpp
#include "window.h"
#include <QDebug>
#include <QString>
Window::~Window()
{
makeCurrent();
teardownGL();
}
/*******************************************************************************
* OpenGL Events
******************************************************************************/
void Window::initializeGL()
{
// Initialize OpenGL Backend
initializeOpenGLFunctions();
printContextInformation();
// Set global information
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void Window::resizeGL(int width, int height)
{
// Currently we are not handling width/height changes
(void)width;
(void)height;
}
void Window::paintGL()
{
// Clear
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(1, 0, 0, 1);
glBegin(GL_LINES);
glVertex4f(0, 0, 0, 1);
glVertex4f(0, 1, 0, 1);
glVertex4f(0, 0, 0, 1);
glVertex4f(1, 0, 0, 1);
glVertex4f(0, 0, 0, 1);
glVertex4f(0, 0, 1, 1);
glEnd();
}
void Window::teardownGL()
{
// Currently we have no data to teardown
}
/*******************************************************************************
* Private Helpers
******************************************************************************/
void Window::printContextInformation()
{
QString glType;
QString glVersion;
QString glProfile;
// Get Version Information
glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
// Get Profile Information
#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
switch (format().profile())
{
CASE(NoProfile);
CASE(CoreProfile);
CASE(CompatibilityProfile);
}
#undef CASE
// qPrintable() will print our QString w/o quotes around it.
qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")";
}
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QOpenGLWindow>
#include <QOpenGLFunctions>
class Window : public QOpenGLWindow,
protected QOpenGLFunctions
{
Q_OBJECT
// OpenGL Events
public:
~Window();
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
void teardownGL();
private:
// Private Helpers
void printContextInformation();
};
#endif // WINDOW_H
离线
#include "myopenglweight.h"
#include <QOpenGLFunctions>
#include <math.h>
MyOpenGLWeight::MyOpenGLWeight(QWidget *parent, Qt::WindowFlags f) :
QOpenGLWidget(parent, f)
{
// QSurfaceFormat format;
// format.setDepthBufferSize(24);
// format.setStencilBufferSize(8);
// format.setVersion(3, 2);
// format.setProfile(QSurfaceFormat::CoreProfile);
// setFormat(format);
rotationX = 0.0;
rotationY = 0.0;
rotationZ = 0.0;
faceColor[0] = Qt::red;
faceColor[1] = Qt::green;
faceColor[2] = Qt::blue;
faceColor[3] = Qt::yellow;
}
MyOpenGLWeight::~MyOpenGLWeight()
{
}
void MyOpenGLWeight::initializeGL()
{
//QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
glClearColor(0.2f, 0.1f, 0.5f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void MyOpenGLWeight::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(w) / h;
glFrustum(-x, +x, -1, +1, 5.0, 30.0);
glMatrixMode(GL_MODELVIEW);
}
void MyOpenGLWeight::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
void MyOpenGLWeight::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
startPoint = event->pos();
}
mouseMoveEvent(event);
}
void MyOpenGLWeight::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
QPoint movePoint = event->pos() - startPoint;
{
rotationX += movePoint.y();
rotationY += movePoint.x();
}
update();
startPoint = event->pos();
}
}
GLfloat ctrlpoints[4][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};
void MyOpenGLWeight::draw()
{
static const GLfloat P1[3] = {0.0, -1.0, 2.0};
static const GLfloat P2[3] = {1.0, -1.0, -2.0};
static const GLfloat P3[3] = {0.5, 1.0, 1.5};
static const GLfloat P4[3] = {-1.0, 0.0, -1.5};
static const GLfloat * const coords[4][3] = {
{P1, P2, P3},
{P1, P3, P4},
{P1, P4, P2},
{P2, P4, P3},
};
glLoadIdentity();
glTranslatef(0.0, 0.0, -10);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
float s = 3;
glColor3f(255.0, 0, 0);
glVertex3f( 0, 0, 0 );
glVertex3f( s, 0, 0 );
glColor3f(0, 255.0, 0);
glVertex3f( 0, 0, 0 );
glVertex3f( 0, s, 0 );
glColor3f(0, 0, 255.0);
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, s );
glEnd();
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* The following code displays the control points as dots. */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (int i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
// for(int i = 0; i < 4; ++i)
// {
// glBegin(GL_TRIANGLES);
// glColor3f(faceColor[i].redF(), faceColor[i].greenF(), faceColor[i].blueF());
// for(int j = 0; j < 4; ++j)
// glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
// glEnd();
// }
}
https://github.com/PhoebeTsou/TestGL.git
本站下载: TestGL.7z
离线
Qt5 用 OpenGL 把 YUV 转 RGB显示到 Widget的故事: https://github.com/MasterAler/SampleYUVRenderer
YUV测试文件这里下载: http://trace.eas.asu.edu/yuv/
解压、改名test.yuv复制到 bin 目录即可运行。
也可以本站下载
YUV: akiyo_cif.7z
离线
顺手推荐一个在线显示YUV/RGB格式文件的网站: https://rawpixels.net/
离线
ffmpeg 转码 MOV/MP4 文件到 yuv, 尺寸 1920x1080:
ffmpeg -i tiky.mov tiky.yuv
如果只需生成一帧, 用这个命令
ffmpeg -i tiky.mov -vframes 1 tiky.yuv
顺手推荐一个在线显示YUV/RGB格式文件的网站: https://rawpixels.net/
视频素材来源:
TKM32F499配4.0寸IPS屏在RGB888模式下截屏功能
http://whycan.com/t_5830.html
(出处:哇酷开发者社区【好钜润半导体(TIKY)】)
最近编辑记录 拉轰的脚踏车 (2021-01-09 15:09:48)
离线
https://github.com/james-yoo/CodeBase
#include "myopenglweight.h"
#include <QOpenGLFunctions>
#include <math.h>
MyOpenGLWeight::MyOpenGLWeight(QWidget *parent, Qt::WindowFlags f) :
QOpenGLWidget(parent, f)
{
zoomScale = 1.0f;
// QSurfaceFormat format;
// format.setDepthBufferSize(24);
// format.setStencilBufferSize(8);
// format.setVersion(3, 2);
// format.setProfile(QSurfaceFormat::CoreProfile);
// setFormat(format);
rotationX = 0.0;
rotationY = 0.0;
rotationZ = 0.0;
faceColor[0] = Qt::red;
faceColor[1] = Qt::green;
faceColor[2] = Qt::blue;
faceColor[3] = Qt::yellow;
}
MyOpenGLWeight::~MyOpenGLWeight()
{
}
void MyOpenGLWeight::initializeGL()
{
//QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
glClearColor(0.2f, 0.1f, 0.5f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void MyOpenGLWeight::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(w) / h;
glFrustum(-x, +x, -1, +1, 5.0, 30.0);
glMatrixMode(GL_MODELVIEW);
}
void MyOpenGLWeight::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw();
}
void MyOpenGLWeight::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
startPoint = event->pos();
}
mouseMoveEvent(event);
}
void MyOpenGLWeight::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton)
{
QPoint movePoint = event->pos() - startPoint;
{
rotationX += movePoint.y();
rotationY += movePoint.x();
}
update();
startPoint = event->pos();
}
}
void MyOpenGLWeight::wheelEvent(QWheelEvent *event)
{
QPoint numDegrees = event->angleDelta();
if (numDegrees.y() < 0) zoomScale = zoomScale/1.1;
if (numDegrees.y() > 0) zoomScale = zoomScale*1.1;
paintGL(); // call paintGL()
//update();
}
GLfloat ctrlpoints[4][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};
void MyOpenGLWeight::draw()
{
static const GLfloat P1[3] = {0.0, -1.0, 2.0};
static const GLfloat P2[3] = {1.0, -1.0, -2.0};
static const GLfloat P3[3] = {0.5, 1.0, 1.5};
static const GLfloat P4[3] = {-1.0, 0.0, -1.5};
static const GLfloat * const coords[4][3] = {
{P1, P2, P3},
{P1, P3, P4},
{P1, P4, P2},
{P2, P4, P3},
};
int range = 1000;
glLoadIdentity();
int height = 320;
int width = 240;
//glScalef(zoomScale, zoomScale, zoomScale);
//glOrtho(-0.5*range*zoomScale, +0.5*range*zoomScale, -0.5*height/width*range*zoomScale, +0.5*height/width*range*zoomScale, -5*range, +5*range);
glTranslatef(0.0, 0.0, -10);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex3f(-0.6f, -0.3f, 0.2f);
glVertex3f(0.6f, 0.3f, 0.3f);
glVertex3f(0.6f, 0.3f, 0.3f);
glVertex3f(0.1f, 0.1f, -0.2f);
glVertex3f(0.1f, 0.1f, -0.2f);
glVertex3f(-0.6f, -0.3f, 0.2f);
glEnd();
glBegin(GL_LINES);
float s = 3;
glColor3f(255.0, 0, 0);
glVertex3f( 0, 0, 0 );
glVertex3f( s, 0, 0 );
glColor3f(0, 255.0, 0);
glVertex3f( 0, 0, 0 );
glVertex3f( 0, s, 0 );
glColor3f(0, 0, 255.0);
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, s );
glEnd();
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
/* The following code displays the control points as dots. */
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (int i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
// for(int i = 0; i < 4; ++i)
// {
// glBegin(GL_TRIANGLES);
// glColor3f(faceColor[i].redF(), faceColor[i].greenF(), faceColor[i].blueF());
// for(int j = 0; j < 4; ++j)
// glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
// glEnd();
// }
}
本站下载: qt_opengl_zoom.7z
https://docs.microsoft.com/en-us/windows/win32/opengl/the-program-ported-to-win32
最近编辑记录 拉轰的脚踏车 (2021-01-09 17:09:16)
离线
https://blog.csdn.net/Fox_Alex/article/details/80163942
#include "widget.h"
Widget::Widget(QWidget *parent)
: QGLWidget(parent)
{
}
Widget::~Widget()
{
}
void Widget::initializeGL()
{
//设置widget的坐标和尺寸
setGeometry(300, 150, 500, 500);
//设置清除时颜色
glClearColor(0.0, 0.0, 0.0, 0);
}
void Widget::resizeGL(int w, int h)
{
//视口变换
glViewport(0,0,(GLsizei)w,(GLsizei)h);
//投影变换
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)w/(GLdouble)h,0.1,10000.0);
//视图变换
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,15.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
void Widget::paintGL()
{
//清屏
glClear(GL_COLOR_BUFFER_BIT);
//绘制七彩三角形
glBegin(GL_TRIANGLES);
glColor3f(1.0,0.0,0.0);
glVertex3f(-2,0,0);
glColor3f(0.0,1.0,0.0);
glVertex3f(2,0,0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0,4,0);
glEnd();
glFlush();
}
离线