• 大小: 8KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: Qt  OpenGL  

资源简介

里面是一个简单的demo,实现用鼠标控制视角的变换。

资源截图

代码片段和文件信息

#include “glwidget.h“
#include
#include
#include
#include
#include
GLWidget::GLWidget(QWidget *parent):QGLWidget(parent)
{

    /*初始化成员*/
    eyex=0.1;
    eyey=0.1;
    eyez=0.1;

    upx=0;
    upy=0.1;
    upz=0;

    CurrentAngleZ=0;
    CurrentAngleY=0;
    LastAngleZ=M_PI/4;
    LastAngleY=M_PI/4;

}


void GLWidget::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.00.00.00.0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST);
}

void GLWidget::resizeGL(int w int h)
{
    glViewport(00(GLint)w(GLint)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();


}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(eyex*0.1eyey*0.1eyez*0.1000upx*0.1upy*0.1upz*0.1);
    drawCoordinate();
    glColor3f(101);

    DrawShape(Id);
}

void GLWidget::drawCoordinate()
{
    /*红色轴是X轴,绿色是Y轴,蓝色是Z轴*/
    glBegin(GL_LINES);
    glColor3f(1.0f0.00.0);
    glVertex3f(0.00.00.0);
    glVertex3f(0.50.00.0);
    glEnd();
    glPushMatrix();
    glTranslatef(0.5 0.0f 0.0f);
    glRotatef(90.0f0.0f1.0f0.0f);
    glutWireCone(0.0270.091010);
    glPopMatrix();


    glBegin(GL_LINES);
    glColor3f(0.01.00.0);
    glVertex3f(0.00.00.0);
    glVertex3f(0.00.50.0);
    glEnd();
    glPushMatrix();
    glTranslatef(0.0 0.5f 0.0f);
    glRotatef(-90.0f1.0f0.0f0.0f);
    glutWireCone(0.0270.091010);
    glPopMatrix();


    glBegin(GL_LINES);
    glColor3f(0.00.01.0);
    glVertex3f(0.00.00.0);
    glVertex3f(0.00.00.5);
    glEnd();
    glPushMatrix();
    glTranslatef(0.0 0.0f 0.5f);
    glutWireCone(0.0270.091010);
    glPopMatrix();

}

void GLWidget::RotateViewPoint()
{
    float avAnale=M_PI/180*0.6; //把每次移动的角度单位化

    /*把每次移动点跟开始按下鼠标记录的点作差,然后乘以avAngle最后把上一次释放鼠标后时记录的
      角度相加起来*/
    CurrentAngleZ=(EndPoint.x()-StartPoint.x())*avAnale;
    CurrentAngleZ+=LastAngleZ;
    CurrentAngleY=(EndPoint.y()-StartPoint.y())*avAnale;
    CurrentAngleY+=LastAngleY;


    QVector3D vector1(sin(CurrentAngleY)*sin(CurrentAngleZ)cos(CurrentAngleY)sin(CurrentAngleY)*cos(CurrentAngleZ));
    vector1=vector1.normalized();  //将坐标单位化
    eyex=vector1.x();
    eyey=vector1.y();
    eyez=vector1.z();

    /*主要计算第三组坐标*/
    QVector3D vectorA(0sin(CurrentAngleY)0);
    QVector3D vectorB=QVector3D(000)-QVector3D(sin(CurrentAngleY)*sin(CurrentAngleZ)0sin(CurrentAngleY)*cos(CurrentAngleZ));
    QVector3D vectorAB=QVector3D::crossProduct(vectorAvectorB);


    QVector3D vectorC=QVector3D(000)-vector1;
    QVector3D vector2=QVector3D::crossProduct(vectorCvectorAB);
    vector2=vector2.normalized();
    upx=vector2.x();
    upy=vector2.y();
    upz=vector2.z();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-09-27 19:56  GLDrawingProject\
     文件         488  2013-09-27 19:53  GLDrawingProject\GLDrawingProject.pro
     文件       31422  2013-09-27 19:56  GLDrawingProject\GLDrawingProject.pro.user
     文件        4936  2013-09-27 19:55  GLDrawingProject\glwidget.cpp
     文件        1234  2013-09-27 19:43  GLDrawingProject\glwidget.h
     文件         273  2013-09-17 16:17  GLDrawingProject\main.cpp
     文件        1532  2013-09-20 16:21  GLDrawingProject\mainwindow.cpp
     文件         789  2013-09-20 16:19  GLDrawingProject\mainwindow.h
     文件         654  2013-09-17 16:11  GLDrawingProject\mainwindow.ui

评论

共有 条评论