光线投射的地板和天花板单线错误

时间:2018-11-16 15:37:48

标签: raycasting

我每个人都实施了此光线投射引擎教程https://www.instructables.com/id/Making-a-Basic-3D-Engine-in-Java/ 成功。

它似乎基于此站点的lode vandevenne教程中:

https://lodev.org/cgtutor/raycasting2.html

在上一个站点中,我已经实现了有关地板和单元格的所有代码,将变量重命名为与我自己的变量匹配(代码几乎相同)。 但是,当我尝试将地板纹理图像信息“发送”到像素(缓冲区)以便可以渲染时,我遇到了困难:

在原始代码中,此缓冲区是一个矩阵[x] [y],但是我的新代码是一个数组。看到的是原始的:

buffer [y] [x] =(纹理[3] [texWidth * floorTexY + floorTexX] >> 1)&8355711;

这是我的代码:

int flColor = textures.get(flNum).pixels [floorTexY + floorTexX];

像素[x + y *(宽度)] = flColor;

我先得到纹理“ id”,然后得到像素信息(有问题的部分),将其保存到int flColor中,然后将其发送到像素数组。(带有一些x / y乱码)。

假设我只是将x * y相乘以获得整个纹理信息,因为它是一个正方形。但是,如果我这样做,则会收到arrayoutofbounds错误。我应该忘记这种方法,而是尝试获取基于矩阵的缓冲区吗?

这里是我的整个地板铸造源代码:

//在这里铺地板? lode vandevenne教程...

                    double floorXWall, floorYWall; //x,y position of the 
                    floor texel at the bottom of the wall
                    // 4 different wall directions possible
                    if(side == 0 && rayDirX > 0)
                    {
                        floorXWall = mapX;
                        floorYWall = mapY + wallX;
                    }
                    else if(side == 0 && rayDirX < 0)
                    {
                      floorXWall = mapX + 1.0;
                      floorYWall = mapY + wallX;
                    }
                    else if(side == 1 && rayDirY > 0)
                    {
                      floorXWall = mapX + wallX;
                      floorYWall = mapY;
                    }
                    else
                    {
                      floorXWall = mapX + wallX;
                      floorYWall = mapY + 1.0;
                    }

                    double distWall, distPlayer, currentDist;

                    distWall = perpWallDist; // The distance from player to the first wall the ray hit
                    distPlayer = 0.0;

                    if (drawEnd < 0) drawEnd = height; //becomes < 0 when the integer overflows. height = screen height.

                    //draw the floor from drawEnd to the bottom of the screen
                    for(int y = drawEnd + 1; y < height; y++)
                    {
                      currentDist = height / (2.0 * y - height); //you could make a small lookup table for this instead

                      double weight = (currentDist - distPlayer) / (distWall - distPlayer);

                      double currentFloorX = weight * floorXWall + (1.0 - weight) * camera.xPos;//posX;
                      double currentFloorY = weight * floorYWall + (1.0 - weight) * camera.yPos;//posY;

                      int floorTexX, floorTexY;
                      floorTexX = (int)((currentFloorX * width) % width);
                      floorTexY = (int)((currentFloorY * height) % height);

                      // floor         
                      // buffer[y][x] = (texture[3][texWidth * floorTexY + floorTexX] >> 1) & 8355711;
                         int flColor = textures.get(flNum).pixels[floorTexY - floorTexX];
                         pixels [x + y * (width)]  = flColor;

                      // ceiling (symmetrical!)
                      // buffer[h - y][x] = texture[6][texWidth * floorTexY + floorTexX];



                    } //FLOOR AND CEILING END

0 个答案:

没有答案