重要的话写在前面~~通过今晚的实验,知道了EBO是不能随便release的~~~一直不要release就可以了,否则vao会失效

Display.h

#ifndef DISPLAYWIDGET_H
#define DISPLAYWIDGET_H #include <QGLWidget> #include <QOpenGLFunctions>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLTexture> class DisplayGLWidget:public QGLWidget,protected QOpenGLFunctions
{
Q_OBJECT
public:
DisplayGLWidget(QWidget *parent = );
~DisplayGLWidget(); void teardownGL();
public slots:
//void CompileAndLinkVertexShader(const QString& shaderText);
//void CompileAndLinkFragmentShader(const QString& shaderText); protected: void initializeGL() ;
void paintGL() ;
void resizeGL(int width, int height) ; //void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; private:
QOpenGLBuffer m_vbo;
QOpenGLBuffer m_ebo;
QOpenGLVertexArrayObject m_vao;
QOpenGLShaderProgram *m_program;
QOpenGLTexture *texture; void printVersionInformation(); void initTextures(); }; #endif

Display.cpp

#include "DisplayGLWidget.h"
#include "Vertex.h" #include <QDebug>
#include <iostream> // Create a colored triangle
static const Vertex sg_vertexes[] = {
Vertex( QVector3D( 0.00f, 0.75f, 1.0f), QVector3D(1.0f, 0.0f, 0.0f) ),
Vertex( QVector3D( 0.75f, -0.75f, 1.0f), QVector3D(0.0f, 1.0f, 0.0f) ),
Vertex( QVector3D(-0.75f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 1.0f) )
}; static const GLfloat vertices[] = {
// Positions // Colors // Texture Coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
}; static const GLfloat vertices1[] =
{
-0.5f, 0.5f, 0.0f, // Top Left
-0.5f,0.0f, 0.0f,
0.0f, 0.5f, 0.0f
}; static const GLuint indices[] = { // Note that we start from 0!
, , , // First Triangle
, , // Second Triangle
}; DisplayGLWidget::DisplayGLWidget(QWidget* parent)
:QGLWidget(parent)
//: QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
, m_vbo(QOpenGLBuffer::VertexBuffer)
, m_ebo(QOpenGLBuffer::IndexBuffer) { int a = ;
//timer.start();
} DisplayGLWidget::~DisplayGLWidget()
{
makeCurrent();
teardownGL();
} void DisplayGLWidget::teardownGL()
{
// Actually destroy our OpenGL information
m_vbo.destroy();
m_ebo.destroy();
m_vao.destroy();
delete m_program;
} void DisplayGLWidget::initializeGL()
{
//glShadeModel(GL_SMOOTH); // 启用阴影光滑
//glClearColor(0.2,0.2,0.2,0); // 设置清除屏幕的颜色
//glClearDepth(1.0); // 设置深度缓存
//glEnable(GL_DEPTH_TEST); // 深度测试
//glDepthFunc(GL_LEQUAL); // 启用深度测试
//glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // 进行最好的透视修正,可能会影响性能
initializeOpenGLFunctions();
printVersionInformation();
glClearColor(0.3,0.3,0.3,); // Application-specific initialization {
// Create Shader (Do not release until VAO is created)
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "./shaders/simple.vert");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "./shaders/simple.frag");
m_program->link();
m_program->bind(); // Create Vertex Array Object
m_vao.create();
m_vao.bind(); // Create Buffer (Do not release until VAO is created)
m_vbo.create();
m_vbo.bind();
m_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vbo.allocate(vertices, sizeof(vertices)); m_ebo.create();
m_ebo.bind();
m_ebo.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_ebo.allocate(indices,sizeof(indices)); m_program->enableAttributeArray();
m_program->enableAttributeArray();
m_program->enableAttributeArray();
m_program->setAttributeBuffer(, GL_FLOAT, , , *sizeof(GLfloat)); // 3表示的是这一个属性里面有几个分量
m_program->setAttributeBuffer(, GL_FLOAT, *sizeof(GLfloat), , *sizeof(GLfloat));
m_program->setAttributeBuffer(, GL_FLOAT, * sizeof(GLfloat), , * sizeof(GLfloat)); initTextures(); // Release (unbind) all m_vbo.release();
m_vao.release();
//m_ebo.release();
m_program->release();
} } void DisplayGLWidget::paintGL()
{
//makeCurrent(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // 清除屏幕和深度缓存
//glLoadIdentity(); // 重置当前模型的观察矩阵
// Render using our shader m_program->bind();
{ m_program->setUniformValue("ourTexture", );
texture->bind();
m_vao.bind();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawElements(GL_TRIANGLES,,GL_UNSIGNED_INT,);
//glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
//glDrawArrays(GL_TRIANGLES, 0, 3);
m_vao.release();
}
m_program->release(); /*m_program1->bind();
{
m_vao1.bind();
glDrawArrays(GL_TRIANGLES,0,3);
m_vao1.release();
}
m_program1->release();*/
} void DisplayGLWidget::resizeGL(int width, int height)
{
if (height == ) // 规定屏幕高度不得低于20
{
height = ;
}
glViewport(,,(GLint)width,(GLint)height); // 重置当前的视口
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity(); // 重置投影矩阵
//gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,100.0); // 建立透视投影矩阵
glMatrixMode(GL_MODELVIEW); // 选择模型观察矩阵
glLoadIdentity(); // 重置模型观察矩阵
} //void DisplayGLWidget::keyPressEvent(QKeyEvent* e)
//{
// switch (e->key())
// {
// default:
// break;
// }
//} void DisplayGLWidget::mouseMoveEvent(QMouseEvent* event)
{ } void DisplayGLWidget::printVersionInformation()
{
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) << ")";
qDebug() << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")";
} void DisplayGLWidget::initTextures()
{
texture = new QOpenGLTexture(QImage("./resources/texture/flower.jpg").mirrored());
texture->setMinificationFilter(QOpenGLTexture::Nearest);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
texture->setWrapMode(QOpenGLTexture::Repeat); } void DisplayGLWidget::mousePressEvent(QMouseEvent* event)
{ }

结果:

Qt5.6.0+OpenGL 纹理贴图首战告捷的更多相关文章

  1. 基于Cocos2d-x学习OpenGL ES 2.0系列——纹理贴图(6)

    在上一篇文章中,我们介绍了如何绘制一个立方体,里面涉及的知识点有VBO(Vertex Buffer Object).IBO(Index Buffer Object)和MVP(Modile-View-P ...

  2. OpenGL 纹理贴图

    前一节实例代码中有个贴图操作. 今天就简单说明一下纹理贴图... 为了使用纹理贴图.我们首先需要启用纹理贴图功能. 我们可以在Renderer实现的onSurfaceCreated中定义启用: // ...

  3. (转载)Cocos2dx-OpenGL ES2.0教程:纹理贴图(6)

    在上一篇文章中,我们介绍了如何绘制一个立方体,里面涉及的知识点有VBO(Vertex Buffer Object).IBO(Index Buffer Object)和MVP(Modile-View-P ...

  4. [OpenGL]纹理贴图实现 总结

    实现步骤 第一步:设置所需要的OpenGL环境 设置上下文环境 删除已经存在的渲染的缓存 设置颜色缓存 设置帧缓存 清除缓存 设置窗口大小 开启功能 编译shander 使用program 获取sha ...

  5. 用OpenGL进行立方体表面纹理贴图

    一.目的 掌握OpenGL中纹理对象的创建.绑定与使用方法. 二.简单介绍 1,连接静态库 #pragma comment(lib, "glut32.lib") #pragma c ...

  6. opengl学习笔记(三):经过纹理贴图的棋盘

    opengl纹理贴图的步骤: 1:创建纹理对象,并为它指定一个纹理 2:确定纹理如何应用到每个像素上 3:启用纹理贴图功能 4:绘制场景,提供纹理坐标和几何图形坐标 注意:纹理坐标必须在RGBA模式下 ...

  7. (转)使用OpenGL显示图像(七)Android OpenGLES2.0——纹理贴图之显示图片

    转:http://blog.csdn.net/junzia/article/details/52842816 前面几篇博客,我们将了Android中利用OpenGL ES 2.0绘制各种形体,并在上一 ...

  8. IOS 中openGL使用教程3(openGL ES 入门篇 | 纹理贴图(texture)使用)

    在这篇文章中,我们将学习如何在openGL中使用纹理贴图. penGL中纹理可以分为1D,2D和3D纹理,我们在绑定纹理对象的时候需要指定纹理的种类.由于本文将以一张图片为例,因此我们为我们的纹理对象 ...

  9. android ndk调用OpenGL 实现纹理贴图Texture

    android ndk调用OpenGL 实现纹理贴图Texture 时间 2014-06-25 05:24:39  CSDN博客 原文  http://blog.csdn.net/chrisfxs/a ...

随机推荐

  1. Eclipse远程调试应用程序

    第一步,在应用程序的配置文件run.xml中加入下面的配置项,启动应用程序: <target name="run" depends="checkBuilderFai ...

  2. 洛谷 [P1119] 灾后重建

    我们发现每次询问都是对于任意两点的,所以这是一道多源最短路径的题,多源最短路径,我们首先想到floyd,因为询问的时间是不降的,所以对于每次询问,我们将还没有进行松弛操作的的点k操作. #includ ...

  3. 自兴人工智能-------------Python入门基础(1)

    Python 是一门简单易学且功能强大的编程语言. 它拥有高效的高级数据结构, 并且能够用简单而又高效的方式进行面向对象编程. Python 优雅的语法和动态 类型,再结合它的解释性,使其在大多数平台 ...

  4. C++数据结构学习之顺序表

    顺序表是数据结构中最基本也是应用相当广泛的一种数据结构类型.它通常包含三个私有成分,即指向数据数组的头指针.当前表长以及表的实际容量.表的头指针通常指向数据数组的基地址,通过数组的形式进行访问数据数组 ...

  5. CentOS 7 搭建基于携程Apollo(阿波罗)配置中心单机模式

    Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性.服务端基于Spring Boot ...

  6. MySQL建立外键(Foreign Key)

    如果在最初建立表的时候就建立外键这样一般不会有什么问题,顺便说一下建立外键的时候,需要注意的地方. cascade方式在父表上update/delete记录时,同步update/delete掉子表的匹 ...

  7. android应用中去android市场去评分的功能实现(吐槽一波个人应用上线...)

    一般的app可能会有这中功能,在应用中去android商店评分来提高排名,前段时间也把我的博客园上传到商店,这里不得不吐槽一些android商店的开放平台. 酷派,vivo,oppo,联想不支持个人开 ...

  8. 使用mybatis插入自增主键ID的数据后返回自增的ID

    在开发中碰到用户注册的功能需要用到用户ID,但是用户ID是数据库自增生成的,这种情况上网查询后使用下面的方式配置mybatis的insert语句可以解决: <insert id="in ...

  9. sql server在一个字段相同值时,另一个字段结果拼接

    如下字段红框里的信息都一样的,通过转换实现字段拼接 SELECT formmain_id,(SELECT field0040+';' FROM formson_5489 WHERE formmain_ ...

  10. 【Unity3D技术文档翻译】第1.7篇 AssetBundles 补丁更新

    上一章:[Unity3D技术文档翻译]第1.6篇 使用 AssetBundle Manager 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced ...