Author Topic: [z80] Needs help debugging a triangle filling routine  (Read 5120 times)

0 Members and 1 Guest 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] Needs help debugging a triangle filling routine
« on: March 07, 2013, 04:57:44 pm »
Hey guys,

I'm trying to build a triangle filler out of the algorithm ben_g sent me, and I'm encountering some problems... In most cases, the triangle is not drawn at the location I wanted it to be.

So here's the pseudocode of the algorithm :
Code: [Select]
inputs pt1,pt2,pt3,color       // color is unused for now

// we sorts the points with the smallest Y first
if (pt1.y > pt2.y) swap(pt1,pt2)     // swaps pt1.x with pt2.x and pt1.y with pt2.y

If (pt2.y > pt3.y) swap(pt2,pt3)

If(pt1.y > pt3.y) swap(pt1,pt3)

dx1 = (x2-x1)/(y2-y1)
dx2 = (x3-x1)/(y3-y1)
slx2 = slx1 = x1
sly = y1

drawLoop:
While (sly < y2)
{
  HorizontalLine(sly, slx1, slx2)
  slx1 = slx1 + dx1
  slx2 = slx2 + dx2
  sly++
}
If (y2 != y3)
{
  dx1 = (x3-x2)/(y3-y2)
  y2 = y3
  goto drawLoop
}

And here's the asm code I have so far (no screenie, I'm on a phone :/ ). So if you guys see anything wrong...

Thanks by advance :)

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: [z80] Needs help debugging a triangle filling routine
« Reply #1 on: March 08, 2013, 01:56:40 am »
What are sub_88div and sub_HLline?

Offline Matrefeytontias

  • Axe roxxor (kinda)
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1982
  • Rating: +310/-12
  • Axe roxxor
    • View Profile
    • RMV Pixel Engineers
Re: [z80] Needs help debugging a triangle filling routine
« Reply #2 on: March 08, 2013, 01:58:29 am »
Oh yeah I forgot to mention that, the routine is incorporated inside an axiom, so these are calls to Axe functions. sub_88Div is a 8.8 fixed signed division (in Axe HL /* DE) and sub_HLine draws an horizontal line (HLine(Y,X1,X2)).

And btw, I took a screenie. The inverted dots are the points supposed to be the corners of the triangles :



EDIT : updated the source at the same address (in the first post).
« Last Edit: March 08, 2013, 02:06:53 am by Matrefeytontias »

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: [z80] Needs help debugging a triangle filling routine
« Reply #3 on: March 08, 2013, 03:25:45 am »
A couple problems, your variables dX1/2 don't handle negative values. You sort according to Y values, which doesn't mean that the Xs will also be smallest to largest. Also, i don't know how Axe's horizontal line routine would handle it, but if say x1 > x2, it might encounter problems expecting to draw from left to right and not the other way around.

You said you found a better algorithm, but looking at if statements and for loops gives me a headache, so i'm not even going to try understanding it. If you make an assembly version of it i'd be more than happy to look through it, though :D

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: Re: [z80] Needs help debugging a triangle filling routine
« Reply #4 on: March 08, 2013, 12:28:32 pm »
I think he's trying to code this algorithm in asm.
I'm not an asm expert so I can't help you though. :-\