home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-15 | 13.0 KB | 307 lines |
- // Parse a Sense8 NFF file and convert it to VRML 2.0 format
-
- // Written by Bernie Roehl, November 1996
-
- import java.io.*;
- import java.util.*;
- import nff.*;
-
- public class nff2vrml {
- public static void main(String args[]) {
- try {
- InputStream d;
- if (args.length == 1)
- d = new FileInputStream(args[0]);
- else
- d = System.in;
- Scene scene = new Scene(new DataInputStream(d));
- dump_scene(scene);
- }
- catch (IOException e) { System.err.println(e); }
- catch (NffSyntaxException e) { System.err.println(e); }
- }
-
- static void dump_scene(Scene scene) {
- System.out.println("#VRML V2.0 utf8");
- System.out.println("\n# Generated by nff2vrml\n");
- for (int i = 0; i < scene.getNumberOfObjects(); ++i)
- dump_object(scene.getObject(i));
- }
-
- static private boolean dumped_coordinates;
- static private boolean dumped_vertex_normals;
- static private boolean dumped_vertex_colors;
- static private boolean dumped_face_colors;
- static private boolean dumped_texture_coordinates;
- static private boolean emit_texture_coordinates = false;
-
- static void dump_object(Obj object) {
- System.out.println("DEF " + object.getName() + " Group {");
- System.out.println("\tchildren [");
- Vector textures = new Vector();
- boolean have_untextured_faces = false;
- for (int i = 0; i < object.getNumberOfFaces(); ++i) {
- Texture texture = object.getFace(i).getTexture();
- if (texture != null) {
- if (!contains_texture(textures, texture))
- textures.addElement(texture);
- }
- else
- have_untextured_faces = true;
- }
- dumped_coordinates = dumped_vertex_normals = false;
- dumped_vertex_colors = dumped_face_colors = false;
- dumped_texture_coordinates = false;
- Enumeration e = textures.elements();
- while (e.hasMoreElements()) // dump out an object for each texture
- dump_textured_object(object, (Texture) e.nextElement());
- if (have_untextured_faces)
- dump_textured_object(object, null); // untextured faces
- System.out.println("\t]");
- System.out.println("}");
- }
-
-
- static void dump_textured_object(Obj object, Texture texture) {
- if (hasAny(object, false, false)) { // no vertex normals or colors
- System.out.println("\t\tShape {");
- dump_appearance(texture);
- System.out.println("\t\t\tgeometry IndexedFaceSet {");
- System.out.println("\t\t\t\tnormalPerVertex FALSE");
- System.out.println("\t\t\t\tcolorPerVertex FALSE");
- dump_coordinates(object);
- if (texture != null)
- dump_texture_coordinates(object);
- dump_faces(object, texture, false, false);
- dump_face_colors(object, texture, false);
- System.out.println("\t\t\t}"); // end IndexedFaceSet
- System.out.println("\t\t}"); // end Shape
- }
- if (hasAny(object, false, true)) { // vertex colors, but no normals
- System.out.println("\t\tShape {");
- dump_appearance(texture);
- System.out.println("\t\t\tgeometry IndexedFaceSet {");
- System.out.println("\t\t\t\tnormalPerVertex FALSE");
- System.out.println("\t\t\t\tcolorPerVertex TRUE");
- dump_coordinates(object);
- if (texture != null)
- dump_texture_coordinates(object);
- dump_vertex_colors(object);
- dump_faces(object, texture, false, true);
- System.out.println("\t\t\t}"); // end IndexedFaceSet
- System.out.println("\t\t}"); // end Shape
- }
- if (hasAny(object, true, false)) { // vertex normals, but no colors
- System.out.println("\t\tShape {");
- dump_appearance(texture);
- System.out.println("\t\t\tgeometry IndexedFaceSet {");
- System.out.println("\t\t\t\tnormalPerVertex TRUE");
- System.out.println("\t\t\t\tcolorPerVertex FALSE");
- dump_coordinates(object);
- if (texture != null)
- dump_texture_coordinates(object);
- dump_vertex_normals(object);
- dump_face_colors(object, texture, true);
- dump_faces(object, texture, true, false);
- System.out.println("\t\t\t}"); // end IndexedFaceSet
- System.out.println("\t\t}"); // end Shape
- }
- if (hasAny(object, true, true)) { // both vertex normals and colors
- System.out.println("\t\tShape {");
- dump_appearance(texture);
- System.out.println("\t\t\tgeometry IndexedFaceSet {");
- System.out.println("\t\t\t\tnormalPerVertex TRUE");
- System.out.println("\t\t\t\tcolorPerVertex TRUE");
- dump_coordinates(object);
- if (texture != null)
- dump_texture_coordinates(object);
- dump_vertex_colors(object);
- dump_vertex_normals(object);
- dump_faces(object, texture, true, true);
- System.out.println("\t\t\t}"); // end IndexedFaceSet
- System.out.println("\t\t}"); // end Shape
- }
- }
-
- static void dump_coordinates(Obj object) {
- if (dumped_coordinates == true)
- System.out.println("\t\t\t\tcoord USE C");
- else {
- System.out.println("\t\t\t\tcoord DEF C Coordinate {");
- System.out.println("\t\t\t\t\tpoint [");
- for (int i = 0; i < object.getNumberOfVertices(); ++i)
- System.out.println("\t\t\t\t\t\t" +
- object.getVertex(i).getPosition());
- System.out.println("\t\t\t\t\t]");
- System.out.println("\t\t\t\t}");
- dumped_coordinates = true;
- }
- }
-
- static void dump_vertex_normals(Obj object) {
- if (dumped_vertex_normals == true)
- System.out.println("\t\t\t\tnormal USE N");
- else {
- System.out.println("\t\t\t\tnormal DEF N Normal {");
- System.out.println("\t\t\t\t\tvector [");
- for (int i = 0; i < object.getNumberOfVertices(); ++i) {
- Vec3f norm = object.getVertex(i).getNormal();
- System.out.print("\t\t\t\t\t\t");
- if (norm == null)
- System.out.println("0 0 0 # no normal");
- else
- System.out.println(norm);
- }
- System.out.println("\t\t\t\t\t]");
- System.out.println("\t\t\t\t}");
- dumped_vertex_normals = true;
- }
- }
-
- static void dump_texture_coordinates(Obj object) {
- if (!emit_texture_coordinates)
- return;
- if (dumped_texture_coordinates)
- System.out.println("\t\t\t\ttexCoord USE T");
- else {
- System.out.println("\t\t\t\ttexCoord DEF T TextureCoordinate {");
- System.out.println("\t\t\t\t\tpoint [");
- for (int i = 0; i < object.getNumberOfVertices(); ++i) {
- Vertex vertex = object.getVertex(i);
- float u = vertex.getU();
- float v = vertex.getV();
- System.out.println("\t\t\t\t\t\t" + u + " " + v);
- }
- System.out.println("\t\t\t\t\t]");
- System.out.println("\t\t\t\t}");
- dumped_texture_coordinates = true;
- }
- }
-
- static void dump_face_colors(Obj object, Texture texture, boolean use_v_normals) {
- if (dumped_face_colors)
- System.out.println("\t\t\t\tcolor USE FC");
- else {
- System.out.println("\t\t\t\tcolor DEF FC Color {");
- System.out.println("\t\t\t\t\tcolor [");
- for (int i = 0; i < object.getNumberOfFaces(); ++i) {
- Face f = object.getFace(i);
- if (f.getColor() != null)
- System.out.println("\t\t\t\t\t\t" + f.getColor());
- else
- System.out.println("\t\t\t\t\t\t0 0 0");
- }
- System.out.println("\t\t\t\t\t]");
- System.out.println("\t\t\t\t}");
- dumped_face_colors = true;
- }
- System.out.println("\t\t\t\tcolorIndex [");
- for (int i = 0; i < object.getNumberOfFaces(); ++i) {
- Face f = object.getFace(i);
- if (f.useVertexNormals != use_v_normals
- || f.useVertexColors != false) // mismatch
- continue;
- if (texture == null) { // looking for untextured faces
- if (f.getTexture() != null) // but this one's textured
- continue;
- }
- else if (!texture.match(f.getTexture())) // look for a match
- continue;
- System.out.println("\t\t\t\t\t" + i);
- if (f.isDoublesided())
- System.out.println("\t\t\t\t\t" + i);
- }
- System.out.println("\t\t\t\t]");
- }
-
- static void dump_vertex_colors(Obj object) {
- if (dumped_vertex_colors)
- System.out.println("\t\t\t\tcolor USE VC");
- else {
- System.out.println("\t\t\t\tcolor DEF VC Color {");
- System.out.println("\t\t\t\t\tcolor [");
- for (int i = 0; i < object.getNumberOfVertices(); ++i) {
- Color c = object.getVertex(i).getColor();
- System.out.print("\t\t\t\t\t\t");
- if (c == null)
- System.out.println("0 0 0");
- else
- System.out.println(c);
- }
- System.out.println("\t\t\t\t\t]");
- System.out.println("\t\t\t\t}");
- dumped_vertex_colors = true;
- }
- }
-
- static void dump_faces(Obj object, Texture texture, boolean use_v_normals, boolean use_v_colors) {
- System.out.println("\t\t\t\tcoordIndex [");
- for (int i = 0; i < object.getNumberOfFaces(); ++i) {
- Face face = object.getFace(i);
- if (face.useVertexNormals != use_v_normals
- || face.useVertexColors != use_v_colors)
- continue;
- if (texture == null) { // looking for untextured faces
- if (face.getTexture() != null) // but this one's textured
- continue;
- }
- else if (!texture.match(face.getTexture())) // look for a match
- continue;
- System.out.print("\t\t\t\t\t");
- for (int j = 0; j < face.getNumberOfPoints(); ++j)
- System.out.print(face.getPoint(j) + " ");
- System.out.println("-1");
- if (face.isDoublesided()) {
- System.out.print("\t\t\t\t\t");
- for (int j = face.getNumberOfPoints()-1; j >= 0; --j)
- System.out.print(face.getPoint(j) + " ");
- System.out.println("-1");
- }
- }
- System.out.println("\t\t\t\t]");
- }
-
- static boolean hasAny(Obj object, boolean vnormals, boolean vcolors) {
- for (int i = 0; i < object.getNumberOfFaces(); ++i) {
- Face f = object.getFace(i);
- if (f.useVertexNormals == vnormals && f.useVertexColors == vcolors)
- return true;
- }
- return false;
- }
-
- static void dump_appearance(Texture texture) {
- System.out.println("\t\t\tappearance Appearance {");
- if (texture == null) {
- System.out.println("\t\t\t\tmaterial Material { }");
- System.out.println("\t\t\t\t}");
- return;
- }
- if (texture.isShaded())
- System.out.println("\t\t\t\tmaterial Material { }");
- System.out.println("\t\t\t\ttexture ImageTexture { url \"" + texture.getFilename() + ".jpg\" }");
- System.out.println("\t\t\t\ttextureTransform TextureTransform {");
- if (texture.getScale() != 1f)
- System.out.println("\t\t\t\t\tscale " + texture.getScale() + " " + texture.getScale());
- if (texture.getTransS() != 0 || texture.getTransT() != 0)
- System.out.println("\t\t\t\t\ttranslation " + texture.getTransS() + " " + texture.getTransT());
- if (texture.getRotation() != 0)
- System.out.println("\t\t\t\t\trotation " + texture.getRotation());
- if (texture.getMirror())
- System.out.println("\t\t\t\t\t# note: mirror ignored");
- System.out.println("\t\t\t\t}");
- System.out.println("\t\t\t}");
- }
-
- static boolean contains_texture(Vector texture_list, Texture texture) {
- Enumeration e = texture_list.elements();
- while (e.hasMoreElements()) { // see if we've got this one
- Texture t = (Texture) e.nextElement();
- if (t != null)
- if (t.match(texture)) // found a match!
- return true;
- }
- return false;
- }
- }
-