import java.io.*; import java.awt.*; import java.awt.image.*; import javax.swing.JFrame; public class Gama3dViewer { public static void main(String args[]) { if (args.length < 1) { System.out.println("Please specify GAMA3D file to load."); System.exit(0); } JFrame app = new JFrame(); Canvas canvas = new Gama3dCanvas(args[0]); app.setIgnoreRepaint( true ); app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); app.getContentPane().add(canvas); app.pack(); app.setVisible(true); } } class Gama3dCanvas extends Canvas { static int WIDTH = 640, HEIGHT = 480; static float CAMERA_DISTANCE = 50; Gama3dMesh gama3d; public Gama3dCanvas(String fname) { try { gama3d = new Gama3dMesh(); gama3d.load(new FileInputStream(fname)); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } setBackground(Color.black); setSize(WIDTH, HEIGHT); } public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor( Color.RED ); g2d.drawString( "Faces: " + gama3d.faceCount, 20, 20 ); g2d.drawString( "Vertices: " + gama3d.vertexCount, 20, 40 ); for (int i = 0; i < gama3d.faceCount; i++) { Point p1, p2; Face f = gama3d.getFace(i); g2d.setColor( new Color(f.colorARGB) ); p1 = project(f.v1); p2 = project(f.v2); g2d.drawLine(p1.x, p1.y, p2.x, p2.y); p1 = project(f.v2); p2 = project(f.v3); g2d.drawLine(p1.x, p1.y, p2.x, p2.y); p1 = project(f.v3); p2 = project(f.v1); g2d.drawLine(p1.x, p1.y, p2.x, p2.y); } } Point project(Vertex v) { float x, y, z; float a = (float) Math.toRadians(45); float dtc = (WIDTH/2) / (float) Math.tan(a/2); x = v.x; y = v.y; z = v.z - CAMERA_DISTANCE; x = dtc * x / -z; y = dtc * y / -z; x = x + WIDTH/2; y = -y + HEIGHT/2; return new Point((int) x, (int) y); } } class Vertex { float x, y, z; public String toString() { return "(" + x + ", " + y + ", " + z + ")"; } } class Face { Vertex v1, v2, v3; int colorARGB; public String toString() { return "(" + v1 + ", " + v2 + ", " + v3 + ")"; } } class Gama3dMesh { int version; int vertexCount; int faceCount; int colorCount; int faceColorCount; float vertices[]; short faces[]; int colors[]; short faceColors[]; public void load(InputStream is) throws Exception { DataInputStream dis = new DataInputStream(is); version = dis.readInt(); vertexCount = dis.readInt(); faceCount = dis.readInt(); colorCount = dis.readInt(); faceColorCount = dis.readInt(); if (version != 1) throw new Exception("Unsuported GAMA3D version"); vertices = new float[vertexCount * 3]; for (int i = 0; i < vertices.length; i++) vertices[i] = dis.readFloat(); faces = new short[faceCount * 3]; for (int i = 0; i < faces.length; i++) faces[i] = dis.readShort(); colors = new int[colorCount]; for (int i = 0; i < colors.length; i++) colors[i] = dis.readInt(); faceColors = new short[faceColorCount]; for (int i = 0; i < faceColors.length; i++) faceColors[i] = dis.readShort(); } Face getFace(int fIdx) { int v1Idx = faces[fIdx*3 + 0]; int v2Idx = faces[fIdx*3 + 1]; int v3Idx = faces[fIdx*3 + 2]; Face f = new Face(); f.v1 = getVertex(v1Idx); f.v2 = getVertex(v2Idx); f.v3 = getVertex(v3Idx); f.colorARGB = colors[ faceColors[fIdx] ]; return f; } Vertex getVertex(int vIdx) { Vertex v = new Vertex(); v.x = vertices[ vIdx*3 + 0 ]; v.y = vertices[ vIdx*3 + 1 ]; v.z = vertices[ vIdx*3 + 2 ]; return v; } }