#include "oglfootball.h"
#include <QtCore>
#include <QMessageBox>
#include <qdebug.h>
OGLfootball::OGLfootball(QWidget *parent)
: QOpenGLWidget(parent)
{
//定时器信号与槽
connect(testTimer, SIGNAL(timeout()), this, SLOT(timerUpDate()));
}
OGLfootball::~OGLfootball()
{
}
void OGLfootball::initializeGL()
{
//初始化OpenGL函数
initializeOpenGLFunctions();
//设置全局变量
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
}
void OGLfootball::paintGL()
{
//初始化OpenGL函数
initializeOpenGLFunctions();
//清理屏幕
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-35.0, 35.0, -35.0, 35.0, -100, 100);
glPointSize(1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(23, 23, 26, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();//旋转除坐标轴之外的物体
glRotated(yRot, 0.0, 1.0, 0.0);
glRotated(xRot, 1.0, 0.0, 0.0);
//画球场
background();
//画坐标系
axis(44);
glPushMatrix();
glRotated(90.0, 0, 1.0, 0);//绕y轴正方向旋转90度
axis(35);
glPopMatrix();
glPushMatrix();
glRotated(-90.0, 1.0, 0.0, 0.0);//绕x轴负方向旋转
axis(35);
glPopMatrix();
glPopMatrix();
}
void OGLfootball::resizeGL(int width, int height)
{
//未使用
Q_UNUSED(width);
Q_UNUSED(height);
}
void OGLfootball::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
case Qt::Key_Right: //Right按下减少
yRot += 5.0f;
update();
break;
case Qt::Key_Left: //Left按下增加
yRot -= 5.0f;
update();
break;
case Qt::Key_Up: //up按下减少
xRot -= 5.0f;
update();
break;
case Qt::Key_Down: //down按下增加
xRot += 5.0f;
update();
break;
}
}
void OGLfootball::axis(double length)//画坐标系
{
glLineWidth(1);//设置线宽
glColor3f(0.0f, 0.0f, 0.0f);
glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, length);
glEnd();
//将当前操作点移到指定位置
/* glTranslated(0.0, 0.0, length - 2);
glColor3f(1.0, 0.0, 0.0);
glutWireCone(0.4, 4, 5, 5);//第一个参数底面半径,第二个参数圆锥高度,第三个和第四个分别为经纬
*/
glPopMatrix();
}
void OGLfootball::background()//画足球场和足球飞行轨迹,在paintGL中被调用
{
//画球场
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(18, 0, 35);
glVertex3f(-16, 0, 35);
glVertex3f(-16, 0, -35);
glVertex3f(18, 0, -35);
glEnd();
glLineWidth(2);//设置线宽
//球场边界
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f(18, 0, 35);
glVertex3f(-16, 0, 35);
glVertex3f(-16, 0, -35);
glVertex3f(18, 0, -35);
glEnd();
//画罚球区
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f(-16, 0, 20);
glVertex3f(0, 0, 20);
glVertex3f(0, 0, -20);
glVertex3f(-16, 0, -20);
glEnd();
//画球门区
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
glVertex3f(-16, 0, 9);
glVertex3f(-10.5, 0, 9);
glVertex3f(-10.5, 0, -9);
glVertex3f(-16, 0, -9);
glEnd();
//画球门
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex3f(-16, 0, 3.5);
glVertex3f(-16, 2.5, 3.5);
glVertex3f(-16, 2.5, -3.5);
glVertex3f(-16, 0, -3.5);
glEnd();
GLGridSide(-18, 0, 3.5, -16, 2.5, 3.5, 5, 5);//画左侧球门网
GLGridSide(-18, 0, -3.5, -16, 2.5, -3.5, 5, 5);//画右侧球门网
GLGridBack(-18, 0, -3.5, -18, 2.5, 3.5, 5, 5);//画后球门网
GLGridTop(-16, 2.5, 3.5, -18, 2.5, -3.5, 5, 5);//画顶球门网
//画轨迹 flag=1表示点击绘图按钮开始绘图
if (flag == 1)
{
ballTrack(l);
}
if (l < num)
{
testTimer->start(1);
}
else if (win){
win = false;
double rx = x[num],rz = z[num];
QMessageBox::information(this, QString::fromLocal8Bit("消息"),
QStringLiteral("进球得分")/* + QString::number(rx) + QString::number(rx)*/);
}
}
void OGLfootball::readData()//读data坐标数据
{
QFile f("F:/MATLAB/work/data.txt");
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(NULL, "title1", "fail");
exit(1);
}
QTextStream txtInput(&f);
QString lineStr;
lineStr = txtInput.readLine();
int i = 1;
num = lineStr.toInt();
x = new double[num]; Vx = new double[num];
y = new double[num]; Vy = new double[num];
z = new double[num]; Vz = new double[num];
// QMessageBox::information(NULL, "title2", lineStr + QString::number(num));//对数组不起作用
while (!txtInput.atEnd())
{
lineStr = txtInput.readLine();
if (lineStr.isEmpty())continue;
QStringList strlist = lineStr.split(QRegExp("[\t]"));//正则表达式分割
x[i] = strlist.at(0).toDouble(); Vx[i] = strlist.at(3).toDouble();
y[i] = strlist.at(1).toDouble(); Vy[i] = strlist.at(4).toDouble();
z[i] = strlist.at(2).toDouble(); Vz[i] = strlist.at(5).toDouble();
if (y[i] > 16)
{
if (x[i]<3.5&&x[i]>-3.5&&z[i] < 2.5)win = true;
break;//判断球是否到球门
}
i++;
}
}
void OGLfootball::setballTrack()
{
readData();
flag = 1; l = 1;
this->setFocus();//使opengl控件获得焦点从而可以响应键盘按键
testTimer->start(0);
// QMessageBox::information(NULL, "title2", QString::number(l));
}
void OGLfootball::ballTrack(int length)
{
//画足球轨迹
glLineWidth(3);//设置线宽
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 1; i <= length; i++ )
{
glVertex3f(-y[i], z[i], -x[i]);
}
glEnd();
}
void OGLfootball::timerUpDate()
{
if (l + 5 > num)l++;//如果有等号的话,l就会比实际多1
else l = l + 5;
testTimer->stop();
update();
}
void OGLfootball::GLGridSide(double x1, double y1, double z1, double x2, double y2, double z2, int n, int m)
{
double h, w;
h = abs(y2 - y1) / n;
w = abs(x2 - x1) / m;
glLineWidth(1);//设置线宽
for (int i = 0; i <= n; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1 + i*h, z1);
glVertex3f(x2, y1 + i*h, z2);
glEnd();
}
for (int i = 0; i <= m; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1 + i*w, y1, z1);
glVertex3f(x1 + i*w, y2, z2);
glEnd();
}
}
void OGLfootball::GLGridBack(double x1, double y1, double z1, double x2, double y2, double z2, int n, int m)
{
double h, w;
h = abs(y2 - y1) / n;
w = abs(z2 - z1) / m;
glLineWidth(1);//设置线宽
for (int i = 0; i <= n; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1 + i*h, z1);
glVertex3f(x2, y1 + i*h, z2);
glEnd();
}
for (int i = 0; i <= m; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, z1 + i*w);
glVertex3f(x2, y2, z1 + i*w);
glEnd();
}
}
void OGLfootball::GLGridTop(double x1, double y1, double z1, double x2, double y2, double z2, int n, int m)
{
double h, w;
h = abs(x2 - x1) / n;
w = abs(z2 - z1) / m;
glLineWidth(1);//设置线宽
for (int i = 0; i <= n; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x2 + i*h, y1, z1);
glVertex3f(x2 + i*h, y2, z2);
glEnd();
}
for (int i = 0; i <= m; i++)
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x1, y1, z2 + i*w);
glVertex3f(x2, y2, z2 + i*w);
glEnd();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于Qt的OpenGL编程并能实现相应按钮点击事件
共61个文件
tlog:18个
obj:12个
cpp:8个
1星 需积分: 50 54 下载量 70 浏览量
2017-09-13
21:00:26
上传
评论 1
收藏 1.75MB RAR 举报
温馨提示
这是我的毕设题目中写的软件,基本功能是输入数据,就会在opengl框中绘制图像,用到了matlab,opengl,是基于qt开发的(vs2013中的qt插件),里面都是调用到的操作opengl的方法,也是比较新的,网上有很多老的方法但是高版本的其他已经被弃用了,就这些。。。本来是不想要资源分的,但是好像并没有免费的选项。。。
资源推荐
资源详情
资源评论
收起资源包目录
test5_21_5.rar (61个子文件)
test5_21_5
test5_21_5.opensdf 0B
test5_21_5.sln 2KB
test5_21_5
test5_21_5.vcxproj.user 2KB
oglfootball.cpp 7KB
Resources
test5_21_5.ui 6KB
GeneratedFiles
Release
moc_oglfootball.cpp 4KB
moc_test5_21_5.cpp 4KB
ui_test5_21_5.h 7KB
qrc_test5_21_5.cpp 1KB
Debug
moc_oglfootball.cpp 4KB
moc_test5_21_5.cpp 4KB
Debug
test5_21_5.log 3B
test5_21_5.h 533B
test5_21_5.qrc 70B
test5_21_5.cpp 2KB
main.cpp 179B
test5_21_5.vcxproj.filters 3KB
test5_21_5.vcxproj 24KB
oglfootball.h 1KB
x64
Release
test5_21_5.Build.CppClean.log 2KB
qrc_test5_21_5.obj 2KB
moc_test5_21_5.obj 197KB
test5_21_5.obj 225KB
test5_21_5.tlog
CL.write.1.tlog 1KB
custombuild.write.1.tlog 918B
custombuild.command.1.tlog 3KB
link.command.1.tlog 2KB
CL.read.1.tlog 224KB
test5_21_5.lastbuildstate 156B
link.write.1.tlog 800B
cl.command.1.tlog 8KB
custombuild.read.1.tlog 652B
link.read.1.tlog 5KB
main.obj 196KB
moc_oglfootball.obj 69KB
test5_21_5.log 4KB
oglfootball.obj 118KB
Debug
test5_21_5.Build.CppClean.log 1KB
vc120.pdb 2.18MB
qrc_test5_21_5.obj 6KB
moc_test5_21_5.obj 453KB
test5_21_5.obj 618KB
test5_21_5.tlog
CL.write.1.tlog 3KB
custombuild.write.1.tlog 910B
custombuild.command.1.tlog 3KB
link.command.1.tlog 2KB
CL.read.1.tlog 224KB
test5_21_5.lastbuildstate 154B
link.write.1.tlog 952B
cl.command.1.tlog 8KB
custombuild.read.1.tlog 652B
link.read.1.tlog 5KB
main.obj 447KB
moc_oglfootball.obj 229KB
test5_21_5.log 3KB
oglfootball.obj 339KB
x64
Release
test5_21_5.exe 60KB
Debug
test5_21_5.ilk 1.51MB
test5_21_5.exe 236KB
test5_21_5.pdb 2.99MB
test5_21_5.v12.suo 31KB
共 61 条
- 1
资源评论
- ansly200602018-06-25没派上用场,先留着
lucky-
- 粉丝: 14
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功