我正在开发一个单独的棋盘游戏,每个方块有一个棋子,每个棋子可以有两种颜色如果我单击一个片段,相邻的四个片段(顶部、底部、左侧和右侧)都将变为下一个颜色。
我在检测鼠标被点击的位置时遇到了问题。
我有以下鼠标回调代码:
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
}
}
}
现在,你看到了吗,我刚刚用
glLoadName
把4放入缓冲区然而,当我在processHits
中取出数字时,我总是得到1。我知道这是因为获得命中的缓冲区的结构,但是这个结构是什么,我如何访问数字4?非常感谢你帮助我。
最佳答案
选择缓冲区的结构比这个复杂一些对于每次命中,由多个值组成的“命中记录”将附加到选择缓冲区。有关详细信息,请参见OpenGL FAQ中的Question 20.020在您的情况下,如果一次堆栈中只有一个名称,则命中记录将包含4个值,其中名称是第四个值因此,在processHits
函数中,您应该
for(i = 0; i < hits; i++) {
printf("hit n. %d ---> %d",i, *(buffer+4*i+3));
}
另外,名称缓冲区的大小也应该是原来的4倍。
关于c - 用鼠标单击时无法选择基元,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4207941/