• 9028阅读
  • 10回复

Qt 通过鼠标拖动实现图形旋转 [复制链接]

上一主题 下一主题
离线无敌
 
只看楼主 倒序阅读 楼主  发表于: 2012-10-17

如何实现通过鼠标拖动随使用者的意愿随意旋转图形,来满足使用者的需求!要求达到随鼠标拖动实现任意旋转!
怎么判断是顺时针旋转还是逆时针旋转?
离线cxfczw
只看该作者 1楼 发表于: 2012-10-17
《C++ GUI Qt4编程》第二版中不是有个例子吗?画金字塔的那个例子。
离线无敌
只看该作者 2楼 发表于: 2012-10-19
有画金字塔的例子吗?你能不能给出详细的代码?谢谢
离线XChinux

只看该作者 3楼 发表于: 2012-10-19
考验你的数学知识的时候到了。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线cxfczw
只看该作者 4楼 发表于: 2012-10-20
回 2楼(无敌) 的帖子
#include <QtGui>
#include <QtOpenGL>
#include <gl/glu.h>
#include "tetrahedron.h"

Tetrahedron::Tetrahedron(QWidget *parent) :
    QGLWidget(parent)
{
    setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));

    rotationX=-21.0;
    rotationY=-57.0;
    rotationZ=0.0;
    faceColors[0]=Qt::red;
    faceColors[1]=Qt::green;
    faceColors[2]=Qt::blue;
    faceColors[3]=Qt::yellow;
}

void Tetrahedron::initializeGL()
{
    qglClearColor(Qt::black);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

}

void Tetrahedron::resizeGL(int width, int height)
{
    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat x=GLfloat(width)/height;
    glFrustum(-x,+x,-1.0,+1.0,4.0,15.0);

    glMatrixMode(GL_MODELVIEW);
}

void Tetrahedron::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    RenderScene();
}

void Tetrahedron::RenderScene()
{
    static const GLfloat P1[3]={0.0f,-1.0f,2.0f};
    static const GLfloat P2[3]={1.73205081f,-1.0f,-1.0f};
    static const GLfloat P3[3]={-1.73205081f,-1.0f,-1.0f};
    static const GLfloat P4[3]={0.0f,2.0f,0.0f};

    static const GLfloat * const coords[4][3]={
        {P1,P2,P3},{P1,P3,P4},{P1,P4,P2},{P2,P4,P3}
    };

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0,0.0,-10.0);
    glRotatef(rotationX,1.0,0.0,0.0);
    glRotatef(rotationY,0.0,1.0,0.0);
    glRotatef(rotationZ,0.0,0.0,1.0);

    for(int i=0;i<4;++i){
        glLoadName(i);;
        glBegin(GL_TRIANGLES);
        qglColor(faceColors);
        for(int j=0;j<3;++j){
            glVertex3f(coords[j][0],coords[j][1],coords[j][2]);
        }
        glEnd();
    }
}

void Tetrahedron::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}

void Tetrahedron::mouseMoveEvent(QMouseEvent *event)
{
    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();

    if (event->buttons() & Qt::LeftButton) {
        rotationX += 180 * dy;
        rotationY += 180 * dx;
        updateGL();
    } else if (event->buttons() & Qt::RightButton) {
        rotationX += 180 * dy;
        rotationZ += 180 * dx;
        updateGL();
    }
    lastPos = event->pos();
}

void Tetrahedron::mouseDoubleClickEvent(QMouseEvent *event)
{
    int face = faceAtPosition(event->pos());
    if (face != -1) {
        QColor color = QColorDialog::getColor(faceColors[face], this);
        if (color.isValid()) {
            faceColors[face] = color;
            updateGL();
        }
    }
}

int Tetrahedron::faceAtPosition(const QPoint &pos)
{
    const int MaxSize = 512;
    GLuint buffer[MaxSize];
    GLint viewport[4];

    makeCurrent();

    glGetIntegerv(GL_VIEWPORT, viewport);
    glSelectBuffer(MaxSize, buffer);
    glRenderMode(GL_SELECT);

    glInitNames();
    glPushName(0);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPickMatrix(GLdouble(pos.x()), GLdouble(viewport[3] - pos.y()),
                  5.0, 5.0, viewport);
    GLfloat x = GLfloat(width()) / height();
    glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
    RenderScene();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    if (!glRenderMode(GL_RENDER))
        return -1;
    return buffer[3];
}
离线cxfczw
只看该作者 5楼 发表于: 2012-10-20
#ifndef TETRAHEDRON_H
#define TETRAHEDRON_H

#include <QGLWidget>

class Tetrahedron : public QGLWidget
{
    Q_OBJECT
public:
    explicit Tetrahedron(QWidget *parent = 0);
    
protected:
    void initializeGL();
    void resizeGL(int width,int height);
    void paintGL();

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);

private:
    void RenderScene();
    int faceAtPosition(const QPoint &pos);

    GLfloat rotationX;
    GLfloat rotationY;
    GLfloat rotationZ;
    QColor faceColors[4];
    QPoint lastPos;

signals:
    
public slots:
    
};

#endif // TETRAHEDRON_H
离线cxfczw
只看该作者 6楼 发表于: 2012-10-20
#include <QApplication>
#include <iostream>

#include "tetrahedron.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    if (!QGLFormat::hasOpenGL()) {
        std::cerr << "This system has no OpenGL support" << std::endl;
        return 1;
    }

    Tetrahedron tetrahedron;
    tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
    tetrahedron.resize(300, 300);
    tetrahedron.show();

    return app.exec();
}
离线cxfczw
只看该作者 7楼 发表于: 2012-10-20
共3个文件
离线ggkuroky
只看该作者 8楼 发表于: 2012-10-21
http://doc.qt.digia.com/stable/demos-affine.html
看这个吧,希望对你有帮助!!
相信自己,天道酬勤!
离线ggkuroky
只看该作者 9楼 发表于: 2012-10-21
相信自己,天道酬勤!
离线无敌
只看该作者 10楼 发表于: 2012-11-01
回 3楼(XChinux) 的帖子
总版主您好!请您老人家给出详细的代码行吗?我还是新手 不知道怎么做!请你多多帮助 多多指教 不胜感激
快速回复
限100 字节
 
上一个 下一个