android - 在 Android OpenGL ES 中使用 Wavefront .obj 的纹理坐标的问题

标签 android opengl-es texture-mapping .obj wavefront

我正在使用 openGL ES 编写一个 Android 应用程序。我遵循了一些在线教程并设法使用硬编码顶点/索引/纹理坐标加载了纹理立方体

作为下一步,我为 wavefront .obj 文件编写了一个解析器。我使用教程中的顶点等制作了一个模拟文件,加载正常。

但是,当我使用使用 3d 建模包制作的文件时,所有纹理都被弄乱了

下面是我目前获取纹理坐标的方式:

首先我加载所有的纹理坐标,vt变成一个大向量

接下来我找到每个 f 的前两个纹理坐标三角形(所以 f 1/2/3 2/5/2 3/4/1 意味着我采用第 2 个和第 5 个纹理坐标。由于 .obj 从 1 而不是 0 开始计数,我必须从位置开始 -1,然后乘以x 坐标位置的位置乘以 2 并执行相同操作,但我的 vt 数组中的 y 坐标位置 +1)

我将刚刚找到的那些纹理坐标添加到另一个向量中。

一旦我遍历了所有顶点。我将向量转换为 FloatBuffer,将其传递给 glTexCoordPointer在我的绘制方法中

下面是解析文件的代码:

private void openObjFile(String filename, Context context, GL10 gl){

    Vector<String> lines = openFile(filename, context); // opens the file

    Vector<String[]> tokens = new Vector<String[]>();

    Vector<Float> vertices = new Vector<Float>();
    Vector<Float> textureCoordinates = new Vector<Float>();
    Vector<Float> vertexNormals = new Vector<Float>();

    // tokenise
    for(int i = 0;i<lines.size();i++){
        String line = lines.get(i);
        tokens.add(line.split(" "));
    }

    for(int j = 0;j<tokens.size();j++){
        String[] linetokens = tokens.get(j);

        // get rid of comments
        //if(linetokens[0].equalsIgnoreCase("#")){
            //tokens.remove(j);
        //}


        // get texture from .mtl file
        if(linetokens[0].equalsIgnoreCase("mtllib")){
            parseMaterials(linetokens[1],context, gl);

        }

        // vertices
        if(linetokens[0].equalsIgnoreCase("v")){
            vertices.add(Float.valueOf(linetokens[1]));
            vertices.add(Float.valueOf(linetokens[2]));
            vertices.add(Float.valueOf(linetokens[3]));
        }


        // texture coordinates
        if(linetokens[0].equalsIgnoreCase("vt")){

            textureCoordinates.add(Float.valueOf(linetokens[1]));
            textureCoordinates.add(Float.valueOf(linetokens[2]));

        }

        // vertex normals
        if(linetokens[0].equalsIgnoreCase("vn")){

            vertexNormals.add(Float.valueOf(linetokens[1]));
            vertexNormals.add(Float.valueOf(linetokens[2]));
            vertexNormals.add(Float.valueOf(linetokens[3]));
        }

    }

    // vertices     
    this.vertices = GraphicsUtil.getFloatBuffer(vertices);


    Mesh mesh = null;

    Vector<Short> indices = null;
    Vector<Float> textureCoordinatesMesh = null;
    Vector<Float> vertexNormalsMesh = null;

    for(int j = 0;j<tokens.size();j++){



        String[] linetokens = tokens.get(j);

        if(linetokens[0].equalsIgnoreCase("g")){

            if(mesh!=null){

                mesh.setIndices(GraphicsUtil.getShortBuffer(indices));
                mesh.setNumindices(indices.size());
                mesh.setNormals(GraphicsUtil.getFloatBuffer(vertexNormalsMesh));
                mesh.setTextureCoordinates(GraphicsUtil.getFloatBuffer(textureCoordinatesMesh));

                meshes.add(mesh);

            }

            mesh = new Mesh();
            indices = new Vector<Short>();
            textureCoordinatesMesh = new Vector<Float>();
            vertexNormalsMesh = new Vector<Float>();


        } else if(linetokens[0].equalsIgnoreCase("usemtl")){

            String material_name = linetokens[1];

            for(int mn = 0;mn<materials.size();mn++){

                if(materials.get(mn).getName().equalsIgnoreCase(material_name)){
                    mesh.setTextureID(materials.get(mn).getTextureID());
                    mn = materials.size();
                }

            }

        } else if(linetokens[0].equalsIgnoreCase("f")){

            for(int v = 1;v<linetokens.length;v++){

                String[] vvtvn = linetokens[v].split("/");

                short index = Short.parseShort(vvtvn[0]);
                index -= 1;                 
                indices.add(index);

                if(v!=3){
                    int texturePosition = (Integer.parseInt(vvtvn[1]) - 1) * 2;
                    float xcoord = (textureCoordinates.get(texturePosition));
                    float ycoord = (textureCoordinates.get(texturePosition+1));


                    // normalise
                    if(xcoord>1 || ycoord>1){
                        xcoord = xcoord / Math.max(xcoord, ycoord);
                        ycoord = ycoord / Math.max(xcoord, ycoord);
                    }

                    textureCoordinatesMesh.add(xcoord);
                    textureCoordinatesMesh.add(ycoord);

                }                   

                int normalPosition = (Integer.parseInt(vvtvn[2]) - 1) *3;

                vertexNormalsMesh.add(vertexNormals.get(normalPosition));
                vertexNormalsMesh.add(vertexNormals.get(normalPosition)+1);
                vertexNormalsMesh.add(vertexNormals.get(normalPosition)+2);

            }

        }

    }

    if(mesh!=null){             

        mesh.setIndices(GraphicsUtil.getShortBuffer(indices));
        mesh.setNumindices(indices.size());
        mesh.setNormals(GraphicsUtil.getFloatBuffer(vertexNormalsMesh));
        mesh.setTextureCoordinates(GraphicsUtil.getFloatBuffer(textureCoordinatesMesh));

        meshes.add(mesh);
    }// Adding the final mesh
}

下面是绘图代码:

public void draw(GL10 gl){

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);

    // Pass the vertex buffer in
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0,
                             vertices);

    for(int i=0;i<meshes.size();i++){
        meshes.get(i).draw(gl);
    }

    // Disable the buffers

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

public void draw(GL10 gl){



    if(textureID>=0){

        // Enable Textures
        gl.glEnable(GL10.GL_TEXTURE_2D);

        // Get specific texture.
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);

        // Use UV coordinates.
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Pass in texture coordinates
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordinates);

    } 

    // Pass in texture normals
    gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);

    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);



        gl.glDrawElements(GL10.GL_TRIANGLES, numindices,GL10.GL_UNSIGNED_SHORT, indices);


    if(textureID>=0){
        // Disable buffers
        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

}

我真的很感激任何帮助。不能完全从文件加载模型令人沮丧,我真的不确定我做错了什么或遗漏了什么

最佳答案

我不得不承认,您的代码框架让我有些困惑。我认为会成为问题的具体事项:

  • 您拒绝将纹理坐标复制到与任何面关联的第三个顶点的最终网格列表;这应该会使你所有的坐标在前两个之后不同步
  • 你的纹理坐标归一化步骤是不必要的——在某种程度上我不确定它为什么在那里——而且可能被破坏了(如果 xcoord 在第一行大于 ycoord,然后在第二行小于 ycoord 怎么办?)<
  • OBJ 将 (0, 0) 视为纹理的左上角,OpenGL 将其视为左下角,因此除非您在未显示的代码中将纹理矩阵堆栈设置为反转纹理坐标,否则您需要自己反转它们,例如textureCoordinatesMesh.add(1.0 - ycoord);

除此之外,我确定您已经很清楚并且与这里的问题无关的通用 OBJ 注释是您应该期望处理不提供法线的文件和不提供法线的文件提供法线或纹理坐标(您目前假设两者都存在),OBJ 可以保存具有任意数量顶点的面,而不仅仅是三角形。但它们始终是平面和凸面的,因此您可以将它们画成扇形或将它们分解成三角形,就好像它们是扇形一样。

关于android - 在 Android OpenGL ES 中使用 Wavefront .obj 的纹理坐标的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585368/

相关文章:

从主线程中删除 C++ 多线程纹理加载器工作项

opengl - 如何使用 VAO/VBO 为相同顶点指定多个 UV 坐标?

使用 digicert 在服务器中更新证书后出现 Android ssl 证书错误

android - Cordova 错误 : Project name must not begin with a number

objective-c - OpenGL ES 混合功能,因此颜色始终显示在背景上

android - Android OpenGL 多重采样/抗锯齿问题

android - 如何在 androidTest 配置中排除或替换应用依赖项

android - 填充标题文本框后,ListView 页脚元素不可单击

iphone - 在 iOS 上使用 OpenGL ES 模拟多边形点画

math - glsl 和不同值的透视校正