Java 转换浮点数组中的混合或obj文件以在OpenGL中绘制

Java 转换浮点数组中的混合或obj文件以在OpenGL中绘制,java,opengl,blender,jogl,wavefront,Java,Opengl,Blender,Jogl,Wavefront,是否可以将.blend或.obj文件转换为浮点数组,以便以后在OpenGL中进行渲染?例如 顶点: float[] vertices = { -1, -1, 0, // some vertices }; 纹理坐标: float[] texCoords = { 0, 0, // some texture coordinates }; 顶点索引: float[] indices = { 0, 1, 2, // some vertices indic

是否可以将.blend或.obj文件转换为浮点数组,以便以后在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);

可能吗?如何做到这一点?

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

但这是没有必要的

以下是OBJs的完整版本:

顺便说一句,这是我的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);
        }
    }
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.ArrayList;
公共类网格{
公共浮点数;
公共浮点数;
公共浮动[]正常值;
公共综合指数;
公共网格(float[]顶点、float[]texcoords、float[]法线、int[]索引){
这个。顶点=顶点;
this.texcoords=texcoords;
this.normals=法线;
该指数=指数;
}
公共网格(字符串文件名)抛出FileNotFoundException、IOException{
文件f=新文件(文件名);
FileInputStream fis=新的FileInputStream(f);
int c=fis.read();
字符串s=“”;
而(c!=-1){
s+=(char)c;
c=fis.read();
}
字符串[]行=s.split(“\n”);
ArrayList vertices2=新的ArrayList();
ArrayList normals2=新的ArrayList();
ArrayList texcoords2=新的ArrayList();
ArrayList NormalIndexes=新的ArrayList();
ArrayList UVIndexes=新的ArrayList();
ArrayList Indicates2=新的ArrayList();
用于(字符串行:行){
String[]parts=line.split(“”);
如果(部分[0]。等于(“v”)){
vertices2.add(Float.parseFloat(第[1]部分));
vertices2.add(Float.parseFloat(第[2]部分));
vertices2.add(Float.parseFloat(第[3]部分));
}
else if(部分[0]。等于(“vt”)){
add(Float.parseFloat(第[1]部分));
add(Float.parseFloat(第[2]部分));
}
else if(部分[0]。等于(“vn”)){
normals2.add(Float.parseFloat(第[1]部分));
normals2.add(Float.parseFloat(第[2]部分));
normals2.add(Float.parseFloat(第[3]部分));
}
else if(部分[0]。等于(“f”)){
对于(int i=1;i1){
indi=Integer.parseInt(byslash[1]);
添加紫外线指数(indi-1);
indi=Integer.parseInt(byslash[2]);
加上(indi-1);
}
否则{
添加紫外线指数(indi-1);
加上(indi-1);
}
}                
}
}
索引=新的int[indices2.size()];
顶点=新的浮动[指示2.size()*3];
texcoords=新浮点[指示2.size()*2];
法线=新的浮点[指示2.size()*3];
对于(int i=0;i
它不支持材质,这使得它与库(JOGL/LWJGL)无关。
希望对您有所帮助。

您应该看看众多WaveFront OBJ导入程序的源代码,基于JOGL编写自己的源代码可能是一个很好的灵感来源,但请记住,OBJ文件也可以包含一些材料。你可以在这里查看我的源代码:@gouessej:[…]”基于JOGL“[…]库很可能不重要,除了obj文件中可能提到的材质/纹理。首先,原始海报的问题被标记为“JOGL”。其次,库很重要,因为到OpenGL和OpenGL ES API的绑定可能使用不同的语法,JOGL有自己的API来处理纹理和图像,即使仍然可以使用较低级别的调用。此外,每个scenegraph API或框架都有自己的API来操作转换、矩阵等。有些引擎只支持可编程管道,有些引擎只支持固定管道。最后,原始海报提到了纹理坐标。JOGL演示中有一个OBJ导入器:JOGL Utils中有另一个:两者都不如我的完整。选择一个最适合你需要的。问题被标记为“jogl”,原始海报在他的源代码中提到了纹理坐标,这似乎表明他需要纹理支持。如果不能绘制纹理,为什么要读取纹理坐标?网上有很多更完整、更经过测试的OBJ进口商。没有必要在更糟糕的情况下重新发明轮子。Github上的elect86在jassimp上工作,在JMonkeyEngine、LibGDX、Unlicense等中有这种格式的导入程序。。。jReality和JogAmp的Ardor3D续集中都有导入器和导出器。@gouessej将向您解释:一些OBJ文件使用MTL。这定义了所需的文本U