无法在 openGL 中绘制对象

can't draw object in openGL

我在第一个 window 中用 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' 时,它应该显示第二个屏幕。第一个屏幕和对象成功到达但是,这里我得到了第二个屏幕,但我没有在第二个屏幕中得到对象 screen.the 在这种情况下,第二个屏幕是空白的。告诉我这段代码有什么问题,或者有其他方法可以实现吗?

我正在使用 C 编程在 ubuntu 18.04 中执行 opengl。

使您的问题有效的一些说明:

  • 在使用glutDisplayFunc之前,您必须select window。如果你只有一个window,问题不问,但是如果你有两个,你必须在glutSetWindow(...)之前调用

  • 还请注意,glutInitWindow... 函数适用于要创建的 next window。

  • glutMainLoop 应该调用一次。
  • 最后,不要忘记在 display 函数的末尾调用 glFlush()
#include <GL/glut.h>


int win1, win2;

void display()
{
    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(1.0f, 0.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();
    glFlush();                  // Render now
}

void keycb(unsigned char key, int x, int y)
{
    if (key == 'a')
        exit(0);
    else if (key == 'b'&&win2==0) {
        glutInitWindowSize(450, 450);   
        glutInitWindowPosition(250, 250);

        win2 = glutCreateWindow("window 2");

        // Select the window for glutDisplayFunc
        glutSetWindow(win2);
        glutDisplayFunc(displayc2);
    }
}

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

    glutInit(&argc, argv);      // Initialize GLUT
    glutInitWindowSize(450, 450);       // Set the window's initial width & height
    glutInitWindowPosition(50, 50);     // Position the window's initial top-left corner
    win1 = glutCreateWindow("OpenGL Setup Test");       // Create a window with the given title
    glutDisplayFunc(display);   // Register display callback handler for window re-paint
    glutKeyboardFunc(keycb);

    glutMainLoop();             // Enter the event-processing loop
    return 0;
}