Author Topic: [z80 ASM] Unnamed set of 3D routines  (Read 8862 times)

0 Members and 4 Guests are viewing this topic.

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
[z80 ASM] Unnamed set of 3D routines
« on: July 04, 2013, 04:49:13 am »
<cross-posted from Cemetech>

Hallaw people,

I finally got something related with 3D working in ASM ! :D <this is a bit like a dream coming true for me :P>

Well, nothing too unbelievable, but I'm happy with what I have so far :)

So, I wrote 3 main routines, and a few other ones you can use, but these latter are not focused on 3D.

Main 3D routines :
  • BuildMatrix : builds the XY composite rotation matrix given the rotation angle about X and Y axis, respectively in b and c. The advantage is that you can rotate several vertices about the same angle without recalculating the rotation for each vertex.
  • ApplyMatrix : applies the matrix stored from AX to CZ (some .db in the program) to the three points stored at inX, inY and inZ and stores the result at outX, outY and outZ (more .dw). This is not necessarily a rotation matrix, but BuildMatrix writes to this area.
  • ProjectVertex : does the necessary calculation to project a vertex on-screen. Takes outX, outY and outZ as parameters and outputs at outX and outY. outZ remains unchanged.
Others routines provided (only because they are used by the 3 above routines) :
  • multHL_DE : performs HL*DE and stores the result in HL.
  • divHL64 : I let you guess.
  • mulHL64 : ↑ this.
  • AextendSignDE : converts the 8-bits signed number in A to a 16-bits signed number in DE.
  • getSin, getCos : retrieves the entry corresponding to the angle A in the trig look-up table (1-byte entries scaled to 64, so it goes from -64 to 64).
  • HLsignedDivDE : performs signed HL/DE, result in HL.
  • HLdivDE : performs unsigned HL/DE, result in HL (by Xeda112358).
Screenshot ? Yes, definitely :P



If you have any comment on the routines I described before I release the source, please tell me :)
« Last Edit: July 04, 2013, 05:03:43 am by Matrefeytontias »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #1 on: July 04, 2013, 05:06:09 am »
Ui, that's looking nice!
So much 3D dev for the z80 recently ^^
* Sorunome wonders when there will be a 3D basic lib

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline tr1p1ea

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 647
  • Rating: +110/-0
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #2 on: July 04, 2013, 05:22:11 am »
Looks great! Im glad you got it working :).

Do you have anything in mind for this project?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."


Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #3 on: July 04, 2013, 05:32:41 am »
Not really, it's just something I really wanted to do, but I didn't have the knowledge to do it - until now :)

I hope I'll be able to reach solid drawing, but it's still something reaaally far away x)

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #4 on: July 04, 2013, 05:34:48 am »
Don't forget to add textures :P

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #5 on: July 04, 2013, 05:35:40 am »
Eeeeeeeeeeeeeeeeeeeer no :P

Well, if that will ever be, it'll be waaaaaaaaaaaaaaaaaaaay later. I'm not sure if I will still be alive at this time ;D

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #6 on: July 04, 2013, 06:41:07 am »
Oh, the signed division wasn't working? It worked for me when I tested it and it was faster than regular division (sometimes a lot faster)

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #7 on: July 04, 2013, 06:43:37 am »
It wasn't working for me, so I got back to the first option (I optimized a bit the abs part tho).

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #8 on: July 04, 2013, 06:44:59 am »
Hmm, what were you passing and what was it returning? It was definitely working for me.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #9 on: July 04, 2013, 09:43:29 am »
Looks really great! I wonder how fast it is with lines connecting each dot?

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #10 on: July 04, 2013, 12:33:12 pm »
@Xeda112358 : I used it in the ProjectVertex routine, it was all weird, I can't really say since it was 2 days ago. I'll test again.

@DJ Omnimaga : can't really say, I'm afraid that this will be a bit slow though :/ I'll make a test with MirageOS's fastline routine.

EDIT : btw, I already use MirageOS's fastcopys routine. IDK if this has any effect on the speed (it has IIRC).
« Last Edit: July 04, 2013, 12:33:45 pm by Matrefeytontias »

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #11 on: July 04, 2013, 12:38:15 pm »
Okay, because the only problem area that I could find was pointed out by Jacobly earlier (if HL=8000h it will return a wrong result that is negative the real answer). The fix is simple:
Code: [Select]
;===============================================================
HL_Div_BC_Signed:
;===============================================================
;Performs HL/BC
;Speed: 1350-55a-2b
;         b is the number of set bits in the result
;         a is the number of leading zeroes in the absolute value of HL, minus 1
;         add 24 if HL is negative
;         add 19 if BC is negative
;         add 28 if the result is negative
;Size:    68 bytes
;Inputs:
;     HL is the numerator
;     BC is the denominator
;Outputs:
;     DE is the quotient
;     HL is the remainder
;     BC is not changed
;Destroys:
;     A
;===============================================================
     ld a,h
     xor b
     push af
;absHL
     xor b
     jp p,$+9
     xor a \ sub l \ ld l,a
     sbc a,a \ sub h \ ld h,a
;absBC:
     bit 7,b
     jr z,$+8
     xor a \ sub c \ ld c,a
     sbc a,a \ sub b \ ld b,a

     ld de,0
     adc hl,hl
     jr z,EndSDiv
     ld a,16

     add hl,hl
     dec a
     jp nc,$-2
     ex de,hl
     jp jumpin
Loop1:
     add hl,bc     ;--
Loop2:
     dec a         ;4
     jr z,EndSDiv  ;12|23
     sla e         ;--
     rl d          ;--
jumpin:            ;
     adc hl,hl     ;15
     sbc hl,bc     ;15
     jr c,Loop1    ;23-2b     ;b is the number of bits in the absolute value of the result.
     inc e         ;--
     jp Loop2      ;--
EndSDiv:
     pop af \ ret p
     xor a \ sub e \ ld e,a
     sbc a,a \ sub d \ ld d,a
     ret
Remember that HL and BC are the inputs, DE is the output (HL is the remainder).

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #12 on: July 04, 2013, 01:07:51 pm »
Okay, it was an error of my part, excuse me :P

And ... oh waw. Didn't think a simple change of division routine could speed up the whole thing that much O.O I think I gained 1 or 2 FPS ...

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #13 on: July 04, 2013, 01:09:20 pm »
Yay! Yeah, for some values, it is almost 3 times faster than the routine I gave you originally. What are your typical numbers for HL?

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80 ASM] Unnamed set of 3D routines
« Reply #14 on: July 04, 2013, 01:10:15 pm »
Hell if I know ... try to figure that out by applying a matrix varying depending on varying angles to varying numbers ;D
« Last Edit: July 04, 2013, 01:28:33 pm by Matrefeytontias »