2010-11-17 123 views
2

我正在開發一種孤立的棋盤遊戲,每個方塊有一塊,每塊可以有兩種顏色。如果我點擊一塊,相鄰的四個(頂部,底部,左側和右側)都會變爲下一個顏色。通過鼠標不起作用點擊時的圖元選擇

我在檢測鼠標點擊哪件時遇到了問題。

我對鼠標回調下面的代碼:

GLuint selectBuf[BUFSIZE]; // BUFSIZE is defined to be 512 
GLint hits; 
GLint viewport[4]; 

if((state != GLUT_DOWN) && (button != GLUT_LEFT_BUTTON)) 
    return; 

glGetIntegerv (GL_VIEWPORT, viewport); 
glSelectBuffer (BUFSIZE, selectBuf); 
(void) glRenderMode (GL_SELECT); 
glInitNames(); 
glPushName(0); 
gluPickMatrix ((GLdouble) x, (GLdouble) y, 20.0,20.0, viewport); 

draw(GL_SELECT); // the function that does the rendering of the pieces 

hits = glRenderMode(GL_RENDER); 

processHits (hits, selectBuf); // a function that displays the hits obtained 

現在,我的問題是,我不太知道如何處理命中發生這些都對selectBuf。我有processHits以下代碼:

void processHits (GLint hits, GLuint buffer[]) 
{ 
    unsigned int i, j; 
    GLuint ii, jj, names, *ptr; 

    printf ("hits = %d\n", hits); 
    ptr = (GLuint *) buffer; 
    for(i = 0; i < hits; i++) { 
     printf("hit n. %d ---> %d",i, *(buffer+i)); 
    } 
} 

最後,在draw功能我:

void draw(GLenum mode) { 
    glClear (GL_COLOR_BUFFER_BIT); 
    GLuint x,y; 

    int corPeca; //colourpiece in english 
    int corCasa; //colourHouse (each square has a diferent color, like checkers) 
    for (x =0; x < colunas; x++) { //columns 
     for(y=0; y < colunas; y++) { 
      if ((tabuleiro[y*colunas+x].peca) == 1) //board 
       corPeca = 1; 
      else 
       corPeca = 2; 

      if((tabuleiro[y*colunas+x].quadrado)==1) //square 
       corCasa = 1; 
      else 
       corCasa = 2; 

      if (mode == GL_SELECT){ 
       GLuint name = 4; 
       glLoadName(name); 
      } 
      desenhaCasa(x,y,corCasa);  //draws square 
      desenhaPeca(x,y,corPeca, mode); //draws piece 
     } 
    } 
} 

現在,已經可以看到,我只是把4到緩衝區glLoadName。然而,當我把processHits中的數字取出時,我總是得到1.我知道這是因爲獲得命中的緩衝區的結構,但是該結構是什麼,我如何訪問數字4?

非常感謝您的幫助。

回答

0

選擇緩衝區的結構比這更復雜一點。對於每個命中,由多個值組成的「命中記錄」被附加到選擇緩衝器。有關詳細信息,請參閱OpenGL FAQ中的Question 20.020。在你的情況下,一次只有一個名字在堆棧中,命中記錄將包含4個值,名字是第四個。所以,在你processHits功能,你應該寫

for(i = 0; i < hits; i++) { 
    printf("hit n. %d ---> %d",i, *(buffer+4*i+3)); 
} 

而且,你的名字緩衝區的大小也許應該是4倍以上爲好。

相關問題