Qt5.7+VS2015 64位的环境,使用Qt的QOpenglWidget,来显示一张图片,采用GLSL。学习入门挺适合的。在网上找了很久资源,还是自己折腾搞定,特地分享出来。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-05 10:45 OpenglWdtTest
目录 0 2017-12-05 10:17 OpenglWdtTestGLSL
文件 242 2017-12-05 10:07 OpenglWdtTestGLSLfshader.glsl
文件 268 2017-12-05 10:17 OpenglWdtTestGLSLvshader.glsl
文件 26338 2017-12-05 10:37 OpenglWdtTestMakefile
文件 33310 2017-12-05 10:37 OpenglWdtTestMakefile.Debug
文件 33254 2017-12-05 10:37 OpenglWdtTestMakefile.Release
文件 449 2017-12-05 10:37 OpenglWdtTestOpenglWdtTest.pro
文件 23794 2017-12-05 10:45 OpenglWdtTestOpenglWdtTest.pro.user
文件 46636 2017-11-21 10:42 OpenglWdtTestS40_duplicate.png
文件 3392 2017-12-05 10:37 OpenglWdtTestcameraglwidget.cpp
文件 854 2017-12-05 10:35 OpenglWdtTestcameraglwidget.h
目录 0 2017-12-05 10:45 OpenglWdtTestdebug
文件 66048 2017-12-05 10:37 OpenglWdtTestdebugOpenglWdtTest.exe
文件 3100672 2017-12-05 10:37 OpenglWdtTestdebugOpenglWdtTest.pdb
文件 190545 2017-12-05 10:35 OpenglWdtTestdebugmainwindow.obj
文件 2715 2017-12-05 10:35 OpenglWdtTestdebugmoc_mainwindow.cpp
文件 116381 2017-12-05 10:35 OpenglWdtTestdebugmoc_mainwindow.obj
文件 339 2017-12-04 15:01 OpenglWdtTestmain.cpp
目录 0 2017-12-05 10:46 OpenglWdtTest
elease
文件 640066 2017-11-20 11:30 OpenglWdtTest est.jpg
文件 2192 2017-12-04 16:06 OpenglWdtTestui_mainwindow.h
#include “cameraglwidget.h“
#include
#include
#include
#include
struct VertexData
{
QVector2D position;
QVector2D texCoord;
};
CameraGLWidget::CameraGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
}
CameraGLWidget::~CameraGLWidget()
{
arrayBuf.destroy();
}
void CameraGLWidget::resizeGL(int w int h)
{
glViewport(00wh);
}
void CameraGLWidget::paintGL()
{
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_texture->bind();
// Use texture unit 0 which contains cube.png
m_program->setUniformValue(m_textureUniform 0);
// Draw cube geometry using indices from VBO 1
glDrawArrays(GL_TRIANGLE_STRIP 0 4);
}
void CameraGLWidget::initializeGL()
{
initializeOpenGLFunctions();
initShaders();
initTextures();
initBuffArray();
}
void CameraGLWidget::initShaders()
{
//Create QOpenGLShaderProgram
m_program = new QOpenGLShaderProgram;
// Compile vertex shader
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex“GLSL/vshader.glsl“);
// Compile fragment shader
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment“GLSL/fshader.glsl“);
// link shader pipeline
m_program->link();
// Bind shader pipeline for use
m_program->bind();
//Get identifer of Attribute or uniform value in vshader.glsl and fshader.glsl
m_vertexAttr = m_program->attributeLocation(“vertexIn“);
m_textureAttr = m_program->attributeLocation(“textureIn“);
m_textureUniform = m_program->uniformLocation(“texture“);
}
void CameraGLWidget::initTextures()
{
// Load image
m_texture = new QOpenGLTexture(QImage(“./S40_duplicate.png“).mirrored());
// Set nearest filtering mode for texture minification
m_texture->setMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification
m_texture->setMagnificationFilter(QOpenGLTexture::Linear);
// Wrap texture coordinates by repeating
// f.ex. texture coordinate (1.1 1.2) is same as (0.1 0.2)
m_texture->setWrapMode(QOpenGLTexture::Repeat);
}
void CameraGLWidget:: initBuffArray()
{
// Vertex data and texture data
VertexData vertices[] = {
{QVector2D(-1.0f -1.0f) QVector2D(0.0f 0.0f)} // v0
{QVector2D( 1.0f -1.0f) QVector2D(1.0f 0.0f)} // v1
{QVector2D(-1.0f 1.0f) QVector2D(0.0f 1.0f)} //v2
{QVector2D( 1.0f 1.0f) QVector2D(1.0f 1.0f)} // v3
};
arrayBuf.create();
arrayBuf.bind();
arrayBuf.allocate(vertices 4 * sizeof(VertexData));
// Offset for position
quintptr offset = 0;
// Tell OpenGL programmable pipeline how to locate vertex position data
m_program->enableAttributeArray(m_vertexAttr);
m_program->setAttributeBuffer(m_vertex
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-05 10:45 OpenglWdtTest
目录 0 2017-12-05 10:17 OpenglWdtTestGLSL
文件 242 2017-12-05 10:07 OpenglWdtTestGLSLfshader.glsl
文件 268 2017-12-05 10:17 OpenglWdtTestGLSLvshader.glsl
文件 26338 2017-12-05 10:37 OpenglWdtTestMakefile
文件 33310 2017-12-05 10:37 OpenglWdtTestMakefile.Debug
文件 33254 2017-12-05 10:37 OpenglWdtTestMakefile.Release
文件 449 2017-12-05 10:37 OpenglWdtTestOpenglWdtTest.pro
文件 23794 2017-12-05 10:45 OpenglWdtTestOpenglWdtTest.pro.user
文件 46636 2017-11-21 10:42 OpenglWdtTestS40_duplicate.png
文件 3392 2017-12-05 10:37 OpenglWdtTestcameraglwidget.cpp
文件 854 2017-12-05 10:35 OpenglWdtTestcameraglwidget.h
目录 0 2017-12-05 10:45 OpenglWdtTestdebug
文件 66048 2017-12-05 10:37 OpenglWdtTestdebugOpenglWdtTest.exe
文件 3100672 2017-12-05 10:37 OpenglWdtTestdebugOpenglWdtTest.pdb
文件 190545 2017-12-05 10:35 OpenglWdtTestdebugmainwindow.obj
文件 2715 2017-12-05 10:35 OpenglWdtTestdebugmoc_mainwindow.cpp
文件 116381 2017-12-05 10:35 OpenglWdtTestdebugmoc_mainwindow.obj
文件 339 2017-12-04 15:01 OpenglWdtTestmain.cpp
目录 0 2017-12-05 10:46 OpenglWdtTest
elease
文件 640066 2017-11-20 11:30 OpenglWdtTest est.jpg
文件 2192 2017-12-04 16:06 OpenglWdtTestui_mainwindow.h
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)