/* ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
* ┃ ┏━┣━┣┓ ┏┓┏┓┳┓┏━━┓┣┣━┓ ┓ ┓┣┳━┓ ┃
* ┃ ┏┏┏╯━┓┳┳━┛┏╯┃┃┃ ┃┣┣━┓┃┃ ┃┃┃ ┃
* ┃ ┃┃┏━╮┃┗┗┏╯┗┃┃╯┃ ┃┏┣━┓┃┃ ┃╯┣━┓ ┃
* ┃ ╰┫┏━┻╯┗┳┣┛┏┛┃┃┣━━┫┃┃ ┃┃┃┗╯ ┃ ┃
* ┃ ┏┫━┳━┫┏┃┣┓┗┃┃╯┃ ┃┃┃ ┃ ┃ ┃ ┣━┓ ┃
* ┃ ┗┗┗━━╯┗┛┛╯┗╯╰ ┗━━╯ ┛ ┛┗╯ ╰┛┗ ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┃ 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: OpenGLView.cpp ┃
* ┃ create date: 2013年9月24日星期二 19时33分16秒 ┃
* ┃ last modified date: 2013年9月24日星期二 22时12分23秒 ┃
* ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/
#include <cmath>
#include <QMatrix4x4>
#include <QtOpenGL>
#include <QResizeEvent>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QPainter>
#include <QMessageBox>
#include "OpenGLView.h"
/*---------------------------------------------------------------------------*/
bool g_ShowAxis = false;
/*---------------------------------------------------------------------------*/
void OpenGLView::PaintGL( void )
{
glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
glPushMatrix( );
m_Camera.Apply( );
m_3DS.RenderGL( );
if ( g_ShowAxis ) DrawAxis( );
glPopMatrix( );
}
/*---------------------------------------------------------------------------*/
void OpenGLView::DrawAxis( void )
{
static const qint32 s_Scale = 64;
m_pShaderProgram->release( );// 绘制坐标轴不需要着色器
glPushAttrib( GL_LINE_BIT );
glLineWidth( 1.0f );
glBegin( GL_LINES );
// X轴
glColor3ub( 255, 0, 0 );
glVertex3i( 0, 0, 0 );
glVertex3i( s_Scale, 0, 0 );
// Y轴
glColor3ub( 0, 255, 0 );
glVertex3i( 0, 0, 0 );
glVertex3i( 0, s_Scale, 0 );
// Z轴
glColor3ub( 0, 0, 255 );
glVertex3i( 0, 0, 0 );
glVertex3i( 0, 0, s_Scale );
glEnd( );
glPopAttrib( );
m_pShaderProgram->bind( );// 开启着色器
}
/*---------------------------------------------------------------------------*/
void PopupError( const QString& info )
{
QString header = QObject::tr( "Sorry but there are some errors in using shaders.\n"
"Here are some useful information:\n" );
QMessageBox::warning(
Q_NULLPTR,
QObject::tr( "Shader program error" ),
header + info );
}
#define CHECK( stmt, info ) \
if ( !stmt )\
{\
PopupError( info );\
return false;\
}
/*---------------------------------------------------------------------------*/
OpenGLView::OpenGLView( QWidget* pParent ):
QGraphicsView( pParent )
{
m_Camera.SetPos( QVector3D( 0.0, 0.0, 20.0 ) );// 设置摄像机坐标
m_3DS.Load( ":/Model/Various.3ds" );
}
/*---------------------------------------------------------------------------*/
OpenGLView::~OpenGLView( void )
{
m_pShaderProgram->release( );
m_pShaderProgram->removeShader( m_pVertexShader );
if ( m_pVertexShader != Q_NULLPTR ) delete m_pVertexShader;
if ( m_pShaderProgram != Q_NULLPTR ) delete m_pShaderProgram;
}
/*---------------------------------------------------------------------------*/
void OpenGLView::setScene( GraphicsScene* pScene )
{
pScene->SetCamera( &m_Camera );
QGraphicsView::setScene( pScene );
}
/*---------------------------------------------------------------------------*/
bool OpenGLView::SetupShaders( void )
{
// 初始化着色器
m_pShaderProgram = new QOpenGLShaderProgram( viewport( ) );
m_pVertexShader = new QOpenGLShader( QOpenGLShader::Vertex,
m_pShaderProgram );
// 先假装添加一下着色器,然后调用SwitchShader()函数走常规路线
CHECK( m_pShaderProgram->addShader( m_pVertexShader ),
m_pShaderProgram->log( ) );
return SwitchShader( ":/Shader/Diffuse.vert" );
}
/*---------------------------------------------------------------------------*/
bool OpenGLView::SwitchShader( const QString& shaderFileName )
{
m_pShaderProgram->release( );
m_pShaderProgram->removeShader( m_pVertexShader );
// 重新编译着色器、连接至着色器程序最后使用它
CHECK( m_pVertexShader->compileSourceFile( shaderFileName ),
m_pVertexShader->log( ) );
CHECK( m_pShaderProgram->addShader( m_pVertexShader ),
m_pShaderProgram->log( ) );
CHECK( m_pShaderProgram->link( ),
m_pShaderProgram->log( ) );
SetLightPos( );
return true;
}
/*---------------------------------------------------------------------------*/
void OpenGLView::SetLightPos( const QVector3D& lightPos )
{
static QVector3D s_LightPos( 30.0f, 30.0f, 30.0f );
if ( !lightPos.isNull( ) )
{
s_LightPos = lightPos;
}
if ( m_pShaderProgram != Q_NULLPTR &&
m_pShaderProgram->isLinked( ) )
{
m_pShaderProgram->bind( );
m_pShaderProgram->setUniformValue( "lightPos", s_LightPos );
m_pShaderProgram->release( );
}
}
/*---------------------------------------------------------------------------*/
void OpenGLView::InitGL( void )
{
glEnable( GL_MULTISAMPLE );
glEnable( GL_DEPTH_TEST );
glClearColor( 0.0, 0.0, 0.0, 1.0 );
static bool runOnce = SetupShaders( );
Q_UNUSED( runOnce );
m_pShaderProgram->bind( );
}
/*---------------------------------------------------------------------------*/
void OpenGLView::resizeEvent( QResizeEvent* pEvent )
{
scene( )->setSceneRect( 0.0,
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
此程序来源于我的一篇博客,欢迎访问。 http://blog.csdn.net/gamesdev/article/details/11994501 ------------------------------------- 欢迎访问我的博客。 http://blog.csdn.net/gamesdev ------------------------------------- Qt的QWidget大家族已经成熟,不会得到太大的发展了,而随着Qt5的发展,新的界面类会崭露头角,比如QWindow、QSurface、QScreen这些类。但现在的状况(Qt5.2即将出了),这些类和模块并不会对现有的GUI模块造成威胁,更不用说取代Qt4的GUI(在Qt5是QtGui和QtWidgets)模块了。自Qt4.4起,Qt拥有了Graphics-View框架,使用这个框架可以更加轻松地在窗口中嵌入任意你想要绘制的元素。但是据我所知,Qt5的那些类无法和Graphics-View框架进行整合,对于想要利用Qt中Graphics-View框架的同行们,只能退回Qt4的类进行开发了,Qt5的这些类对于创建纯OpenGL还是有利的。
资源详情
资源评论
资源推荐
收起资源包目录
Qt、OpenGL、GLSL以及Qt的Graphics-View框架结合(演示程序+源代码).7z (34个子文件)
source
Format3DS.h 4KB
appIcon.ico 66KB
zh_CN.ts 5KB
ShaderKeywordHighlight.h 4KB
Translation.qrc 98B
Shader
Diffuse.vert 493B
Specular.vert 847B
Specular_Opt.vert 800B
OpenGLView.cpp 11KB
ShaderOptionDialog.h 4KB
Model.qrc 95B
QtGLSL_LightShader_5.pro 4KB
Various.3ds 50KB
lib
libFeedback.a 581KB
lib3ds.a 73KB
OpenGLView.h 4KB
ShaderOptionDialog.ui 7KB
main.cpp 4KB
Shader.qrc 186B
Feedback
Feedback.qrc 147B
FeedbackDialog.h 4KB
Feedback.pri 205B
textEditBackground.png 98KB
pushButtonBackground.png 3KB
ShaderOptionDialog.cpp 7KB
MainWindow.h 3KB
Camera.h 4KB
zh_CN.qm 2KB
ShaderKeywordHighlight.cpp 6KB
Camera.cpp 5KB
MainWindow.cpp 4KB
GraphicsScene.h 4KB
GraphicsScene.cpp 8KB
bin
windows
ShaderForLight.exe 5.97MB
共 34 条
- 1
彩阳
- 粉丝: 2168
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- WEB访问日志分析与入侵检测可视化系统源码(95分以上课设).zip
- UE4-UE5的官方预览模型
- 电子通信设计资料自动关断继电器驱动电路
- 基于Python使用递归和动态规划解决背包问题.zip
- 从0开始建立dSPACE RT Simulink工程:CAN通信(Bus Manager方法)DBC
- 轮胎X光缺陷检测数据集VOC+YOLO格式1903张4类别.zip
- 基于Matlab和遗传算法解决组合优化背包问题.zip
- mycode.asm
- 基于Python和粒子群算法解决01背包问题并进行可视化.zip
- Qt/qt creator实现TCP通信,多线程实现服务器的并发(server/client)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论30