3D圆柱如何在openGl中计算vertexSize,indexesSize和textCoordinateSize

问题描述 投票:0回答:1

我正在尝试通过LWJGL绘制3D圆柱体,

并且我正在尝试生成顶点,索引和textCoordinate并将它们存储在数组中

,但是我被困在如何计算顶点,索引和textCoordinate数组的大小...等

任何人都知道我该怎么做:

这里是代码段:

 // 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);
            }
        }
    }
opengl lwjgl procedural-generation
1个回答
0
投票

httpdigest所说:

您知道每个循环执行多少次迭代,并且知道如何您为每个数组执行的许多增量/添加。应该很简单现在算数。
© www.soinside.com 2019 - 2024. All rights reserved.