001: /*
002: * $RCSfile: LwsBackground.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:08 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.io.*;
048: import javax.media.j3d.*;
049: import javax.vecmath.*;
050: import java.util.Enumeration;
051: import com.sun.j3d.loaders.ParsingErrorException;
052:
053: /**
054: * This class creates a Background object (solid color only, no geometry)
055: * according to some of the data stored in a Scene file. Note: Lightwave
056: * defines much more complex backgrounds that the loader currently
057: * handles. It should be possible to se Background Geometry to handle
058: * most of these cases, if there's time and will to work on the problem.
059: */
060:
061: class LwsBackground extends TextfileParser {
062:
063: // data from the file
064: int solidBackdrop;
065: Color3f color, zenithColor, skyColor, groundColor, nadirColor;
066: Background backgroundObject = null;
067:
068: /**
069: * Constructor: parses stream and retrieves all Background-related data
070: */
071: LwsBackground(StreamTokenizer st, int debugVals)
072: throws ParsingErrorException {
073:
074: debugPrinter.setValidOutput(debugVals);
075: debugOutput(TRACE, "LwsBackground()");
076: color = new Color3f(0f, 0f, 0f);
077: zenithColor = new Color3f(0f, 0f, 0f);
078: skyColor = new Color3f(0f, 0f, 0f);
079: groundColor = new Color3f(0f, 0f, 0f);
080: nadirColor = new Color3f(0f, 0f, 0f);
081:
082: solidBackdrop = (int) getNumber(st);
083: while (!isCurrentToken(st, "FogType")) {
084: debugOutputLn(LINE_TRACE, "currentToken = " + st.sval);
085:
086: if (isCurrentToken(st, "BackdropColor")) {
087: color.x = (float) getNumber(st) / 255f;
088: color.y = (float) getNumber(st) / 255f;
089: color.z = (float) getNumber(st) / 255f;
090: } else if (isCurrentToken(st, "NadirColor")) {
091: nadirColor.x = (float) getNumber(st) / 255f;
092: nadirColor.y = (float) getNumber(st) / 255f;
093: nadirColor.z = (float) getNumber(st) / 255f;
094: } else if (isCurrentToken(st, "SkyColor")) {
095: skyColor.x = (float) getNumber(st) / 255f;
096: skyColor.y = (float) getNumber(st) / 255f;
097: skyColor.z = (float) getNumber(st) / 255f;
098: } else if (isCurrentToken(st, "GroundColor")) {
099: groundColor.x = (float) getNumber(st) / 255f;
100: groundColor.y = (float) getNumber(st) / 255f;
101: groundColor.z = (float) getNumber(st) / 255f;
102: } else if (isCurrentToken(st, "NadirColor")) {
103: nadirColor.x = (float) getNumber(st) / 255f;
104: nadirColor.y = (float) getNumber(st) / 255f;
105: nadirColor.z = (float) getNumber(st) / 255f;
106: }
107: try {
108: st.nextToken();
109: } catch (IOException e) {
110: throw new ParsingErrorException(e.getMessage());
111: }
112: }
113: st.pushBack(); // push token back on stack
114: }
115:
116: /**
117: * Creates Java3d objects from the background data. Note that there
118: * are plenty of lw3d background attributes that the loader currently
119: * ignores. Some of these may best be handled by creating background
120: * geometry rather than a solid background color
121: */
122: void createJava3dObject() {
123: // TODO: there are various attributes of
124: // backdrops that we're not currently handling. In
125: // particular, if the file calls for a gradient background
126: // (solidBackdrop == 0), we ignore the request and just
127: // create a solid background from the sky color instead.
128: // We should be able to do something with the
129: // various colors specified, perhaps by creating
130: // background geometry with the appropriate vertex
131: // colors?
132:
133: if (solidBackdrop != 0) {
134: backgroundObject = new Background(color);
135: debugOutput(VALUES, "Background color = " + color);
136: } else {
137: backgroundObject = new Background(skyColor);
138: debugOutput(VALUES, "Background color = " + skyColor);
139: }
140: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
141: 0.0, 0.0), 100000.0);
142: backgroundObject.setApplicationBounds(bounds);
143: }
144:
145: Background getObjectNode() {
146: return backgroundObject;
147: }
148:
149: void printVals() {
150: debugOutputLn(VALUES, " BACKGROUND vals: ");
151: }
152:
153: }
|