具有意外线条的glPolygonOffset

时间:2014-01-12 20:51:20

标签: java opengl lwjgl

我正在使用glpolygonMode(GL_FRONT_AND_BACK,GL_LINE)渲染三角形条。 然后我将行偏移设置为此设置glPolygonOffset(-0.8f,-1.0f)。

无论我使用什么设置进行偏移,我最终都会获得闪烁的随机线条 四处走动时进出。这是一个gif。看起来像什么如果有人可以帮忙 提出他们认为错误的想法,这将是伟大的。

enter image description here

这是我的方法,如果有人需要查看它,我会渲染线条。

public void lines() {
    GL11.glColor3f(0.0f, 0.0f, 0.0f);
    GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);       

    Triangle currentTri = new Triangle();
    List<Triangle> triList = new ArrayList<Triangle>();                               
    boolean xSwitch = false;

    for (int z=0; z<=100; z+=2) {
        GL11.glEnable(GL11.GL_POLYGON_OFFSET_LINE);
        GL11.glPolygonOffset (-0.8f, -1.0f);

        GL11.glBegin(GL11.GL_TRIANGLE_STRIP);

        int triPointIndex = 0;
        boolean zSwitch = true;
        xSwitch = !xSwitch;
        float zVal;
        float xVal;

        for (int x=0; x<100; x+=1) {
            xVal = x;
            float randY;
            zSwitch = !zSwitch;    

            // this is what determines the xSwitch and zSwitch outcomes.
            // changes the zVal and randY to match what must alternate.
            if (xSwitch) {
                if (zSwitch) {
                    zVal = z;
                    randY = randYList[z][x];
                } else {
                    zVal = z + 2.0f;
                    randY = randYList[z+2][x];
                }
            } else {                        
                if (zSwitch) {
                    zVal = z + 2.0f;
                    randY = randYList[z+2][x];
                } else {
                    zVal = z;
                    randY = randYList[z][x];
                }
            }   

            // set up the currentVert within the currentTri so that we can use it for computing normals.
            Vert currentVert = currentTri.triVerts[triPointIndex];
            currentVert.x = xVal;
            currentVert.y = randY;
            currentVert.z = zVal;
            triPointIndex++;

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

                Vector3f normal = new Vector3f();
                float Ux; float Uy; float Uz;
                float Vx; float Vy; float Vz;

                if (xSwitch) {
                    if (triList.indexOf(currentTri) % 2 == 0) {
                        Vx = currentTri.triVerts[1].x - currentTri.triVerts[0].x;
                        Vy = currentTri.triVerts[1].y - currentTri.triVerts[0].y;
                        Vz = currentTri.triVerts[1].z - currentTri.triVerts[0].z;

                        Ux = currentTri.triVerts[2].x - currentTri.triVerts[0].x;
                        Uy = currentTri.triVerts[2].y - currentTri.triVerts[0].y;
                        Uz = currentTri.triVerts[2].z - currentTri.triVerts[0].z;               
                    } else {
                        Ux = currentTri.triVerts[1].x - currentTri.triVerts[0].x;
                        Uy = currentTri.triVerts[1].y - currentTri.triVerts[0].y;
                        Uz = currentTri.triVerts[1].z - currentTri.triVerts[0].z;

                        Vx = currentTri.triVerts[2].x - currentTri.triVerts[0].x;
                        Vy = currentTri.triVerts[2].y - currentTri.triVerts[0].y;
                        Vz = currentTri.triVerts[2].z - currentTri.triVerts[0].z;   
                    }
                } else {
                    if (triList.indexOf(currentTri) % 2 == 0) {
                        Ux = currentTri.triVerts[1].x - currentTri.triVerts[0].x;
                        Uy = currentTri.triVerts[1].y - currentTri.triVerts[0].y;
                        Uz = currentTri.triVerts[1].z - currentTri.triVerts[0].z;

                        Vx = currentTri.triVerts[2].x - currentTri.triVerts[0].x;
                        Vy = currentTri.triVerts[2].y - currentTri.triVerts[0].y;
                        Vz = currentTri.triVerts[2].z - currentTri.triVerts[0].z;   
                    } else {
                        Vx = currentTri.triVerts[1].x - currentTri.triVerts[0].x;
                        Vy = currentTri.triVerts[1].y - currentTri.triVerts[0].y;
                        Vz = currentTri.triVerts[1].z - currentTri.triVerts[0].z;

                        Ux = currentTri.triVerts[2].x - currentTri.triVerts[0].x;
                        Uy = currentTri.triVerts[2].y - currentTri.triVerts[0].y;
                        Uz = 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);

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

                triPointIndex = 2;
            } // close triPointIndex == 3 
            GL11.glVertex3f(xVal, randY, zVal);
        } // close x loop
        GL11.glEnd();
    } // close z loop
    GL11.glDisable(GL11.GL_POLYGON_OFFSET_LINE);

} 

0 个答案:

没有答案