当我在windows7的代码块中运行一个glut项目时,我的OpenGL glFlush()没有显示任何东西。
#include <windows.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
float Color1=0.0, Color2=0.0, Color3=0.0;
int r,p,q;
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27: // ESCAPE key
exit (0);
break;
case 'r':
Color1=1.0, Color2=0.0, Color3=0.0;
break;
case 'g':
Color1=0.0, Color2=1.0, Color3=0.0;
break;
case 'b':
Color1=0.0, Color2=0.0, Color3=1.0;
break;
}
glutPostRedisplay();
}
void Init(int w, int h)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport(0,0, (GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D( (GLdouble)w/-2,(GLdouble)w/2, (GLdouble)h/-2, (GLdouble)h/2);
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int i=0;
glColor4f(0,0,0,1);
glPointSize(1);
glBegin(GL_POINTS);
for( i=-320;i<=320;i++)
glVertex2f(i,0);
for( i=-240;i<=240;i++)
glVertex2f(0,i);
glEnd();
glColor4f(Color1,Color2, Color3,1);
glPointSize(1);
glBegin(GL_POINTS);
int x=0, y = r;
int d= 1-r;
while(y>=x)
{
glVertex2f(x+p, y+q);
glVertex2f(y+p, x+q);
glVertex2f(-1*y+p, x+q);
glVertex2f(-1*x+p, y+q);
glVertex2f(-1*x+p, -1*y+q);
glVertex2f(-1*y+p, -1*x+q);
glVertex2f(y+p, -1*x+q);
glVertex2f(x+p, -1*y+q);
if(d<0)
d += 2*x + 3;
else
{
d += 2*(x-y) + 5;
y--;
}
x++;
}
glEnd();
glFlush();
//glutSwapBuffers();
}
int main(int argc, char *argv[])
{
printf("Enter the center point and radius: ");
scanf("%d %d %d",&p,&q,&r);
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Circle drawing");
Init(640, 480);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}但是,当我更改这两行代码时,它工作得很好。
glFlush();glutSwapBuffers();glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
谁能告诉我我的代码出了什么问题,为什么glFlush()不能工作?
发布于 2018-05-04 02:41:38
现代图形系统(Windows DWM/Aero、MacOS Quartz Extreme、X11复合)都是围绕合成的概念构建的。合成总是意味着双缓冲,因此依赖于缓冲器交换来启动合成刷新。
您可以在Windows上禁用DWM/Aero,并限制在X11上使用合成窗口管理器,然后单缓冲OpenGL应该可以按预期工作。
但是你到底为什么想要单缓冲绘图呢?现代GPU实际上假定使用双缓冲来有效地泵送它们的表示流水线。单缓冲没有任何好处。
发布于 2018-05-04 03:00:24
glFlush works as documented
The glFlush function forces execution of OpenGL functions in finite time.它的作用是强制所有未完成的OpenGL操作完成对后台缓冲区的呈现。这将不会神奇地显示后台缓冲区。为此,您需要交换字体缓冲区和后台缓冲区。
因此,正确使用glFlush是与glutSwapBuffers结合使用的。但这是多余的,因为glutSwapBuffers无论如何都会刷新所有未完成的渲染操作。
看起来您使用的是一个旧的OpenGL 1.1教程,其中双缓冲是一个昂贵的新奇事物。目前,双缓冲是常态,您需要经历一些相当昂贵的循环才能获得单缓冲。
由于OpenGL目前的版本是4.6,我鼓励您至少开始使用4.0。
https://stackoverflow.com/questions/50161822
复制相似问题