📄 readerobj.java
字号:
{ return ImagePersistence.importRGBA( new File(dirObj+System.getProperty("file.separator")+nomImage)); } catch(ImageNotRecognizedException inre) { return null; } } /** This method reads a polygon from a face line. It returns a set of triangles as an ArrayList of matrices. For each matrix, there is the information of a single triangle, where there are 3 vertexes with: vertex position index, texture coordinates index and vertex normal index. Note that the triangle set is builded as a triangle fan: the first vertex (p0) is a pivot which is fixed for all triangles, the second point determines the first triangle edge, and for each following vertex, a new triangle is builded. */ private static ArrayList<_ReaderObjVertex[]> readPolygonAsTriangleFan(String lineOfText) { ArrayList<_ReaderObjVertex[]> ret; StringTokenizer st = new StringTokenizer(lineOfText, " \n\r\t"); st.nextToken(); // The "f" token int numberOfTokens = st.countTokens(); _ReaderObjVertex p0 = null; _ReaderObjVertex p1 = null; _ReaderObjVertex p2 = null; _ReaderObjVertex[] aux = null; int i; ret = new ArrayList<_ReaderObjVertex[]>(); for( i = 0; i < numberOfTokens; i++ ) { String token = st.nextToken(); _ReaderObjVertex indexes = readFaceVertex(token); if( i == 0 ) { p0 = indexes; } else if( i == 1 ) { p1 = indexes; } else { p2 = indexes; aux = new _ReaderObjVertex[3]; aux[0] = new _ReaderObjVertex(p0); aux[1] = new _ReaderObjVertex(p1); aux[2] = new _ReaderObjVertex(p2); ret.add(aux); p1 = new _ReaderObjVertex(p2); } } return ret; } /** In some obj files (particulary those exported from 3DSMax) some array indexes contains negative numbers. This method is supposed to parse integer numbers from a string token, and returning the absolute value of it. */ private static int readIndexInteger(String inToken) { int val = Integer.parseInt(inToken); if ( val < 0 ) val *= -1; return val; } /** Returns three indices: vertex position, texture coordinates and normal, as a vertex */ private static _ReaderObjVertex readFaceVertex(String lineOfText) { _ReaderObjVertex ret = new _ReaderObjVertex(); StringTokenizer st=new StringTokenizer(lineOfText, "/"); if ( st.countTokens() == 2 ) { if( lineOfText.endsWith("/") ) { // Has vertex and texture try { ret.vertexPositionIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexPositionIndex = -1; } try { ret.vertexTextureCoordinateIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexTextureCoordinateIndex = -1; } ret.vertexNormalIndex = -1; } else { // Has vertex and normal try { ret.vertexPositionIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexPositionIndex = -1; } ret.vertexTextureCoordinateIndex=-1; try { ret.vertexNormalIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexNormalIndex = -1; } } } else { // Has all try { ret.vertexPositionIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexPositionIndex = -1; } try { ret.vertexTextureCoordinateIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexTextureCoordinateIndex = -1; } try { ret.vertexNormalIndex = readIndexInteger(st.nextToken()); } catch ( NumberFormatException nfe ) { ret.vertexNormalIndex = -1; } } ; return ret; } private static Vector3D readVertex(String lineOfText) { Vector3D vert = new Vector3D(); StringTokenizer st = new StringTokenizer(lineOfText); st.nextToken(); vert.x = Double.parseDouble(st.nextToken()); vert.y = Double.parseDouble(st.nextToken()); vert.z = Double.parseDouble(st.nextToken()); return vert; } private static Vector3D readVertexTexture(String lineOfText) { Vector3D vert = new Vector3D(); StringTokenizer st = new StringTokenizer(lineOfText); st.nextToken(); vert.x = Double.parseDouble(st.nextToken()); vert.y = Double.parseDouble(st.nextToken()); try { vert.z = Double.parseDouble(st.nextToken()); } catch( Exception e ) {} return vert; } private static HashMap<String, Material> readMaterials(String material, String fileName) { HashMap<String, Material> ret = new HashMap<String, Material>(); StringTokenizer st = new StringTokenizer(material, " "); st.nextToken(); // "mtlib" token File arc = new File(fileName); File dirArc = arc.getParentFile(); String nomArc; nomArc = dirArc + System.getProperty("file.separator")+st.nextToken(); try { BufferedReader in=new BufferedReader(new FileReader(nomArc)); String lineOfText=""; Material activeMaterial=new Material(); activeMaterial.setName("default"); while( (lineOfText = in.readLine()) != null ) { if ( lineOfText.startsWith("Ns") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken(); // Ns activeMaterial.setPhongExponent( Float.parseFloat(stMat.nextToken())); } if ( lineOfText.startsWith("Kd") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken(); // Kd ColorRgb color=new ColorRgb(); color.r=Float.parseFloat(stMat.nextToken()); color.g=Float.parseFloat(stMat.nextToken()); color.b=Float.parseFloat(stMat.nextToken()); activeMaterial.setDiffuse(color); } if ( lineOfText.startsWith("Ka") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken(); // Ka ColorRgb color=new ColorRgb(); color.r=Float.parseFloat(stMat.nextToken()); color.g=Float.parseFloat(stMat.nextToken()); color.b=Float.parseFloat(stMat.nextToken()); activeMaterial.setAmbient(color); } if ( lineOfText.startsWith("Ks") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken(); // Ks ColorRgb color=new ColorRgb(); color.r=Float.parseFloat(stMat.nextToken()); color.g=Float.parseFloat(stMat.nextToken()); color.b=Float.parseFloat(stMat.nextToken()); activeMaterial.setSpecular(color); } if ( lineOfText.startsWith("d") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken(); // d //activeMaterial.setAlpha(Float.parseFloat(stMat.nextToken())); } if ( lineOfText.startsWith("newmtl") ) { StringTokenizer stMat=new StringTokenizer(lineOfText, " "); stMat.nextToken();//newmtl ret.put(activeMaterial.getName(), activeMaterial); activeMaterial = new Material(); activeMaterial.setName(stMat.nextToken()); } } ret.put(activeMaterial.getName(), activeMaterial); } catch( IOException ioe ) { } return ret; } private static Material defaultMaterial() { Material m = new Material(); m.setAmbient(new ColorRgb(0.2, 0.2, 0.2)); m.setDiffuse(new ColorRgb(0.5, 0.9, 0.5)); m.setSpecular(new ColorRgb(1, 1, 1)); return m; } private static void addThing(Geometry g, ArrayList<SimpleBody> inoutSimpleBodiesArray) { if ( inoutSimpleBodiesArray == null ) return; SimpleBody thing; thing = new SimpleBody(); thing.setGeometry(g); thing.setPosition(new Vector3D()); thing.setRotation(new Matrix4x4()); thing.setRotationInverse(new Matrix4x4()); thing.setMaterial(defaultMaterial()); inoutSimpleBodiesArray.add(thing); } public static void importEnvironment(File inSceneFileFd, SimpleScene inoutSimpleScene) throws Exception { //----------------------------------------------------------------- ArrayList<SimpleBody> simpleBodiesArray = inoutSimpleScene.getSimpleBodies(); ArrayList<Light> lightsArray = inoutSimpleScene.getLights(); ArrayList<Background> backgroundsArray = inoutSimpleScene.getBackgrounds(); ArrayList<Camera> camerasArray = inoutSimpleScene.getCameras(); //----------------------------------------------------------------- TriangleMeshGroup mg = null; mg = read(inSceneFileFd.getAbsolutePath()); addThing(mg, simpleBodiesArray); }}//===========================================================================//= EOF =//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -