使用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)次动画更新后它总是会挂断。
答案 0 :(得分:2)
问题可能出在您使用XNextEvent
上;首先,如果您产生事件的速度比呈现事件的速度快,您将不会清除事件队列。另一方面,如果事件队列中没有事件,您将一直挂在此功能上,直到您这样做。
尝试
for(int i = 0; i < XEventsQueued(dpy, QueuedAlready); ++i) {
XNextEvent(dpy, &event);
// handle event here
}
相反。免责声明:带着一粒盐,因为我一生中从未直接使用过Xlib
。