2010-11-17 119 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倍以上为好。

相关问题