0

I'm currently working on a project about 3D rendering, and I'm trying to make simplistic program that can display a simple 3D room (static shading, no player movement, only rotation) with pygame

So far I've worked through the theory:

  • Start with a list of coordinates for the X and Z of each "Node"
  • Nodes are kept in an order which forms a closed loop, so that a pair of nodes will form either side of a wall
  • The height of the wall is determined when it is rendered, being relative to distance from the camera
  • Walls are rendered using painter's algorithm, so closer objects are drawn on top of further ones
  • For shading "fake contrast", which brightens/darkens walls based on the gradient between it's two nodes

While it seems simple enough, the process behind translating the 3D coordinates into 2D points on the screen is proving the difficult for me to understand.

Googling this topic has so far only yeilded these equations:

screenX = (worldX/worldZ)

screenY = (worldY/worldZ)

Which seem flawed to me, as you would get a divide by zero error if any Z coordinate is 0.

So if anyone could help explain this, I'd be really greatful.

4
  • Maybe some of Bisqwit's video might help you. This one maybe? youtube.com/watch?v=HQYsFshbkYw
    – simon
    Dec 16, 2016 at 21:07
  • Thanks for the video, but I don't really feel that it's helpful for explaining how the code works, just the general concepts behind it. I got that the camera never actually moves, but after that I get lost, and the code isn't much help because the variables aren't labeled in any meaningful way.
    – user2883136
    Dec 16, 2016 at 21:16
  • Although StackOverflow isn't meant for providing links to tutorials, this question really has already been addressed many times on other sites, such as this one I found which has examples of the code you'd need (in Java, but easy to understand): instructables.com/id/Making-a-Basic-3D-Engine-in-Java/?ALLSTEPS. Step 4 goes over the actual rendering. Dec 16, 2016 at 21:23
  • There's an "old" book you might want to check out: amazon.com/Microcomputer-Displays-Graphics-Animation-Artwick/dp/… Dec 16, 2016 at 22:47

1 Answer 1

1

Well the

screenX = (worldX/worldZ)
screenY = (worldY/worldZ)

is not the whole stuff that is just the perspective division by z and it is not meant for DOOM or Wolfenstein techniques.

Well in Doom there is only single angle of viewing (you can turn left/right but cannot look up/down only duck or jump which is not the same). So we need to know our player position and direction px,py,pz,pangle. The z is needed only if you want to implement also z axis movement/looking...

If you are looking in a straight line (Red) all the object that cross that line in the 3D are projected to single x coordinate in the player screen...

line of sight

So if we are looking at some direction (red) any object/point crossing/touching this red line will be place at the center of screen (in x axis). What is left from it will be rendered on the left and similarly whats on right will be rendered on the right too...

With perspective we need to define how large viewing angle we got...

FOVx

This limits our view so any point touches the green line will be projected on the edge of view (in x axis). From this we can compute screen x coordinate sx of any point (x,y,z) directly:

// angle of point relative to player direction
sx = point_ang - pangle;
if (sx<-M_PI) sx+=2.0*M_PI;
if (sx>+M_PI) sx-=2.0*M_PI;
// scale to pixels
sx = screen_size_x/2 + sx*screen_size_x/FOVx

where screen_size_x is resolution of our view area and point ang is angle of point x,y,z relative to origin px,py,pz. You can compute it like this:

point_ang = atan2(y-py,x-px)

but if you truly do a DOOM ray-casting then you already got this angle.

Now we need to compute the screen y coordinate sy which is dependent on the distance from player and wall size. We can exploit triangle similarity.

FOVy

so:

sy = screen_size_y/2 (+/-) wall_height*focal_length/distance

Where focal length is the distance at which wall with 100% height will cover exactly the whole screen in y axis. As you can see we dividing by distance which might be zero. Such state must be avoided so you need to make sure your rays will be evaluated at the next cell if standing directly on cell boundary. Also we need to select the focal length so square wall will be projected as square.

Here a piece of code from mine Doom engine (putted all together):

double divide(double x,double y)
    {
    if ((y>=-1e-30)&&(y<=+1e-30)) return 0.0;
    return x/y;
    }
bool Doom3D::cell2screen(int &sx,int &sy,double x,double y,double z)
    {
    double a,l;
    // x,y relative to player
    x-=plrx;
    y-=plry;
    // convert z from [cell] to units
    z*=_Doom3D_cell_size;
    // angle -> sx
    a=atan2(y,x)-plra;
    if (a<-pi) a+=pi2;
    if (a>+pi) a-=pi2;
    sx=double(sxs2)*(1.0+(2.0*a/view_ang));
    // perpendicular distance -> sy
    l=sqrt((x*x)+(y*y))*cos(a);
    sy=sys2+divide((double(plrz+_Doom3D_cell_size)-z-z)*wall,l);
    // in front of player?
    return (fabs(a)<=0.5*pi);
    }

where:

_Doom3D_cell_size=100; // [units] cell cube size
view_ang=60.0*deg;     // FOVx
focus=0.25;            // [cells] view focal length (uncorrected)
wall=double(sxs)*(1.25+(0.288*a)+(2.04*a*a))*focus/double(_Doom3D_cell_size); // [px] projected wall size ratio size = height*wall/distance
sxs,sys = screen resolution
sxs2,sys2 = screen half resolution
pi=M_PI, pi2=2.0*M_PI

Do not forget to use perpendicular distances (multiplied by cos(a) as I did) otherwise serious fish-eye effect will occur. For more info see:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.