我正在使用 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();
}
输出
它只是没有响应(渲染屏幕),另一方面终端工作正常....
最佳答案
std::cin
会阻塞,直到用户输入内容。由于应用程序被阻塞,windows消息循环没有执行,窗口停止响应。
当您需要在 OpenGL 应用程序中输入时,您将不得不监听 glutKeyboardFunc
和 glutSpecialFunc
并从那里构建输入。
关于c++ - 我用于渲染随机多边形的代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153271/