Some faces are transparent, others are opaque.

I created a regular dodecahedron with OpenGL. I wanted to make faces transparent (as in the image on Wikipedia), but this does not always work. After some digging in the OpenGL documentation, it appears that I need to sort the transparent faces from the back to the front . "Hectometer How to do this?

I mean, I call glRotatef () to rotate the coordinate system, but the reference coordinates of the faces remain unchanged; the rotation effect is applied "outside" of my code.

If you apply the transformation to the coordinates, then everything else will stop moving.

How can I sort the faces in this case?

[EDIT] I know why this is happening. I have no idea what the solution might look like. Can someone direct me to the right OpenGL calls or sample code? I know when the coordinate transformation is finished, and I have the coordinates of the vertices of the faces. I know how to calculate the center coordinates of faces. I understand that I need to sort them by Z. How do I convert the 3f vector to the current view matrix (or whatever it is called, which rotates my coordinate system)?

Code to rotate the view:

    glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    glRotatef(yrot, 0.0f, 1.0f, 0.0f);
+3
source share
4 answers

OpenGL " ", " , ". , , : , , .

- (, ), .

, OpenGL Z-buffering.

( , " " , . , , Z-.)


: , , , , , Z . ? , . , , , , ?

, , . OpenGL FAQ 8.010:

OpenGL, . , (0., 0., 0.).

. , , , ? , . OpenGL FAQ 9.130:

ModelView. .

glGetFloatv(GL_MODELVIEW_MATRIX, dst), 16 . , : , OpenGL API .

+3

, ( lwjgl 2.0.1). , :

        float one = 1f * scale;

        // Cube of size 2*scale
        float[][] coords = new float[][] {
            {  one,  one,  one }, // 0
            { -one,  one,  one },
            {  one, -one,  one },
            { -one, -one,  one },
            {  one,  one, -one },
            { -one,  one, -one },
            {  one, -one, -one },
            { -one, -one, -one }, // 7
        };

int. - :

        int[][] faces = new int[][] {
            { 0, 2, 3, 1, },
            { 0, 4, 6, 2, },
            { 0, 1, 5, 4, },
            { 4, 5, 7, 6, },
            { 5, 1, 3, 7, },
            { 4, 5, 1, 0, },
        };

Model/View:

        Matrix4f matrix = new Matrix4f ();
        FloatBuffer params = FloatBuffer.allocate (16);
        GL11.glGetFloat (GL11.GL_MODELVIEW_MATRIX, params );
        matrix.load (params);

:

public static class Face
{
    public int id;
    public Vector3f center;

    @Override
    public String toString ()
    {
        return String.format ("%d %.2f", id, center.z);
    }
}

Z:

public static final Comparator<Face> FACE_DEPTH_COMPARATOR = new Comparator<Face> ()
{
    @Override
    public int compare (Face o1, Face o2)
    {
        float d = o1.center.z - o2.center.z;
        return d < 0f ? -1 : (d == 0 ? 0 : 1);
    }

};

getCenter() :

    public static Vector3f getCenter (float[][] coords, int[] face)
    {
        Vector3f center = new Vector3f ();
        for (int vertice = 0; vertice < face.length; vertice ++)
        {
            float[] c = coords[face[vertice]];
            center.x += c[0];
            center.y += c[1];
            center.z += c[2];
        }
        float N = face.length;
        center.x /= N;
        center.y /= N;
        center.z /= N;
        return center;
    }

:

        Face[] faceArray = new Face[faces.length]; 
        Vector4f v = new Vector4f ();
        for (int f = 0; f < faces.length; f ++)
        {
            Face face = faceArray[f] = new Face ();
            face.id = f;
            face.center = getCenter (coords, faces[f]);
            v.x = face.center.x;
            v.y = face.center.y;
            v.z = face.center.z;
            v.w = 0f;
            Matrix4f.transform (matrix, v, v);
            face.center.x = v.x;
            face.center.y = v.y;
            face.center.z = v.z;
        }

faceArray, Z:

        Arrays.sort (faceArray, FACE_DEPTH_COMPARATOR);
        //System.out.println (Arrays.toString (faceArray));

:

        float[] faceColor = new float[] { .3f, .7f, .9f, .3f };
        for (Face f: faceArray)
        {
            int[] face = faces[f.id];
            glColor4fv(faceColor);

            GL11.glBegin(GL11.GL_TRIANGLE_FAN);
            for (int vertice = 0; vertice < face.length; vertice ++)
            {
                glVertex3fv (coords[face[vertice]]);
            }
            GL11.glEnd();
        }
+1

? , OpenGL . , , , . OpenGL .

(glGetMatrix()) , /.

0

- .

, - z- ( z-).

, . , , , ( , ). , , , . BSP- Quad- R- .

, , "is-obsucred-by", , ( ), , , ( A B && B A). , , , .

EDIT:

z- , , glVertex3f(), 4D ( ), 1, , , . OpenGL 2, " ".

, - API . , OpenGL , , (, ). - ( IIUC - OpenGL , ). , - , .., - (OpenInventor - )

0

Source: https://habr.com/ru/post/1703493/


All Articles