Package engine.model

Source Code of engine.model.ObjModel

package engine.model;

import static engine.GfxEngine.loadMaterial;
import engine.Vec2f;
import engine.Vec3f;
import java.util.Vector;
/**
* This class handles obj file loading, parsing and drawing
*
* @author simo <simppak@gmail.com>
* @author jari <rasaari@gmail.com>
*
*/
public class ObjModel extends Model {
  protected String filePath;
 
  public ObjModel(String path) {
    super();
    if(path == null) {
      return;
    }
   
    filePath = path.trim();
    if(this.load(filePath) == false){
      return;
    }
   
    bindBuffer();
    defaultmaterial = loadMaterial(filePath.substring(0, filePath.length()-3)+"mat");
  }
 
 
  /**
   * complicated .obj fileloader
   * @param path filepath to the obj file
   *
   * @return success
   */
  public final boolean load(String path) {
    Vector<String> file = this.readFile(path);
    if(file.isEmpty()){
      System.out.println("Failed to load " + path);
      return false;
    }
   
    this.processString(file);
    this.processToTriangles();
    //this.generateNormalTangents();
    System.out.println("Loaded " + path + " tris:" + this.tri_vertcount/3 + " verts:" + this.tri_vertcount + " normals:" + this.normalcount + " lines:" + file.size());
    return true;
  }
 
  /**
   * Parses the bulk obj file strings to ordered vectors
   *
   * @param file the string to be processed
   * @return success
   */
  public boolean processString(Vector<String> file){
    String[] temp,ftemp;
    for(int i=0; i<file.size();i++){
      if(file.get(i).length() > 0){
        if(file.get(i).startsWith("v ")){
          temp = file.get(i).split("\\s+");
          this.obj_vertices.addElement(new Vec3f(Float.parseFloat(temp[1]),Float.parseFloat(temp[2]),Float.parseFloat(temp[3])));
          //System.out.println(temp[1]);
        }
        else if(file.get(i).startsWith("vt ")){
          temp = file.get(i).split("\\s+");
          this.obj_texcoords.addElement(new Vec2f(Float.parseFloat(temp[1]),Float.parseFloat(temp[2])));
        }
        else if(file.get(i).startsWith("vn ")){
          temp = file.get(i).split("\\s+");
          this.obj_normals.addElement(new Vec3f(Float.parseFloat(temp[1]),Float.parseFloat(temp[2]),Float.parseFloat(temp[3])));
        }
        else if(file.get(i).startsWith("f ")){
          temp = file.get(i).split("\\s+");
          this.tri_vertcount += temp.length-3;
          Vector<Face> fvect = new Vector<Face>();
          for(int n=1;n<temp.length;n++){
            ftemp = temp[n].split("/");
            Face ft = new Face();
            if(ftemp.length > 0){
              ft.v = Integer.parseInt(ftemp[0]);
            }
            if(ftemp.length > 1){
              ft.vt = Integer.parseInt(ftemp[1]);
            }
            if(ftemp.length > 2){
              ft.vn = Integer.parseInt(ftemp[2]);
            }
           
            fvect.addElement(ft);
          }
          this.obj_faces.addElement(fvect);
        }
      }
    }
    this.tri_vertcount *= 3;
    this.normalcount = this.obj_normals.size();
    this.texcoords_enabled = (this.obj_texcoords.size() > 0)?true:false;
    this.normals_enabled = (this.obj_normals.size() > 0)?true:false;
    return true;
  }
 
  public String getFilePath(){
    return this.filePath;
  }
};
TOP

Related Classes of engine.model.ObjModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.