/* ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
* ┃ ┏━┣━┣┓ ┏┓┏┓┳┓┏━━┓┣┣━┓ ┓ ┓┣┳━┓ ┃
* ┃ ┏┏┏╯━┓┳┳━┛┏╯┃┃┃ ┃┣┣━┓┃┃ ┃┃┃ ┃
* ┃ ┃┃┏━╮┃┗┗┏╯┗┃┃╯┃ ┃┏┣━┓┃┃ ┃╯┣━┓ ┃
* ┃ ╰┫┏━┻╯┗┳┣┛┏┛┃┃┣━━┫┃┃ ┃┃┃┗╯ ┃ ┃
* ┃ ┏┫━┳━┫┏┃┣┓┗┃┃╯┃ ┃┃┃ ┃ ┃ ┃ ┣━┓ ┃
* ┃ ┗┗┗━━╯┗┛┛╯┗╯╰ ┗━━╯ ┛ ┛┗╯ ╰┛┗ ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┃ 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 [email protected] ┃
* ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
* ┃ file name: GLWidget.cpp ┃
* ┃ create date: 2013年7月16日星期二 19时45分31秒 ┃
* ┃ last modified date: 2013年7月12日星期五 17时46分9秒 ┃
* ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/
#include <cmath>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QKeySequence>
#include "GLWidget.h"
/*---------------------------------------------------------------------------*/
void MyPerspective( GLdouble fov, GLdouble aspectRatio, GLdouble zNear, GLdouble zFar )
{
using namespace std;
// 使用OpenGL函数,但是需要添加math.h头文件
GLdouble rFov = fov * 3.14159265 / 180.0;
GLdouble up = -zNear * tan( rFov / 2.0 );
GLdouble down = -up;
GLdouble left = up * aspectRatio;
GLdouble right = -left;
glFrustum( left, right, up, down, zNear, zFar );
}
/*---------------------------------------------------------------------------*/
GLWidget::GLWidget( QWidget* pParent ):
QGLWidget( pParent )
{
// 设置窗口
resize( 640, 360 ); // 初始大小为640×360(小宽屏)
m_AspectRatio = qreal( width( ) ) / qreal( height( ) ); // 初始的宽高比
}
void GLWidget::initializeGL( void )
{
initializeOpenGLFunctions( );
// 打开开关
qglClearColor( QColor( 0, 0, 0, 255 ) );
qglColor( QColor( 255, 0, 0 ) );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
// 创建顶点缓存并且设置顶点
glGenBuffers( 1, &m_VertexBuffer );
glBindBuffer( GL_ARRAY_BUFFER, m_VertexBuffer );
CubeVertices( 20.0f );
// 创建索引缓存以及设置索引
glGenBuffers( 1, &m_ColorBuffer );
glBindBuffer( GL_ARRAY_BUFFER, m_ColorBuffer );
CubeColors( );
glEnable( GL_DEPTH_TEST );
glFrontFace( GL_CW );
glEnable( GL_CULL_FACE );
glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );
glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
glShadeModel( GL_SMOOTH );
// 设置摄像机
m_Camera.SetPos( QVector3D( 0.0, 0.0, 50.0 ) );// 设置摄像机坐标
}
/*---------------------------------------------------------------------------*/
void GLWidget::resizeGL( int width, int height )
{
// 改变大小时程序如何应对?
GLdouble aspectRatio = GLdouble( width ) / GLdouble( height );
// 设置视口
if ( aspectRatio < m_AspectRatio )
{
GLint smallHeight = GLint( GLdouble( width ) / m_AspectRatio );
GLint heightBlank = ( GLint( height ) - smallHeight ) / 2;
glViewport( 0, heightBlank, GLint( width ), smallHeight );
}
else
{
GLint smallWidth = GLint( GLdouble( height ) * m_AspectRatio );
GLint widthBlank = ( GLint( width ) - smallWidth ) / 2;
glViewport( widthBlank, 0, smallWidth, GLint( height ) );
}
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
// 设置裁剪区域
MyPerspective( 45.0, m_AspectRatio, 0.5, 1000.0 );
// 为模型视图载入标准矩阵
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
}
/*---------------------------------------------------------------------------*/
void GLWidget::CubeVertices( GLfloat length )
{
// 设置立方体的顶点
GLfloat semiLength = length / 2.0f;
GLfloat cubeVertices[] =
{
-semiLength, -semiLength, semiLength, // 0
semiLength, -semiLength, semiLength, // 1
semiLength, -semiLength, -semiLength, // 2
-semiLength, -semiLength, -semiLength, // 3
-semiLength, semiLength, semiLength, // 4
semiLength, semiLength, semiLength, // 5
semiLength, semiLength, -semiLength, // 6
-semiLength, semiLength, -semiLength, // 7
};
glBufferData( GL_ARRAY_BUFFER, sizeof( cubeVertices ),
cubeVertices, GL_STATIC_DRAW );
glVertexPointer( 3, GL_FLOAT, 0, 0 );
}
/*---------------------------------------------------------------------------*/
void GLWidget::CubeColors( void )
{
GLubyte cubeColors[] =
{
255, 0, 0, 255,
255, 255, 0, 255,
0, 255, 0, 255,
0, 0, 0, 255,
255, 0, 255, 255,
255, 255, 255, 255,
0, 255, 255, 255,
0, 0, 255, 255,
};
glBufferData( GL_ARRAY_BUFFER, sizeof( cubeColors ),
cubeColors, GL_STATIC_DRAW );
glColorPointer( 4, GL_UNSIGNED_BYTE, 0, 0 );
}
/*---------------------------------------------------------------------------*/
void GLWidget::paintGL( void )
{
GLubyte cubeIndices[] =
{
0, 1, 2, 0, 2, 3,// 下面
7, 6, 4, 6, 5, 4,// 上面
7, 4, 3, 4, 0, 3,// 左面
5, 6, 1, 6, 2, 1,// 右面
4, 5, 0, 5, 1, 0,// 前面
3, 2, 6, 3, 6, 7,// 背面
};
glClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
glPushMatrix( );
m_Camera.Apply( );
glBindBuffer( GL_ARRAY_BUFFER, m_VertexBuffer );
glBindBuffer( GL_ARRAY_BUFFER, m_ColorBuffer );
glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, cubeIndices );
glPopMatrix( );
}
/*-------------------------------

彩阳
- 粉丝: 2173
最新资源
- 孤岛型微电网中改进下垂控制策略:'虚拟阻抗与无功均分的应用'
- 医药洁净室温湿度串级PID控制:基于200smart PLC的创新实现与挑战 专业版
- 基于Vuejs框架构建的现代化前端单页面应用项目-包含热重载开发服务器和Webpack生产环境构建配置-通过npm脚本命令实现依赖安装开发调试与生产打包-使用vue-loader.zip
- MATLAB中灰狼算法与改狼算法对23种测试函数的性能探究及应用前景 系统版
- 基于混合决策规则与Wasserstein度量的分布式鲁棒多阶段框架:适应风电渗透下的机组不确定性承诺与调度策略优化
- 电力电子领域Z源逆变器并网闭环仿真的L滤波器动态性能分析与应用
- 单相有源Boost PFC功率因数矫正电路设计原理与应用(220V交流转400V直流,功率200W)
- 基于Abaqus与Matlab蜂群算法耦合的结构优化程序研究及其工程应用 - Abaqus
- 基于配置化数据表格与动态图表展示的交互式数据可视化工具-支持拖拽排序-自定义样式-实时预览-多格式导出-响应式布局-数据绑定-配置驱动-JSON导入导出-Excel兼容-数据筛选-.zip
- 电机控制领域FOC电流环PI参数自整定Simulink仿真模型及其应用
- 一个目标检测图像增强的示例脚本
- 基于遗产算法的多目标分布式电源选址定容策略仿真研究:以投资成本等三目标实现方案验证 - 多目标优化
- BabeLua,一款vs的lua开发软件
- (雷同的那个是营销号)YOLOv8检测模块组合优化改进(成功涨点):添加GAM注意力机制;添加小目标检测头;替换为Wise-IoU损失函数+完整web端展示(实现简单目标跟踪功能)
- 基于MATLAB的LSTM与分位数回归多输入单输出时间序列预测模型
- MATLAB实现电-气-热综合能源系统耦合优化调度模型及其应用 综合能源系统 (2025-08-24)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



- 1
- 2
- 3
- 4
- 5
- 6
前往页