[JOGL] mtl parsen

Halfbax

Erfahrenes Mitglied
Guten Tag,

ich behandle gerade das Thema OpenGL in Java. Dazu arbeite ich mich JOGL. Nun möchte ich ein Objekt laden, welches auch funktioniert. (*.obj). Gewöhnlicherweise enthält ein Objekt auch Material, welches ich derzeit nicht in der Lage bin zu laden, da ich keine Ahnung hab wofür was in dieser Datei steht. o_O

Wäre es möglich von euch Hilfe zu erhalten?

Desweiteren habe ich gerade gedankliche Probleme, die es mir nicht möglich machen den Ablauf der Integration einer MTL Datei nachzuvollziehen.

Java:
       g.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
       
       g.glBegin(GL2.GL_TRIANGLES);
       for(Face face: m.faces) {
           Float[] n1 = m.normals.get(face.normal[0].intValue() -1 );
           g.glNormal3f(n1[0], n1[1], n1[2]);
           
           Float[] v1 = m.vertices.get(face.vertex[0].intValue() -1 );
           g.glVertex3f(v1[0], v1[1], v1[2]);
           
           Float[] n2 = m.normals.get(face.normal[1].intValue() -1 );
           g.glNormal3f(n2[0], n2[1], n2[2]);
           
           Float[] v2 = m.vertices.get(face.vertex[1].intValue() -1 );
           g.glVertex3f(v2[0], v2[1], v2[2]);
           
           Float[] n3 = m.normals.get(face.normal[2].intValue() -1 );
           g.glNormal3f(n2[0], n2[1], n2[2]);
           
           Float[] v3 = m.vertices.get(face.vertex[2].intValue() -1 );
           g.glVertex3f(v3[0], v3[1], v3[2]);
       }

e.g.
Code:
# Blender3D MTL File: ext_chairs_and_table.blend
# Material Count: 9
newmtl wood
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.676069 0.557764 0.247251
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd tipical.jpg


newmtl revista_caja_falsa_datascopia
Ns 260.784314
Ka 0.000000 0.000000 0.000000
Kd 1.000000 1.000000 1.000000
Ks 1.245230 1.245230 1.245230
Ni 1.200000
d 1.000000
illum 2
map_Kd caja_falsa_datascopia.jpg


newmtl blanco_sofaEXT
Ns 0.000000
Ka 0.000000 0.000000 0.000000
Kd 1.000000 1.000000 1.000000
Ks 0.440604 0.440604 0.440604
Ni 1.000000
d 1.000000
illum 2


newmtl lateral_revista_caja_falsa_datascopia
Ns 66.666667
Ka 0.000000 0.000000 0.000000
Kd 0.747174 0.744819 0.744819
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd caja_falsa_datascopia.jpg


newmtl madera_blanca
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.800000 0.800000 0.800000
Ks 0.050000 0.050000 0.050000
Ni 1.000000
d 1.000000
illum 2
map_Kd tipical.jpg


newmtl cojoncisEXT
Ns 52.941176
Ka 0.000000 0.000000 0.000000
Kd 0.125278 0.125278 0.125278
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1


newmtl jarreo_vidre
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.002932 0.041763 0.205404
Ks 0.500000 0.500000 0.500000
Ni 1.500000
d 0.001000
illum 2


newmtl orangeglass
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.800000 0.400940 0.000000
Ks 0.500000 0.500000 0.500000
Ni 1.500000
d 0.001000
illum 2


newmtl madera_negra
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.179095 0.179095 0.179095
Ks 0.050000 0.050000 0.050000
Ni 1.000000
d 1.000000
illum 2
map_Kd tipical.jpg
 
Mittlerweile bin soweit das ich die Daten laden kann, aber ich frage mich wie diese laden soll (siehe Code#1, halt nur fürs Material).

SEHR UNSAUBER!!
Java:
    public static void loadMaterial(File f) throws FileNotFoundException, IOException {
       BufferedReader reader = new BufferedReader(new FileReader(f));

       String line;
       while ((line = reader.readLine()) != null) {
           if (line.startsWith("newmtl")) {
               line = line.substring(6).trim();
               libs.put(line, new Material(line));
               currentName = line;
           } else if (line.startsWith("Ka") || line.startsWith("Kd") || line.startsWith("Ks")
                   || line.startsWith("Tf")) {
               addReflectivityTransmiss(line);
           } else if (line.startsWith("illum")) {
               libs.get(currentName).illumModel = Integer.parseInt(line.split(" ")[1]);
           } else if (line.startsWith("d")) {
               libs.get(currentName).dFactor = Integer.parseInt(line.split(" ")[1]);
           } else if (line.startsWith("-halo")) {
           } else if (line.startsWith("Ns")) {
               libs.get(currentName).nsExponent = Integer.parseInt(line.split(" ")[1]);
           } else if (line.startsWith("sharpness")) {
           } else if (line.startsWith("Ni")) {
               libs.get(currentName).niOpticalDensity = Integer.parseInt(line.split(" ")[1]);
           } else if (line.startsWith("map_Ka")) {
               libs.get(currentName).mapKaFilename = line.split(" ")[1];
           } else if (line.startsWith("map_Kd")) {
               libs.get(currentName).mapKdFilename = line.split(" ")[1];
           } else if (line.startsWith("map_Ks")) {
               libs.get(currentName).mapKsFilename = line.split(" ")[1];
           } else if (line.startsWith("map_Ns")) {
               libs.get(currentName).mapNsFilename = line.split(" ")[1];
           } else if (line.startsWith("map_d")) {
               libs.get(currentName).mapDFilename = line.split(" ")[1];
           } else if (line.startsWith("disp")) {
           } else if (line.startsWith("decal")) {
           } else if (line.startsWith("bump")) {
           } else if (line.startsWith("refl")) {
           }
       }
       reader.close();
   }

   private static void addReflectivityTransmiss(String line) {
       double rx = Double.parseDouble(line.split(" ")[1]);
       double gy = Double.parseDouble(line.split(" ")[2]);
       double bz = Double.parseDouble(line.split(" ")[3]);

       if (line.startsWith("Ka"))
           libs.get(currentName).ka = new Double[] { rx, gy, bz };
       else if (line.startsWith("Kd"))
           libs.get(currentName).kd = new Double[] { rx, gy, bz };
       else if (line.startsWith("Ks"))
           libs.get(currentName).ks = new Double[] { rx, gy, bz };
       else if (line.startsWith("tf"))
           libs.get(currentName).tf = new Double[] { rx, gy, bz };
   }
 
Es funktioniert mittlerweile. Ich werde meinen Sourcecode im Laufe der Tage zur Hilfe der anderen teilen. Diesmal aber sauber o_O!!

LG
Leon
 

Neue Beiträge

Zurück