gpt4 book ai didi

c - 单击鼠标时选择基元不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:04 27 4
gpt4 key购买 nike

我正在开发一款单人棋盘游戏,每格有一个棋子,每个棋子可以有两种颜色。如果我点击一个棋子,四个相邻的棋子(上、下、左、右)都会变成下一种颜色。

我在检测鼠标点击的部分时遇到问题。

我有以下鼠标回调代码:

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?

非常感谢你帮助我。

最佳答案

选择缓冲区的结构比这复杂一点。对于每次命中,包含多个值的“命中记录”将附加到选择缓冲区。你可以看看Question 20.020在 OpenGL 常见问题解答中了解详细信息。在您的情况下,堆栈中一次只有一个名称,命中记录将包含 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com