Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: c4ooo on February 06, 2016, 04:39:05 pm

Title: What is wrong with my 3D code?
Post by: c4ooo on February 06, 2016, 04:39:05 pm
This code *should* draw a verticle line that rotates around the camera, but it does not.
Code: [Select]
.A
0->O
Repeate getKey(41)
ClrDraw
sin(O)->S
cos(O)->C
ROT(0,0,3,L1)
ROT(0,10,3,L1+4)
Line({L1}^r,{L1+2}^r,{L1+4}^r,{L1+6}^r)
O++
Pause 15
DispGraph
End
Return
.Rotate (r1,r2,r3) around the Y axis by O degrees; then project the point into 3D and store it to r4 as two 16 bit (signed) numbers.
Lbl ROT
.Z
r3*C-(r1*S)//128->D
.X
r3*S+(r1*C)//128//D+48->{r4}^r
.Y
r2//D+32->{r4+2}^r
Return
Title: Re: What is wrong with my 3D code?
Post by: ben_g on February 07, 2016, 08:36:59 pm
I tried messing around with it, but I couldn't get your current algorithm to work, so I tried it with a different one.
Code: [Select]
Lbl ROT
r1*C-(r3*S)//128->r5
r1*S+(R3*C)//128->r3
r5->r1
r1*48//r3+48->{r4}^^r
r2*48//r3+32->{r4}^^r
Return
If the sign bit of r3 is set, then the vertex was behind the camera and you shouldn't draw it. This code is just something that seemed to work for me, nothing more, so it's possible that it isn't the most optimized version and that there are still a few problems with it (especially since I programmed it at 2:00).
Keep in mind that a line of 10 units tall and only 3 units away from the camera requires a very wide field of view to be seen. I've tried a line of 10 units tall and 20 units away.

On irc, you also asked me what the 48 means at the start of the last 2 lines. This is a constant related to the screen's size and the FOV.
This is a sketch of the camera:
(http://img.codewalr.us/camera.png)
The angle between the black lines is the field of view (in this case 90°). The green line represents the screen, which is in our case 96 pixels wide. The blue distance, which is perpendicular to the screen, is the constant we need. With a field of view of 90°, this distance is equal to half of the screen width, which is 48.
I hope that this makes it clear. If not, feel free to ask and I'll try to improve my explanation tomorrow.
Title: Re: What is wrong with my 3D code?
Post by: c4ooo on November 11, 2016, 08:47:25 am
Looking back, the times 48 thing makes sense now :)