Pergunta

I watched TheCherno's game programming tutorial:
http://www.youtube.com/watch?v=RKPEQfkhbAY

And in this episode he wrote this code to make the 3d world.

package game.display.graphics;

public class Render3D extends Render {

    public Render3D(int width, int height) {
        super(width, height);
    }

    public void floor() {
        for(int y = 0; y < height; y++) {
            double ydepth = y - height / 2;
            double z = 100.0 / ydepth;

            for(int x = 0; x < width; x++) {
                double xdepth = x - width / 2;
                xdepth *= z;
                int xx = (int) (xdepth) & 5;

                pixels[x+y*width] = xx * 128;
            }
        }
    }
}

I don't really understand the code.. so someone can explain it to me?

Foi útil?

Solução

I still think the calculations themselves are not relevant, it just seems like some experiment (especially with the screens from the video in mind).

However, I'll try and explain what I think the code does (without more information it's not that easy):

First, he loops over all screen pixels and calculates a color for those.

  • double ydepth = y - height / 2; would cause the depth to be negative for all pixels at the top half of the screen
  • double xdepth = x - width / 2; would cause the depth to be negative for the left half of the screen

This seems to be done in order to center the generated pattern within the screen. There are those 2 black segments (lower left and upper right), but the code you posted doesn't explain what is done here.

int xx = (int) (xdepth) & 5; would set the value of xx to 0 (i.e. black) whenever neither bits 1 nor 3 (5 is binary 101) are set. Thus xx can only have the values 0, 1, 4 and 5 which would cause those black streaks.

xx * 128 would boost the value to 0, 128, 512 and 640, which would result in a different depending on how pixels is used. (If pixels represents colors in ARGB format, 0 would be black, 128 would be dark blue, 512 would be dark green (almost black) and 640 would also be dark blue with some slight green component.

That being said, I just skimmed over the code and did some example calculations where necessary. For more information just follow that trail.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top