Rasterisation Algorithm: finding the “ST” coordinates of point in 2D quad and Inverse Projection

依然范特西╮ 提交于 2019-12-11 13:25:49

问题


My goal is to render the image of a quad using the rasterisation algorithm. I have been as far as:

  • creating the quad in 3D
  • projecting the quad's vertices onto the screen using a perspective divide
  • converting the resulting coordinates from screen space to raster space, and comput the bounding box of the quad in raster space
  • looping over all pixels inside this bounding box, and finding out if the current pixel P is contained within the quad. For this I am using a simple test which consist of taking the dot between the edge AB of the quad and the vector defined between the vertex A and the point P. I repeat this process for all 4 edges and if the sign is the same, then the point is inside the quad.

I have implemented this successfully (see code below). But I am stuck with the remaining bits which I'd like to play with which is essentially finding the st or texture coordinates of my quad.

  • I don't know if it's possible to find the st coordinates of the current pixel P in the quad in raster space, and then convert that back into world space? Could you someone please point me in the right direction of tell me how to do this?
  • alternatively how can I compute the z or depth value of the pixel contained in the quad. I guess it's related to finding the st coordinates of the point in the quad, and then interpolating z values of vertices?

PS: this is NOT a homework. I do this to understand the rasterization algorithm, and precisely where I am stuck now, is the bit I don't understand which I believe in the GPU rendering pipeline involves some sort of inverse projection, but I am just lost at this point. Thanks for your help.

    Vec3f verts[4]; // vertices of the quad in world space
    Vec2f vraster[4]; // vertices of the quad in raster space
    uint8_t outside = 0; // is the quad in raster space visible at all?
    Vec2i bmin(10e8), bmax(-10e8);
    for (uint32_t j = 0; j < 4; ++j) {
        // transform unit quad to world position by transforming each
        // one of its vertices by a transformation matrix (represented
        // here by 3 unit vectors and a translation value)
        verts[j].x = quads[j].x * right.x + quads[j].y * up.x + quads[j].z * forward.x + pt[i].x;
        verts[j].y = quads[j].x * right.y + quads[j].y * up.y + quads[j].z * forward.y + pt[i].y;
        verts[j].z = quads[j].x * right.z + quads[j].y * up.z + quads[j].z * forward.z + pt[i].z;

        // project the vertices on the image plane (perspective divide)
        verts[j].x /= -verts[j].z;
        verts[j].y /= -verts[j].z;

        // assume the image plane is 1 unit away from the eye
        // and fov = 90 degrees, thus bottom-left and top-right
        // coordinates of the screen are (-1,-1) and (1,1) respectively.
        if (fabs(verts[j].x) > 1 || fabs(verts[j].y) > 1) outside |= (1 << j);

        // convert image plane coordinates to raster
        vraster[j].x = (int32_t)((verts[j].x + 1) * 0.5 * width);
        vraster[j].y = (int32_t)((1 - (verts[j].y + 1) * 0.5) * width);


        // compute box of the quad in raster space
        if (vraster[j].x < bmin.x) bmin.x = (int)std::floor(vraster[j].x);
        if (vraster[j].y < bmin.y) bmin.y = (int)std::floor(vraster[j].y);
        if (vraster[j].x > bmax.x) bmax.x = (int)std::ceil(vraster[j].x);
        if (vraster[j].y > bmax.y) bmax.y = (int)std::ceil(vraster[j].y);
    }

    // cull if all vertices are outside the canvas boundaries
    if (outside == 0x0F) continue;

    // precompute edge of quad
    Vec2f edges[4];
    for (uint32_t j = 0; j < 4; ++j) {
        edges[j] = vraster[(j + 1) % 4] - vraster[j];
    }

    // loop over all pixels contained in box
    for (int32_t y = std::max(0, bmin.y); y <= std::min((int32_t)(width -1), bmax.y); ++y) {
        for (int32_t x = std::max(0, bmin.x); x <= std::min((int32_t)(width -1), bmax.x); ++x) {
            bool inside = true;
            for (uint32_t j = 0; j < 4 && inside; ++j) {
                Vec2f v = Vec2f(x + 0.5, y + 0.5) - vraster[j];
                float d = edges[j].x * v.x + edges[j].y * v.y;
                inside &= (d > 0);
            }
            // pixel is inside quad, mark in the image
            if (inside) {
                buffer[y * width + x] = 255;
            }
        }
    }

来源:https://stackoverflow.com/questions/28012828/rasterisation-algorithm-finding-the-st-coordinates-of-point-in-2d-quad-and-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!