外观为三角形条.表面法线?还是绕组? [英] Appearance of a triangle strip. Surface normals? Or windings?

查看:97
本文介绍了外观为三角形条.表面法线?还是绕组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的结果的图片.

Below is a picture of what my outcome is.

我正在使用平面阴影,并将每个顶点放在其受人尊敬的三角形对象中.然后,我使用这些顶点来计算表面法线.我一直在读,因为我的三角形共享相似的顶点,所以计算法线可能是个问题?但是对我来说,这似乎是一个绕线问题,因为其他每个人都不在家.

I am using flat shading and have put each vertex in their respectable triangle objects. Then I use these vertices to calculate the surface normals. I have been reading that because my triangles share similar vertices that calculating the normals may be an issue? But to me this looks like a windings problem given that every other one is off.

我将下面的一些代码提供给想要仔细查看它的人,以便更好地了解问题的根源.

I provided some of my code below to anyone who wants to look through it and get a better idea what the issue could be.

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();

推荐答案

您应该在调用glVertex3f (...)之前设置正常的 .对glVertex*的调用基本上确定了顶点,它将当前的颜色,法线,纹理坐标等与您经过的位置处的顶点相关联,并发出新的顶点.

You should be setting the normal before calling glVertex3f (...). A call to glVertex* is basically what finalizes a vertex, it associates the current color, normal, texture coordinates, etc... with the vertex at the position you pass and emits a new vertex.


说明

glBegin / glEnd 对中使用

glVertex 命令来指定点,线和面顶点.调用 glVertex 时,当前颜色,法线,纹理坐标和雾坐标与该顶点关联.

glVertex commands are used within glBegin / glEnd pairs to specify point, line, and polygon vertices. The current color, normal, texture coordinates, and fog coordinate are associated with the vertex when glVertex is called.

仅指定x和y时,z默认为 0.0 ,而w默认为 1.0 .指定x,y和z时,w默认为 1.0 .

When only x and y are specified, z defaults to 0.0 and w defaults to 1.0. When x, y, and z are specified, w defaults to 1.0.


很有可能是您问题的很大一部分.三角带设计用于解决隐式绕组问题.使用条带时,必须反转每个三角形的绕线,但是光栅化器通过在每个备用三角形内部翻转用于前/后的绕线顺序来对此进行补偿.


Chances are very good that this is a large part of your problem. Triangle strips are designed to workaround implicit winding issues. You have to reverse the winding of every triangle when you use a strip, but the rasterizer compensates for this by flipping the winding order used for front/back internally on each alternate triangle.

当然可以理解,光栅器足够聪明,可以在使用条带时翻转每个替代三角形的前/后绕组,但是您的代码不是(至少目前不是).在CPU端自己计算法线时,您需要补偿交替反向绕组.

Understand of course that the rasterizer is smart enough to flip the front/back winding for each alternate triangle when using a strip but your code is not (at least not currently). You need to compensate for the alternately reversed winding when you calculate the normals yourself on the CPU side.

这篇关于外观为三角形条.表面法线?还是绕组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆