001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/internalloaders/WindowFixer.java,v 1.1 2005/04/20 21:05:04 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.internalloaders;
019:
020: import java.io.*;
021: import javax.media.j3d.*;
022: import java.awt.image.RenderedImage;
023: import javax.vecmath.Color3f;
024: import com.sun.j3d.utils.image.TextureLoader;
025: import com.sun.j3d.utils.scenegraph.io.SceneGraphFileReader;
026: import com.sun.j3d.utils.scenegraph.io.SceneGraphFileWriter;
027:
028: /**
029: *
030: * Class to fix the windows in the Van demo for JavaOne
031: *
032: * @author paulby
033: * @version
034: */
035: public class WindowFixer extends java.lang.Object {
036:
037: /** Creates new WindowFixer */
038: public WindowFixer() {
039: try {
040: File file = new File("glass.j3f");
041: SceneGraphFileReader reader = new SceneGraphFileReader(file);
042:
043: BranchGroup bg = reader.readBranchGraph(0)[0];
044:
045: Shape3D shape = (Shape3D) bg.getChild(0);
046:
047: org.jdesktop.j3d.utils.scenegraph.traverser.GeometryArrayStripifier
048: .stripify(bg);
049: bg.removeAllChildren();
050: System.out
051: .println("geom " + shape.numGeometries() + " "
052: + shape.getGeometry(0) + " "
053: + shape.getGeometry(2));
054: javax.media.j3d.TriangleStripArray stripArray = (javax.media.j3d.TriangleStripArray) shape
055: .getGeometry(2);
056:
057: Appearance app = new Appearance();
058: PolygonAttributes polyAttr = new PolygonAttributes();
059: polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
060: app.setPolygonAttributes(polyAttr);
061:
062: TransparencyAttributes transAttr = new TransparencyAttributes();
063: transAttr
064: .setTransparencyMode(TransparencyAttributes.BLENDED);
065: transAttr.setTransparency(0.3f);
066: app.setTransparencyAttributes(transAttr);
067:
068: addWalls(bg);
069:
070: shape.removeAllGeometries();
071: System.out.println("Strip count "
072: + stripArray.getNumStrips());
073: int[] vertexCounts = new int[stripArray.getNumStrips()];
074: stripArray.getStripVertexCounts(vertexCounts);
075: int pos = 0;
076: Color3f windowClr = new Color3f(51f / 255f, 145f / 255f,
077: 8f / 255f);
078: for (int i = 0; i < vertexCounts.length; i++) {
079: float[] coords = new float[vertexCounts[i] * 3];
080: float[] normals = new float[coords.length];
081: //System.out.println("Getting "+pos+" "+coords.length+" "+stripArray.getVertexCount() );
082: stripArray.getCoordinates(pos, coords);
083: stripArray.getNormals(pos, normals);
084: pos += vertexCounts[i];
085: TriangleStripArray newStrip = new TriangleStripArray(
086: vertexCounts[i], TriangleArray.COORDINATES
087: | TriangleArray.NORMALS
088: | TriangleArray.COLOR_3,
089: new int[] { vertexCounts[i] });
090: newStrip.setCoordinates(0, coords);
091: newStrip.setNormals(0, normals);
092: for (int j = 0; j < normals.length / 3; j++)
093: newStrip.setColor(j, windowClr);
094: Shape3D s = new Shape3D(newStrip, app);
095: bg.addChild(s);
096: }
097:
098: //reader = new SceneGraphFileReader( new File( "geometry"+File.separatorChar+"Dodge_Caravan_Interior_95.j3f"));
099: //Transform3D t3d = new Transform3D();
100: //t3d.rotY( Math.toRadians(-90 ));
101: //TransformGroup interior = new TransformGroup( t3d );
102: //interior.addChild( reader.readBranchGraph(0)[0] );
103:
104: //bg.addChild( interior );
105:
106: Background background = new Background(new Color3f(
107: 11f / 255f, 93f / 255f, 165f / 255f));
108: background.setApplicationBounds(new BoundingSphere(
109: new javax.vecmath.Point3d(),
110: Double.POSITIVE_INFINITY));
111: bg.addChild(background);
112:
113: File outFile = new File("newglass2.j3f");
114: SceneGraphFileWriter writer = new SceneGraphFileWriter(
115: outFile, null, false, null, null);
116: writer.writeBranchGraph(bg);
117: writer.close();
118: } catch (Exception e) {
119: e.printStackTrace();
120: }
121:
122: System.exit(0);
123: }
124:
125: private void addWalls(BranchGroup bg) {
126: TriangleStripArray wallGeom = new TriangleStripArray(4,
127: TriangleStripArray.COORDINATES
128: | TriangleStripArray.TEXTURE_COORDINATE_2
129: | TriangleStripArray.COLOR_3, new int[] { 4 });
130: float w = 1.3f;
131: float base = -0.4f;
132: float top = 0.15f;
133: float offset = 1.3f;
134: wallGeom.setCoordinates(0, new float[] { -w, base, offset, w,
135: base, offset, -w, top, offset, w, top, offset });
136: wallGeom.setTextureCoordinates(0, 0, new float[] { 0f, 0f, 1f,
137: 0f, 0f, 1f, 1f, 1f });
138:
139: TriangleStripArray backWallGeom = new TriangleStripArray(4,
140: TriangleStripArray.COORDINATES
141: | TriangleStripArray.TEXTURE_COORDINATE_2
142: | TriangleStripArray.COLOR_3, new int[] { 4 });
143: backWallGeom.setCoordinates(0, new float[] { -w, base, -offset,
144: w, base, -offset, -w, top, -offset, w, top, -offset });
145: backWallGeom.setTextureCoordinates(0, 0, new float[] { 0f, 0f,
146: 1f, 0f, 0f, 1f, 1f, 1f });
147:
148: TriangleStripArray leftWallGeom = new TriangleStripArray(4,
149: TriangleStripArray.COORDINATES
150: | TriangleStripArray.TEXTURE_COORDINATE_2
151: | TriangleStripArray.COLOR_3, new int[] { 4 });
152: leftWallGeom.setCoordinates(0, new float[] { -w, base, -offset,
153: -w, base, offset, -w, top, -offset, -w, top, offset });
154:
155: leftWallGeom.setTextureCoordinates(0, 0, new float[] { 0f, 0f,
156: 1f, 0f, 0f, 1f, 1f, 1f });
157:
158: TriangleStripArray rightWallGeom = new TriangleStripArray(4,
159: TriangleStripArray.COORDINATES
160: | TriangleStripArray.TEXTURE_COORDINATE_2
161: | TriangleStripArray.COLOR_3, new int[] { 4 });
162: rightWallGeom.setCoordinates(0, new float[] { w, base, -offset,
163: w, base, offset, w, top, -offset, w, top, offset });
164:
165: rightWallGeom.setTextureCoordinates(0, 0, new float[] { 0f, 0f,
166: 1f, 0f, 0f, 1f, 1f, 1f });
167:
168: Color3f clr = new Color3f(1f, 1f, 1f);
169: for (int i = 0; i < 4; i++) {
170: wallGeom.setColor(i, clr);
171: backWallGeom.setColor(i, clr);
172: leftWallGeom.setColor(i, clr);
173: rightWallGeom.setColor(i, clr);
174: }
175:
176: PolygonAttributes polyAttr = new PolygonAttributes();
177: polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
178: Appearance wallApp = new Appearance();
179: wallApp.setPolygonAttributes(polyAttr);
180: TransparencyAttributes wallTrans = new TransparencyAttributes();
181: wallTrans.setTransparency(0.5f);
182: wallTrans.setTransparencyMode(TransparencyAttributes.BLENDED);
183: wallApp.setTransparencyAttributes(wallTrans);
184:
185: Shape3D frontWall = new Shape3D(wallGeom, wallApp);
186: bg.addChild(frontWall);
187:
188: Shape3D backWall = new Shape3D(backWallGeom, wallApp);
189: bg.addChild(backWall);
190:
191: Shape3D leftWall = new Shape3D(leftWallGeom, wallApp);
192: bg.addChild(leftWall);
193:
194: Shape3D rightWall = new Shape3D(rightWallGeom, wallApp);
195: bg.addChild(rightWall);
196:
197: TextureLoader tl = new TextureLoader("geometry"
198: + File.separatorChar + "asphlt.gif", "RGBA", null);
199: //System.out.println("Loaded Image ");
200:
201: Appearance floorApp = new Appearance();
202: TextureAttributes textureAttr = new TextureAttributes();
203: textureAttr.setTextureMode(TextureAttributes.MODULATE);
204: ImageComponent2D image = tl.getImage();
205: Texture2D floorTexture = new Texture2D(Texture2D.BASE_LEVEL,
206: Texture2D.RGBA, image.getWidth(), image.getHeight());
207: floorTexture.setImage(0, image);
208: floorApp.setTexture(floorTexture);
209: System.out.println("Got texture");
210:
211: float size = 4f;
212: float height = -0.35f;
213:
214: TriangleStripArray floorGeom = new TriangleStripArray(4,
215: TriangleArray.COORDINATES | TriangleArray.NORMALS
216: | TriangleArray.TEXTURE_COORDINATE_2,
217: new int[] { 4 });
218: floorGeom.setCoordinates(0, new float[] { -size, height, -size,
219: -size, height, size, size, height, -size, size, height,
220: size });
221: floorGeom.setNormals(0, new float[] { 0f, 1f, 0f, 0f, 1f, 0f,
222: 0f, 1f, 0f });
223: floorGeom.setTextureCoordinates(0, new float[] { 0f, 0f, 0f,
224: 5f, 5f, 0f, 5f, 5f });
225: Shape3D floor = new Shape3D(floorGeom, floorApp);
226: bg.addChild(floor);
227: }
228:
229: public static void main(String[] args) {
230: new WindowFixer();
231: }
232: }
|