将网格剪切到用户定义的区域

deb*_*air 8 opengl 3d

我正在为网格实现剪切算法,其中剪切区域是规范化空间[-1,1]中的矩形.所以我的算法如下:

  1. 创建要用于剪裁的区域的边界框,这些坐标将在标准化空间中.

  2. 在局部空间中创建网格的边界框.使用MVP矩阵将它们转换为剪辑坐标空间.除以w将它们转换为标准化空间.

    vec4 transformed_coords = MVP * vec4(vertex,1.0);
    transformed_coords.xyz /= transformed_coords.w;
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检查两个矩形是否重叠.仅使用xy坐标.

使用此算法,我得到错误的输出.

model matrix
  1.000000  0.000000  0.000000  0.000000
  0.000000  0.000000 -1.000000  0.000000
  0.000000  1.000000  0.000000  0.000000
  0.000000  0.000000  0.000000  1.000000

view * projection
  0.920664 -0.285565 -0.266194 -0.266141
  0.039495  0.746434 -0.664418 -0.664286
 -0.388354 -0.601072 -0.698633 -0.698493
 -0.063192 -1.194295  0.894055  1.093857

MVP matrix 
  0.920664 -0.285565 -0.266194 -0.266141
  0.388354  0.601072  0.698633  0.698493
  0.039495  0.746434 -0.664418 -0.664286
 -0.063192 -1.194295  0.894055  1.093857

bounding box in object space
  -23.376505 -23.376505 -0.023644
   23.376505 -23.376505 -0.023644
  -23.376505  23.376505 -0.023644
   23.376505  23.376505 -0.023644
  -23.376505 -23.376505  1.430214
   23.376505 -23.376505  1.430214
  -23.376505  23.376505  1.430214
   23.376505  23.376505  1.430214

after transformation with MVP
  -30.664371  -8.587399  -9.199142  -8.997323
   12.379420 -21.938423 -21.644522 -21.440216
  -12.507672  19.514534  23.464052  23.659344
   30.536121   6.163512  11.018670  11.216449
  -30.606953  -7.502190 -10.165111  -9.963099
   12.436841 -20.853212 -22.610493 -22.405994
  -12.450252  20.599745  22.498081  22.693565
   30.593540   7.248722  10.052701  10.250672

 the bounding box after MinMax NDC min -0.577393 0.549507 1.000000 max 3.408166 1.023237 1.000000 
 after NDC 
   3.408166 0.954439 1.022431 1.000000
  -0.577393 1.023237 1.009529 1.000000
  -0.528657 0.824813 0.991746 1.000000
   2.722441 0.549507 0.982367 1.000000
   3.072031 0.752998 1.020276 1.000000
  -0.555068 0.930698 1.009127 1.000000
  -0.548625 0.907735 0.991386 1.000000
   2.984540 0.707146 0.980687 1.000000

.min -0.577393 0.549507 max 3.408166 1.023237
Run Code Online (Sandbox Code Playgroud)

区域的最小和最大角为min:-0.53,-0.53 max:0.53,0.53.

这可能会出错?