#include "glwidget.h"
const GLfloat PI = 3.1415926536f;
GLWidget::GLWidget(QGLWidget *parent) : QGLWidget (parent)
{
setMinimumSize(320,240);
resize(640,480);
setWindowTitle(tr("Hello OpenGL"));
short angle = 18;
for(short i=0; i<5; i++) {
Point[i][0] = cos(angle * PI/180);
Point[i][1] = sin(angle * PI/180);
Point[i][2] = 0.0;
angle += 72;
}
}
GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
// 初始化函数
initializeOpenGLFunctions();
glClearColor(0.0,0.0,0.0,1.0);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
glEnable(GL_DEPTH);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void GLWidget::paintGL()
{
//清除之前图形并将背景设置为 blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef( -1.5, 0.0, -6.0 );
glBegin( GL_QUADS );
glVertex3f( -1.0, 1.0, 0.0 );
glVertex3f( 1.0, 1.0, 0.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glEnd();
glTranslatef( 3.0, 0.0, 0.0 );
glBegin( GL_TRIANGLES );
glVertex3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glEnd();
}
void GLWidget::resizeGL(int width, int height)
{
// 窗口大小变化时 调整大小
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble fovy=45.0;
GLdouble glWidth=static_cast<GLdouble>(width);
GLdouble glHeight=static_cast<GLdouble>(height);
GLdouble aspect=glWidth/glHeight;
GLdouble zNear=0.1;
GLdouble zFar=100.0;
gluPerspective(fovy,aspect,zNear,zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::mouseDoubleClickEvent( QMouseEvent *event )
{
Q_UNUSED(event);
if(windowState() & Qt::WindowFullScreen)
showNormal();
else
showFullScreen();
}
void GLWidget::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Escape)
close();
}
没有合适的资源?快使用搜索试试~ 我知道了~
helloopengl.zip
共8个文件
cpp:3个
h:2个
ui:1个
需积分: 18 13 下载量 121 浏览量
2019-09-03
15:34:45
上传
评论
收藏 6KB ZIP 举报
温馨提示
windows 10平台,Qt5+Qt Creator 4.9.1+ OpenGL(QT 自带的OpenGL库) 简单实例,实例测试通过,希望更多需要的人下载参考,少走弯路~~~~~
资源推荐
资源详情
资源评论
收起资源包目录
helloopengl.zip (8个子文件)
helloopengl
glwidget.h 878B
glwidget.cpp 2KB
widget.h 279B
helloopengl.pro 1KB
widget.ui 441B
helloopengl.pro.user 24KB
main.cpp 305B
widget.cpp 201B
共 8 条
- 1
资源评论
happyrabbit456
- 粉丝: 110
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功