/* ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
* ┃ ┏━┣━┣┓ ┏┓┏┓┳┓┏━━┓┣┣━┓ ┓ ┓┣┳━┓ ┃
* ┃ ┏┏┏╯━┓┳┳━┛┏╯┃┃┃ ┃┣┣━┓┃┃ ┃┃┃ ┃
* ┃ ┃┃┏━╮┃┗┗┏╯┗┃┃╯┃ ┃┏┣━┓┃┃ ┃╯┣━┓ ┃
* ┃ ╰┫┏━┻╯┗┳┣┛┏┛┃┃┣━━┫┃┃ ┃┃┃┗╯ ┃ ┃
* ┃ ┏┫━┳━┫┏┃┣┓┗┃┃╯┃ ┃┃┃ ┃ ┃ ┃ ┣━┓ ┃
* ┃ ┗┗┗━━╯┗┛┛╯┗╯╰ ┗━━╯ ┛ ┛┗╯ ╰┛┗ ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┃ Copyright (c) 2013 jiangcaiyang ┃
* ┃ This software is provided 'as-is', without any express or implied ┃
* ┃ warranty. In no event will the authors be held liable for any damages ┃
* ┃ arising from the use of this software. ┃
* ┃ ┃
* ┃ Permission is granted to anyone to use this software for any purpose, ┃
* ┃ including commercial applications, and to alter it and redistribute it ┃
* ┃ freely, subject to the following restrictions: ┃
* ┃ ┃
* ┃ 1. The origin of this software must not be misrepresented; you must ┃
* ┃ not claim that you wrote the original software. If you use this ┃
* ┃ software in a product, an acknowledgment in the product ┃
* ┃ documentation would be appreciated but is not required. ┃
* ┃ 2. Altered source versions must be plainly marked as such, and must ┃
* ┃ not be misrepresented as being the original software. ┃
* ┃ 3. This notice may not be removed or altered from any source ┃
* ┃ distribution. ┃
* ┃ ┃
* ┃ jiangcaiyang jiangcaiyang123@163.com ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┃ file name: GLWindow.cpp ┃
* ┃ create date: 2014年2月19日星期三 18时55分33秒 ┃
* ┃ last modified date: 2014年2月19日星期三 18时55分33秒 ┃
* ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/
#include <QApplication>
#include <QFile>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include "GLWindow.h"
/*---------------------------------------------------------------------------*/
GLWindow::GLWindow( QWindow* pParent ):
QWindow( pParent ),
m_pContext( Q_NULLPTR )
{
setSurfaceType( OpenGLSurface );
resize( 640, 360 );
setTitle( tr( "OpenGL 4.x Tutorial" ) );
connect( qApp, SIGNAL( lastWindowClosed( ) ),
this, SLOT( onClose( ) ) );
}
/*---------------------------------------------------------------------------*/
void GLWindow::exposeEvent( QExposeEvent* pEvent )
{
Q_UNUSED( pEvent );
if ( m_pContext == Q_NULLPTR )
{
QSurfaceFormat format;
format.setVersion( 4, 3 ); // 需要OpenGL4.3版本
format.setSamples( 4 ); // 四倍采样
m_pContext = new QOpenGLContext( this );
m_pContext->setFormat( format );
m_pContext->create( );
m_pContext->makeCurrent( this );
initializeOpenGLFunctions( );
InitGL( );
}
ResizeGL( width( ), height( ) );
RenderGL( );
m_pContext->swapBuffers( this );
}
/*---------------------------------------------------------------------------*/
void GLWindow::onClose( void )
{
ReleaseGL( );
}
/*---------------------------------------------------------------------------*/
// 这里存放了一些全局变量
GLuint g_VertexShader = 0;
GLuint g_FragmentShader = 0;
GLuint g_Program = 0;
GLuint g_VertexArray = 0;
/*---------------------------------------------------------------------------*/
inline QByteArray ShaderData( const QString& fileName )
{
QByteArray result;
QFile shaderFile( fileName );
if ( shaderFile.open( QIODevice::ReadOnly ) )
{
result = shaderFile.readAll( );
shaderFile.close( );
}
return result;
}
/*---------------------------------------------------------------------------*/
void GLWindow::InitGL( void )
{
// 初始化顶点着色器
QByteArray vsData = ShaderData( ":/Shader.vert" );
const char* vsPointer = vsData.constData( );
g_VertexShader = glCreateShader( GL_VERTEX_SHADER );
glShaderSource( g_VertexShader, // 着色器标识
1, // 着色器文本数目
&vsPointer, // 着色器字符串指针
Q_NULLPTR ); // 字符串长度
glCompileShader( g_VertexShader );
// 初始化片断着色器
QByteArray fsData = ShaderData( ":/Shader.frag" );
const char* fsPointer = fsData.constData( );
g_FragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
glShaderSource( g_FragmentShader, // 着色器标识
1, // 着色器文本数目
&fsPointer, // 着色器字符串
Q_NULLPTR ); // 字符串长度
glCompileShader( g_FragmentShader );
// 初始化着色器程序
g_Program = glCreateProgram( );
glAttachShader( g_Program, g_VertexShader );
glAttachShader( g_Program, g_FragmentShader );
glLinkProgram( g_Program );
// 删除着色器
glDeleteShader( g_VertexShader );
glDeleteShader( g_FragmentShader );
// 初始化顶点数组对象(VAO)
glGenVertexArrays( 1, &g_VertexArray );
glBindVertexArray( g_VertexArray );
}
/*---------------------------------------------------------------------------*/
void GLWindow::ResizeGL( int width, int height )
{
glViewport( 0, 0, width, height );
}
/*---------------------------------------------------------------------------*/
void GLWindow::RenderGL( void )
{
static const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glClearBufferfv( GL_COLOR, 0, black );
glUseProgram( g_Program );
glDrawArrays( GL_TRIANGLES, 0, 3 );
}
/*---------------------------------------------------------------------------*/
void GLWindow::ReleaseGL( void )
{
glDeleteVertexArrays( 1, &g_VertexArray );
glDeleteProgram( g_Program );
}
没有合适的资源?快使用搜索试试~ 我知道了~
用OpenGL4.x绘制三角形图元
共7个文件
cpp:2个
h:1个
qrc:1个
3星 · 超过75%的资源 需积分: 9 103 下载量 145 浏览量
2014-02-19
22:03:12
上传
评论
收藏 3KB 7Z 举报
温馨提示
OpenGL很多人都有所了解,也有很多人使用OpenGL开发过工业级的应用和游戏应用,不过它的最新版本OpenGL4.x了解的人就不是太多了。目前OpenGL的最新版本是4.4,并且NVIDIA和AMD厂商发布了支持OpenGL最新版本的驱动程序,我想基于OpenGL4.x讲解的书籍也会逐渐增多。 这回我将上一次的代码稍微作了一下修改,绘制出了一个三角形。其实代码也非常简单,在Client端将GL_POINTS改为GL_TRIANGLES就可以了,片断着色器也不需要做更多的修改,主要是顶点着色器。
资源推荐
资源详情
资源评论
收起资源包目录
OpenGL4.x绘制三角形图元.7z (7个子文件)
OpenGL_4_x_Tutorial_3
GLWindow.h 4KB
main.cpp 3KB
Shader.vert 601B
GLWindow.cpp 7KB
Shader.frag 152B
OpenGL_4_x_Tutorial_3.pro 3KB
Resource.qrc 123B
共 7 条
- 1
资源评论
- vera_72014-05-30解压有问题 = =
彩阳
- 粉丝: 2169
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 转载使用许可协议范本(互联网行业)模版.doc
- 软件产业运行情况调研问卷模版.doc
- 软件产品发布管理流程.doc
- 软件仿真多机串行通信.doc
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功