0

我必须计算三角形条的法线,并且遇到一个问题,即每个其他三角形都是暗的并且阴影不好。我正在使用平面阴影模型。我不知道它是否与缠绕方向有关。当我查看三角形条下方时,我注意到它与顶部相同,除了黑暗区域或切换。我认为问题可能是我试图计算的表面法线使用共享顶点。如果是这种情况,您会建议切换到 GL_TRIANGLES 吗?你会如何解决这个问题?

这是我现在所拥有的。三角形类中包含 triVerts 数组,其中包含三个 Vert 对象。Vert 对象具有变量 x、y 和 z。

   Triangle currentTri = new Triangle();
   int triPointIndex = 0;
   List<Triangle> triList = new ArrayList<Triangle>()                               

    GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
            int counter1 = 0;               
            float stripZ = 1.0f;
            float randY;
            for (float x=0.0f; x<20.0f; x+=2.0f) {
                if (stripZ == 1.0f) {
                    stripZ = -1.0f;
                } else { stripZ = 1.0f; }

                randY = (Float) randYList.get(counter1);
                counter1 += 1;

                GL11.glVertex3f(x, randY, stripZ);

                Vert currentVert = currentTri.triVerts[triPointIndex];
                currentVert.x = x;
                currentVert.y = randY;
                currentVert.z = stripZ;

                triPointIndex++;

                System.out.println(triList);

                Vector3f normal = new Vector3f();
                float Ux = currentTri.triVerts[1].x - currentTri.triVerts[0].x;
                float Uy = currentTri.triVerts[1].y - currentTri.triVerts[0].y;
                float Uz = currentTri.triVerts[1].z - currentTri.triVerts[0].z;

                float Vx = currentTri.triVerts[2].x - currentTri.triVerts[0].x;
                float Vy = currentTri.triVerts[2].y - currentTri.triVerts[0].y;
                float Vz = currentTri.triVerts[2].z - currentTri.triVerts[0].z;

                normal.x = (Uy * Vz) - (Uz * Vy);
                normal.y = (Uz * Vx) - (Ux * Vz);
                normal.z = (Ux * Vy) - (Uy * Vx);

                GL11.glNormal3f(normal.x, normal.y, normal.z);

                if (triPointIndex == 3) {
                    triList.add(currentTri);
                    Triangle nextTri = new Triangle();

                    nextTri.triVerts[0] = currentTri.triVerts[1];
                    nextTri.triVerts[1] = currentTri.triVerts[2];
                    currentTri = nextTri;
                    triPointIndex = 2;
                }           

            }
     GL11.glEnd();
4

1 回答 1

0

我必须绘制一个大约有 8-10 个面和一些照明的金字塔,并且我使用三角形来适当地照明。对于每个三角形,我必须计算法线。这样它就起作用了。此外,我认为保持为每个三角形绘制顶点的顺时针/逆向意义很重要。我希望它有所帮助。

于 2013-12-04T09:48:26.160 回答