001: /*
002: * $RCSfile: PhongProcessor.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.1 $
041: * $Date: 2007/08/28 16:44:32 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.loaders.collada.xml_walker;
046:
047: import java.util.List;
048: import javax.media.j3d.Appearance;
049: import javax.media.j3d.Material;
050: import javax.media.j3d.Node;
051: import javax.media.j3d.Shape3D;
052: import javax.vecmath.Color3f;
053: import javax.vecmath.Color4f;
054: import org.collada.colladaschema.CommonColorOrTextureType;
055: import org.collada.colladaschema.CommonColorOrTextureType.Color;
056: import org.collada.colladaschema.ProfileCOMMON.Technique.Phong;
057:
058: /**
059: *
060: * @author paulby
061: */
062: public class PhongProcessor extends Processor {
063:
064: private float[] ambientColor = null;
065: private float[] emissiveColor = null;
066: private float[] diffuseColor = null;
067: private float[] specularColor = null;
068: private float[] shininess = null;
069:
070: private Appearance app = null;
071:
072: public PhongProcessor(Phong phong, Processor parent) {
073: super (phong, parent);
074:
075: ambientColor = processColorOrTexture(phong.getAmbient());
076: emissiveColor = processColorOrTexture(phong.getEmission());
077: diffuseColor = processColorOrTexture(phong.getDiffuse());
078: specularColor = processColorOrTexture(phong.getSpecular());
079: // shininess = processColor(phong.getShininess());
080: }
081:
082: @Override
083: public void createSceneGraph(Node parent) {
084: assert (parent instanceof Shape3D);
085:
086: if (app == null) {
087: app = new Appearance();
088: Material mat = new Material();
089: mat.setAmbientColor(new Color3f(ambientColor));
090: if (diffuseColor.length == 3)
091: mat.setDiffuseColor(new Color3f(diffuseColor));
092: else
093: mat.setDiffuseColor(diffuseColor[0], diffuseColor[1],
094: diffuseColor[2], diffuseColor[3]);
095: mat.setEmissiveColor(new Color3f(emissiveColor));
096: mat.setSpecularColor(new Color3f(specularColor));
097:
098: if (shininess != null)
099: mat.setShininess(shininess[0]);
100:
101: app.setMaterial(mat);
102: }
103:
104: ((Shape3D) parent).setAppearance(app);
105: }
106:
107: /**
108: * Create a color3f from the supplied color
109: */
110: private float[] processColorOrTexture(
111: CommonColorOrTextureType colorOrTexture) {
112: if (colorOrTexture == null)
113: return null;
114:
115: List<Double> c = colorOrTexture.getColor().getValues();
116:
117: float[] result = new float[c.size()];
118: int i = 0;
119: for (Double d : c) {
120: result[i++] = d.floatValue();
121: }
122:
123: return result;
124: }
125:
126: }
|