使用OpenGL和X11,我有一个更新X11窗口以创建动画的程序,但动画在几帧后挂起。我该如何解决?
我曾尝试过glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT)和XClearWindow,但无济于事。
完整的代码在这里:https://github.com/alexrockhill/V1Model但这里是与问题相关的部分:
int plot_network(nw)
struct network nw;
{
Display *dpy;
Window win;
Bool doubleBuffer = True;
XVisualInfo *vi = NULL;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
int screen_num, t_ind, ori_ind;
unsigned int display_width, display_height, size;
float delta;
static Bool displayListInited = False;
if(!(dpy = XOpenDisplay(NULL)))
fatalError("could not open display");
if(!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf))) {
if(!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), sngBuf)))
fatalError("no RGB visual with depth buffer");
doubleBuffer = False;
}
if(vi->class != TrueColor)
fatalError("TrueColor visual required for this program");
if(!(cx = glXCreateContext(dpy, vi, None, True)))
fatalError("could not create rendering context");
screen_num = DefaultScreen(dpy);
display_width = DisplayWidth(dpy, screen_num);
display_height = DisplayHeight(dpy, screen_num);
if (display_width < display_height) {
size = display_width*0.8;
} else {
size = display_height*0.8;
}
delta = 2.0/nw.dim;
cmap = XCreateColormap(dpy,RootWindow(dpy,vi->screen),vi->visual,AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy,RootWindow(dpy,vi->screen),0,0,size,size,
0,vi->depth,InputOutput,vi->visual,
CWBorderPixel | CWColormap | CWEventMask,
&swa);
XSetStandardProperties(dpy,win,"V1 Model","V1 Model",None,None,0,NULL);
glXMakeCurrent(dpy, win, cx);
XMapWindow(dpy, win);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,1.0,0.0,0.0,0.0,1.0);
t_ind = 0;
while(t_ind < nw.n_steps) {
XNextEvent(dpy, &event);
//XClearWindow(dpy, win);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
for(int i=0;i<nw.dim;i++){
for(int j=0;j<nw.dim;j++){
float v = 0;
for (int k=0; k < nw.oris; k++) {
v += nw.cols[i][j].ns[k].v[t_ind];
}
v /= (float) nw.oris;
float x = nw.cols[i][j].ns[0].x*delta-1;
float y = nw.cols[i][j].ns[0].y*delta-1;
draw_box(red(v), green(v), blue(v), x, y, delta, delta);
}
}
if(doubleBuffer)
glXSwapBuffers(dpy, win);
else
glFlush();
printf("Time: %i\n", t_ind);
usleep(500000);
t_ind++;
}
XCloseDisplay(dpy);
return(0);
}
我希望动画能够继续播放n_steps个时间点>> 7,但它只会在几(7)个动画更新后挂起。
问题可能是你使用XNextEvent
;例如,如果您生成的事件比渲染速度快,则无法清除事件队列。另一方面,如果事件队列中没有事件,您将在此功能中挂起,直到您执行此操作。
尝试
for(int i = 0; i < XEventsQueued(dpy, QueuedAlready); ++i) {
XNextEvent(dpy, &event);
// handle event here
}
代替。免责声明:因为我从未在生活中直接使用过Xlib
,所以要带上一粒盐。