简体   繁体   中英

How to Draw a hollow circle in openGL C++

I am trying to draw a hollow circle with smooth lines in a window. And for some reason the circle is not appearing. So far I have found code for circles but the lines are jagged and I am needing them to be smooth. I want to be able to put other object in the circle eventually where the circle borders the object. Below is my code:

#include <iostream>
#include <math.h>
#include <GL/glut.h>                // include GLUT library
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <Windows.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
//#include "RGBpixmap.h"
using namespace std;

void DrawCircle()
{
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i <= 300; i++) {
        double angle = 2 * M_PI * i / 300;
        double x = cos(angle);
        double y = sin(angle);
        glVertex2d(x, y);
    }
    glEnd();
}


//***********************************************************************************
void myInit()
{
    glClearColor(1, 1, 1, 0);           // specify a background clor: white 
    gluOrtho2D(-300, 300, -300, 300);  // specify a viewing area
    glPointSize(1);     // change point size back to 1
}

//***********************************************************************************
void myDisplayCallback()
{
    DrawCircle();
    glClear(GL_COLOR_BUFFER_BIT);   // draw the background



    glFlush(); // flush out the buffer contents
}

//***********************************************************************************
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);               // specify a window size
    glutInitWindowPosition(100, 0);         // specify a window position
    glutCreateWindow("Drawing Window"); // create a titled window

    myInit();                                   // setting up

    glutDisplayFunc(myDisplayCallback);     // register a callback


    glutMainLoop();                         // get into an infinite loop

    return 0;
}
void myDisplayCallback()
{
    // This call does NOT draw the background, it clears the screen.
    // You should be doing this before drawing anything!
    glClear(GL_COLOR_BUFFER_BIT);

    // and now you can draw on a nice clear screen
    DrawCircle();

    // This doesn't 'flush' the buffers, it waits until the OpenGL
    // command queue has finished executing (i.e. drawing is complete)
    glFlush();
}

For 'smooth' lines in OpenGL, the best approach is to enable multi-sampling on the back buffer. See the section here on glut: https://www.khronos.org/opengl/wiki/Multisampling

In your case (if you don't intend to use double buffering at all), you probably want to use

glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE | GLUT_MULTISAMPLE);

and later turn on multisampling with:

glEnable(GL_MULTISAMPLE_ARB);

I'd probably also recommend using a number of divisions on your circle such as 180, or 360 (rather than 300). That way with the common angles (eg 30, 45, 90, 180, etc) you'll know you always have a point on the circle at that location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM