1

我的目标是使用光栅化算法渲染四边形的图像。我一直到:

  • 在 3D 中创建四边形
  • 使用透视分割将四边形的顶点投影到屏幕上
  • 将生成的坐标从屏幕空间转换为栅格空间,并在栅格空间中计算四边形的边界框
  • 循环遍历此边界框内的所有像素,并找出当前像素 P 是否包含在四边形中。为此,我使用了一个简单的测试,其中包括在四边形的边 AB 和顶点 A 和点 P 之间定义的向量之间取点。我对所有 4 条边重复这个过程,如果符号相同,那么该点在四边形内。

我已经成功地实现了这个(见下面的代码)。但是我被我想玩的剩余位所困扰,这些位基本上是找到我的四边形的 st 或纹理坐标。

  • 我不知道是否可以在光栅空间的四边形中找到当前像素 P 的 st 坐标,然后将其转换回世界空间?有人能指出我正确的方向,告诉我该怎么做吗?
  • 或者,我如何计算四边形中包含的像素的 z 或深度值。我想这与在四边形中找到点的st坐标,然后对顶点的z值进行插值有关?

PS:这不是作业。我这样做是为了理解光栅化算法,而正是我现在卡在哪里,是我不明白的一点,我相信 GPU 渲染管道涉及某种逆投影,但我现在迷失了。谢谢你的帮助。

    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;
            }
        }
    }
4

0 回答 0