【发布时间】:2014-10-30 17:02:55
【问题描述】:
我希望使用 pthread 库来渲染纹理。这是我希望通过更新纹理的线程渲染的函数。最初,我的纹理通过初始化函数加载并由显示函数渲染(都在主线程中)。
void *moveMap(void *x)
{
printf("move called\n");
tim.tv_sec = 0;
tim.tv_nsec = 500000000;
glBindTexture(GL_TEXTURE_2D, texName[0]);
int index;
for(index=pathLength;index>=0;index--)
{
printf("Go %d %d\n", Path[index][0], Path[index][1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, Path[index][0]*20, Path[index][1]*20, surface->w, surface->h, GL_RGB,GL_UNSIGNED_BYTE, surface->pixels);
display();
}
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
pressedXCoord=x/20;
pressedYCoord=y/20;
printf("Pressed on %d %d\n",x/20,y/20);
}
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
releasedXCoord=x/20;
releasedYCoord=y/20;
printf("Released on %d %d\n",x/20,y/20);
findPath(pressedXCoord,pressedYCoord,releasedXCoord,releasedYCoord);
if(pthread_create(&inc_x_thread, NULL, moveMap, NULL)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}
}
findpath 函数基本上计算两个坐标之间的最短路径,函数 moveMap 通过该路径更新纹理。当我调用没有线程的 moveMap 函数时,纹理会根据需要更新,但通过线程调用时不会。
【问题讨论】:
-
openGL 不做多线程,除非你跳过很多圈
-
那么有什么办法可以解决这个问题呢?有没有什么方法可以通过线程控制glutMouseFunc调用的鼠标函数。
-
为什么要在单独的线程中?
-
因为我希望我的鼠标功能每次都处于活动状态。它进入 moveMap 的那一刻,我必须等待它完成。
-
@MohitJain 我写过关于固定时间间隔的文章是有原因的。您完成它的方式 - 移动速度将取决于帧速率,而不是经过的时间。您需要保存运动信息并以固定的时间间隔执行步骤,而线程只会增加更多问题。
标签: c multithreading opengl pthreads