#include <QApplication>
#include <QFile>
#include <QKeyEvent>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include "GLWindow.h"
/*---------------------------------------------------------------------------*/
GLWindow::GLWindow( QWindow* pParent ):
QWindow( pParent ),
m_Texture( QOpenGLTexture::Target2D ),
m_VertexBuffer( QOpenGLBuffer::VertexBuffer ),
m_pContext( Q_NULLPTR )
{
setSurfaceType( OpenGLSurface );
resize( 640, 360 );
setTitle( tr( "GPGPU OpenGL Texture 2" ) );
connect( qApp, SIGNAL( lastWindowClosed( ) ),
this, SLOT( onClose( ) ) );
}
/*---------------------------------------------------------------------------*/
void GLWindow::exposeEvent( QExposeEvent* pEvent )
{
Q_UNUSED( pEvent );
if ( m_pContext == Q_NULLPTR )
{
QSurfaceFormat format;
m_pContext = new QOpenGLContext( this );
m_pContext->setFormat( format );
m_pContext->create( );
m_pContext->makeCurrent( this );
initializeOpenGLFunctions( );
InitGL( );
}
else m_pContext->makeCurrent( this );
ResizeGL( width( ), height( ) );
RenderGL( );
m_pContext->swapBuffers( this );
}
/*---------------------------------------------------------------------------*/
void GLWindow::keyPressEvent( QKeyEvent *pEvent )
{
Q_UNUSED( pEvent );
m_Colors.clear( );
m_Colors.append( 0.4f );
m_Colors.append( 0.5f );
m_Colors.append( 0.9f );
m_Colors.append( 0.9f );
m_Texture.setData( QOpenGLTexture::Luminance,
QOpenGLTexture::Float32,
m_Colors.data( ) );
QCoreApplication::postEvent( this, new QEvent( QEvent::UpdateRequest ) );
}
/*---------------------------------------------------------------------------*/
void GLWindow::onClose( void )
{
ReleaseGL( );
}
/*---------------------------------------------------------------------------*/
void GLWindow::InitGL( void )
{
float vertices[] =
{
0.2f, 0.2f, 0.0f, 1.0f,
0.2f, 0.8f, 0.0f, 1.0f,
0.8f, 0.2f, 0.0f, 1.0f,
0.8f, 0.8f, 0.0f, 1.0f
};
m_Colors.append( 0.8f );
m_Colors.append( 0.5f );
m_Colors.append( 0.2f );
m_Colors.append( 0.9f );
m_Texture.setMagnificationFilter( QOpenGLTexture::Nearest );
m_Texture.setMinificationFilter( QOpenGLTexture::Nearest );
m_Texture.setSize( 4, 1 );
m_Texture.setFormat( QOpenGLTexture::LuminanceFormat );
m_Texture.allocateStorage( );
m_Texture.setData( QOpenGLTexture::Luminance,
QOpenGLTexture::Float32,
m_Colors.data( ) );
m_VertexBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
m_VertexBuffer.create( );
m_VertexBuffer.bind( );
m_VertexBuffer.allocate( vertices, sizeof( vertices ) );
m_ShaderProgram.addShaderFromSourceFile( QOpenGLShader::Vertex,
":/Texture.vsh" );
m_ShaderProgram.addShaderFromSourceFile( QOpenGLShader::Fragment,
":/Texture.fsh" );
m_ShaderProgram.link( );
}
/*---------------------------------------------------------------------------*/
void GLWindow::ResizeGL( int width, int height )
{
glViewport( 0, 0, width, height );
}
/*---------------------------------------------------------------------------*/
void GLWindow::RenderGL( void )
{
glClearColor( 0.2f, 0.5f, 0.8f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
m_ShaderProgram.bind( );
m_VertexBuffer.bind( );
int positionLoc = m_ShaderProgram.attributeLocation( "position" );
m_ShaderProgram.enableAttributeArray( positionLoc );
m_ShaderProgram.setAttributeBuffer( "position", GL_FLOAT, 0, 4 );
glVertexAttribPointer(
positionLoc, // 属性在着色器中的位置
4, // 属性的个数
GL_FLOAT, // 属性的类型
GL_FALSE, // 属性是否被单位化
0, // 跨度(又称迈)
Q_NULLPTR ); // 属性指针(如果绑定VBO,那么此项为空)
glActiveTexture( GL_TEXTURE0 );
m_Texture.bind( );
m_ShaderProgram.setUniformValue( "texID", 0 );
m_ShaderProgram.setUniformValue( "texSize", m_Colors.size( ) );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );// 绘图
m_Texture.release( );
m_VertexBuffer.release( );
m_ShaderProgram.release( );
}
/*---------------------------------------------------------------------------*/
void GLWindow::ReleaseGL( void )
{
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
博客地址: http://blog.csdn.net/gamesdev/article/details/19507325 在显卡并行计算起源的领域,比如说游戏开发领域,GPU的发展使得其迈向了新的纪元,画面愈加精致,效果愈加震撼,同时并行计算框架CUDA和OpenCL的兴起,则将显卡的计算潜能充分地释放了出来。最后令人高兴的是,以前就受惠于显卡发展的游戏开发领域,将显卡的功能做了新的扩展,以前只用来渲染用的显卡,目前可以做更多逻辑领域相关的事儿了。
资源推荐
资源详情
资源评论
收起资源包目录
GPGPU方法将大量数据通过纹理传输至GPU进行计算.7z (7个子文件)
GPGPU_OpenGL_Texture_2
Texture.fsh 354B
main.cpp 178B
Texture.vsh 82B
Texture.qrc 123B
GLWindow.cpp 5KB
GLWindow.h 1KB
GPGPU_OpenGL_Texture_2.pro 204B
共 7 条
- 1
资源评论
- xgcdd2014-06-10一般,没有帮上我的忙啊
- 酣酣2014-07-25太多分了!一次76分就没了! 评论起来要一个一个来! 乖乖!
彩阳
- 粉丝: 2169
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功