2014-10-30 68 views
0

我希望使用pthread庫渲染紋理。這裏是我希望通過線程渲染紋理的函數。最初,我的紋理通過init函數加載,並通過顯示函數呈現(均在主線程中)。Opengl在輔助線程中渲染(紋理)

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通過path.When我打電話的要求,但沒有線程質地moveMap功能更新通過線程調用時它不更新的紋理。

+0

除非你通過跳躍的openGL沒有做多線程很多環節 – 2014-10-30 17:08:14

+0

那麼可以解決這個問題呢? glutMouseFunc調用的鼠標函數是否可以通過線程來控制? – 2014-10-30 17:14:49

+0

爲什麼你想要它在單獨的線程? – keltar 2014-10-30 17:36:30

回答

0

OpenGL對多線程無法正常工作,它預計所有調用都來自單個線程。

void idleCopy(void){ 
    if(copyIndex==0)return; 

    printf("Go %d %d\n", Path[copyIndex][0], Path[copyIndex][1]); 
    glTexSubImage2D(GL_TEXTURE_2D, 0, Path[copyIndex][0]*20, Path[copyIndex][1]*20, surface->w, surface->h, GL_RGB,GL_UNSIGNED_BYTE, surface->pixels); 
    glutPostRedisplay();  
    copyIndex--; 
    if(copyIndex==0)glutIdleFunc(null);//disable callback 

} 

和鼠標():

您可以同時使用一個glutIdleFunc讓它給位複製

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); 
    glutIdleFunc(&idleCopy); 
}