The shoutbox is currently out of service. Join us on Discord instead.
You can help CodeWalrus stay online by donating here.

3D rendering of simple objects in TI-BASIC

Started by _iPhoenix_, May 15, 2017, 08:25:56 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

_iPhoenix_

May 15, 2017, 08:25:56 pm Last Edit: May 15, 2017, 08:55:26 pm by _iPhoenix_
*This is for the CE* (At least, I programmed it for and on the CE)


I've been working on this little sub-project (It's part of a larger thing), but have hit a roadblock. I need to be able to fill in the sides with a single color, and I need it to be somewhat fast. It should also cover up the edges that should be on the back. I'm thinking to use flood fill, but that is not exactly a fast way of doing it. I split the code into several programs for ease-of-development.


Sets the current object being drawn to a cube.
prgmOBJCUBE
Ans->|LTEMP
|LTEMP(1->A
|LTEMP(2->B
|LTEMP(3->C
|LTEMP(4->W
|LTEMP(5->H
|LTEMP(6->D
[[(A),(B),(C)][(A),(B),(C+D)][(A),(B+H),(C)][(A),(B+H),(C+D)][(A+W),(B),(C)][(A+W),(B),(C+D)][(A+W),(B+H),(C)][(A+W),(B+H),(C+D)]]->[A]
[[1,2][2,4][4,3][3,1][5,6][6,8][8,7][7,5][1,5][2,6][3,7][4,8]]->[B]



Sets the current object to a triangular prism
prgmOBJTRIP
Ans->|LTEMP
|LTEMP(1->A
|LTEMP(2->B
|LTEMP(3->C
|LTEMP(4->W
|LTEMP(5->H
|LTEMP(6->D
[[(A),(B),(C)][(A+W),(B),(C)][(A+W),(B),(C+D)][(A),(B),(C+D)][(A+W/2),(B+H),(C+D/2)]]->[A]
[[1,2][2,3][3,4][4,1][1,5][2,5][3,5][4,5]]->[B]



Sets the current object to a tetrahedron
prgmOBJTETR
Ans->|LTEMP
|LTEMP(1->A
|LTEMP(2->B
|LTEMP(3->C
|LTEMP(4->W
|LTEMP(5->H
|LTEMP(6->D
[[(A),(B),(C)][(A+W/2),(B),(C+D)][(A+W),(B),(C)][(A+W/2),(B+H),(C+D/2)]]->[A]
[[1,2][2,3][3,1][1,4][2,4][3,4]]->[B]



Lets you move independently of the object.
prgmMOVE
Ans->A
(A=0)-(A=2)->B
Xmin+B->Xmin
Xmax+B->Xmax
(A=1)-(A=3)->B
Ymin+B->Ymin
Ymax+B->Ymax



Scaling, used for 'movement' on the z axis.
prgmSCALE3D
Ans->theta
dim([A]->L1
For(A,1,L1(1
For(B,1,L1(2
theta*[A](A,B->[A](A,B
End
End



Translates the object, but interferes with rotation.
prgmTRANSL8
Ans->L1
L1(1->Z
dim([A]->L2
L2(1->T
For(A,1,T
[A](A,Z)+L1(2->[A](A,Z
End



Draws the object.
prgmDRAW3D
dim([B]->L1
For(E,1,L1(1
Line([A]([B](E,1),1),[A]([B](E,1),2),[A]([B](E,2),1),[A]([B](E,2),2),1,Black,1
End



Helpful info:
[A] is the list of Vertices, and B (the matrix, screw you, bbcode..) is the list of edges. A '1' in  would correspond to the first row/vertex, a '2' for the second, and so on.

Rotates, in 3 dimensions.
prgmROTATE:
[/b]Ans->L1
Ans(2->theta
sin(theta->A
cos(theta->B
dim([A]->L2
For(N,1,L2(1
[A](N,1->I
[A](N,2->J
[A](N,3->K
If 0=L1(1
Then
JB-KA->[A](N,2
KB+JA->[A](N,3
Else
If 1=L1(1
Then
IB-KA->[A](N,1
KB+IA->[A](N,3
Else
If 2=L1(1
Then
IB-JA->[A](N,1
JB+IA->[A](N,2
End
End
End
End


Renders, and lets you interact:
prgmRENDER3D
Disp "DIM. OF OBJECT:
Input "X: ",A
Input "Y: ",B
Input "Z: ",C


Input "OFFSETX: ",S
Input "OFFSETY: ",T
Input "OFFSETZ: ",U
ClrDraw:BackgroundOff
0->|N
//change object type by appending "CUBE", "TRIP", or "TETR" to the following line.
{S,T,U,A,B,C:prgmOBJ
prgmDRAW3D
Pause
ClrDraw
Repeat 0
Repeat Ans
getKey
End
Ans->F
//too lazy to optimize this, shouldn't really affect speed.
If F=73:Then
{0,pi/16:prgmROTATE:Else
If F=93:Then
{0,~pi/16:prgmROTATE:Else
If F=82:Then
{1,pi/16:prgmROTATE:Else
If F=84:Then
{1,~pi/16:prgmROTATE
End
End
End
End
If sum(F={43,52,54,63
Then
1(F=43)+2(F=52)+0(F=54)+3(F=63):prgmMOVE
End
If sum(F={21,31
Then
.8
If F=21
Ans^^-1
prgmSCALE3D
End
ClrDraw
prgmDRAW3D
End



Renders and slowly rotates the object (I'm really inspired by gLib's screenshots)
prgmROTATE3D
Disp "DIM. OF OBJECT:
Input "X: ",A
Input "Y: ",B
Input "Z: ",C


Input "OFFSETX: ",S
Input "OFFSETY: ",T
Input "OFFSETZ: ",U
ClrDraw:BackgroundOff
//change object type by appending "CUBE", "TRIP", or "TETR" to the following line.
{S,T,U,A,B,C:prgmOBJ
prgmDRAW3D
Pause
For(Z,0,1
{Z,pi/16:prgmROTATE
End
Repeat 0
{1,pi/64:prgmROTATE
ClrDraw
prgmDRAW3D
End



With my luck, I missed a program, but I need help :(


Most of the changes would be in DRAW3D, presumably, as that is where everything (so far) is drawn.


Controls (for RENDER3D):


Use the numberpad to rotate. 8 and 2 rotate on the 'y' axis, 4 and 6 rotate on the 'x' axis.
Use [2nd] to zoom in, [alpha] to zoom out (move closer and further)
Use [prgm] to move up, [sin] to move left, [tan] to move right, and [)] to move down.


I think that's it.
EDIT: No it isn't.
Instructions to run:
Press these exact buttons:
[zoom][6][zoom][5][prgm]
Then you can run the program (either RENDER3D or ROTATE3D). Be sure to change prgmOBJ to the selected shape before running!
Please spam here: https://legend-of-iphoenix.github.io/spam/

"walruses are better than tuxedo chickens, all hail the great :walrii:" ~ me
Evolution of my avatar:

TheMachine02

What you want to do is most likely triangle rasterisation. You can work with quadrilateral too but it is slighty harder. You'll two thing based on your program. First implement triangle support :it shouldnt be too hard. The algorithm to fill the triangle is based off the general idea to calculate new x endpoint and startpoint at each y coordinate. What is Nice is that you can compute by how much x coordinate vary when incrementing y by one and only deal with addition in inner loop. Drawing is a simple hline with whatever color you want (lightning ? :p)

_iPhoenix_

Quote from: TheMachine02 on May 18, 2017, 08:59:28 am
What you want to do is most likely triangle rasterisation. You can work with quadrilateral too but it is slighty harder. You'll two thing based on your program. First implement triangle support :it shouldnt be too hard. The algorithm to fill the triangle is based off the general idea to calculate new x endpoint and startpoint at each y coordinate. What is Nice is that you can compute by how much x coordinate vary when incrementing y by one and only deal with addition in inner loop. Drawing is a simple hline with whatever color you want (lightning ? :p)


That's a ton of help; thanks!

I have a semi-working algorithm, but memory leaks...

Of course this is the ONLY DAY IN ABOUT A YEAR I forgot to backup my calculator... which I usually do twice daily...

Silly me!
Please spam here: https://legend-of-iphoenix.github.io/spam/

"walruses are better than tuxedo chickens, all hail the great :walrii:" ~ me
Evolution of my avatar:


_iPhoenix_

No, I was working on it constantly for like 2 hours. I have the other programs in a group, so they are fine.
Please spam here: https://legend-of-iphoenix.github.io/spam/

"walruses are better than tuxedo chickens, all hail the great :walrii:" ~ me
Evolution of my avatar:

Powered by EzPortal