/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "mainwidget.h"
#include <QMouseEvent>
#include <math.h>
MainWidget::MainWidget(QWidget *parent)
#ifdef GEELY_CMA_PROJECT_PC
:QGLWidget(parent),
#else
:QOpenGLWidget(parent),
#endif
line1(0),
line2(0),
line3(0),
line4(0),
texture(0),
angularSpeed(0)
{
}
MainWidget::~MainWidget()
{
// Make sure the context is current when deleting the texture
// and the buffers.
makeCurrent();
delete texture;
delete line1;
delete line2;
delete line3;
delete line4;
doneCurrent();
}
//! [0]
void MainWidget::mousePressEvent(QMouseEvent *e)
{
// Save mouse press position
mousePressPosition = QVector2D(e->localPos());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *e)
{
// Mouse release position - mouse press position
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition;
// Rotation axis is perpendicular to the mouse position difference
// vector
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
// Accelerate angular speed relative to the length of the mouse sweep
qreal acc = diff.length() / 100.0;
// Calculate new rotation axis as weighted sum
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
// Increase angular speed
angularSpeed += acc;
}
//! [0]
//! [1]
void MainWidget::timerEvent(QTimerEvent *)
{
// Decrease angular speed (friction)
// angularSpeed *= 0.99;
angularSpeed *= 0.95;
// Stop rotation when speed goes below threshold
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
// Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
// Request an update
update();
}
}
//! [1]
void MainWidget::initializeGL()
{
initializeOpenGLFunctions();
// setGeometry(0,0,1092,656);
// resize(1092,656);
glClearColor(214.0f/255, 217.0f/255, 224.0f/255, 1);
initShaders();
initTextures();
//! [2]
// Enable depth buffer
//glEnable(GL_DEPTH_TEST);
// Enable back face culling
glEnable(GL_CULL_FACE);
//! [2]
line1 = new GeometryLine;
line1->initLine(QVector2D(100,700),QVector2D(1092,900),6.0f,QVector4D(1.0f,0,0,1.0));
line2 = new GeometryLine;
line2->initLine(QVector2D(100,700),QVector2D(1092,900),4.0f,QVector4D(1.0,1.0f,1.0,1.0));
line3 = new GeometryLine;
line3->initLine(QVector2D(100,500),QVector2D(1092,700),6.0f,QVector4D(1.0f,0,0,1.0));
line4 = new GeometryLine;
line4->initLine(QVector2D(100,500),QVector2D(1092,700),4.0f,QVector4D(1.0,1.0f,1.0,1.0));
line5 = new GeometryLine;
line5->initLine(QVector2D(100,300),QVector2D(1092,500),6.0f,QVector4D(1.0f,0,0,1.0));
line6 = new GeometryLine;
line6->initLine(QVector2D(100,300),QVector2D(1092,500),4.0f,QVector4D(1.0,1.0,1.0,1.0));
// Use QBasicTimer because its faster than QTimer
timer.start(12, this);
}
//! [3]
void MainWidget::initShaders()
{
// Compile vertex shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl"))
close();
// Compile fragment shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl"))
close();
// Link shader pipeline
if (!program.link())
close();
// Bind shader pipeline for use
if (!program.bind())
close();
}
//! [3]
//! [4]
void MainWidget::initTextures()
{
// Load cube.png image
texture = new QOpenGLTexture(QImage(":/cube.png").mirrored());
// Set nearest filtering mode for texture minification
texture->setMinificationFilter(QOpenGLTexture::Nearest);
// Set bilinear filtering mode for texture magnification
texture->setMagnificationFilter(QOpenGLTexture::Linear);
// Wrap texture coordinates by repeating
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
texture->setWrapMode(QOpenGLTexture::Repeat);
}
//! [4]
//! [5]
void MainWidget::resizeGL(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
#ifdef GEELY_CMA_PROJECT_PC
//投影变换
glMatrixMode(GL_PROJECTION);
#endif
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);
// Reset projection
projection.setToIdentity();
// Set perspective projection
projection.perspective(50, aspect, 0.1, 10000);
view.setToIdentity();
float midx = w/2;
float midy = h/2;
QVector3D eye = QVector3D(midx,midy - 300, 100);
QVector3D center = QVector3D(midx,midy,0);
QVector3D up = QVector3D(0,0,1);
view.lookAt(eye,center,up);
}
//! [5]
void MainWidget::paintGL()
{
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glClearColor(1.0, 1.0, 1.0, 1.0);
glDisable(GL_DEPTH);
texture->bind();
//! [6]
// Calculate model view transformation
QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation);
// Set modelview-projection matrix
program.setUniformValue("mvp_matrix", projection * view);
//! [6]
// Use texture unit 0 which contains cube.png
program.setUniformValue("texture", 0);
// Draw line geometry
line1->drawCubeGeometry(&program);
line2->drawCubeGeometry(&program);
line3->drawCubeGeometry(&program);
line4->drawCubeGeometry(&program);
line5->drawCubeGeometry(&program);
line6->drawCubeGeometry(&program);
}
没有合适的资源?快使用搜索试试~ 我知道了~
qt QOpenGLWidget QGLWidget 描画线段
共13个文件
cpp:3个
h:2个
qrc:2个
4星 · 超过85%的资源 需积分: 50 98 下载量 23 浏览量
2017-11-06
08:57:11
上传
评论 2
收藏 43KB ZIP 举报
温馨提示
在QT 框架下, 描画线段。 工作原理: 根据两点扩展成面,进行描画。
资源推荐
资源详情
资源评论
收起资源包目录
cube.zip (13个子文件)
cube
fshader.glsl 333B
cube.pro.user.18 43KB
cube.pro.user 35KB
vshader.glsl 567B
mainwidget.cpp 7KB
cube.pro 414B
mainwidget.h 3KB
main.cpp 3KB
textures.qrc 87B
geometryengine.cpp 5KB
shaders.qrc 125B
cube.png 30KB
geometryengine.h 2KB
共 13 条
- 1
资源评论
- q2nAmor2018-10-31很不错。资料真实有效
- CoreUML2018-10-22两个版本吗?
xdchenmozhe
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功