Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qopenglwidget trying to output to 3d model in format .obj .

qopenglwidget trying to output to 3d model in format .obj .

Scheduled Pinned Locked Moved Unsolved General and Desktop
qopenglwidget3d model
2 Posts 2 Posters 435 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    timob256
    wrote on 1 Dec 2021, 09:39 last edited by
    #1

    trying to output to 3d model in format .obj .

    I want to output a 3d model to QOpenGLWidget in obj format.

    How to do it?? is there an example of "how to display a 3D model on the screen" ??

    dialogokno.h

    #ifndef DIALOGOKNO_H
    #define DIALOGOKNO_H
    
    #include <QMainWindow>
    #include <QFile>
    #include <QPushButton>
    #include <QLabel>
    #include <QSize>
    
    #include <QApplication>
    #include <QDesktopWidget>
    #include <QRect>
    #include <QDebug>
    #include <QLineEdit>
    #include <QGridLayout>
    #include <QSpacerItem>
    #include <QObject>
    #include <QOpenGLWidget>
    
    #include "myglwidget.h"
    
    class DialogOkno : public QWidget
    {
        Q_OBJECT
    
    public:
        DialogOkno(QWidget *parent = 0);
        ~DialogOkno();
    
    protected:
    //    QWidget* centralWidget;
    
        QFile* file;         // загрузить файл
        QGridLayout* gl_layaout[4];
        QPushButton* b_load; // кнопка для выбора и загрузки файла
        QLabel* l_label[3];
        QLineEdit* le_edit[7];
        QSpacerItem* si_spacer[4];
    };
    
    #endif // DIALOGOKNO_H
    

    glwidget.h

    #ifndef MYGLWIDGET_H
    #define MYGLWIDGET_H
    
    #include <QOpenGLWidget>
    #include <QDebug>
    
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <fstream>
    #include <vector>
    
    class MyGLWidget : public QOpenGLWidget
    {
        Q_OBJECT
    public:
        MyGLWidget(QWidget* parent = nullptr);
    
        // Globals.
         std::vector<float> verticesVector; // Vector to read in vertex x, y and z values fromt the OBJ file.
         std::vector<int> facesVector; // Vector to read in face vertex indices from the OBJ file.
         float *vertices = NULL;  // Vertex array of the object x, y, z values.
        int *faces = NULL; // Face (triangle) vertex indices.
         int numIndices; // Number of face vertex indices.
         float Xangle = 0.0, Yangle = 0.0, Zangle = 0.0; // Angles to rotate the object.
        void loadOBJ(std::string fileName);
    
    protected:
        void initializeGL();
        void resizeGL(int w, int h);
        void paintGL();
    };
    
    #endif // MYGLWIDGET_H
    

    dialogokno.cpp

    #include "dialogokno.h"
    
    DialogOkno::DialogOkno(QWidget *parent)
        : QWidget(parent)
    {
        MyGLWidget * openGL = new MyGLWidget(parent);
    
        // обявл элементы
        gl_layaout[0] = new QGridLayout(parent);
        gl_layaout[1] = new QGridLayout(parent);
        gl_layaout[2] = new QGridLayout(parent);
    
        gl_layaout[3] = new QGridLayout(parent);
    
        l_label[0] = new QLabel(parent);
        l_label[1] = new QLabel(parent);
        l_label[2] = new QLabel(parent);
    
        le_edit[0] = new QLineEdit(parent);
        le_edit[1] = new QLineEdit(parent);
        le_edit[2] = new QLineEdit(parent);
        le_edit[3] = new QLineEdit(parent);
        le_edit[4] = new QLineEdit(parent);
        le_edit[5] = new QLineEdit(parent);
        le_edit[6] = new QLineEdit(parent);
    
        // настройки элеметов (стилизация)
    
        // заполнение элементов
        l_label[0]->setText("hla_global");
        l_label[1]->setText("hla_local");
        l_label[2]->setText("logger");
    
        le_edit[0]->setText("server_ip_global");
        le_edit[1]->setText("server_port_global");
    
        le_edit[2]->setText("server_port_local");
        le_edit[3]->setText("server_port_local");
    
        le_edit[4]->setText("log");
        le_edit[5]->setText("log_period_ms");
        le_edit[6]->setText("log_size_in_mb");
    
        //разложение элементов по группам
    
        gl_layaout[0]->addWidget(l_label[0], 0, 0, 1, 1);
        gl_layaout[0]->addWidget(le_edit[0], 1, 0, 1, 1);
        gl_layaout[0]->addWidget(le_edit[1], 2, 0, 1, 1);
    
        gl_layaout[1]->addWidget(l_label[1], 0, 0, 1, 1);
        gl_layaout[1]->addWidget(le_edit[2], 1, 0, 1, 1);
        gl_layaout[1]->addWidget(le_edit[3], 2, 0, 1, 1);
    
        gl_layaout[2]->addWidget(l_label[2], 0, 0, 1, 1);
        gl_layaout[2]->addWidget(le_edit[4], 1, 0, 1, 1);
        gl_layaout[2]->addWidget(le_edit[5], 2, 0, 1, 1);
        gl_layaout[2]->addWidget(le_edit[6], 3, 0, 1, 1);
    
    
        gl_layaout[3]->addLayout(gl_layaout[0], 0, 0, 1, 1);
    
        si_spacer[0] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    
        gl_layaout[3]->addItem(si_spacer[0], 0, 1, 1, 1);
    
        gl_layaout[3]->addLayout(gl_layaout[1], 0, 2, 1, 1);
        si_spacer[1] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
        gl_layaout[3]->addItem(si_spacer[1], 0, 3, 1, 1);
        si_spacer[2] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
        gl_layaout[3]->addItem(si_spacer[2],1, 0, 1, 1);
        gl_layaout[3]->addLayout(gl_layaout[2], 2, 0, 1, 1);
        si_spacer[3] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
        gl_layaout[3]->addItem(si_spacer[3], 3, 0, 1, 1);
        gl_layaout[3]->addWidget(openGL , 1, 1, 3, 3);
    
        this->setLayout(gl_layaout[3]);
    }
    
    DialogOkno::~DialogOkno()
    {
    
    }
    

    main.cpp

    #include "dialogokno.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QApplication::setAttribute(Qt::AA_ForceRasterWidgets, false);
        DialogOkno w;
        QDesktopWidget desktop;
        QRect rect = desktop.availableGeometry(desktop.primaryScreen()); // прямоугольник с размерами экрана
        QPoint center = rect.center(); //координаты центра экрана
        w.resize(center.x(), center.y());
    
        w.setWindowTitle("программа");
        w.show();
    
        return a.exec();
    }
    

    glwidget.cpp

    #include "myglwidget.h"
    
    MyGLWidget::MyGLWidget(QWidget * parent): QOpenGLWidget(parent)
    {
    
    }
    
    void MyGLWidget::initializeGL()
    {
        glEnableClientState(GL_VERTEX_ARRAY);
    
        glClearColor(1.0, 1.0, 1.0, 0.0);
    }
    
    void MyGLWidget::resizeGL(int w, int h)
    {
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
    }
    
    void MyGLWidget::paintGL()
    {
        glClearColor(1.0, 1.0, 0.0, 0.0);
    
        // Read the external OBJ file into the internal vertex and face vectors.
        loadOBJ("gourd.obj");
    
        qDebug() << "tyt1";
        // Size the vertex array and copy into it x, y, z values from the vertex vector.
        vertices = new float[verticesVector.size()];
        for (int i = 0; i < verticesVector.size(); i++) vertices[i] = verticesVector[i];
    
        qDebug() << "tyt2";
        // Size the faces array and copy into it face index values from the face vector.
        faces = new int[facesVector.size()];
        for (int i = 0; i < facesVector.size(); i++) faces[i] = facesVector[i];
        numIndices = facesVector.size();
    
        glVertexPointer(3, GL_FLOAT, 0, vertices);
    
        qDebug() << "tyt3";
    }
    
    void MyGLWidget::loadOBJ(std::string fileName)
    {
       std::string line;
       int count, vertexIndex1, vertexIndex2, vertexIndex3;
       float coordinateValue;
       char currentCharacter, previousCharacter;
    
       // Open the OBJ file.
       std::ifstream inFile(fileName.c_str(), std::ifstream::in);
    
       // Read successive lines.
       while (getline(inFile, line))
       {
          // Line has vertex data.
          if (line.substr(0, 2) == "v ")
          {
             // Initialize a string from the character after "v " to the end.
             std::istringstream currentString(line.substr(2));
    
             // Read x, y and z values. The (optional) w value is not read.
             for (count = 1; count <= 3; count++)
             {
                currentString >> coordinateValue;
                verticesVector.push_back(coordinateValue);
             }
          }
    
          // Line has face data.
          else if (line.substr(0, 2) == "f ")
          {
             // Initialize a string from the character after "f " to the end.
             std::istringstream currentString(line.substr(2));
    
             // Strategy in the following to detect a vertex index within a face line is based on the
             // fact that vertex indices are exactly those that follow a white space. Texture and
             // normal indices are ignored.
             // Moreover, from the third vertex of a face on output one triangle per vertex, that
             // being the next triangle in a fan triangulation of the face about the first vertex.
             previousCharacter = ' ';
             count = 0;
             while (currentString.get(currentCharacter))
             {
                // Stop processing line at comment.
                if ((previousCharacter == '#') || (currentCharacter == '#')) break;
    
                // Current character is the start of a vertex index.
                if ((previousCharacter == ' ') && (currentCharacter != ' '))
                {
                   // Move the string cursor back to just before the vertex index.
                   currentString.unget();
    
                   // Read the first vertex index, decrement it so that the index range is from 0, increment vertex counter.
                   if (count == 0)
                   {
                      currentString >> vertexIndex1;
                      vertexIndex1--;
                      count++;
                   }
    
                   // Read the second vertex index, decrement it, increment vertex counter.
                   else if (count == 1)
                   {
                      currentString >> vertexIndex2;
                      vertexIndex2--;
                      count++;
                   }
    
                   // Read the third vertex index, decrement it, increment vertex counter AND output the first triangle.
                   else if (count == 2)
                   {
                      currentString >> vertexIndex3;
                      vertexIndex3--;
                      count++;
                      facesVector.push_back(vertexIndex1);
                      facesVector.push_back(vertexIndex2);
                      facesVector.push_back(vertexIndex3);
                   }
    
                   // From the fourth vertex and on output the next triangle of the fan.
                   else
                   {
                      vertexIndex2 = vertexIndex3;
                      currentString >> vertexIndex3;
                      vertexIndex3--;
                      facesVector.push_back(vertexIndex1);
                      facesVector.push_back(vertexIndex2);
                      facesVector.push_back(vertexIndex3);
                   }
    
                   // Begin the process of detecting the next vertex index just after the vertex index just read.
                   currentString.get(previousCharacter);
                }
    
                // Current character is not the start of a vertex index. Move ahead one character.
                else previousCharacter = currentCharacter;
             }
          }
    
          // Nothing other than vertex and face data is processed.
          else
          {
          }
       }
    
       // Close the OBJ file.
       inFile.close();
    
       qDebug() << "tyt0";
    }
    
    P 1 Reply Last reply 1 Dec 2021, 10:36
    0
    • T timob256
      1 Dec 2021, 09:39

      trying to output to 3d model in format .obj .

      I want to output a 3d model to QOpenGLWidget in obj format.

      How to do it?? is there an example of "how to display a 3D model on the screen" ??

      dialogokno.h

      #ifndef DIALOGOKNO_H
      #define DIALOGOKNO_H
      
      #include <QMainWindow>
      #include <QFile>
      #include <QPushButton>
      #include <QLabel>
      #include <QSize>
      
      #include <QApplication>
      #include <QDesktopWidget>
      #include <QRect>
      #include <QDebug>
      #include <QLineEdit>
      #include <QGridLayout>
      #include <QSpacerItem>
      #include <QObject>
      #include <QOpenGLWidget>
      
      #include "myglwidget.h"
      
      class DialogOkno : public QWidget
      {
          Q_OBJECT
      
      public:
          DialogOkno(QWidget *parent = 0);
          ~DialogOkno();
      
      protected:
      //    QWidget* centralWidget;
      
          QFile* file;         // загрузить файл
          QGridLayout* gl_layaout[4];
          QPushButton* b_load; // кнопка для выбора и загрузки файла
          QLabel* l_label[3];
          QLineEdit* le_edit[7];
          QSpacerItem* si_spacer[4];
      };
      
      #endif // DIALOGOKNO_H
      

      glwidget.h

      #ifndef MYGLWIDGET_H
      #define MYGLWIDGET_H
      
      #include <QOpenGLWidget>
      #include <QDebug>
      
      #include <iostream>
      #include <sstream>
      #include <string>
      #include <fstream>
      #include <vector>
      
      class MyGLWidget : public QOpenGLWidget
      {
          Q_OBJECT
      public:
          MyGLWidget(QWidget* parent = nullptr);
      
          // Globals.
           std::vector<float> verticesVector; // Vector to read in vertex x, y and z values fromt the OBJ file.
           std::vector<int> facesVector; // Vector to read in face vertex indices from the OBJ file.
           float *vertices = NULL;  // Vertex array of the object x, y, z values.
          int *faces = NULL; // Face (triangle) vertex indices.
           int numIndices; // Number of face vertex indices.
           float Xangle = 0.0, Yangle = 0.0, Zangle = 0.0; // Angles to rotate the object.
          void loadOBJ(std::string fileName);
      
      protected:
          void initializeGL();
          void resizeGL(int w, int h);
          void paintGL();
      };
      
      #endif // MYGLWIDGET_H
      

      dialogokno.cpp

      #include "dialogokno.h"
      
      DialogOkno::DialogOkno(QWidget *parent)
          : QWidget(parent)
      {
          MyGLWidget * openGL = new MyGLWidget(parent);
      
          // обявл элементы
          gl_layaout[0] = new QGridLayout(parent);
          gl_layaout[1] = new QGridLayout(parent);
          gl_layaout[2] = new QGridLayout(parent);
      
          gl_layaout[3] = new QGridLayout(parent);
      
          l_label[0] = new QLabel(parent);
          l_label[1] = new QLabel(parent);
          l_label[2] = new QLabel(parent);
      
          le_edit[0] = new QLineEdit(parent);
          le_edit[1] = new QLineEdit(parent);
          le_edit[2] = new QLineEdit(parent);
          le_edit[3] = new QLineEdit(parent);
          le_edit[4] = new QLineEdit(parent);
          le_edit[5] = new QLineEdit(parent);
          le_edit[6] = new QLineEdit(parent);
      
          // настройки элеметов (стилизация)
      
          // заполнение элементов
          l_label[0]->setText("hla_global");
          l_label[1]->setText("hla_local");
          l_label[2]->setText("logger");
      
          le_edit[0]->setText("server_ip_global");
          le_edit[1]->setText("server_port_global");
      
          le_edit[2]->setText("server_port_local");
          le_edit[3]->setText("server_port_local");
      
          le_edit[4]->setText("log");
          le_edit[5]->setText("log_period_ms");
          le_edit[6]->setText("log_size_in_mb");
      
          //разложение элементов по группам
      
          gl_layaout[0]->addWidget(l_label[0], 0, 0, 1, 1);
          gl_layaout[0]->addWidget(le_edit[0], 1, 0, 1, 1);
          gl_layaout[0]->addWidget(le_edit[1], 2, 0, 1, 1);
      
          gl_layaout[1]->addWidget(l_label[1], 0, 0, 1, 1);
          gl_layaout[1]->addWidget(le_edit[2], 1, 0, 1, 1);
          gl_layaout[1]->addWidget(le_edit[3], 2, 0, 1, 1);
      
          gl_layaout[2]->addWidget(l_label[2], 0, 0, 1, 1);
          gl_layaout[2]->addWidget(le_edit[4], 1, 0, 1, 1);
          gl_layaout[2]->addWidget(le_edit[5], 2, 0, 1, 1);
          gl_layaout[2]->addWidget(le_edit[6], 3, 0, 1, 1);
      
      
          gl_layaout[3]->addLayout(gl_layaout[0], 0, 0, 1, 1);
      
          si_spacer[0] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
      
          gl_layaout[3]->addItem(si_spacer[0], 0, 1, 1, 1);
      
          gl_layaout[3]->addLayout(gl_layaout[1], 0, 2, 1, 1);
          si_spacer[1] = new QSpacerItem(120, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
          gl_layaout[3]->addItem(si_spacer[1], 0, 3, 1, 1);
          si_spacer[2] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
          gl_layaout[3]->addItem(si_spacer[2],1, 0, 1, 1);
          gl_layaout[3]->addLayout(gl_layaout[2], 2, 0, 1, 1);
          si_spacer[3] = new QSpacerItem(20, 82, QSizePolicy::Minimum, QSizePolicy::Expanding);
          gl_layaout[3]->addItem(si_spacer[3], 3, 0, 1, 1);
          gl_layaout[3]->addWidget(openGL , 1, 1, 3, 3);
      
          this->setLayout(gl_layaout[3]);
      }
      
      DialogOkno::~DialogOkno()
      {
      
      }
      

      main.cpp

      #include "dialogokno.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QApplication::setAttribute(Qt::AA_ForceRasterWidgets, false);
          DialogOkno w;
          QDesktopWidget desktop;
          QRect rect = desktop.availableGeometry(desktop.primaryScreen()); // прямоугольник с размерами экрана
          QPoint center = rect.center(); //координаты центра экрана
          w.resize(center.x(), center.y());
      
          w.setWindowTitle("программа");
          w.show();
      
          return a.exec();
      }
      

      glwidget.cpp

      #include "myglwidget.h"
      
      MyGLWidget::MyGLWidget(QWidget * parent): QOpenGLWidget(parent)
      {
      
      }
      
      void MyGLWidget::initializeGL()
      {
          glEnableClientState(GL_VERTEX_ARRAY);
      
          glClearColor(1.0, 1.0, 1.0, 0.0);
      }
      
      void MyGLWidget::resizeGL(int w, int h)
      {
          glViewport(0, 0, w, h);
          glMatrixMode(GL_PROJECTION);
          glLoadIdentity();
          glMatrixMode(GL_MODELVIEW);
      }
      
      void MyGLWidget::paintGL()
      {
          glClearColor(1.0, 1.0, 0.0, 0.0);
      
          // Read the external OBJ file into the internal vertex and face vectors.
          loadOBJ("gourd.obj");
      
          qDebug() << "tyt1";
          // Size the vertex array and copy into it x, y, z values from the vertex vector.
          vertices = new float[verticesVector.size()];
          for (int i = 0; i < verticesVector.size(); i++) vertices[i] = verticesVector[i];
      
          qDebug() << "tyt2";
          // Size the faces array and copy into it face index values from the face vector.
          faces = new int[facesVector.size()];
          for (int i = 0; i < facesVector.size(); i++) faces[i] = facesVector[i];
          numIndices = facesVector.size();
      
          glVertexPointer(3, GL_FLOAT, 0, vertices);
      
          qDebug() << "tyt3";
      }
      
      void MyGLWidget::loadOBJ(std::string fileName)
      {
         std::string line;
         int count, vertexIndex1, vertexIndex2, vertexIndex3;
         float coordinateValue;
         char currentCharacter, previousCharacter;
      
         // Open the OBJ file.
         std::ifstream inFile(fileName.c_str(), std::ifstream::in);
      
         // Read successive lines.
         while (getline(inFile, line))
         {
            // Line has vertex data.
            if (line.substr(0, 2) == "v ")
            {
               // Initialize a string from the character after "v " to the end.
               std::istringstream currentString(line.substr(2));
      
               // Read x, y and z values. The (optional) w value is not read.
               for (count = 1; count <= 3; count++)
               {
                  currentString >> coordinateValue;
                  verticesVector.push_back(coordinateValue);
               }
            }
      
            // Line has face data.
            else if (line.substr(0, 2) == "f ")
            {
               // Initialize a string from the character after "f " to the end.
               std::istringstream currentString(line.substr(2));
      
               // Strategy in the following to detect a vertex index within a face line is based on the
               // fact that vertex indices are exactly those that follow a white space. Texture and
               // normal indices are ignored.
               // Moreover, from the third vertex of a face on output one triangle per vertex, that
               // being the next triangle in a fan triangulation of the face about the first vertex.
               previousCharacter = ' ';
               count = 0;
               while (currentString.get(currentCharacter))
               {
                  // Stop processing line at comment.
                  if ((previousCharacter == '#') || (currentCharacter == '#')) break;
      
                  // Current character is the start of a vertex index.
                  if ((previousCharacter == ' ') && (currentCharacter != ' '))
                  {
                     // Move the string cursor back to just before the vertex index.
                     currentString.unget();
      
                     // Read the first vertex index, decrement it so that the index range is from 0, increment vertex counter.
                     if (count == 0)
                     {
                        currentString >> vertexIndex1;
                        vertexIndex1--;
                        count++;
                     }
      
                     // Read the second vertex index, decrement it, increment vertex counter.
                     else if (count == 1)
                     {
                        currentString >> vertexIndex2;
                        vertexIndex2--;
                        count++;
                     }
      
                     // Read the third vertex index, decrement it, increment vertex counter AND output the first triangle.
                     else if (count == 2)
                     {
                        currentString >> vertexIndex3;
                        vertexIndex3--;
                        count++;
                        facesVector.push_back(vertexIndex1);
                        facesVector.push_back(vertexIndex2);
                        facesVector.push_back(vertexIndex3);
                     }
      
                     // From the fourth vertex and on output the next triangle of the fan.
                     else
                     {
                        vertexIndex2 = vertexIndex3;
                        currentString >> vertexIndex3;
                        vertexIndex3--;
                        facesVector.push_back(vertexIndex1);
                        facesVector.push_back(vertexIndex2);
                        facesVector.push_back(vertexIndex3);
                     }
      
                     // Begin the process of detecting the next vertex index just after the vertex index just read.
                     currentString.get(previousCharacter);
                  }
      
                  // Current character is not the start of a vertex index. Move ahead one character.
                  else previousCharacter = currentCharacter;
               }
            }
      
            // Nothing other than vertex and face data is processed.
            else
            {
            }
         }
      
         // Close the OBJ file.
         inFile.close();
      
         qDebug() << "tyt0";
      }
      
      P Online
      P Online
      Pl45m4
      wrote on 1 Dec 2021, 10:36 last edited by
      #2

      @timob256

      This is some years old but maybe it helps.

      Also found this:
      https://github.com/GarageGames/Qt/blob/master/qt-5/qt3d/src/render/io/objloader.cpp


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0

      2/2

      1 Dec 2021, 10:36

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved