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. Draw circle in 3d

Draw circle in 3d

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 7.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.
  • G Offline
    G Offline
    grzesczwa
    wrote on last edited by
    #1

    Hello

    I am a novice programmer in OpenGL.
    My question is how to draw a circle in 3D

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      Using GL_TRIANGLE_FAN maybe ;)

      !http://www.videotutorialsrock.com/opengl_tutorial/crab_pong/triangle_fan.png(GL_TRIANGLE_FAN)!

      Just add more sections to approximate a circle, that is how 3d programs do it. You can also use bezier patches and surfaces but it is much more complex and not applicable in many cases like games.

      Here is how to do it with Qt's Qt3D API:

      @ QVector2DArray vertices;
      QVector3DArray normals;
      int step = qMin(rect.width() / 8, rect.height() / 8);
      int midx = rect.x() + rect.width() / 2;
      int midy = rect.y() + rect.height() / 2;

       vertices.append(midx - step, midy + step);
       vertices.append(midx + step * 2, midy + step * 2);
       vertices.append(midx + step * 2, midy);
       vertices.append(midx + step, midy - step);
       vertices.append(midx - step, midy - step * 2);
      
       normals.append(0.0f, 0.0f, 1.0f);
       normals.append(0.0f, 0.0f, 1.0f);
       normals.append(0.0f, 0.0f, 1.0f);
       normals.append(0.0f, 0.0f, 1.0f);
       normals.append(0.0f, 0.0f, 1.0f);
      
       painter->clearAttributes();
       painter->setVertexAttribute(QGL::Position, vertices);
       painter->setVertexAttribute(QGL::Normal, normals);
       painter->draw(QGL::TriangleFan, 5);@
      

      From the "Shapes Demo":http://doc-snapshot.qt-project.org/qt3d-1.0/qt3d-shapes.html

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RaubTieR
        wrote on last edited by
        #3

        May be you could give more info about what have you already done? I mean first ofс you should have started adding
        @qt += opengl@

        to your .pro file, then subclass QGLWidget and reimplement some of its functions... lets see if I find some snippets... yup here they are:

        @#include <QtOpenGL/QGLWidget>

        class cView3D : public QGLWidget
        {
        Q_OBJECT

        public:
        cView3D(QWidget* parent = 0);

        protected:
        void initializeGL(); // opengl initializations, settings
        void resizeGL(int nWidth, int nHeight); // resize event
        void paintGL(); // draw things here

        };@

        So if this is done, you can start calling GL functions in paintGL() method as you would in any opengl application. As for the sircles... in opengl you usually do use GL_TRIANGLES or GL_LINES to aproximate circles with composition of 3D accelerated objects. However you can draw a QUAD with texture, where circle is drawn.
        Please specify more exact requirement for what you want to achieve and info about your current state.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          grzesczwa
          wrote on last edited by
          #4

          Hello
          Part of the problem solved. The following is a snippet of code:

          @ GLfloat x,y,z,kat;
          const GLfloat GL_PI=3.1415f;

          daneStarsRA = f.deg2rad(0);
          daneStarsDEC = f.deg2rad(40);
          glPointSize(1);
          glBegin(GL_POINTS);
          glVertex3d(-sin(daneStarsRA) * cos(daneStarsDEC),
                      sin(daneStarsDEC),
                      sin(daneStarsRA-M_PI/2) * cos(daneStarsDEC));
          glEnd();
          glBegin(GL_POINTS);
          for(kat = 0.0f; kat < (2.0f*GL_PI); kat += (GL_PI/32.0f))
          {
              x = daneStarsRA  + 0.005 * sin(kat);
              y = daneStarsDEC  + 0.005 * cos(kat);
              glVertex3d(-sin(x) * cos(y),
                          sin(y),
                          sin(x-M_PI/2) * cos(y));
          }
          glEnd();@
          

          The function deg2rad converts degrees to radians.
          How do I give daneStarsRA = daneStarsDEC = 0 to draw circle to me and I'll tell you how
          daneStarsRA = 0; daneStarsDEC = 70 what I draw an ellipse
          what do bug

          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