How to load a 3d object in Android?

I have a .obj and .mtl file, but I don't know how to import it. I tried tutorials, they do not work.

+5
source share
3 answers

One way to load an object from a .obj file is to first get the input stream from the file, and then go through each line, since one line consists of one vertex. The part in which you then use the youre data from the .obj file to draw it is what you can easily find in a tutorial on the Internet, for example: http://www.droidnova.com/android-3d-game- tutorial-part-i, 312.html . If you want to penetrate deeper into Opengl and study actively, you can use the online tutorial Nehe allmightys, which covers a lot and goes pretty deep into OpenGL programming: http://nehe.gamedev.net/ .

In any case, loading the .obj file may begin with something like this:

        while ((line = reader.readLine()) != null)
    {
        if (line.startsWith("f"))
        {
            faces++;
            processFLine(line);
        } else if (line.startsWith("vn"))
        {
            normals++;
            processVNLine(line);
        } else if (line.startsWith("vt"))
        {
            UVCoords++;
            processVTLine(line);
        } else if (line.startsWith("v"))
        {
            vertices++;
            processVLine(line);
        }
    }

, , 'f', , thoose -, Vector:

    private void processVNLine(String line)
{
    String[] tokens = line.split("[ ]+");
    int c = tokens.length;
    for (int i = 1; i < c; i++)
    { // add the normals to the normal vector
        _vn.add(Float.valueOf(tokens[i]));
    }
}

( 'f', , "/" :

    private void processFLine(String line)
{
    String[] tokens = line.split("[ ]+");
    int c = tokens.length;

    if (tokens[1].matches("[0-9]+"))
    {
        caseFEqOne(tokens, c);
    }
    if (tokens[1].matches("[0-9]+/[0-9]+"))
    {
        caseFEqTwo(tokens, c);
    }
    if (tokens[1].matches("[0-9]+//[0-9]+"))
    {
        caseFEqOneAndThree(tokens, c);
    }
    if (tokens[1].matches("[0-9]+/[0-9]+/[0-9]+"))
    {
        caseFEqThree(tokens, c);
    }
}  

, v/vt/vn.

, , v/vt. .

    private void caseFEqTwo(String[] tokens, int c)
{
    for (int i = 1; i < c; i++)
    {
        Short s = Short.valueOf(tokens[i].split("/")[0]);
        s--;
        _vPointer.add(s);

        s = Short.valueOf(tokens[i].split("/")[1]);
        s--;
        _vtPointer.add(s);
    }
}

, .

, , (v, vt vn). . , , 0 .

, , .

, , :

private void reArrange()
{
    Iterator<Short> i;
    short s;

    i = _vPointer.iterator();
    while (i.hasNext())
    {
        s = (short) (i.next() * 3);
        for (int k = 0; k < 3; k++)
        {
            _vResult.add(_v.get(s + k));
        }
    }

    i = _vnPointer.iterator();
    while (i.hasNext())
    {
        s = (short) (i.next() * 3);
        for (int k = 0; k < 3; k++)
        {
            _vnResult.add(_vn.get(s + k));
        }
    }

    i = _vtPointer.iterator();
    while (i.hasNext())
    {
        s = (short) (i.next() * 2);
        for (int k = 0; k < 2; k++)
        {
            _vtResult.add(1f - _vt.get(s + k));
        }
    }

    _indices = new short[faces * 3];
    for (short k = 0; k < faces * 3; k++)
    {
        _indices[k] = k;
    }
}

.obj. , thoose FloatBuffers ShortBuffer, , 0,1,2,3,4,5... .., thoose OpenGL-ES .

( , , - , , , , , )

, .mtl, , . , : http://people.sc.fsu.edu/~jburkardt/data/mtl/mtl.html

, , , , .

- , .

( , .obj, 9 , , .)

+8

OpenGL . , . 3D-, .. - , , .

+3

... : min3D 3d- android

syeps:

1) Rajawali lib.

2) RenderActivity

Object3dContainer faceObject3D;       private Object3dContainer objModel;

    @Override 
    public void initScene() {
        scene.lights().add(new Light());
        scene.lights().add(new Light());

        Light myLight = new Light();
        myLight.position.setZ(150);
        scene.lights().add(myLight);

        IParser parser = Parser.createParser(Parser.Type.OBJ,
                getResources(), "com.azoi.opengltutor:raw/monster_high", true);
        parser.parse();

        objModel = parser.getParsedObject();
        objModel.scale().x = objModel.scale().y = objModel.scale().z = .7f;
        scene.addChild(objModel);
    }

ur 3d- obj res/raw directory... it monster_high

... :).

3d- updateScene (..):

public void updateScene() {
    super.updateScene();

    objModel.rotation().x++;
    objModel.rotation().y--;
    objModel.rotation().z++;
}

http://code.google.com/p/min3d/wiki/HowToLoadObjFile :)

+1

All Articles