m3gmidlet.java
来自「3D手机游戏开发实例源代码」· Java 代码 · 共 213 行
JAVA
213 行
import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.m3g.*;public class M3GMIDlet extends MIDlet { private static final String MODEL_FN = "/pogoroo.m3g"; //字符串指定文件名 private float trans[] = new float[16]; private Transform modelTransform = new Transform(); private Group modelGroup = null; // top-level Group node for the modelprivate boolean foundGroup = false; // Group node found during the search?private boolean isSceneRoot = true; // is found Group node the root of the scene graph? /** * Starts application and shows the list of samples. The user can * select a sample for execution and return to the selection screen. */ public void startApp() { System.out.println("Loading model from " + MODEL_FN); //控制台输出加载文件信息 Object3D[] parts = null; //Object3D数组用来保存场景信息 try { parts = Loader.load(MODEL_FN); //根据MODEL_FN文件名加载文件 }catch (Exception e) //捕捉异常 { e.printStackTrace(); } //输出异常信息 parseObjects(parts, "", null, 0); //解析对象数组 destroyApp(true); //结束程序 notifyDestroyed(); } // ---------------- parse model ---------------- private void parseObjects(Object3D[] parts, String section, Object3D parent, int level) /* Examine each of the objects in the parts[] array. Print each object's class name and user ID. */ { String name, subsection; for (int i=0; i < parts.length; i++) { name = parts[i].getClass().getName(); subsection = section + (i+1) + "."; printlnIndent(subsection + " " + name + ", ID: " + parts[i].getUserID(), level); parseNode(parts[i], name, parent, level); parseRefs(parts[i], subsection, level+1); } } // end of parseObjects() private void parseNode(Object3D part, String name, Object3D parent, int level) /* Store the top-level Group node in modelGroup. Examine Group, VertexBuffer, and Material nodes in more detail. */ { if (name.endsWith("Group")) { storeGroup((Group) part, parent, level); parseGroup((Group) part, level); } else if (name.endsWith("VertexBuffer")) parseVertexBuffer((VertexBuffer) part, level); else if (name.endsWith("Material")) parseMaterial( (Material) part, level); } // end of parseNode() private void storeGroup(Group g, Object3D parent, int level) /* Store the found Group node in modelGroup, and attempt to detach it from its parent if it isn't the root node of the scene. */ { if (!foundGroup) { // if not already found a Group node modelGroup = g; System.out.println("modelGroup assigned Group with ID " + g.getUserID() + "; at level " + level); foundGroup = true; if (level != 0) { // the Group is not the root node isSceneRoot = false; if (parent instanceof Group) { Group gPar = (Group) parent; gPar.removeChild(modelGroup); System.out.println("Detached modelGroup from parent with ID " + gPar.getUserID() ); } else { System.out.println("Could not detach modelGroup; set to null"); modelGroup = null; // since cannot attach it to our scene } } } } // end of storeGroup() private void parseGroup(Group g, int level) // Print the the composite transform matrix for the Group { g.getCompositeTransform(modelTransform); modelTransform.get(trans); for(int i=0; i < 16; i= i+4) printlnIndent( round2dp(trans[i]) + " " + round2dp(trans[i+1]) + " " + round2dp(trans[i+2]) + " " + round2dp(trans[i+3]), level); } // end of parseGroup() private float round2dp(float num) // round num to 2 decimal places { return ((int)((num+0.005)*100.0f))/100.0f; } private void parseVertexBuffer(VertexBuffer vb, int level) /* Print information about the VertexBuffer's contents: the positions, colours, normals, and number of textures. */ { float[] scaleBias = new float[4]; VertexArray va = vb.getPositions(scaleBias); printlnIndent("Positions: " + (va != null) + "; No. Coords: " + (vb.getVertexCount()/3), level); if (va != null) printlnIndent("Posn ID: " + va.getUserID() + "; User Object: " + (va.getUserObject() != null), level); printlnIndent("Scale/Bias: " + round2dp(scaleBias[0]) + " " + round2dp(scaleBias[1]) + " " + round2dp(scaleBias[2]) + " " + round2dp(scaleBias[3]), level); int texCount = 0; while (true) { if (vb.getTexCoords(texCount, scaleBias) == null) break; texCount++; } printlnIndent("Colours: " + (vb.getColors() != null) + "; Normals: " + (vb.getNormals() != null) + "; No. of textures: " + texCount, level); } // end of parseVertexBuffer() private void parseMaterial(Material m, int level) /* Show colour and shininess info for the material. */ { int ambColour = m.getColor(Material.AMBIENT); int diffColour = m.getColor(Material.DIFFUSE); int emisColour = m.getColor(Material.EMISSIVE); int specColour = m.getColor(Material.SPECULAR); printlnIndent("Colours: " + ambColour + ", " + diffColour + ", " + emisColour + ", " + specColour + ", " + m.getShininess(), level); printlnIndent("Colours in Hex: " + Integer.toHexString(ambColour) + ", " + Integer.toHexString(diffColour) + ", " + Integer.toHexString(emisColour) + ", " + Integer.toHexString(specColour), level); } // end of parseMaterial() private void parseRefs(Object3D obj, String section, int level) /* Examine all the references connected to obj. */ { int numRefs = obj.getReferences(null); if (numRefs > 0) { Object3D[] parts = new Object3D[numRefs]; obj.getReferences(parts); parseObjects(parts, section, obj, level); } } // end of parseRefs() private void printlnIndent(String msg, int level) // indent msg by level*2 spaces { for(int i=0; i < level; i++) System.out.print(" "); // 2 spaces System.out.println(msg); } // end of printlnIndent() /** * Not used. */ public void pauseApp() { } /** * Not used. */ public void destroyApp(boolean unconditional) { } /** * Receives the command actions from the selection screen as well as * the sample screens. * * @param command command * @param displayable source of the command */}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?