转换float数组中的blend或obj文件,以便在OpenGL中绘图

时间:2018-01-21 16:05:30

标签: java opengl blender jogl wavefront

是否可以将.blend或.obj文件转换为float数组,以便以后用于在OpenGL中进行渲染?例如, 顶点:

float[] vertices = {
    -1, -1, 0,
    // some vertices
};

纹理坐标:

float[] texCoords = {
    0, 0,
    // some texture coordinates
};

顶点索引:

float[] indices = {
    0, 1, 2,
    // some vertices indices
};

获得我们绘制的数字类型也很好:

glDrawElements(GL.GL_TRIANGLES /* type */, intbuff.capacity(), GL.GL_UNSIGNED_INT, intbuff);

有可能吗?怎么能这样做呢?

1 个答案:

答案 0 :(得分:-1)

为什么要打开.blend文件? 你需要动画吗?如果没有,您不必导入.blend文件。 对于大多数OBJ文件来说,创建自己的文件非常容易。

OBJ File Format Specifications

但这不是必要的。

以下是OBJ的完整内容:https://github.com/javagl/Obj

顺便说一下,这是我的Mesh类,只能加载单组OBJ:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class Mesh {
    public float[] vertices;
    public float[] texcoords;
    public float[] normals;
    public int[] indices;

    public Mesh(float[] vertices, float[] texcoords, float[] normals, int[] indices) {
        this.vertices = vertices;
        this.texcoords = texcoords;
        this.normals = normals;
        this.indices = indices;
    }

    public Mesh(String filename) throws FileNotFoundException, IOException {
        File f=new File(filename);
        FileInputStream fis=new FileInputStream(f);
        int c=fis.read();
        String s="";
        while (c != -1) {
            s+=(char)c;
            c=fis.read();
        }
        String[] lines=s.split("\n");
        ArrayList<Float> vertices2=new ArrayList<Float>();
        ArrayList<Float> normals2=new ArrayList<Float>();
        ArrayList<Float> texcoords2=new ArrayList<Float>();
        ArrayList<Integer> normalindices=new ArrayList<Integer>();
        ArrayList<Integer> uvindices=new ArrayList<Integer>();
        ArrayList<Integer> indices2=new ArrayList<Integer>();
        for (String line:lines) {
            String[] parts=line.split(" ");
            if (parts[0].equals("v")) {
                vertices2.add(Float.parseFloat(parts[1]));
                vertices2.add(Float.parseFloat(parts[2]));
                vertices2.add(Float.parseFloat(parts[3]));
            }
            else if (parts[0].equals("vt")) {
                texcoords2.add(Float.parseFloat(parts[1]));
                texcoords2.add(Float.parseFloat(parts[2]));
            }
            else if (parts[0].equals("vn")) {
                normals2.add(Float.parseFloat(parts[1]));
                normals2.add(Float.parseFloat(parts[2]));
                normals2.add(Float.parseFloat(parts[3]));
            }
            else if (parts[0].equals("f")) {
                for (int i=1; i < parts.length; i++) {
                    String part=parts[i];
                    String[] byslash=part.split("/");
                    int indi=Integer.parseInt(byslash[0]);
                    indices2.add(indi-1);
                    if (byslash.length > 1) {
                    indi=Integer.parseInt(byslash[1]);
                    uvindices.add(indi-1);
                    indi=Integer.parseInt(byslash[2]);
                    normalindices.add(indi-1);
                    }
                    else {
                        uvindices.add(indi-1);
                        normalindices.add(indi-1);
                    }
                }                
            }
        }
        indices=new int[indices2.size()];
        vertices=new float[indices2.size()*3];
        texcoords=new float[indices2.size()*2];
        normals=new float[indices2.size()*3];
        for (int i=0; i < indices.length; i++) {
            indices[i]=i;
            int vuvi=indices2.get(i)*3;
            int fuvi=uvindices.get(i)*2;
            int nuvi=normalindices.get(i)*3;
            vertices[i*3]=vertices2.get(vuvi);
            vertices[i*3+1]=vertices2.get(vuvi+1);
            vertices[i*3+2]=vertices2.get(vuvi+2);
            texcoords[i*2]=texcoords2.get(fuvi);
            texcoords[i*2+1]=texcoords2.get(fuvi+1);
            normals[i*3]=normals2.get(nuvi);
            normals[i*3+1]=normals2.get(nuvi+1);
            normals[i*3+2]=normals2.get(nuvi+2);
        }
    }
}

它不支持Materials,它使库(JOGL / LWJGL)独立。 希望它可以帮到你。