首页 > 解决方案 > 无法在openGL中绘制对象

问题描述

我在第一个窗口的 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。

标签: clinuxopenglglutopengl-compat

解决方案


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

  • 在使用之前glutDisplayFunc,您必须选择窗口。如果你只有一个窗口,问题不问,但如果你有两个,你必须先打电话glutSetWindow(...)

  • 还要注意该glutInitWindow...功能适用​​于要创建的下一个窗口。

  • glutMainLoop应该调用一次。
  • 最后,不要忘记glFlush()在函数结束时display调用:
#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;
}

推荐阅读