Drawing normal faces with triangle strips?

355 views Asked by At

I am having to calculate the normals for a triangle strip and am having a issue where every other triangle is dark and not shaded well. I am using the flat shade model. I can't tell if it has to do with the winding direction. When I look under the triangle strip i notice that it is the same thing as the top except the dark areas or switched. I think what the problem may be is that the surface normals I am trying to calculate are using shared vertices. If that is the case would you recommend switching to GL_TRIANGLES? How would you resolve this?

Here is what I have as of now. The triangle class is has the triVerts array in it which have three Vert objects. The Vert objects have variables x, y, and 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();
1

There are 1 answers

0
John On

I had to draw a pyramid with about 8-10 faces and some lighting and I used triangles to be properly lighted. For each triangle I had to calculate the normal. This way it worked. Also I think is important to keep the clockwise/counter sense in which you draw the vertices for each triangle. I hope it helps.