Sorry to bump the thread, but I did have a question related to an earlier post.
Low-level info you don't need to know IF you're using the leaked Nintendo or PsyQ SDK to write your N64 programs. I use libdragon, a completely Nintendo-code free development kit for programming the N64. It has support for the Rectangle RDP commands, but not the Triangle RDP commands. I was working on my own "microcode" (silly name for a pcode interpreter if you ask me) and while a mixer is dead simple, I wanted to add in handling vertexes and the like, as well as adding support for triangle commands. While I could scrape enough brain cells together to figure out how to calculate the values the triangle command needs, I noticed your post here and decided to ask... yes, I would very much like to know about the conversion between vertex values and edge coefficients.
Thanks.
Okay, sure. Right now, I'll just talk about converting the x/y coordinates to edge coefficients. The texture, Z and shading values are a little complex and I have yet to fully figure out an algorithm for converting to the RDP coefficient format. However, once you get the basic idea, it shouldn't be too much of a stretch. See sheet 73,74 of patent 6,239,810 for details on the RDP edge coefficients. I assume that you understand how the edge coefficients work, none of this will really make sense unless you understand how the triangles are rendered from the edge coeficcients (see my earlier posts).
Let's say you have three typical vertices (i'll represent this in C), each with an X and Y coordinate:
Vertex2f v[3] = { {47, 126}, {200, 20}, {300, 260} };
The first thing we do is sort the vertices by their Y coordinate, low to high. Just doing this, we already have our "Y coordinate of high minor, middle minor and low major edge". The X major (XH) and X middle (XM) edge coefficients can either be the same or have one pixel difference (to make a convincing triangle). Either way, XH and XM will be derived from the X value of the vertex with the lowest Y value (v[0] in our case, since we sorted them). X low (XL) will be the X value of the vertex with the middle Y value (v[1] in our case). The RDP also needs to know which direction the triangle is facing (which side XL is on). The triangle is a left major triangle if (v[2].x < v[1].x), else it's a right major triangle. If you take a look at the diagram on sheet 74, you can see why this is true.
Now that we have all the edge values, we need to calculate the inverse slopes, DxHDy, DxMDy and DxLDy. The inverse slopes effectively tell the RDP how to interpolate the spans of the triangle which are being rendered. That is, for each scanline, the RDP will increase each edge value by it's corresponding inverse slope. To calculate an inverse slope of two XY points, you use the following formula (remembering to use signed arithmetic).
(x1 - x0) / (y1 - y0)
So, given that the vertices are all sorted in Y order, it's quite trivial to calculate each inverse slope.
e.g. DxHDy = (v[2].x - v[0]) / (v[2] - v[0])
etc...
Now, given all that, we have calculated the values for all needed edge coefficients. However, the RDP expects these values in a specific format. For example, all X edges and inverse slopes are expect to be in fixed 16.16 format (16 bits integer, 16 bits fractional). Instead of telling you how to convert these values, I will just post
this snippet of code that I was in the process of writing. I (for whatever reason) never finished it, however, it does take 3 (C float) vertices and outputs a binary RDP triangle command (although, only flat shaded triangles). Feel free to use this meager code in anyway you want.
The next step I was trying to achieve was to calculate the coefficients for Goraud shaded, textured or Z buffered triangles. However, I never quite understood one of the RDP's coefficient values, the "change in <whatever value> along the edge". This is present in the shade, texture and Z coefficients. If anyone can shed a light on this, it would be helpful. I did at one point ask the original architect of the RDP (Tim Van Hook) what this value was, but I don't think he could really recall.