LoginLogin

3D projection

Root / Programming Questions / [.]

DFrostCreated:
could someone make a DEF block for perspective projection, please

DEF DRAW_WIRE_RECTPRISM
 GLINE 200-8,120-8,200+8,120-8,#RED
 GLINE 200-8,120+8,200+8,120+8,#RED
 GLINE 200-8,120-8,200-8,120+8,#RED
 GLINE 200+8,120-8,200+8,120+8,#RED
 
 GLINE 200-4,120-4,200+4,120-4,RGB(90,0,0)
 GLINE 200-4,120+4,200+4,120+4,RGB(90,0,0)
 GLINE 200-4,120-4,200-4,120+4,RGB(90,0,0)
 GLINE 200+4,120-4,200+4,120+4,RGB(90,0,0)
 
 GLINE 200-8,120-8,200-4,120-4,RGB(175,0,0)
 GLINE 200+8,120-8,200+4,120-4,RGB(175,0,0)
 GLINE 200-8,120+8,200-4,120+4,RGB(175,0,0)
 GLINE 200+8,120+8,200+4,120+4,RGB(175,0,0)
 END
Draws a tiny red "cube" shape. You can see the lines get darker making it look like it has depth

i want a function:
DEF PERSPECTIVE X,Y,Z OUT X2,Y2
'code
END

DEF PERSPECTIVE X,Y,Z,VP OUT X2,Y2
 X2=X/(Z-VP)
 Y2=Y/(Z-VP)
END
VP is the vanishing point distance. No "z" value should be bigger than this number and this number should be consistent for all calculations.

what about rotation?

This is the function I usually use for two-axis rotation.
DEF ROT_3D X,Y,Z,A1,A2 OUT X3,Y3,Z3
 VAR W=SIN(RAD(A1)),W2=SIN(RAD(A2))
 VAR H=COS(RAD(A1)),H2=COS(RAD(A2))
 VAR Z2=Z*H-X*W
 X3=Z*W+X*H
 Y3=Y*H2-Z2*W2
 Z3=Y*W2+Z2*H2
END
It's also faster if you calculate all the sin/cos calculation before you calculate all the point's locations.

Cool.

So I could pass XYZ into ROT_3D and use the output in PERSPECTIVE?