Note that there are some explanatory texts on larger screens.

plurals
  1. POPerspective correct texture mapping; z distance calculation might be wrong
    primarykey
    data
    text
    <p>I'm making a software rasterizer, and I've run into a bit of a snag: I can't seem to get perspective-correct texture mapping to work.</p> <p>My algorithm is to first sort the coordinates to plot by <code>y</code>. This returns a highest, lowest and center point. I then walk across the scanlines using the delta's:</p> <pre><code>// ordering by y is put here order[0] = &amp;a_Triangle.p[v_order[0]]; order[1] = &amp;a_Triangle.p[v_order[1]]; order[2] = &amp;a_Triangle.p[v_order[2]]; float height1, height2, height3; height1 = (float)((int)(order[2]-&gt;y + 1) - (int)(order[0]-&gt;y)); height2 = (float)((int)(order[1]-&gt;y + 1) - (int)(order[0]-&gt;y)); height3 = (float)((int)(order[2]-&gt;y + 1) - (int)(order[1]-&gt;y)); // x float x_start, x_end; float x[3]; float x_delta[3]; x_delta[0] = (order[2]-&gt;x - order[0]-&gt;x) / height1; x_delta[1] = (order[1]-&gt;x - order[0]-&gt;x) / height2; x_delta[2] = (order[2]-&gt;x - order[1]-&gt;x) / height3; x[0] = order[0]-&gt;x; x[1] = order[0]-&gt;x; x[2] = order[1]-&gt;x; </code></pre> <p>And then we render from <code>order[0]-&gt;y</code> to <code>order[2]-&gt;y</code>, increasing the <code>x_start</code> and <code>x_end</code> by a delta. When rendering the top part, the delta's are <code>x_delta[0]</code> and <code>x_delta[1]</code>. When rendering the bottom part, the delta's are <code>x_delta[0]</code> and <code>x_delta[2]</code>. Then we linearly interpolate between x_start and x_end on our scanline. UV coordinates are interpolated in the same way, ordered by y, starting at begin and end, to which delta's are applied each step.</p> <p>This works fine except when I try to do perspective correct UV mapping. The basic algorithm is to take <code>UV/z</code> and <code>1/z</code> for each vertex and interpolate between them. For each pixel, the UV coordinate becomes <code>UV_current * z_current</code>. However, this is the result:</p> <p><img src="https://imgur.com/woOqP.png" alt="alt text"></p> <p>The inversed part tells you where the delta's are flipped. As you can see, the two triangles both seem to be going towards different points in the horizon.</p> <p>Here's what I use to calculate the Z at a point in space:</p> <pre><code>float GetZToPoint(Vec3 a_Point) { Vec3 projected = m_Rotation * (a_Point - m_Position); // #define FOV_ANGLE 60.f // static const float FOCAL_LENGTH = 1 / tanf(_RadToDeg(FOV_ANGLE) / 2); // static const float DEPTH = HALFHEIGHT * FOCAL_LENGTH; float zcamera = DEPTH / projected.z; return zcamera; } </code></pre> <p>Am I right, is it a z buffer issue?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload