March 14, 2011

webadana webadana
Lab Rat
7 posts

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

 

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..

4 replies

March 14, 2011

hakan hakan
Lab Rat
6 posts

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.

March 14, 2011

ZapB ZapB
Robot Herder
1354 posts

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

  1. #include <QtGui/QApplication>
  2. #include "lines.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     Lines w;
  8.     w.show();
  9.     return a.exec();
  10. }

lines.h

  1. #ifndef LINES_H
  2. #define LINES_H
  3.  
  4. #include <QGLWidget>
  5. #include <QVector2D>
  6.  
  7. const float pi = 3.141592653f;
  8. const float twoPi = 2.0f * pi;
  9. const float piBy2 = 0.5f * pi;
  10. const float degToRad = pi / 180.0f;
  11. const float radToDeg = 180.0f / pi;
  12.  
  13. class Lines : public QGLWidget
  14. {
  15.     Q_OBJECT
  16.  
  17. public:
  18.     Lines( QWidget* parent = 0 );
  19.  
  20. protected:
  21.     virtual void initializeGL();
  22.     virtual void resizeGL( int w, int h );
  23.     virtual void paintGL();
  24.  
  25.     virtual void keyPressEvent( QKeyEvent* e );
  26.  
  27. private:
  28.     float m_theta;  /**< Rotation about x-axis */
  29.     float m_phi;    /**< Rotation about y-axis */
  30.     float m_aspectRatio;
  31.     QVector2D m_lineWidthRange;
  32.     float m_lineWidthStep;
  33.     float m_lineWidth;
  34. };
  35.  
  36. #endif // LINES_H

lines.cpp

  1. #include "lines.h"
  2.  
  3. #include <QCoreApplication>
  4. #include <QDebug>
  5. #include <QKeyEvent>
  6.  
  7. #include <math.h>
  8.  
  9. Lines::Lines( QWidget* parent )
  10.     : QGLWidget( parent ),
  11.       m_theta( 0.0f ),
  12.       m_phi( 0.0f ),
  13.       m_aspectRatio( 1.0 ),
  14.       m_lineWidthRange(),
  15.       m_lineWidthStep( 0.0f ),
  16.       m_lineWidth( 1.0f )
  17. {
  18. }
  19.  
  20. void Lines::initializeGL()
  21. {
  22.     // Set the clear color to black
  23.     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
  24.  
  25.     // Set the drawing color to green
  26.     glColor3f( 0.0f, 1.0f, 0.0f );
  27.  
  28.     // Query some info about supported point sizes
  29.     glGetFloatv( GL_LINE_WIDTH_RANGE, reinterpret_cast<float*>( &m_lineWidthRange ) );
  30.     glGetFloatv( GL_LINE_WIDTH_GRANULARITY, &m_lineWidthStep );
  31.  
  32.     qDebug() << "Point size range:" << m_lineWidthRange;
  33.     qDebug() << "Point size step:" << m_lineWidthStep;
  34.  
  35.     m_lineWidth = m_lineWidthRange.x();
  36. }
  37.  
  38. void Lines::resizeGL( int w, int h )
  39. {
  40.     // Prevent a divde by zero
  41.     if ( h == 0 )
  42.         h = 1;
  43.  
  44.     // Set the viewport to window dimensions
  45.     glViewport( 0, 0, w, h );
  46.  
  47.     // reset the coordinate system
  48.     glMatrixMode( GL_PROJECTION );
  49.     glLoadIdentity();
  50.  
  51.     // Establish the clipping volume by setting up an orthographic projection
  52.     double range = 100.0;
  53.     m_aspectRatio = double( w ) / double( h );
  54.     if ( w <=h )
  55.         glOrtho( -range, range, -range / m_aspectRatio, range / m_aspectRatio, range, -range );
  56.     else
  57.         glOrtho( -range * m_aspectRatio, range * m_aspectRatio, -range, range, range, -range );
  58.  
  59.     glMatrixMode( GL_MODELVIEW );
  60.     glLoadIdentity();
  61. }
  62.  
  63. void Lines::paintGL()
  64. {
  65.     // Clear the buffer with the current clearing color
  66.     glClear( GL_COLOR_BUFFER_BIT );
  67.  
  68.     // Set drawing colour to red
  69.     glColor3f( 1.0f, 0.0f, 0.0f );
  70.  
  71.     // Save matrix state and do the custom rotation
  72.     glPushMatrix();
  73.     glRotatef( m_theta, 1.0f, 0.0f, 0.0f );
  74.     glRotatef( m_phi,   0.0f, 1.0f, 0.0f );
  75.  
  76.     // Draw some Lines in a helix
  77.     glLineWidth( m_lineWidth );
  78.     glBegin( GL_LINE_STRIP );
  79.     float z = -50.0f;
  80.     float angle = 0.0f;
  81.     for ( angle = 0.0f; angle <= twoPi * 3.0f; angle += 0.1f, z += 0.5f )
  82.     {
  83.         float x = 50.0 * sin( angle );
  84.         float y = 50.0 * cos( angle );
  85.         glVertex3f( x, y, z );
  86.     }
  87.     glEnd();
  88.  
  89.     // Restore the matrix state
  90.     glPopMatrix();
  91. }
  92.  
  93. void Lines::keyPressEvent( QKeyEvent* e )
  94. {
  95.     switch ( e->key() )
  96.     {
  97.         case Qt::Key_Escape:
  98.             QCoreApplication::instance()->quit();
  99.             break;
  100.  
  101.         case Qt::Key_Left:
  102.             m_phi += 1.0f;
  103.             updateGL();
  104.             break;
  105.  
  106.         case Qt::Key_Right:
  107.             m_phi -= 1.0f;
  108.             updateGL();
  109.             break;
  110.  
  111.         case Qt::Key_Up:
  112.             m_theta += 1.0f;
  113.             updateGL();
  114.             break;
  115.  
  116.         case Qt::Key_Down:
  117.             m_theta -= 1.0f;
  118.             updateGL();
  119.             break;
  120.  
  121.         case Qt::Key_Plus:
  122.             m_lineWidth = qMin( m_lineWidth + m_lineWidthStep, float( m_lineWidthRange.y() ) );
  123.             qDebug() << "m_lineWidth =" << m_lineWidth;
  124.             updateGL();
  125.             break;
  126.  
  127.         case Qt::Key_Minus:
  128.             m_lineWidth = qMax( m_lineWidth - m_lineWidthStep, float( m_lineWidthRange.x() ) );
  129.             qDebug() << "m_lineWidth =" << m_lineWidth;
  130.             updateGL();
  131.             break;
  132.  
  133.         default:
  134.             QGLWidget::keyPressEvent( e );
  135.     }
  136. }

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

March 18, 2011

webadana webadana
Lab Rat
7 posts

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

March 18, 2011

ZapB ZapB
Robot Herder
1354 posts

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 ;-)

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ [Solved] GUI plugin problems      [SOLVED] Label display of an Int ››

You must log in to post a reply. Not a member yet? Register here!