|
|
Hi,
因为是初学者,我在作一个简单3D的五子棋,现在要在棋盘上放棋子,可是怎么也不出来,请哪位高手指点一下,我可以得到他的位置了,只是为什么却不出现棋子呢?
...............
void draw_pieces(void)
{
GLfloat white[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat black[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
GLfloat *color;
int x,z;
glPushAttrib(GL_LIGHTING | GL_TEXTURE_BIT);
glPushAttrib(GL_TEXTURE_BIT);
glEnable(GL_TEXTURE_2D);
for (x = 0; x < 11; x++)
for (z = 0; z < 11; z++)
{
if (board[x][z] != -1)
{
color = board[x][z] == 0 ? white : black;
glColor4fv(color);
glPushMatrix();
glTranslatef(z * 2.0 - 10, 0.0, 10 - x * 2.0);
glCallList(piece);
glPopMatrix();
}
}
glPopAttrib();
}
void buildBoard(void)
{
GLuint i, j;
int sq;
board_list = glGenLists(1);
glNewList(board_list, GL_COMPILE);
glPushAttrib(GL_TEXTURE_BIT);
glEnable(GL_TEXTURE_2D);
for (i = 0; i < 11; i++) {
//if (mode==GL_SELECT)
glLoadName (i + BOARD_NAME_OFFSET);
for (j = 0; j < 11; j ++) {
//if(mode==GL_SELECT)
glPushName (j + BOARD_NAME_OFFSET);
sq = (i * 11) + j;
if (sq % 11 == 0)
{
if (sq == 0)
glBindTexture(GL_TEXTURE_2D, textures[3]);
else
if (sq == 110)
glBindTexture(GL_TEXTURE_2D, textures[0]);
else
glBindTexture(GL_TEXTURE_2D, textures[7]);
}
else
if (sq % 11 == 10)
{
if (sq == 10)
glBindTexture(GL_TEXTURE_2D, textures[2]);
else
if (sq == 120)
glBindTexture(GL_TEXTURE_2D, textures[1]);
else
glBindTexture(GL_TEXTURE_2D, textures[5]);
}
else
if (sq > 0 && sq < 10)
glBindTexture(GL_TEXTURE_2D, textures[6]);
else
if (sq > 110 && sq < 120)
glBindTexture(GL_TEXTURE_2D, textures[4]);
else
glBindTexture(GL_TEXTURE_2D, textures[8]);
/* below translating scheme centers the board in the scene */
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(j * 2.0f - 11.0f, 0.0f, 11.0f - i * 2.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(j * 2.0f - 9.0f, 0.0f, 11.0f - i * 2.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(j * 2.0f - 9.0f, 0.0f, 9.0f - i * 2.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(j * 2.0f - 11.0f, 0.0f, 9.0f - i * 2.0f);
glEnd();
//if(mode==GL_SELECT)
glPopName ();
}
}
glPopAttrib();
glEndList();
printf("board_list= %d\n", board_list);
}
/* processHits prints out the contents of the
* selection array.
*/
void processHits (GLint hits, GLuint buffer[])
{
GLint i;
GLuint j, ii = 0, jj = 0, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++)
{
/* for each hit */
names = *ptr;
printf (" number of names for this hit = %d\n", names); ptr++;
printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr - BOARD_NAME_OFFSET); /* must subtract this to get true [x][y] coordinate */
if (j == 0) /* set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
ptr++;
}
printf ("\n");
/*board[ii][jj] = (board[ii][jj] + 1) % 3;*/
}
}
/* pickSquares() sets up selection mode, name stack,
* and projection matrix for picking. Then the
* objects are drawn.
*/
#define BUFSIZE 512
void pickSquares(int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
//(void) mouseButtonAction();
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 2x2 pixel picking region near cursor location*/
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),1.0, 1.0, viewport);
gluPerspective(40.0, (GLfloat) viewport[2] / (GLfloat) viewport[3],1.0, 1000.0);
//buildBoard(GL_SELECT);
glCallList(board_list);
//glMatrixMode (GL_PROJECTION);
glPopName();
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
...................... |
|