【发布时间】:2020-04-27 11:01:16
【问题描述】:
我在第一个窗口的 opengl 中绘制了一个正方形,当我尝试在第二个屏幕上绘制一些对象时。我得到一个空白屏幕。
这是我的代码。
#include <GL/glut.h>
void display() {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
//glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void displayc2()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
void keycb(unsigned char key,int x , int y)
{
int win2;
if(key=='a') exit(0);
else if(key == 'b')
{
win2 = glutCreateWindow("window 2");
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50);
glutDisplayFunc(displayc2);
glutMainLoop(); // Enter the event-processing loop
}
}
int main(int argc, char** argv) {
int win1;
glutInit(&argc, argv); // Initialize GLUT
win1 = glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutKeyboardFunc(keycb);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
我想要做的是,当我按下键盘上的字符“b”时,它应该显示第二个屏幕。第一个屏幕和对象成功出现但是,在这里我得到了第二个屏幕,但我没有在第二个屏幕中得到对象。在这种情况下,第二个屏幕是空白的。告诉我这段代码有什么问题,或者有其他方法可以实现吗?
我正在使用 C 编程在 ubuntu 18.04 中做 opengl。
【问题讨论】:
-
keycb 不应该有 glutMainLoop()
标签: c linux opengl glut opengl-compat