c++ - 我用于渲染随机多边形的代码无法正常工作

标签 c++ opengl graphics glut glu

我正在使用 opengl c++ 并尝试编写用于在终端上渲染随机多边形的代码。我正在使用 CodeBlocks 13.12。

int width=800;
int height=600;

void RandomPolygons()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    GLint x[100],y[100],n,r,g,b;
    GLint i,j;
    cout<<"Enter the sides of the polygon to be displayed:"<<endl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        x[i]=rand()%800;
        cout<<"x["<<i<<"]=  "<<x[i]<<endl;
        y[i]=rand()%600;
        cout<<"y["<<i<<"]=  "<<y[i]<<endl;
    }
    x[i]=x[1];
    cout<<"x["<<i<<"]=  "<<x[i]<<endl;
    y[i]=y[1];
    cout<<"y["<<i<<"]=  "<<y[i]<<endl;

    r=rand()%2;
    g=rand()%2;
    b=rand()%2;

    glColor3f(r,g,b);
    glBegin(GL_POLYGON);
    for(j=1;j<=n;j++)
    {
        glVertex2i(x[j],y[j]);
    }
    glVertex2i(x[j],y[j]);
    glEnd();
    glFlush();
    glutSwapBuffers();
}


int main(int argc, char **argv)
{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGBA|GLUT_DOUBLE);
    glutInitWindowSize(width,height);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Random_Polygons!!!!");
    glClearColor(0,0,0,0);
    gluOrtho2D(0,800,0,600);
    glutDisplayFunc(RandomPolygons);
    glutIdleFunc(RandomPolygons);
    glutMainLoop();
}

输出

它只是没有响应(渲染屏幕),另一方面终端工作正常....

enter image description here

最佳答案

std::cin 会阻塞,直到用户输入内容。由于应用程序被阻塞,windows消息循环没有执行,窗口停止响应。

当您需要在 OpenGL 应用程序中输入时,您将不得不监听 glutKeyboardFuncglutSpecialFunc 并从那里构建输入。

关于c++ - 我用于渲染随机多边形的代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153271/

相关文章:

c++ - 在运行时加载 OpenGL 扩展

在 C 中创建您自己的控制台图形库

c++ - 如何计算插值三角曲面的主曲率?

opengl - 深度测试后将两个单独的帧缓冲区合并到默认帧缓冲区

image - Delphi 中伪造的类似 Web 2.0 的动画工具提示

c++ - 使用 openGL 加速时不考虑 QChart z 值?

c++ - 优于 C++ unordered_set 的哈希函数

c++ - 消息包,C++ : How to use MSGPACK_DEFINE with c++11 enum classes

C++ 对堆对象的引用?

具有自定义渲染/绘图的 Python GUI