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. How to draw 3d line(x,y,z)
QtWS25 Last Chance

How to draw 3d line(x,y,z)

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 20.4k 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.
  • W Offline
    W Offline
    webadana
    wrote on last edited by
    #1

    i can draw the 2d line and i want to draw 3d (x,y,z) dimension for graph the math equation..

    Where i should start ?
    there is some explanation
    http://www.digitalfanatics.org/projects/qt_tutorial/chapter14.html

    but it is too complex.. i need to draw just line on (x,y,z) dimention..

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hakan
      wrote on last edited by
      #2

      to draw on 3D you have to learn some basics. then things will get easier. The link which you have given seems good. Just try to implement examples then you'll see how easy to draw a 3d line.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        This simple app should get you started with drawing lines in 3D using OpenGL and Qt. I suggest you also have a read through the QGLWidget docs and get yourself a good book on OpenGL programming. I can recommend the OpenGL Superbible.

        == main.cpp ==
        @
        #include <QtGui/QApplication>
        #include "lines.h"

        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        Lines w;
        w.show();
        return a.exec();
        }
        @

        == lines.h ==
        @
        #ifndef LINES_H
        #define LINES_H

        #include <QGLWidget>
        #include <QVector2D>

        const float pi = 3.141592653f;
        const float twoPi = 2.0f * pi;
        const float piBy2 = 0.5f * pi;
        const float degToRad = pi / 180.0f;
        const float radToDeg = 180.0f / pi;

        class Lines : public QGLWidget
        {
        Q_OBJECT

        public:
        Lines( QWidget* parent = 0 );

        protected:
        virtual void initializeGL();
        virtual void resizeGL( int w, int h );
        virtual void paintGL();

        virtual void keyPressEvent( QKeyEvent* e );
        

        private:
        float m_theta; /< Rotation about x-axis */
        float m_phi; /
        < Rotation about y-axis */
        float m_aspectRatio;
        QVector2D m_lineWidthRange;
        float m_lineWidthStep;
        float m_lineWidth;
        };

        #endif // LINES_H
        @

        == lines.cpp ==
        @
        #include "lines.h"

        #include <QCoreApplication>
        #include <QDebug>
        #include <QKeyEvent>

        #include <math.h>

        Lines::Lines( QWidget* parent )
        : QGLWidget( parent ),
        m_theta( 0.0f ),
        m_phi( 0.0f ),
        m_aspectRatio( 1.0 ),
        m_lineWidthRange(),
        m_lineWidthStep( 0.0f ),
        m_lineWidth( 1.0f )
        {
        }

        void Lines::initializeGL()
        {
        // Set the clear color to black
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );

        // Set the drawing color to green
        glColor3f( 0.0f, 1.0f, 0.0f );
        
        // Query some info about supported point sizes
        glGetFloatv( GL_LINE_WIDTH_RANGE, reinterpret_cast<float*>( &m_lineWidthRange ) );
        glGetFloatv( GL_LINE_WIDTH_GRANULARITY, &m_lineWidthStep );
        
        qDebug() << "Point size range:" << m_lineWidthRange;
        qDebug() << "Point size step:" << m_lineWidthStep;
        
        m_lineWidth = m_lineWidthRange.x();
        

        }

        void Lines::resizeGL( int w, int h )
        {
        // Prevent a divde by zero
        if ( h == 0 )
        h = 1;

        // Set the viewport to window dimensions
        glViewport( 0, 0, w, h );
        
        // reset the coordinate system
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        
        // Establish the clipping volume by setting up an orthographic projection
        double range = 100.0;
        m_aspectRatio = double( w ) / double( h );
        if ( w <=h )
            glOrtho( -range, range, -range / m_aspectRatio, range / m_aspectRatio, range, -range );
        else
            glOrtho( -range * m_aspectRatio, range * m_aspectRatio, -range, range, range, -range );
        
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        

        }

        void Lines::paintGL()
        {
        // Clear the buffer with the current clearing color
        glClear( GL_COLOR_BUFFER_BIT );

        // Set drawing colour to red
        glColor3f( 1.0f, 0.0f, 0.0f );
        
        // Save matrix state and do the custom rotation
        glPushMatrix();
        glRotatef( m_theta, 1.0f, 0.0f, 0.0f );
        glRotatef( m_phi,   0.0f, 1.0f, 0.0f );
        
        // Draw some Lines in a helix
        glLineWidth( m_lineWidth );
        glBegin( GL_LINE_STRIP );
        float z = -50.0f;
        float angle = 0.0f;
        for ( angle = 0.0f; angle <= twoPi * 3.0f; angle += 0.1f, z += 0.5f )
        {
            float x = 50.0 * sin( angle );
            float y = 50.0 * cos( angle );
            glVertex3f( x, y, z );
        }
        glEnd();
        
        // Restore the matrix state
        glPopMatrix();
        

        }

        void Lines::keyPressEvent( QKeyEvent* e )
        {
        switch ( e->key() )
        {
        case Qt::Key_Escape:
        QCoreApplication::instance()->quit();
        break;

            case Qt::Key_Left:
                m_phi += 1.0f;
                updateGL();
                break;
        
            case Qt::Key_Right:
                m_phi -= 1.0f;
                updateGL();
                break;
        
            case Qt::Key_Up:
                m_theta += 1.0f;
                updateGL();
                break;
        
            case Qt::Key_Down:
                m_theta -= 1.0f;
                updateGL();
                break;
        
            case Qt::Key_Plus:
                m_lineWidth = qMin( m_lineWidth + m_lineWidthStep, float( m_lineWidthRange.y() ) );
                qDebug() << "m_lineWidth =" << m_lineWidth;
                updateGL();
                break;
        
            case Qt::Key_Minus:
                m_lineWidth = qMax( m_lineWidth - m_lineWidthStep, float( m_lineWidthRange.x() ) );
                qDebug() << "m_lineWidth =" << m_lineWidth;
                updateGL();
                break;
        
            default:
                QGLWidget::keyPressEvent( e );
        }
        

        }
        @

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • W Offline
          W Offline
          webadana
          wrote on last edited by
          #4

          @ZapB thank you very much... its awesome :)
          @hakan ayrıca teşekkür ederim hakan bey yorumunuz için.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #5

            No problem. Good luck with OpenGL it is good fun although it can be confusing and frustrating to start with when you spend a long time coding something only to be presented with an empty window ;-)

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0

            • Login

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