#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QFile>#include <QFileDialog>#include "mypoint.h"#include "opengl.h"#include "mypoligon.h"namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); OpenGL GL; QString pointpath; //путь к файлу с точкам//QList<myPoint*> MyPointList; //список точекmyPoint getpoint(QString str); //функция преобразования строки в точку.myPoint *pt; //глобально объявленная ссылка на объект точки.myPoligon *poligon;//глобально объявленная ссылка на объект полигона.int NN = -1;private slots: void on_pathButton_clicked(); void on_readButton_clicked(); void on_getPoints_clicked(); void on_MyPointsOutput_clicked(); void on_OpenGL_clicked(); void on_GetTriIndexes_clicked(); void on_newPoligon_clicked();private: Ui::MainWindow *ui;};#endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"#include "mypoint.h"#include "opengl.h"#include <opengl.h>#include "mypoligon.h"#include <QDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); pointpath = ui->lineEdit->text(); GL.vertexRedy = false; GL.poligonRedy = false;}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pathButton_clicked(){ //получение пути к файлу из QFileDialog pointpath = QFileDialog::getOpenFileName(this); ui->lineEdit->setText(pointpath); ui->textEdit->append("file path get complite");}void MainWindow::on_readButton_clicked(){ //открытие файла и чтение (работает) QFile file(pointpath); if(file.open(QIODevice::ReadOnly)){ } ui->textEdit->append("file open complite");}void MainWindow::on_getPoints_clicked(){int i; QFile file(pointpath); if(file.open(QIODevice::ReadOnly)){while(!file.atEnd()){pt = new myPoint;QString str = file.readLine();if(i > NN){pt->getPoint(str);GL.MyPointList.push_back(pt);if(pt->color>GL.colorMax) GL.colorMax = pt->color;i=0;}i++;}ui->textEdit->append("pointList: "+QString::number(GL.MyPointList.count()));}}void MainWindow::on_MyPointsOutput_clicked(){for (int i = 0; i< GL.MyPointList.count();++i){ ui->textEdit->append(QString::number(GL.MyPointList[i]->returnVector3D().x()) + " " +QString::number(GL.MyPointList[i]->returnVector3D().y()) + " " +QString::number(GL.MyPointList[i]->returnVector3D().z()) + " " +QString::number((float)(GL.MyPointList[i]->returnColor()))); }ui->textEdit->append(QString::number(GL.colorMax));}void MainWindow::on_OpenGL_clicked(){ GL.resize(600,600); GL.show(); GL.updateGL();}void MainWindow::on_GetTriIndexes_clicked(){ QString str = "C:/Users/Геннадий/Desktop/Аспирантура/Программа расчёта ИК-заметности/с полигонами (триугольники) ++/"; QString str2 = "треугольники"; QFileDialog::Options opt; QFile trifile(QFileDialog::getOpenFileName(this,str2,str)); if(trifile.open(QIODevice::ReadOnly| QIODevice::Text)){ ui->textEdit->append("trifile open"); } QList<QString> sList; while(!trifile.atEnd()){ sList.push_back((trifile.readLine())); } ui->textEdit->append("sList created"+QString::number(sList.count())); QList<QString>::iterator ii; for(ii=sList.begin(); ii != sList.end(); ++ii){ GL.vertexIndexes.push_back((*ii).section(',',0,0).toFloat()); GL.vertexIndexes.push_back((*ii).section(',',1,1).toFloat()); GL.vertexIndexes.push_back((*ii).section(',',2,2).toFloat()); }ui->textEdit->append("vertexIndexes: "+QString::number(GL.vertexIndexes.count())); QList<float>::iterator ff; int i; i=1; ui->textEdit->append("print vertexIndexes"); for(ff=GL.vertexIndexes.begin(); ff != GL.vertexIndexes.end(); ff++){ ui->textEdit->append(QString::number((*ff))+" "+QString::number(GL.MyPointList[(*ff)]->color)); i++; } ui->textEdit->append("print vertexIndexes complite"+QString::number(GL.vertexIndexes.count())); GL.vertexRedy = true;}void MainWindow::on_newPoligon_clicked(){ for(int i=0;i<=GL.vertexIndexes.count()-3;i = i+3){ poligon = new myPoligon((*(GL.MyPointList[GL.vertexIndexes[i]])),(*(GL.MyPointList[GL.vertexIndexes[i+1]])),(*(GL.MyPointList[GL.vertexIndexes[i+2]]))); GL.MyPoligonList.push_back(poligon); qDebug(QString::number(i).toUtf8()); } ui->textEdit->append("создано "+QString::number(GL.MyPoligonList.count())+" полигонов"); GL.poligonRedy = true;}
#ifndef MYPOINT_H#define MYPOINT_H#include <QWidget>#include <QGLWidget>#include <QVector3D>class myPoint : public QWidget{Q_OBJECTprivate:public: QVector3D point; myPoint(QWidget *parent = 0); myPoint(const myPoint &other); ~myPoint(); GLfloat color; void getPoint(QString str ); GLfloat returnColor(); QVector3D returnVector3D();signals:public slots:};#endif // MYPOINT_Hmypoint.cpp:#include "mypoint.h"#include <QWidget>myPoint::myPoint ( QWidget* parent) : QWidget(parent){}myPoint::myPoint (const myPoint &other ) : QWidget( ){point = other.point;color = other.color;}myPoint::~myPoint(){}void myPoint::getPoint(QString str){ point = QVector3D(str.section(";",0,0).toFloat(),str.section(";",1,1).toFloat(),str.section(";",2,2).toFloat()); color = (GLfloat)str.section(";",3,3).toFloat();}GLfloat myPoint::returnColor(){ return color;}QVector3D myPoint::returnVector3D(){ return point;}
#ifndef MYPOLIGON_H#define MYPOLIGON_H#include <QWidget>#include <QGLWidget>#include <QVector3D>#include "mypoint.h"class myPoligon : public QWidget{Q_OBJECTprivate:public: myPoint point1, point2, point3; myPoligon(QWidget *parent = 0); myPoligon(const myPoligon &other); myPoligon(myPoint pt1, myPoint pt2, myPoint pt3); ~myPoligon(); float S(); float SS; //площадь полигона GLfloat color; GLfloat getColor(); QVector3D center; QVector3D normal;signals:public slots:};#endif //MYPOLIGON_Hmypoligon.cpp:#include "mypoligon.h"#include <QWidget>#include "mypoint.h"myPoligon::myPoligon( QWidget* parent) : QWidget(parent){}myPoligon::myPoligon (const myPoligon &other ) : QWidget( ){}myPoligon::myPoligon(myPoint pt1, myPoint pt2 , myPoint pt3){point1.point = pt1.point;point2.point = pt2.point;point3.point = pt3.point;color = (pt1.color + pt2.color + pt3.color)/3.0;center = QVector3D((pt1.point.x()+pt2.point.x()+pt3.point.x())/3.0,(pt1.point.y()+pt2.point.y()+pt3.point.y())/3.0,(pt1.point.z()+pt2.point.z()+pt3.point.z())/3.0 );normal = QVector3D::crossProduct(pt1.point-pt3.point,pt3.point-pt2.point);}myPoligon::~myPoligon(){}float myPoligon::S(){SS=QVector3D().dotProduct(point1.point-point2.point,point1.point-point3.point);return SS;}GLfloat myPoligon::getColor(){ return color;}
#ifndef OPENGL_H#define OPENGL_H#include <QWidget>#include <QGLWidget>#include <QKeyEvent>#include "mypoint.h"#include "mypoligon.h"class OpenGL : public QGLWidget{ Q_OBJECTprotected: void initializeGL(); void resizeGL(int nWidth, int nHeight); void paintGL();public: OpenGL(QWidget *parent = 0);void keyPressEvent(QKeyEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);QPoint pressPosition;GLfloat xAxisRotation;GLfloat yAxisRotation;GLfloat zAxisRotation;GLfloat currentHeight;GLfloat currentWidth;GLfloat zoom;GLfloat translateX;GLfloat translateY;GLfloat colorMax;QList<float> vertexIndexes; //номера вершин в каждом треугольнике.bool vertexRedy;bool poligonRedy;QList<myPoint*> MyPointList; //список точекQList<myPoligon*> MyPoligonList;//список полигонов};#endif // OPENGL_H
#include "opengl.h"#include <QDebug>#include "mainwindow.h"OpenGL::OpenGL(QWidget* parent) : QGLWidget(parent){}void OpenGL::initializeGL(){ glClearColor(0.5, 0.5, 0.5, 0.5); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); //glShadeModel(GL_FLAT); //отключает плавные переходы цветов glEnable(GL_CULL_FACE); //если включено - задние грани не видны zoom = 1.0;}void OpenGL::resizeGL(int nWidth, int nHeight){ glMatrixMode(GL_PROJECTION); if(nWidth < nHeight){ nWidth = nHeight; } else { nHeight = nWidth; } glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glViewport(0, 0, (GLint)nWidth, (GLint)nHeight); currentHeight =(GLint)nWidth; currentWidth =(GLint)nHeight;}void OpenGL::paintGL(){ glMatrixMode(GL_MODELVIEW); // // глобальная система координат glLoadIdentity(); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPointSize(15); glRotatef(yAxisRotation, 0.0, 1.0, 0.0); glRotatef(xAxisRotation, 1.0, 0.0, 0.0); glRotatef(zAxisRotation, 0.0, 0.0, 1.0); glScalef(zoom, zoom, zoom); glTranslatef(translateX,translateY,0);if(!MyPointList.empty()){ glBegin(GL_POINTS); QList<myPoint*>::iterator pt; for(pt = MyPointList.begin(); pt!=MyPointList.end();++pt){ glColor3f((*pt)->color/colorMax,0,0);glVertex3f((*pt)->returnVector3D().x(),(*pt)->returnVector3D().y(),(*pt)->returnVector3D().z()); } glEnd();}; if(poligonRedy){ glBegin(GL_TRIANGLES); QList<myPoligon * >::iterator pol; for(pol = MyPoligonList.begin(); pol != MyPoligonList.end(); ++pol){ glColor3f((*pol)->getColor()/colorMax,0,0); glVertex3f((*pol)->point1.point.x(),(*pol)->point1.point.y(),(*pol)->point1.point.z()); glVertex3f((*pol)->point2.point.x(),(*pol)->point2.point.y(),(*pol)->point2.point.z()); glVertex3f((*pol)->point3.point.x(),(*pol)->point3.point.y(),(*pol)->point3.point.z()); } glEnd(); }; if(poligonRedy){ QList<myPoligon * >::iterator pol; for(pol = MyPoligonList.begin(); pol != MyPoligonList.end(); ++pol){glBegin(GL_LINE_STRIP);glLineWidth(1); glColor3f(1,1,1); glVertex3f((*pol)->center.x(),(*pol)->center.y(),(*pol)->center.z()); glVertex3f((*pol)->normal.x()+(*pol)->center.x(),(*pol)->normal.y()+(*pol)->center.y(),(*pol)->normal.z()+(*pol)->center.z());glEnd(); } };}//Close PaintGLvoid OpenGL::mousePressEvent(QMouseEvent *event){ pressPosition = event->pos();}void OpenGL::mouseMoveEvent(QMouseEvent *event){ xAxisRotation += (180 * ((GLfloat)event->y() - (GLfloat)pressPosition.y())) / (currentHeight); yAxisRotation += (180 * ((GLfloat)event->x() - (GLfloat)pressPosition.x())) / (currentWidth); pressPosition = event->pos(); updateGL();}void OpenGL::keyPressEvent(QKeyEvent *event){ if(event->key()==Qt::Key_PageUp){ zoom += 0.01; updateGL(); } else if(event->key()==Qt::Key_PageDown){ zoom -= 0.01; updateGL(); } else if(event->key()==Qt::Key_Enter){ xAxisRotation = 0.0; yAxisRotation = 0.0; updateGL(); } else if(event->key()==Qt::Key_Right){ translateX += 0.1; updateGL(); } else if(event->key()==Qt::Key_Left) { translateX -= 0.1; updateGL(); } else if(event->key()==Qt::Key_Up) { translateY += 0.1; updateGL(); } else if(event->key()==Qt::Key_Down) { translateY -= 0.1; updateGL(); } else if(event->key()==Qt::Key_Q){zAxisRotation += 0.1;updateGL(); }}