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++)
{
_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 , , .)