游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1733|回复: 4

拾取问题!急

[复制链接]

3

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2005-3-17 05:08:00 | 显示全部楼层 |阅读模式
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();
}

......................

190

主题

1801

帖子

2096

积分

金牌会员

Rank: 6Rank: 6

积分
2096
QQ
发表于 2005-3-18 00:33:00 | 显示全部楼层

Re:拾取问题!急

depth_buffer打开没

3

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2005-3-18 02:31:00 | 显示全部楼层

Re:拾取问题!急

打开了,在main loop里
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);

15

主题

74

帖子

74

积分

注册会员

Rank: 2

积分
74
发表于 2005-3-18 07:49:00 | 显示全部楼层

Re:拾取问题!急

一个是看深度缓存有没有激活,还有一个就是以下这段是否应该写在拾取中?
//glMatrixMode (GL_PROJECTION);
   glPopName();
   glPopMatrix ();
   glFlush ();

3

主题

9

帖子

9

积分

新手上路

Rank: 1

积分
9
 楼主| 发表于 2005-3-18 09:56:00 | 显示全部楼层

Re:拾取问题!急

什么意思呢?请问你觉得应该怎样该??
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-19 10:55

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表