3D Cylinder, как рассчитать vertexSize, indicesSize и textCoordinateSize В openGl - PullRequest
0 голосов
/ 04 мая 2020

Я пытаюсь нарисовать трехмерный цилиндр с помощью LWJGL,

, и я пытаюсь сгенерировать вершины, индексы и координаты текста и сохранить их в массивах

, но я застрял, как рассчитать размер вершин, индексов и массивов textCoordinate ... et c.

кто-нибудь знает, как я могу это сделать, пожалуйста:

Вот фрагмент код:

 // generate vertices for a cylinder
    void buildVerticesSmooth() {

      //=====>  vertices = new float[];  <========
     //=====>  normals = new float[];    <========
    //=====>   texcoords = new float[];  <========

        int texCoordsIndex = -1;
        int verticesIndex = -1;
        int normalsIndex = -1;
        int indicesIndex = -1;        // get unit circle vectors on XY-plane
        float[] unitVertices = getUnitCircleVertices();

        // put side vertices to arrays
        for (int i = 0; i < 2; ++i) {
            float h = -height / 2.0f + i * height;           // z value; -h/2 to h/2
            float t = 1.0f - i;                              // vertical tex coord; 1 to 0

            for (int j = 0, k = 0; j <= sectors; ++j, k += 3) {
                float ux = unitVertices[k];
                float uy = unitVertices[k + 1];
                float uz = unitVertices[k + 2];
                // position vector
                vertices[++verticesIndex] = (ux * radius);             // vx
                vertices[++verticesIndex] = (uy * radius);             // vy
                vertices[++verticesIndex] = (h);                       // vz
                // normal vector
                normals[++normalsIndex] = (ux);                       // nx
                normals[++normalsIndex] = (uy);                       // ny
                normals[++normalsIndex] = (uz);                       // nz
                // texture coordinate
                texcoords[++texCoordsIndex] = ((float) j / sectors); // s
                texcoords[++texCoordsIndex] = (t);                      // t
            }
        }

        // the starting index for the base/top surface
        //NOTE: it is used for generating indices later
        int baseCenterIndex = vertices.length / 3;
        int topCenterIndex = baseCenterIndex + sectors + 1; // include center vertex

        // put base and top vertices to arrays
        for (int i = 0; i < 2; ++i) {
            float h = -height / 2.0f + i * height;           // z value; -h/2 to h/2
            float nz = -1 + i * 2;                           // z value of normal; -1 to 1

            // center point
            vertices[++verticesIndex] = 0;
            vertices[++verticesIndex] = 0;
            vertices[++verticesIndex] = h;
            normals[++normalsIndex] = 0;
            normals[++normalsIndex] = 0;
            normals[++normalsIndex] = nz;
            texcoords[++texCoordsIndex] = 0.5f;
            texcoords[++texCoordsIndex] = 0.5f;

            for (int j = 0, k = 0; j < sectors; ++j, k += 3) {
                float ux = unitVertices[k];
                float uy = unitVertices[k + 1];
                // position vector
                vertices[++verticesIndex] = (ux * radius);             // vx
                vertices[++verticesIndex] = (uy * radius);             // vy
                vertices[++verticesIndex] = (h);                       // vz
                // normal vector
                normals[++normalsIndex] = (0);                        // nx
                normals[++normalsIndex] = (0);                        // ny
                normals[++normalsIndex] = (nz);                       // nz
                // texture coordinate
                texcoords[++texCoordsIndex] = (-ux * 0.5f + 0.5f);      // s
                texcoords[++texCoordsIndex] = (-uy * 0.5f + 0.5f);      // t
            }
        }

        int[] indices;
        int k1 = 0;                         // 1st vertex index at base
        int k2 = sectors + 1;           // 1st vertex index at top

        // indices for the side surface
        for(int i = 0; i < sectors; ++i, ++k1, ++k2)
        {
            // 2 triangles per sector
            // k1 => k1+1 => k2
            indices[++indicesIndex] = (k1);
            indices[++indicesIndex] = (k1 + 1);
            indices[++indicesIndex] = (k2);

            // k2 => k1+1 => k2+1
            indices[++indicesIndex] = (k2);
            indices[++indicesIndex] = (k1 + 1);
            indices[++indicesIndex] = (k2 + 1);
        }

        // indices for the base surface
        // NOTE: baseCenterIndex and topCenterIndices are pre-computed during vertex generation
        // please see the previous code snippet
        for(int i = 0, k = baseCenterIndex + 1; i < sectors; ++i, ++k)
        {
            if(i < sectors - 1)
            {
                indices[++indicesIndex] = (baseCenterIndex);
                indices[++indicesIndex] = (k + 1);
                indices[++indicesIndex] = (k);
            }
            else // last triangle
            {
                indices[++indicesIndex] = (baseCenterIndex);
                indices[++indicesIndex] = (baseCenterIndex + 1);
                indices[++indicesIndex] = (k);
            }
        }

        // indices for the top surface
        for(int i = 0, k = topCenterIndex + 1; i < sectors; ++i, ++k)
        {
            if(i < sectors - 1)
            {
                indices[++indicesIndex] = (topCenterIndex);
                indices[++indicesIndex] = (k);
                indices[++indicesIndex] = (k + 1);
            }
            else // last triangle
            {
                indices[++indicesIndex] = (topCenterIndex);
                indices[++indicesIndex] = (k);
                indices[++indicesIndex] = (topCenterIndex + 1);
            }
        }
    }

1 Ответ

0 голосов
/ 08 мая 2020

Как сказал httpdigest :

вы знаете, сколько итераций выполняет каждый l oop, и знаете, сколько приращений / сложений вы делаете для каждого массива. Теперь должна быть очень простая математика.

...