001: /*
002: * $RCSfile: Material.java,v $
003: *
004: * @(#)Material.java 1.29 98/11/05 20:34:44
005: *
006: * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
009: * modify and redistribute this software in source and binary code form,
010: * provided that i) this copyright notice and license appear on all copies of
011: * the software; and ii) Licensee does not utilize the software in a manner
012: * which is disparaging to Sun.
013: *
014: * This software is provided "AS IS," without a warranty of any kind. ALL
015: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
016: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
017: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
018: * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
019: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
020: * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
021: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
022: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
023: * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGES.
025: *
026: * This software is not designed or intended for use in on-line control of
027: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
028: * the design, construction, operation or maintenance of any nuclear
029: * facility. Licensee represents and warrants that it will not use or
030: * redistribute the Software for such purposes.
031: * $Revision: 1.5 $
032: * $Date: 2006/04/18 19:33:51 $
033: * $State: Exp $
034: */
035: /*
036: * @author Rick Goldberg
037: * @author Doug Gehringer
038: * @author Nikolai V. Chr.
039: *
040: */
041: package org.jdesktop.j3d.loaders.vrml97.impl;
042:
043: import java.beans.*;
044: import javax.vecmath.Color3f;
045:
046: /** Description of the Class */
047: public class Material extends Node {
048:
049: javax.media.j3d.Material impl;
050:
051: // exposedFields
052: SFFloat ambientIntensity;
053: SFColor diffuseColor;
054: SFColor emissiveColor;
055: SFFloat shininess;
056: SFColor specularColor;
057: SFFloat transparency;
058:
059: PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
060: this );
061: final public static String TRANSPARENCY = "transparency";
062:
063: Color3f ambColor = new Color3f();
064: Color3f emmColor = new Color3f();
065: Color3f diffColor = new Color3f();
066: Color3f specColor = new Color3f();
067:
068: /**
069: *Constructor for the Material object
070: *
071: *@param loader Description of the Parameter
072: */
073: public Material(Loader loader) {
074: super (loader);
075:
076: ambientIntensity = new SFFloat(0.2f);
077: diffuseColor = new SFColor(0.8f, 0.8f, 0.8f);
078: emissiveColor = new SFColor(0.0f, 0.0f, 0.0f);
079: shininess = new SFFloat(0.2f);
080: specularColor = new SFColor(0.0f, 0.0f, 0.0f);
081: transparency = new SFFloat(0.0f);
082:
083: initFields();
084: }
085:
086: /**
087: *Constructor for the Material object
088: *
089: *@param loader Description of the Parameter
090: *@param ambientIntensity Description of the Parameter
091: *@param diffuseColor Description of the Parameter
092: *@param emissiveColor Description of the Parameter
093: *@param shininess Description of the Parameter
094: *@param specularColor Description of the Parameter
095: *@param transparency Description of the Parameter
096: */
097: Material(Loader loader, SFFloat ambientIntensity,
098: SFColor diffuseColor, SFColor emissiveColor,
099: SFFloat shininess, SFColor specularColor,
100: SFFloat transparency) {
101: super (loader);
102:
103: this .ambientIntensity = ambientIntensity;
104: this .diffuseColor = diffuseColor;
105: this .emissiveColor = emissiveColor;
106: this .shininess = shininess;
107: this .specularColor = specularColor;
108: this .transparency = transparency;
109:
110: initFields();
111: }
112:
113: /** Description of the Method */
114: void initImpl() {
115: float ambIntensity = ambientIntensity.getValue();
116: ambColor.x = ambIntensity * diffuseColor.color[0];
117: ambColor.y = ambIntensity * diffuseColor.color[1];
118: ambColor.z = ambIntensity * diffuseColor.color[2];
119: emmColor.x = emissiveColor.color[0];
120: emmColor.y = emissiveColor.color[1];
121: emmColor.z = emissiveColor.color[2];
122: diffColor.x = diffuseColor.color[0];
123: diffColor.y = diffuseColor.color[1];
124: diffColor.z = diffuseColor.color[2];
125: specColor.x = specularColor.color[0];
126: specColor.y = specularColor.color[1];
127: specColor.z = specularColor.color[2];
128: float val = (shininess.value * 127.0f) + 1.0f;
129: if (val > 127.0f) {
130: val = 127.0f;
131: }// should be 128.0f
132: else if (val < 1.0f) {
133: val = 1.0f;
134: }
135: impl = new javax.media.j3d.Material(ambColor, emmColor,
136: diffColor, specColor, val);
137: propertyChangeSupport.firePropertyChange(TRANSPARENCY, null,
138: new Float(transparency.value));
139: impl.setLightingEnable(true);
140: implReady = true;
141: }
142:
143: /**
144: * Description of the Method
145: *
146: *@return Description of the Return Value
147: */
148: public Object clone() {
149:
150: Material a = new Material(loader, (SFFloat) ambientIntensity
151: .clone(), (SFColor) diffuseColor.clone(),
152: (SFColor) emissiveColor.clone(), (SFFloat) shininess
153: .clone(), (SFColor) specularColor.clone(),
154: (SFFloat) transparency.clone());
155: return a;
156: }
157:
158: /**
159: * Gets the type attribute of the Material object
160: *
161: *@return The type value
162: */
163: public String getType() {
164: return "Material";
165: }
166:
167: /**
168: * Description of the Method
169: *
170: *@param eventInName Description of the Parameter
171: *@param time Description of the Parameter
172: */
173: public void notifyMethod(String eventInName, double time) {
174: //System.out.println("Material.notifyMethod(): " + eventInName);
175: if (eventInName.equals("ambientIntensity")) {
176: float ambIntensity = ambientIntensity.value;
177: ambColor.x = ambIntensity * diffuseColor.color[0];
178: ambColor.y = ambIntensity * diffuseColor.color[1];
179: ambColor.z = ambIntensity * diffuseColor.color[2];
180: impl.setAmbientColor(ambColor);
181: } else if (eventInName.equals("diffuseColor")) {
182: float ambIntensity = ambientIntensity.value;
183: ambColor.x = ambIntensity * diffuseColor.color[0];
184: ambColor.y = ambIntensity * diffuseColor.color[1];
185: ambColor.z = ambIntensity * diffuseColor.color[2];
186: impl.setAmbientColor(ambColor);
187: diffColor.x = diffuseColor.color[0];
188: diffColor.y = diffuseColor.color[1];
189: diffColor.z = diffuseColor.color[2];
190: impl.setDiffuseColor(diffColor);
191: } else if (eventInName.equals("emissiveColor")) {
192: emmColor.x = emissiveColor.color[0];
193: emmColor.y = emissiveColor.color[1];
194: emmColor.z = emissiveColor.color[2];
195: impl.setEmissiveColor(emmColor);
196: } else if (eventInName.equals("shininess")) {
197: float val = (shininess.value * 127.0f) + 1.0f;
198: if (val > 128.0f) {
199: val = 127.0f;
200: } else if (val < 1.0f) {
201: val = 1.0f;
202: }
203: impl.setShininess(val);
204: } else if (eventInName.equals("specularColor")) {
205: specColor.x = specularColor.color[0];
206: specColor.y = specularColor.color[1];
207: specColor.z = specularColor.color[2];
208: impl.setSpecularColor(specColor);
209: } else if (eventInName.equals("transparency")) {
210: propertyChangeSupport.firePropertyChange(TRANSPARENCY,
211: null, new Float(transparency.value));
212: } else if (eventInName.equals("route_ambientIntensity")
213: || eventInName.equals("route_diffuseColor")
214: || eventInName.equals("route_emissiveColor")
215: || eventInName.equals("route_shininess")
216: || eventInName.equals("route_specularColor")) {
217: impl
218: .setCapability(javax.media.j3d.Material.ALLOW_COMPONENT_WRITE);
219: } else if (eventInName.equals("route_transparency")) {
220: //TODO: implement again
221: //implTransp.setCapability(
222: // javax.media.j3d.TransparencyAttributes.ALLOW_MODE_WRITE);
223: } else {
224: System.err.println("Material: unknown eventInName "
225: + eventInName);
226: }
227: }
228:
229: public float getTransparency() {
230: return transparency.getValue();
231: }
232:
233: public void addPropertyChangeListener(String n,
234: PropertyChangeListener l) {
235: propertyChangeSupport.addPropertyChangeListener(n, l);
236: }
237:
238: public void removePropertyChangeListener(String n,
239: PropertyChangeListener l) {
240: propertyChangeSupport.removePropertyChangeListener(n, l);
241: }
242:
243: /** Description of the Method */
244: void initFields() {
245: ambientIntensity.init(this , FieldSpec, Field.EXPOSED_FIELD,
246: "ambientIntensity");
247: diffuseColor.init(this , FieldSpec, Field.EXPOSED_FIELD,
248: "diffuseColor");
249: emissiveColor.init(this , FieldSpec, Field.EXPOSED_FIELD,
250: "emissiveColor");
251: shininess.init(this , FieldSpec, Field.EXPOSED_FIELD,
252: "shininess");
253: specularColor.init(this , FieldSpec, Field.EXPOSED_FIELD,
254: "specularColor");
255: transparency.init(this , FieldSpec, Field.EXPOSED_FIELD,
256: "transparency");
257: }
258:
259: }
|