【问题标题】:Cylinder partially visible WebGL圆柱部分可见的WebGL
【发布时间】:2015-04-22 15:35:51
【问题描述】:

我正在使用 WebGL 1.0(基于 OpenGL ES 2.0)设计一个圆柱体。

它最初是一个带有 m 个堆栈的 n 边多边形(n 个切片)。它的法线指定如下:

即使正确绘制了多边形/圆柱体,它的面也不是从每个角度都可见。它们只能从内部看到,如下图所示:

我的目标是制作一个完全可见的圆柱体,如下所示(不需要顶面/底面):

有人知道如何解决这个问题吗?代码如下:

//End if stacks = 0
 if (this.stacks <= 0) return;

 this.vertices = []; 
 this.indices = [];
 this.normals = [];

//--- Vertices & Normals ---

 var angle;
 var alpha = 360 / this.slices;
 var zCoord = 0;

 // (N) stacks -> (N + 1) faces -> (faces * this.slices) vertex
 for ( var stackIndex = 0; stackIndex < this.stacks + 1; stackIndex++) {

     //Reset angle for each face of the stack
     angle = 0;

     for ( var sliceIndex = 0; sliceIndex < this.slices; sliceIndex++) {

         this.vertices.push(Math.cos(angle * degToRad)); //X
         this.vertices.push(Math.sin(angle * degToRad)); //Y
         this.vertices.push(zCoord); //Z

         this.normals.push(Math.cos(angle * degToRad)); //X
         this.normals.push(Math.sin(angle * degToRad)); //Y
         this.normals.push(zCoord); //Z

         //Updating angle
         angle = angle + alpha; 
         }
     //Updating z coordinate
     zCoord = zCoord + (1 / this.stacks);
 }   

//--- Indices ---

 var stackInc; stackIndex = 0; sliceIndex = 0;

 for (stackIndex = 0; stackIndex < this.stacks; stackIndex++) {

     stackInc = stackIndex * this.slices;

     for (sliceIndex = 0; sliceIndex < this.slices; sliceIndex++) {

         if (sliceIndex != this.slices - 1) { 
             //T1
             this.indices.push(sliceIndex + stackInc);
             this.indices.push(sliceIndex + stackInc + this.slices);
             this.indices.push(sliceIndex + stackInc + this.slices + 1);

             //T2
             this.indices.push(sliceIndex + stackInc + this.slices + 1); //this.slices
             this.indices.push(sliceIndex + stackInc + 1); //0
             this.indices.push(sliceIndex + stackInc); //int4
             }
        //Handling last face which uses repeated vertices
         else {
             this.indices.push(sliceIndex + stackInc);
             this.indices.push(sliceIndex + stackInc + this.slices);
             this.indices.push(stackInc + this.slices);

             this.indices.push(stackInc + this.slices);
             this.indices.push(stackInc);
             this.indices.push(sliceIndex + stackInc); //int4
             }
         }
     }

【问题讨论】:

  • 我没有详细查看您的代码,只是一个简单的问题:您是否启用了背面剔除?
  • 是的,它已启用。我切换了索引顺序,它现在似乎按预期工作!

标签: javascript webgl opengl-es-2.0


【解决方案1】:

无论面法线如何,WebGL 都会根据您定义三角形的顺序定义正面和背面。

有这个三角形:

A-------B
 \     /
  \   /
   \ /
    C

如果将顶点按 A、B、C(顺时针)的顺序添加到索引中,而您没有看到它们,则需要按 A、C、B 的顺序(逆时针)切换。

这是因为:https://www.opengl.org/wiki/Face_Culling

【讨论】:

  • 我切换了索引顺序,现在似乎可以正常工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多