Trying to understand the VERA FX Polygon Tutorial

All aspects of programming on the Commander X16.
cosmicr
Posts: 34
Joined: Tue Nov 14, 2023 4:29 am

General - CX16 Trying to understand the VERA FX Polygon Tutorial

Post by cosmicr »

The 15-bit signed fixed point number is not explained very well. Is it signed using 2's complement?

The example gives magic numbers of -110 for the left slope and 380 for the right slope without any explanation of where the numbers came from. The concept of filling a triangle is well known -it needs to explain more on how to set it up.

I'm struggling to translate what those values equate to for the actual slope values, so that I can input my own numbers.

Plugging -110 (1111111110010010) into the registers gets you:

Code: Select all

lda #%10010010 ; X Increment (-2:-9) (signed)
sta $9f29
lda #%11111111 ; X Incr. 32x 	X Increment (5:0) (signed) 	X Incr. (-1) 
and #%01111111 ; why do this if we're using magic numbers? why not just use $7F92 ?
sta $9f2a

What is (-110) and (380)? If I read it right, -110 should equate to
110010010 = 402
111111 = -1?

402 / 512 (9 bits) = 0.75815625

so is the result -1.758? - it only gets more complicated when it's actually half-pixels we're talking about apparently, so the slope is -3.516? What if it's 4-bit?

Am I on the right track?

How the hell am I supposed to calculate this on the fly? is there a simple formula? I tried looking at the hardware tests for examples, but they are even more complicated. I'm sure the person who coded it understood their own code.
DragWx
Posts: 342
Joined: Tue Mar 07, 2023 9:07 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by DragWx »

Yes, "signed" means 2's complement. The example values are just there for demonstration; they're not calculated from anything, it's just to show a triangle on the screen.

If you wanted to draw a triangle based on vertices, here's what you do:

1) Identify Vertices

For each triangle you want to draw, you'll need to figure out:
  • Which vertex is the top vertex (T)?
  • Which vertex is the bottom vertex (B)?
  • Which vertex is in the middle vertex (M)?
When using the VERA's acceleration to draw filled triangles, the triangles will be drawn row-by-row, starting at the top and working downward.

The VERA can only draw triangles with either a flat bottom or a flat top, so you use vertex M to bisect the triangle into a top half where each row is progressively wider, and a bottom half where each row is progressively smaller.

If you detect that your triangle already has a flat top or a flat bottom, then you don't need to bisect anything; you just draw that "half" and that's it.


2) Calculate Slopes and Draw

When you know vertex T, you can calculate two slopes using vertices M and B:
  • SlopeTM = (Mx - Tx) / (My - Ty)
  • SlopeTB = (Bx - Tx) / (By - Ty)
Remember, when drawing the top half of the triangle, each row needs to be wider than the previous row. Therefore, the left slope is the LESSER slope, and the right slope is the GREATER slope.

Once you've gotten to the row where vertex M is, you'll need one more slope:
  • SlopeMB = (Bx - Mx) / (By - My)
Whichever side (left or right) M is on, that slope needs to be updated with SlopeMB. Leave the other slope alone. You are now drawing the bottom half of the triangle, where each row is smaller than the previous row, and you stop once you finish drawing the row where B is.


Yes, calculating slopes involves division, so you need a subroutine that can divide integers and give you a fixed point result.
cosmicr
Posts: 34
Joined: Tue Nov 14, 2023 4:29 am

Re: Trying to understand the VERA FX Polygon Tutorial

Post by cosmicr »

Thanks for the reply - like I said I have a good handle on how triangle rasterisation works, it's the setting up of the VERA that I am struggling with and in particular the fixed point numbers. I feel the tutorial would have been clearer if it explained where the hardcoded values came from first. Like; "Here is how to draw a triangle from point V1 = 90,20; V2 = XX,YY; V3 = XX, YY (insert values). We calculate the fixed point value for the slope as being NNNN... because..." etc. Instead it just dives right in with values -110 and 380 pulled out of nowhere. Why is the top point of the triangle considered a constant, but the slope isn't? Why is the slope hardcoded, but then then 15th bit is masked out, when it could have just been in the original value? It's very confusing.
mgkaiser
Posts: 60
Joined: Sat Dec 02, 2023 6:49 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by mgkaiser »

Write a draft of it, submit it here and let the board evaluate it. Even though BASIC isn't the best for speed, it's good for clarity, so maybe I'll write a "BASLOAD" program demonstrating what you describe? Then we will submit it to the documentation Repo as a Pull Request.
mgkaiser
Posts: 60
Joined: Sat Dec 02, 2023 6:49 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by mgkaiser »

The top point is a constant because it is a point, nothing to calculate, you know exactly what it is.

VERA is not really drawing triangles. It's drawing lines of varying length. So top point is the starting place. On the first line you draw a single point in exactly that place.

Now we move to the next line, It moves the starting point to the left by an amount specified by the left slope and write points until we get to the the rightmost point, who's position is calculated by the right slope.

Repeat on next line until we've done enough lines to get from the top point to the mid point.

Then we recalculate one of the slopes so that we now have the line get shorter and shorter until we reach the bottom point.

The helper is helpful because we only do 3 calculations, the slopes, instead of a calculation at the start and stop of each line.

Does that help? When I get a chance I'll write a HEAVILY commented demo in BASLOAD.
cosmicr wrote: Wed May 29, 2024 11:24 pm Thanks for the reply - like I said I have a good handle on how triangle rasterisation works, it's the setting up of the VERA that I am struggling with and in particular the fixed point numbers. I feel the tutorial would have been clearer if it explained where the hardcoded values came from first. Like; "Here is how to draw a triangle from point V1 = 90,20; V2 = XX,YY; V3 = XX, YY (insert values). We calculate the fixed point value for the slope as being NNNN... because..." etc. Instead it just dives right in with values -110 and 380 pulled out of nowhere. Why is the top point of the triangle considered a constant, but the slope isn't? Why is the slope hardcoded, but then then 15th bit is masked out, when it could have just been in the original value? It's very confusing.
cosmicr
Posts: 34
Joined: Tue Nov 14, 2023 4:29 am

Re: Trying to understand the VERA FX Polygon Tutorial

Post by cosmicr »

Does that help?
thanks a lot for the help, but not really. As I said, I fully understand how rasterisation works. I understand what it's doing. I have already written my own rasteriser (see my post on porting "Another World" to the CX16). I'm trying to understand how to use the polygon filler helper and the code in the tutorial.

In trying to write these posts I''m getting a better handle on the example in the tutorial, but it still doesn't make a lot of sense why the top point is a constant but the slopes arent. In a real world example, it wouldn't be a constant. So why in this example? Why not make all values constant? It doesn't make sense why the 15-bit is masked out when that could have been done before hand since we're hard coding values anyway.

Anyway I'll work it out. I think I've got as far as I can. Maybe I'll write a new tutorial :)
mgkaiser
Posts: 60
Joined: Sat Dec 02, 2023 6:49 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by mgkaiser »

Ok, the top point is not absolutely constant but is calculated by taking "x + y * (width of screen)". Its where you start for line 1. Then there 's a left and right slope to decide where line 2 begins and ends. I'll try to draw a picture to illustrate. VERA can only draw triangles with a flat top or bottom and a point at the other end. Other triangles are actually TWO triangles that oppose each other. This will take a picture, but once I get to draw it it will make sense.
cosmicr
Posts: 34
Joined: Tue Nov 14, 2023 4:29 am

Re: Trying to understand the VERA FX Polygon Tutorial

Post by cosmicr »

Thanks - you don't need to illustrate. I get what the top point is lol. I want to understand the implementation. I understand everything about coordinates, triangles, pixels, VERA, rasterisation, slopes etc. I do not need an explanation of anything about triangles or the maths involved. Like I said I've already written several polygon rasterisers of my own. I know how the polygon helper works.

The part I was struggling with is the implementation - ie the code. I don't think it's entirely clear. Maybe it just needs better formatting.

It doesn't matter. I've spent some more time on it and I understand it all now and have it working. Thanks for your help!

edit: I forgot to include the screenshot! Here's an animated gif - this is running in C, but it should be a bit faster in assembly. The slope calculation and actual filling loop uses heaps of cycles. I'll probably use this in my Another World port.
quads.gif
quads.gif (2.99 MiB) Viewed 2948 times
Last edited by cosmicr on Tue Jun 04, 2024 2:36 am, edited 1 time in total.
mortarm
Posts: 299
Joined: Tue May 16, 2023 6:21 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by mortarm »

DragWx wrote: Wed May 29, 2024 5:22 pm For each triangle you want to draw, you'll need to figure out:
  • Which vertex is the top vertex (T)?
  • Which vertex is the bottom vertex (B)?
  • Which vertex is in the middle vertex (M)?
As I see it, the only time you could have a middle vertex is if the triangle is if the triangle is laying on its side, i.e., the flat side is on the left or right.

What am I missing?
DragWx
Posts: 342
Joined: Tue Mar 07, 2023 9:07 pm

Re: Trying to understand the VERA FX Polygon Tutorial

Post by DragWx »

mortarm wrote: Mon Jun 03, 2024 5:27 am
DragWx wrote: Wed May 29, 2024 5:22 pm For each triangle you want to draw, you'll need to figure out:
  • Which vertex is the top vertex (T)?
  • Which vertex is the bottom vertex (B)?
  • Which vertex is in the middle vertex (M)?
As I see it, the only time you could have a middle vertex is if the triangle is if the triangle is laying on its side, i.e., the flat side is on the left or right.

What am I missing?
vera_triangles_resized.png
vera_triangles_resized.png (33 KiB) Viewed 3123 times
You have to think about the triangle in terms of drawing it from top to bottom, so "top" "middle" and "bottom" are literally just the Y coordinate of each vertex: which one's on top, which one's on bottom, and which one's in the middle?
Post Reply