Home Forums GAMES在线课程(现代计算机图形学入门)讨论区 作业二,z-buffer中间插值出现问题

Viewing 1 reply thread
  • Author
    Posts
    • #7607 Score: 0
      czya
      Participant

      在作业二基础部分,已经比较过点的z-buffer值与对应位置屏幕空间的z-buffer值,也可以替换一部分屏幕空间的值,但中途会出现vector超出范围。请教一下大家,这是在for循环中间的错误,应该怎么解决
      `
      for (int x = min_x; x <= max_x; x++) {
      for (int y = min_y; y <= max_y; y++) {
      // 记录最小深度
      float minDepth = FLT_MAX;
      // 四个小点中落入三角形中的点的个数
      int count = 0;
      // 对四个小点坐标进行判断
      for (int i = 0; i < 4; i++) {
      // 小点是否在三角形内
      if (insideTriangle((float)x + pos[i][0], (float)y + pos[i][1], t.v)) {
      // 如果在,对深度z进行插值
      auto tup = computeBarycentric2D((float)x + pos[i][0], (float)y + pos[i][1], t.v);
      float alpha;
      float beta;
      float gamma;
      std::tie(alpha, beta, gamma) = tup;
      float w_reciprocal = 1.0 / (alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
      float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
      z_interpolated *= w_reciprocal;
      minDepth = std::min(minDepth, z_interpolated);
      count++;
      }
      }
      if (count != 0) {
      if (depth_buf[get_index(x, y)] > minDepth) {
      Vector3f color = t.getColor() * count / 4.0;
      Vector3f point(3);
      point << (float)x, (float)y, minDepth;
      // 替换深度
      depth_buf[get_index(x, y)] = minDepth;
      // 修改颜色
      set_pixel(point, color);
      }
      }
      }
      }
      `

    • #7608 Score: 0
      czya
      Participant

      报错的图片

      Attachments:
      You must be logged in to view attached files.
Viewing 1 reply thread
  • You must be logged in to reply to this topic.