【问题标题】:How to Draw a hollow circle in openGL C++如何在openGL C++中画一个空心圆
【发布时间】:2019-10-15 03:50:44
【问题描述】:

我正在尝试在窗口中绘制一个带有平滑线条的空心圆。由于某种原因,圆圈没有出现。到目前为止,我已经找到了圆的代码,但是线条是锯齿状的,我需要它们是平滑的。我希望能够最终将其他对象放入圆圈与对象接壤的圆圈中。以下是我的代码:

#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;
}

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:
    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();
    }
    

    对于 OpenGL 中的“平滑”线条,最好的方法是在后台缓冲区上启用多重采样。请参阅此处有关过剩的部分:https://www.khronos.org/opengl/wiki/Multisampling

    在您的情况下(如果您根本不打算使用双缓冲),您可能想要使用

    glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE | GLUT_MULTISAMPLE);
    

    然后打开多重采样:

    glEnable(GL_MULTISAMPLE_ARB);
    

    我可能还建议在您的圈子上使用多个分区,例如 180 或 360(而不是 300)。这样,对于常见的角度(例如 30、45、90、180 等),您就会知道圆上的那个位置总是有一个点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多