GLUT is working fine but OpenGL is not doing anything

試著忘記壹切 提交于 2019-12-11 20:24:58

问题


The exact same code snippet is working on another machine but its not working properly for me. The GLUT is working absolutely fine as it open the created window but the line segment is not shown on the window which means there is a problem with opengl. It is not even changing the background color of the window.

I even test the opengl on my windows with a testing application and its working fine.

#ifdef WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

void init(void){
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 400.0, 0.0, 400.0);
}

void linesegment(void){
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex2i(180, 15);
    glVertex2i(10, 145);
    glEnd();

    glFlush();
}
int main(int argc, char* argv[])
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Testing Open GL");

    init();
    glutDisplayFunc(linesegment);
    glutMainLoop();
    return 0;

}

回答1:


Most likely you're running into the rather new class of problems introduced by compositing graphics systems (Aero in Windows, Quartz Extreme on MacOS X and a multitude of various programs on Linux/X11). The gist of the problem is, that compositing is inherently double buffered: There's always an offscreen (back) buffer for the window to be drawn. And when a program indicates that it's finished with drawing the compositor will integrate it into the on-screen image.

This however brings a few caveats. Most importantly, single buffered drawing somehow needs to indicate that it's finished. While OpenGL's glFinish call should suffice from a implementors point of view, most compositing systems are not sensitive to it. You'll have to create a double buffered window pixel format and do a buffer swap to make the compositor present your image.

So for your program:

  • replace GLUT_SINGLE with GLUT_DOUBLE in glutInitDisplayMode
  • replace glFlush with glutSwapBuffers


来源:https://stackoverflow.com/questions/19753322/glut-is-working-fine-but-opengl-is-not-doing-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!