Mr_Nudge
21st April 2007 09:28 UTC
rotation matricies for triangle ape
okay i need help with projecting a 2d square made with tl's triangle ape into 3d. i have the code for the square but i can't figure out how to 3d it.
anyone care to explain it/post up some code?
cheers.
PAK-9
24th April 2007 18:26 UTC
Assuming you already know how to describe a point in 3 dimensions, you simply need to extend that idea to describing the vertices of a triangle. That is, you need to define the three vertices of a triangle, then rotate and project each vertex independantly to get your final x1,y1 etc..
A simple framework for 3D triangles looks something like this:
xs1=0;
xs2=0;
xs3=0;
ys1=0;
ys2=0;
ys3=0;
zs1=0;
zs2=0;
zs3=0;
xf2=xs1*crotz-ys1*srotz;
yf2=xs1*srotz+ys1*crotz;
xf3=xf2*croty+zs1*sroty;
zf3=-xf2*sroty+zs1*croty;
yf1=yf2*crotx-zf3*srotx;
zf1=yf2*srotx+zf3*crotx;
zf2=1/(zf1+d);
x1=xf3*zf2*asp;
y1=yf1*zf2;
xf2=xs2*crotz-ys2*srotz;
yf2=xs2*srotz+ys2*crotz;
xf3=xf2*croty+zs2*sroty;
zf3=-xf2*sroty+zs2*croty;
yf1=yf2*crotx-zf3*srotx;
zf1=yf2*srotx+zf3*crotx;
zf2=1/(zf1+d);
x2=xf3*zf2*asp;
y2=yf1*zf2;
xf2=xs3*crotz-ys3*srotz;
yf2=xs3*srotz+ys3*crotz;
xf3=xf2*croty+zs3*sroty;
zf3=-xf2*sroty+zs3*croty;
yf1=yf2*crotx-zf3*srotx;
zf1=yf2*srotx+zf3*crotx;
zf2=1/(zf1+d);
x3=xf3*zf2*asp;
y3=yf1*zf2;
(xs1,ys1,zs1) and the like are vertices
srot# = sin of rotation about # axis
crot# = cos of rotation about # axis
d = projection z offset (1 is fine)
asp = aspect ratio (h/w or whatever)
Mr_Nudge
25th April 2007 08:40 UTC
so you do have to do the transformations for all three? i ended up doing that too i just wasn't sure if it was the most efficient way.
i came up with this
s=(s+1)%2;
ix1=-1+s*2;
iy1=-1;
iz1=0;
x2a=ix1*reg05+iz1*reg04;
y2a=ix1*reg04-iz1*reg05;
z2a=x2a*reg02+iy1*reg03;
x3b=x2a*reg03-iy1*reg02;
y3b=y2a*reg01+z2a*reg00;
z3b=y2a*reg00-z2a*reg01;
z1=1/(z3b+6);
ix2=1;
iy2=-1+s*2;
iz2=0;
x2a2=ix2*reg05+iz2*reg04;
y2a2=ix2*reg04-iz2*reg05;
z2a2=x2a2*reg02+iy2*reg03;
x3b2=x2a2*reg03-iy2*reg02;
y3b2=y2a2*reg01+z2a2*reg00;
z3b2=y2a2*reg00-z2a2*reg01;
z2=1/(z3b2+6);
ix3=-1;
iy3=1;
iz3=0;
x2a3=ix3*reg05+iz3*reg04;
y2a3=ix3*reg04-iz3*reg05;
z2a3=x2a3*reg02+iy3*reg03;
x3b3=x2a3*reg03-iy3*reg02;
y3b3=y2a3*reg01+z2a3*reg00;
z3b3=y2a3*reg00-z2a3*reg01;
z3=1/(z3b3+6);
x1=x3b*z1;
y1=y3b*z1*reg06;
x2=x3b2*z2;
y2=y3b2*z2*reg06;
x3=x3b3*z3;
y3=y3b3*z3*reg06;
were reg00-05 are cos/sin stuff and reg06 is aspect correction.
PAK-9
25th April 2007 12:48 UTC
Originally posted by Mr_Nudge
so you do have to do the transformations for all three? i ended up doing that too i just wasn't sure if it was the most efficient way.
Superficially the code you have looks fine. Yes you have to rotate and project all three since they are different points in 3D space, the fact that they happen to be the vertices of a triangle is incidental. There arent any more efficient ways that I know of unless you are dealing with special cases.
Often with complicated triangle presets I have a loop somewhere which calculates all the vertices for the model or shape, rotates and projects them then stores the final x,y for each vertex in the gmegabuf. That way in the triangle APE I can just loop through the gmegabuf spitting out vertices. For a simple square thats obviously a bit excessive but I imagine you had grander preset visions for the future.
Mr_Nudge
26th April 2007 14:18 UTC
thanks pak. the square was actually the last thing i added to a preset thats pretty much finished now. it'll be in v10 when thats released, which shouldn't be too far away cause i only need an intro preset and re-mixes. i'm bundling all the presets together into a zip tonight. if you want to see what i've been up to or even make a remix or two i can mail you the zip.
PAK-9
27th April 2007 12:29 UTC
Sure, mail it to me and I'll take a look (pak.nine on gmail)