1 /*
2  * $RCSfile: LwsFog.java,v $
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistribution of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc. or the names of
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * This software is provided "AS IS," without a warranty of any
23  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
24  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
26  * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
27  * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
28  * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
29  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
30  * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
31  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
32  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
33  * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or
37  * intended for use in the design, construction, operation or
38  * maintenance of any nuclear facility.
39  *
40  * $Revision: 1.4 $
41  * $Date: 2007/02/09 17:20:08 $
42  * $State: Exp $
43  */
44 
45 package com.sun.j3d.loaders.lw3d;
46 
47 
48 
49 import java.io.*;
50 import javax.media.j3d.*;
51 import javax.vecmath.*;
52 import java.util.Enumeration;
53 import com.sun.j3d.loaders.ParsingErrorException;
54 
55 
56 /**
57  * This class creates a Fog object from the data in a Scene file.
58  */
59 
60 class LwsFog extends TextfileParser {
61 
62     // data from the file
63     float minDist, maxDist, minAmount, maxAmount;
64     int backdropFog;
65     Color3f color;
66     int type;
67     Fog fogObject = null;
68 
69     /**
70      * Constructor: parses stream and stores fog data
71      */
LwsFog(StreamTokenizer st, int debugVals)72     LwsFog(StreamTokenizer st, int debugVals) throws ParsingErrorException {
73 	debugPrinter.setValidOutput(debugVals);
74 	debugOutput(TRACE, "LwsFog()");
75 	color = new Color3f(0f, 0f, 0f);
76 
77 	while (!isCurrentToken(st, "DitherIntensity")) {
78 	    debugOutputLn(LINE_TRACE, "currentToken = " + st.sval);
79 
80 	    if (isCurrentToken(st, "FogMinDist")) {
81 		minDist = (float)getNumber(st);
82 	    }
83 	    else if (isCurrentToken(st, "FogMaxDist")) {
84 		maxDist = (float)getNumber(st);
85 	    }
86 	    else if (isCurrentToken(st, "FogMinAmount")) {
87 		minAmount = (float)getNumber(st);
88 	    }
89 	    else if (isCurrentToken(st, "FogMaxAmount")) {
90 		maxAmount = (float)getNumber(st);
91 	    }
92 	    else if (isCurrentToken(st, "BackdropFog")) {
93 		backdropFog = (int)getNumber(st);
94 	    }
95 	    else if (isCurrentToken(st, "FogColor")) {
96 		color.x = (float)getNumber(st)/255f;
97 		color.y = (float)getNumber(st)/255f;
98 		color.z = (float)getNumber(st)/255f;
99 	    }
100 	    try {
101 	      st.nextToken();
102 	    }
103 	    catch (IOException e) {
104 		throw new ParsingErrorException(e.getMessage());
105 	    }
106 	}
107 	st.pushBack();   // push token back on stack
108     }
109 
110     /**
111      * Creates Java3d Fog object given the fog parameters in the file.
112      * Note that various fog parameters in lw3d are not currently handled.
113      */
createJava3dObject()114     void createJava3dObject() {
115 	// TODO:  there are various attributes of lw fog that
116 	// we're not currently handing, including non-linear fog
117 	// (need to understand the two different types - these may
118 	// map onto java3d's expontential fog node), non-solid
119 	// backdrop colors (how to handle this?), min/max amount
120 	// (j3d only handles 0 -> 1 case)
121 
122 	fogObject = new LinearFog(color, minDist, maxDist);
123 	debugOutputLn(VALUES,
124 		      "just set linearFog with color, minDist, maxDist = " +
125 		      color + ", " +
126 		      minDist + ", " +
127 		      maxDist);
128 	BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100000.0);
129 	fogObject.setInfluencingBounds(bounds);
130     }
131 
getObjectNode()132     Fog getObjectNode()
133     {
134 	return fogObject;
135     }
136 
printVals()137     void printVals()
138     {
139 	debugOutputLn(VALUES, "  FOG vals: ");
140     }
141 
142 
143 }
144