Draw circle in 3d

  • Hello

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

  • Using GL_TRIANGLE_FAN maybe ;)


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

  • 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

    cView3D(QWidget* parent = 0);

    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.

  • 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);
    glVertex3d(-sin(daneStarsRA) * cos(daneStarsDEC),
                sin(daneStarsRA-M_PI/2) * cos(daneStarsDEC));
    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(x-M_PI/2) * cos(y));

    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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.