Загрузчик файлов Android .obj в Eclipse - PullRequest
1 голос
/ 18 июля 2011

Я новичок в android и opengl es. Здесь я попытался сделать загрузчик файлов .objt для andriod с помощью Eclipse. Вот мой код для файла сетки. Я сохранил объектный файл в папке сборки. Вот код:

public class Mesh {
    List<String> LINes;
    float[] vertices; 
    float[] normals ;
    float[] uv ;

    // List<String> lines;
    int numVertices = 0;
    int numNormals = 0;
    int numUV = 0;
    int numFaces = 0;

    short[] facesVerts;
    short[] facesNormals;
    short[] facesUV ;
    int vertexIndex = 0;
    int normalIndex = 0;
    int uvIndex = 0;
    int faceIndex = 0;
    ShortBuffer index;
    ShortBuffer mnormal;
    ShortBuffer text;
    FloatBuffer vertex;
    FloatBuffer normal;
    FloatBuffer texcoords;
    int textureId=-1;
    int ilength;

    public Model(GL10 gl, Context parent) {

        //Meshes = new Vector<Mesh>();

        openObjFile("tetrahedron.obj", parent, gl);

    }

    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, vertex);
        if(textureId>=0){

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

            gl.glActiveTexture(GL10.GL_TEXTURE0);

            gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
            gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

            gl.glEnable(GL10.GL_TEXTURE_2D);

            gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);


            // Pass in texture normals
            gl.glNormalPointer(GL10.GL_FLOAT, 0,mnormal);
            // Pass in texture coordinates
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, text);
        }

        //gl.glColor4f (1.0f, 1.0f, 1.0f, 1.0f);

        // Draw the triangles

        gl.glDrawElements(GL10.GL_TRIANGLES, ilength, GL10.GL_UNSIGNED_SHORT, index);

        if(textureId>=0){

            gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            // Disable the use of textures.
            gl.glDisable(GL10.GL_TEXTURE_2D);
        }
        // Disable the buffers

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    }

    // reads the model file in
    private List<String> openFile(String filename, Context context) {

        InputStream instream = null;

        List<String> LINes = new ArrayList<String>();

        try {
            instream = context.getAssets().open(filename);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(instream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLINE;
            // Read File LINE By LINE
            while ((strLINE = br.readLine()) != null) {
                LINes.add(strLINE);
            }
            // Close the Input stream
            instream.close();

        } catch (Exception e) {// Catch exception if any
            e.toString();
        }

        return LINes;
    }

    // high level model file parser
    private void openObjFile(String filename, Context context, GL10 gl) {

        List<String> LINes = openFile(filename, context); // opens the file
        vertices = new float[LINes.size() * 3];
        normals = new float[LINes.size() * 3];
        uv = new float[LINes.size() * 2];
        facesVerts = new short[LINes.size() * 3];
        facesNormals = new short[LINes.size() * 3];
        facesUV = new short[LINes.size() * 3];

        for (int i = 0; i < LINes.size(); i++) {
            String line = LINes.get(i);
            if (line.startsWith("v ")) {
                String[] tokens = line.split("[ ]+");
                vertices[vertexIndex] = Float.parseFloat(tokens[1]);
                vertices[vertexIndex + 1] = Float.parseFloat(tokens[2]);
                vertices[vertexIndex + 2] = Float.parseFloat(tokens[3]);
                vertexIndex += 3;
                numVertices++;
                continue;
            }

            if (line.startsWith("vn ")) {
                String[] tokens = line.split("[ ]+");
                normals[normalIndex] = Float.parseFloat(tokens[1]);
                normals[normalIndex + 1] = Float.parseFloat(tokens[2]);
                normals[normalIndex + 2] = Float.parseFloat(tokens[3]);
                normalIndex += 3;
                numNormals++;
                continue;
            }

            if (line.startsWith("vt")) {
                String[] tokens = line.split("[ ]+");
                uv[uvIndex] = Float.parseFloat(tokens[1]);
                uv[uvIndex + 1] = Float.parseFloat(tokens[2]);
                uvIndex += 2;
                numUV++;
                continue;
            }

            if (line.startsWith("f ")) {
                String[] tokens = line.split("[ ]+");

                String[] parts = tokens[1].split("/");
                facesVerts[faceIndex] = getIndexs(parts[0], numVertices);
                if (parts.length > 2)
                    facesNormals[faceIndex] = getIndexs(parts[2], numNormals);
                if (parts.length > 1)
                    facesUV[faceIndex] = getIndexs(parts[1], numUV);
                faceIndex++;

                parts = tokens[2].split("/");
                facesVerts[faceIndex] = getIndexs(parts[0], numVertices);
                if (parts.length > 2)
                    facesNormals[faceIndex] = getIndexs(parts[2], numNormals);
                if (parts.length > 1)
                    facesUV[faceIndex] = getIndexs(parts[1], numUV);
                faceIndex++;

                parts = tokens[3].split("/");
                facesVerts[faceIndex] = getIndexs(parts[0], numVertices);
                if (parts.length > 2)
                    facesNormals[faceIndex] = getIndexs(parts[2], numNormals);
                if (parts.length > 1)
                    facesUV[faceIndex] = getIndexs(parts[1], numUV);
                faceIndex++;
                numFaces++;
                continue;
            }
        }
        index=createsBuffer(facesVerts);
        ilength=facesVerts.length;
        mnormal=createsBuffer(facesNormals);
        text=createsBuffer(facesUV);
        vertex=createfBuffer(vertices);
        normal=createfBuffer(normals);
        texcoords=createfBuffer(uv);
        // Meshes.add(new Mesh(text,mnormal, index, textureId, facesVerts.length ));

    }

    ShortBuffer createsBuffer(short[] indices) {

        ShortBuffer sbuffer;
        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        sbuffer = ibb.asShortBuffer();
        sbuffer.put(indices);
        sbuffer.position(0);
        return sbuffer;

    }

    FloatBuffer createfBuffer(float[] indices) {

        FloatBuffer fbuffer;
        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 4);
        ibb.order(ByteOrder.nativeOrder());
        fbuffer = ibb.asFloatBuffer();
        fbuffer.put(indices);
        fbuffer.position(0);
        return fbuffer;

    }

    short getIndexs(String index,int size){
        short idx = Short.parseShort(index);
        if (idx < 0)
            return (short) (size + idx);
        else
            return (short) (idx-1);
    }

    int getIndex(String index, int size) {
        int idx = Integer.parseInt(index);
        if (idx < 0)
            return size + idx;
        else
            return idx - 1;
    }

}

Но код не работает, и я получаю только черный экран в качестве вывода. Я очень признателен за любую помощь с этим. Я не понимаю, что я делаю неправильно или отсутствует здесь. Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 31 января 2013

Вы пробовали использовать библиотеку min3d и пример проекта?

Я только что видел это. И это хороший загрузчик для файлового загрузчика .obj.

Пожалуйста, отметьте это здесь.

Надеюсь, это поможет вам.

0 голосов
/ 01 августа 2011

если бы вы пробовали

gl.glTranslatef(0, 0, -4);

вы, возможно, не рисуете свой OBJ там, где вы можете его видеть, как обычно он рисуется прямо над видом.Без перевода, который переместит его в вашу сцену, вы не сможете его увидеть.

...