текстурный движок испорчен - PullRequest
0 голосов
/ 09 ноября 2019

я создаю движок для радиопередачи, и текстуры испорчены, полная текстура не отображается, а сверху есть черная полоса

Я попытался использовать текстурный код в этом уроке -> https://lodev.org/cgtutor/raycasting.html

картина того, что происходит -> https://imgur.com/8yQkKSV

Я новичок в создании движков для радиопередачи, и я не очень понимаю алгоритм, так что я не уверен, что сказать

и это мало что дало

код:

         for(int x = 0;x<Width;x++) 
         {
             double camx = 2 * x / (double)(Width) - 1;
             double raydirx = player.dirx + player.xplane * camx;
             double raydiry = player.diry + player.yplane * camx;

             int mapx = (int)player.posx;
             int mapy = (int)player.posy;

             if(mapx < 0) 
             {
                 mapx = 0;
             }
             if(mapx > MapWidth) 
             {
                 mapx = MapWidth - 1;
             }
             if(mapy < 0) 
             {
                 mapy = 0;
             }
             if(mapy > MapHeight) 
             {
                 mapy = MapHeight - 1;
             }
             double sidedistx;
             double sidedisty;

             double deltadistx = Math.sqrt(1 + (raydiry * raydiry) / (raydirx * raydirx));
             double deltadisty = Math.sqrt(1 + (raydirx * raydirx) / (raydiry * raydiry));
             double perpwalldist;

             int stepx,stepy;

             boolean hit = false;

             int side = 0;

             if(raydirx < 0) 
             {
                 stepx = -1;
                 sidedistx = (player.posx - mapx) * deltadistx; 
             }
             else 
             {
                 stepx = 1;
                 sidedistx = (mapx + 1.0 - player.posx) * deltadistx;
             }
             if(raydiry < 0) 
             {
                 stepy = -1;
                 sidedisty = (player.posy - mapy) * deltadisty;
             }
             else 
             {
                 stepy = 1;
                 sidedisty = (mapy + 1.0 - player.posy) * deltadisty;
             }
             while(!hit) 
             {
                 if(sidedistx < sidedisty) 
                 {
                     sidedistx += deltadistx;
                     mapx += stepx;

                     side = 0;
                 }
                 else 
                 {
                     sidedisty += deltadisty;
                     mapy += stepy;
                     side = 1;
                 }
                 try 
                 {
                     if(mapx < 0) 
                     {
                         mapx = 0;
                     }
                     if(mapx > MapWidth) 
                     {
                         mapx = MapWidth;
                     }
                     if(mapy < 0) 
                     {
                         mapy = 0;
                     }
                     if(mapy > MapHeight) 
                     {
                         mapy = MapHeight;
                     }
                     if(map[mapx][mapy] > 0) 
                     {
                         hit = true;
                     }
                 }
                 catch(ArrayIndexOutOfBoundsException e) 
                 {
                     System.out.println("MapX:" + mapx + " MapY:" + mapy + " MapW:" + map[0].length + " MapH" + map[1].length);
                 }
             }
             if(side == 0) 
             {
                 perpwalldist = Math.abs((mapx - player.posx + (1 - stepx) / 2) / raydirx);
             }
             else 
             {
                 perpwalldist = Math.abs((mapy - player.posy + (1 - stepy) / 2) / raydiry);
             }

             int lineheight;
             if(perpwalldist > 0) 
             {
                 lineheight = Math.abs((int)(Height / perpwalldist));
             }
             else 
             {
                 lineheight = Height;
             }
             int drawstart = -lineheight / 2 + Height / 2;
             if(drawstart < 0) 
             {
                 drawstart = 0;
             }
             int drawend = lineheight / 2 + Height / 2;
             if(drawend >= Height) 
             {
                 drawend = Height - 1;
             }
             int texnum = map[mapx][mapy] - 1;
             double wallx;
             if(side == 1) 
             {
                 wallx = (player.posx + ((mapy - player.posy + (1 - stepy) / 2) / raydiry) * raydirx);
             }
             else 
             {
                 wallx = (player.posy + ((mapx - player.posx + (1 - stepx) / 2) / raydirx) * raydiry);
             }
             wallx -= Math.floor(wallx);
             int texx = (int)(wallx * Textures.get(texnum).Width);
             if(side == 0 && raydirx > 0) 
             {
                 texx = Textures.get(texnum).Width - texx - 1;
             }
             if(side == 1 && raydiry < 0) 
             {
                 texx = Textures.get(texnum).Width - texx - 1;
             }
             for(int y = drawstart; y < drawend;y++) 
             {

                 int texy = (((y* 2 - Height + lineheight) << 6) / lineheight) / 2;
                 int color = 0;
                 if(side == 0) 
                 {
                     int index = texx + (texy * 16);
                     if(index > Textures.get(texnum).Pixels.length - 1 || index < 0) 
                     {
                         index = 0;
                     }
                     color = Textures.get(texnum).Pixels[index];
                 }
                 else 
                 {
                     int index = texx + (texy * Textures.get(texnum).Width);
                     if(index > Textures.get(texnum).Pixels.length - 1 || index < 0) 
                     {
                         index = 0;
                     }
                     color = (Textures.get(texnum).Pixels[index] >> 1) & 8355711;


                 }
                 pixels[x + y * (Width)] = color;
             }
...