繁体   English   中英

OpenGL ES2:顶点指数和纹理坐标?

[英]OpenGL ES2 : Vertex Indices and Texture coordinates?

我使用顶点索引来渲染我的网格。我没有问题传递顶点索引和顶点位置。

但我想知道在纹理坐标的情况下,我是否应该使用指数将它们传递给它们?

编辑:我试图传递和渲染OBJ文件,并混淆如何处理“fx / y / z”中描述的纹理坐标

例如,在立方体的情况下,我传递我的顶点坐标:

* 8顶点坐标

* 36顶点指数:

传递纹理(甚至法线)时,我应该将它们传递为:

* (8个纹理坐标+36个纹理坐标指数) 如果是这样,如何在渲染过程中分配它们?

要么

* (36个纹理坐标)

这是我的抽象网格类,

public class AbstractMesh3D {
    //private int buffers[]={0,0};
    private int vbVertices[]=new int[1];
    private int vbIndices[]=new int[1];
    private int vbNormals[]=new int[1];
    private int vbColors[]=new int[1];
    private int vbTextureCords[]=new int[1];
    private boolean bVertices=false;
    private boolean bIndices=false;
    private boolean bNormals=false;
    private int numOfVertices;
    private int numOfIndices;
    private int numOfNormals;
    private World3D _world;
    private String textureId;

    public void setTextureId(String textureId) {
        this.textureId = textureId;
    }




    public AbstractMesh3D(){


    }
    public void setWorld(World3D world){
        this._world=world;
    }
    public void setVertexBuffer(float vertexData[]){
        GLES20.glGenBuffers(1, vbVertices, 0);
        //Short
        FloatBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(vertexData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        vertexFloatBuffer.put(vertexData);
        vertexFloatBuffer.position(0);

        //GLES20.glGenBuffers(1, vbVertices, 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbVertices[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.length*4, vertexFloatBuffer, GLES20.GL_STATIC_DRAW);
        numOfVertices=vertexData.length/3;
        bVertices=true;

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);


        //vertexFloatBuffer.
    }

    public void setNormalBuffer(float normalData[]){
        GLES20.glGenBuffers(1, vbNormals, 0);
        //Short
        FloatBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(normalData.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        vertexFloatBuffer.put(normalData);
        vertexFloatBuffer.position(0);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbNormals[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, normalData.length*4, vertexFloatBuffer, GLES20.GL_STATIC_DRAW);
        bNormals=true;
        numOfNormals=normalData.length/3;

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

    }

    public void setIndices(short indices[]){
        GLES20.glGenBuffers(1, vbIndices, 0);
        ShortBuffer vertexFloatBuffer=ByteBuffer.allocateDirect(indices.length*2).order(ByteOrder.nativeOrder()).asShortBuffer();
        vertexFloatBuffer.put(indices);
        vertexFloatBuffer.position(0);

        //GLES20.glGenBuffers(1, buffers[1], 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vbIndices[0]);
        GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indices.length*2, vertexFloatBuffer, GLES20.GL_STATIC_DRAW);
        numOfIndices=indices.length;
        bIndices=true;

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

    }

    public void render(){
        int posHandle=_world.getOgl_aPositionHandle();
        int normalHandle=_world.getOgl_aNormalHandle();

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbVertices[0]);
        GLES20.glEnableVertexAttribArray(posHandle);
        GLES20.glVertexAttribPointer(posHandle, 3, GLES20.GL_FLOAT, false, 12, 0);

        if(bNormals){
            GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbNormals[0]);
            GLES20.glEnableVertexAttribArray(normalHandle);
            GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT, false, 12, 0);
        }

        if(bIndices){   
            //System.out.println("render indices");
            GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, vbIndices[0]);

            GLES20.glDrawElements(GLES20.GL_TRIANGLES, numOfIndices, GLES20.GL_UNSIGNED_SHORT, 0);          

        }else{
            GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, numOfVertices);
        }
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

        GLES20.glDisableVertexAttribArray(posHandle);
        GLES20.glDisableVertexAttribArray(normalHandle);
        //System.out.println("rendered");





    }

}

您必须重新排序texcoords和顶点,以便它们共享相同的索引。 您不能为不同的属性使用单独的索引。

始终顶点的数量必须等于texcoords的数量必须等于法线的数量等。

解析OBJ时,这将涉及复制一些顶点/ texcoords,以便您可以满足此要求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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