简体   繁体   中英

simplifying texture and corner coordinates 'openGL

this is the coordinates part of my dice (a cube covered by a dice texture). I've seen in some codes it is possible to omit the common coordinates which are pointing to same position ? any way I do it my shape gets disordered! how can I simplify it ?

here is my code --> the cube corner coordinates , the texture coordinates and the vertex indexes

final float pt2[] = new float[] { 
                0, 0, 0,
                0, 1, 0,
                1, 0, 0, 
                1, 1, 0,

                0, 0, 1,
                0, 1, 1, 
                1, 0, 1,
                1, 1, 1,

                1, 1, 1, /* 8 */
                1, 1, 0,
                0, 1, 0, 
                0, 1, 1,

                1, 0, 1, 
                1, 0, 0,
                0, 0, 0,
                0, 0, 1,

                0, 0, 1, /* 16 */
                0, 1, 1,
                1, 1, 1, 
                1, 0, 1,

                1, 0, 0,
                0, 0, 0, 
                0, 1, 0, 
                1, 1, 0 };
        final float texture[] = new float[] { 
                1f, 0.66f, 
                0.66f, 0.66f,
                0f,0.33f, 
                0.33f, 0.33f,

                1f, 0.33f, 
                0.66f, 0.33f,
                0.0f, 0.0f,
                0.33f, 0f,

                0.33f, 0f,
                0.33f, 0.33f,
                0.66f, 0.33f,
                0.66f, 0f,

                0.33f, 0.33f,
                0.33f, 0.66f,
                0.66f, 0.66f,
                0.66f, 0.33f,

                0.66f, 0f,
                0.66f, 0.33f,
                1f, 0.33f, 
                1f, 0f,

                0f, 0.33f,
                0f, 0.66f,
                0.33f, 0.66f,
                0.33f, 0.33f,

        };

        final byte[] vertexIndex = new byte[] { 
                6, 2, 3,7, //1
                5, 1, 0, 4, //2
                8,9,10, 11, //3
                15, 14, 13,12, //4
                20, 21,22, 23, //5
                16, 19, 18, 17 }; //6

Yes, as a matter of fact what you're trying to do here is definitely the way to go - only specify each vertex once. It not not only saves memory, but avoids subtle visual artifacts where 2 or more vertices (that are supposed to be in the same place) don't have exactly the same floating point values.

Here's an example that I got from a combination of my codebase and this DX tutorial.

You are using separate data structures for vertex coordinates and texture coordinates, whereas here they are all jammed together, but the idea is the same.

The important take-away here is that there are only 8 entries in the vertex table - exactly the number of vertices in a cube - nothing extra. You can have multiple references to the same vertex in your index buffer, but that's what you want - to reuse the same vertex entry.

struct MyVertex
{
    float x, y, z;
    float t, v;
};

// the cube corners, note that there are only 8 entries
// in table - no duplicates

MyVertex vx[] = 
{
    { 0.0f, 0.0f, 0.0f, 0.0f,1.0f },    // one corner of the cube
    { 0.0f, 1.0f, 0.0f, 0.0f,0.0f },
    { 1.0f, 1.0f, 0.0f, 1.0f,0.0f },
    { 1.0f, 0.0f, 0.0f, 1.0f,1.0f },
    { 0.0f, 0.0f, 1.0f, 0.0f,0.0f },
    { 1.0f, 0.0f, 1.0f, 0.0f,1.0f },
    { 1.0f, 1.0f, 1.0f, 0.0f,0.0f },
    { 0.0f, 1.0f, 1.0f, 0.0f,1.0f }
};

// index buffer that points back into vx[]

short indexBuffer[] = 
{
    0,1,2, 2,3,0,   // cube face 0 (2 tris here)
    4,5,6, 6,7,4,   // 1
    0,3,5, 5,4,0,   // 2
    3,2,6, 6,5,3,   // 3
    2,1,7, 7,6,2,   // 4
    1,0,4, 4,7,1    // 5
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM