Package com.sun.j3d.loaders.objectfile

Source Code of com.sun.j3d.loaders.objectfile.ObjectFileMaterials$ObjectFileMaterial

/*     */ package com.sun.j3d.loaders.objectfile;
/*     */
/*     */ import com.sun.j3d.loaders.ParsingErrorException;
/*     */ import com.sun.j3d.utils.image.TextureLoader;
/*     */ import java.awt.Image;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.awt.image.ImageObserver;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.Reader;
/*     */ import java.io.StringReader;
/*     */ import java.net.MalformedURLException;
/*     */ import java.net.URL;
/*     */ import java.util.HashMap;
/*     */ import javax.media.j3d.Appearance;
/*     */ import javax.media.j3d.GeometryArray;
/*     */ import javax.media.j3d.Material;
/*     */ import javax.media.j3d.Shape3D;
/*     */ import javax.media.j3d.TexCoordGeneration;
/*     */ import javax.media.j3d.Texture2D;
/*     */ import javax.media.j3d.TransparencyAttributes;
/*     */ import javax.vecmath.Color3f;
/*     */
/*     */ class ObjectFileMaterials
/*     */   implements ImageObserver
/*     */ {
/*     */   private static final int DEBUG = 0;
/*  84 */   private String curName = null;
/*  85 */   private ObjectFileMaterial cur = null;
/*     */   private HashMap materials;
/*     */   private String basePath;
/*     */   private boolean fromUrl;
/*     */
/*     */   void assignMaterial(String matName, Shape3D shape)
/*     */   {
/* 119 */     ObjectFileMaterial p = null;
/*     */
/* 123 */     Material m = new Material();
/* 124 */     p = (ObjectFileMaterial)this.materials.get(matName);
/* 125 */     Appearance a = new Appearance();
/*     */
/* 127 */     if (p != null)
/*     */     {
/* 129 */       if (p.Ka != null) m.setAmbientColor(p.Ka);
/* 130 */       if (p.Kd != null) m.setDiffuseColor(p.Kd);
/*     */
/* 133 */       if ((p.Ks != null) && (p.illum != 1)) m.setSpecularColor(p.Ks);
/* 134 */       else if (p.illum == 1) m.setSpecularColor(0.0F, 0.0F, 0.0F);
/*     */
/* 136 */       if (p.illum >= 1) m.setLightingEnable(true);
/* 137 */       else if (p.illum == 0) m.setLightingEnable(false);
/*     */
/* 139 */       if (p.Ns != -1.0F) m.setShininess(p.Ns);
/*     */
/* 141 */       if (p.t != null) {
/* 142 */         a.setTexture(p.t);
/*     */
/* 144 */         if ((((GeometryArray)shape.getGeometry()).getVertexFormat() & 0x20) == 0)
/*     */         {
/* 146 */           TexCoordGeneration tcg = new TexCoordGeneration();
/* 147 */           a.setTexCoordGeneration(tcg);
/*     */         }
/*     */       }
/*     */
/* 151 */       if (p.transparent) {
/* 152 */         a.setTransparencyAttributes(new TransparencyAttributes(1, 0.0F));
/*     */       }
/*     */     }
/* 155 */     a.setMaterial(m);
/*     */
/* 157 */     shape.setAppearance(a);
/*     */   }
/*     */
/*     */   private void readName(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 164 */     st.getToken();
/*     */
/* 166 */     if (st.ttype == -3)
/*     */     {
/* 168 */       if (this.curName != null) this.materials.put(this.curName, this.cur);
/* 169 */       this.curName = new String(st.sval);
/* 170 */       this.cur = new ObjectFileMaterial(null);
/*     */     }
/* 172 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readAmbient(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 179 */     Color3f p = new Color3f();
/*     */
/* 181 */     st.getNumber();
/* 182 */     p.x = ((float)st.nval);
/* 183 */     st.getNumber();
/* 184 */     p.y = ((float)st.nval);
/* 185 */     st.getNumber();
/* 186 */     p.z = ((float)st.nval);
/*     */
/* 188 */     this.cur.Ka = p;
/*     */
/* 190 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readDiffuse(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 197 */     Color3f p = new Color3f();
/*     */
/* 199 */     st.getNumber();
/* 200 */     p.x = ((float)st.nval);
/* 201 */     st.getNumber();
/* 202 */     p.y = ((float)st.nval);
/* 203 */     st.getNumber();
/* 204 */     p.z = ((float)st.nval);
/*     */
/* 206 */     this.cur.Kd = p;
/*     */
/* 208 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readSpecular(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 215 */     Color3f p = new Color3f();
/*     */
/* 217 */     st.getNumber();
/* 218 */     p.x = ((float)st.nval);
/* 219 */     st.getNumber();
/* 220 */     p.y = ((float)st.nval);
/* 221 */     st.getNumber();
/* 222 */     p.z = ((float)st.nval);
/*     */
/* 224 */     this.cur.Ks = p;
/*     */
/* 226 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readIllum(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 235 */     st.getNumber();
/* 236 */     this.cur.illum = ((int)st.nval);
/*     */
/* 238 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readShininess(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 247 */     st.getNumber();
/* 248 */     this.cur.Ns = ((float)st.nval);
/* 249 */     if (this.cur.Ns < 1.0F) this.cur.Ns = 1.0F;
/* 250 */     else if (this.cur.Ns > 128.0F) this.cur.Ns = 128.0F;
/*     */
/* 252 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   public void readMapKd(ObjectFileParser st)
/*     */   {
/* 260 */     st.lowerCaseMode(false);
/*     */
/* 263 */     String tFile = null;
/*     */     do {
/* 265 */       st.getToken();
/* 266 */       if (st.ttype == -3) tFile = st.sval;
/*     */     }
/* 267 */     while (st.ttype != 10);
/*     */
/* 269 */     st.lowerCaseMode(true);
/*     */
/* 271 */     if (tFile != null)
/*     */     {
/* 273 */       if (tFile.lastIndexOf('.') != -1) {
/*     */         try
/*     */         {
/* 276 */           String suffix = tFile.substring(tFile.lastIndexOf('.') + 1).toLowerCase();
/*     */
/* 279 */           TextureLoader t = null;
/*     */
/* 281 */           if ((suffix.equals("int")) || (suffix.equals("inta")) || (suffix.equals("rgb")) || (suffix.equals("rgba")) || (suffix.equals("bw")) || (suffix.equals("sgi")))
/*     */           {
/*     */             RgbFile f;
/*     */             RgbFile f;
/* 285 */             if (this.fromUrl)
/* 286 */               f = new RgbFile(new URL(this.basePath + tFile).openStream());
/*     */             else {
/* 288 */               f = new RgbFile(new FileInputStream(this.basePath + tFile));
/*     */             }
/* 290 */             BufferedImage bi = f.getImage();
/*     */
/* 292 */             boolean luminance = (suffix.equals("int")) || (suffix.equals("inta"));
/* 293 */             boolean alpha = (suffix.equals("inta")) || (suffix.equals("rgba"));
/* 294 */             this.cur.transparent = alpha;
/*     */
/* 296 */             String s = null;
/* 297 */             if ((luminance) && (alpha)) s = "LUM8_ALPHA8";
/* 298 */             else if (luminance) s = "LUMINANCE";
/* 299 */             else if (alpha) s = "RGBA"; else {
/* 300 */               s = "RGB";
/*     */             }
/* 302 */             t = new TextureLoader(bi, s, 1);
/*     */           }
/* 305 */           else if (this.fromUrl) {
/* 306 */             t = new TextureLoader(new URL(this.basePath + tFile), "RGB", 1, null);
/*     */           }
/*     */           else {
/* 309 */             t = new TextureLoader(this.basePath + tFile, "RGB", 1, null);
/*     */           }
/*     */
/* 313 */           Texture2D texture = (Texture2D)t.getTexture();
/* 314 */           if (texture != null) this.cur.t = texture;
/*     */         }
/*     */         catch (FileNotFoundException e)
/*     */         {
/*     */         }
/*     */         catch (MalformedURLException e)
/*     */         {
/*     */         }
/*     */         catch (IOException e)
/*     */         {
/*     */         }
/*     */       }
/*     */     }
/* 327 */     st.skipToNextLine();
/*     */   }
/*     */
/*     */   private void readFile(ObjectFileParser st)
/*     */     throws ParsingErrorException
/*     */   {
/* 335 */     st.getToken();
/* 336 */     while (st.ttype != -1)
/*     */     {
/* 347 */       if (st.ttype == -3) {
/* 348 */         if (st.sval.equals("newmtl"))
/* 349 */           readName(st);
/* 350 */         else if (st.sval.equals("ka"))
/* 351 */           readAmbient(st);
/* 352 */         else if (st.sval.equals("kd"))
/* 353 */           readDiffuse(st);
/* 354 */         else if (st.sval.equals("ks"))
/* 355 */           readSpecular(st);
/* 356 */         else if (st.sval.equals("illum"))
/* 357 */           readIllum(st);
/* 358 */         else if (st.sval.equals("d"))
/* 359 */           st.skipToNextLine();
/* 360 */         else if (st.sval.equals("ns"))
/* 361 */           readShininess(st);
/* 362 */         else if (st.sval.equals("tf"))
/* 363 */           st.skipToNextLine();
/* 364 */         else if (st.sval.equals("sharpness"))
/* 365 */           st.skipToNextLine();
/* 366 */         else if (st.sval.equals("map_kd"))
/* 367 */           readMapKd(st);
/* 368 */         else if (st.sval.equals("map_ka"))
/* 369 */           st.skipToNextLine();
/* 370 */         else if (st.sval.equals("map_ks"))
/* 371 */           st.skipToNextLine();
/* 372 */         else if (st.sval.equals("map_ns"))
/* 373 */           st.skipToNextLine();
/* 374 */         else if (st.sval.equals("bump")) {
/* 375 */           st.skipToNextLine();
/*     */         }
/*     */       }
/*     */
/* 379 */       st.skipToNextLine();
/*     */
/* 382 */       st.getToken();
/*     */     }
/* 384 */     if (this.curName != null) this.materials.put(this.curName, this.cur);
/*     */   }
/*     */
/*     */   void readMaterialFile(boolean fromUrl, String basePath, String fileName)
/*     */     throws ParsingErrorException
/*     */   {
/* 394 */     this.basePath = basePath;
/* 395 */     this.fromUrl = fromUrl;
/*     */     Reader reader;
/*     */     try
/*     */     {
/*     */       Reader reader;
/* 398 */       if (fromUrl) {
/* 399 */         reader = new InputStreamReader(new BufferedInputStream(new URL(basePath + fileName).openStream()));
/*     */       }
/*     */       else
/*     */       {
/* 403 */         reader = new BufferedReader(new FileReader(basePath + fileName));
/*     */       }
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 408 */       return;
/*     */     }
/*     */
/* 413 */     ObjectFileParser st = new ObjectFileParser(reader);
/* 414 */     readFile(st);
/*     */   }
/*     */
/*     */   ObjectFileMaterials()
/*     */     throws ParsingErrorException
/*     */   {
/* 421 */     Reader reader = new StringReader(new DefaultMaterials().materials);
/*     */
/* 423 */     ObjectFileParser st = new ObjectFileParser(reader);
/* 424 */     this.materials = new HashMap(50);
/* 425 */     readFile(st);
/*     */   }
/*     */
/*     */   public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
/*     */   {
/* 436 */     return (flags & 0xA0) == 0;
/*     */   }
/*     */
/*     */   private class ObjectFileMaterial
/*     */   {
/*     */     public Color3f Ka;
/*     */     public Color3f Kd;
/*     */     public Color3f Ks;
/*     */     public int illum;
/*     */     public float Ns;
/*     */     public Texture2D t;
/*     */     public boolean transparent;
/*     */
/*     */     private ObjectFileMaterial()
/*     */     {
/*     */     }
/*     */
/*     */     public void ObjectFileMaterial()
/*     */     {
/* 106 */       this.Ka = null;
/* 107 */       this.Kd = null;
/* 108 */       this.Ks = null;
/* 109 */       this.illum = -1;
/* 110 */       this.Ns = -1.0F;
/* 111 */       this.t = null;
/*     */     }
/*     */
/*     */     ObjectFileMaterial(ObjectFileMaterials.1 x1)
/*     */     {
/*  94 */       this();
/*     */     }
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name:     com.sun.j3d.loaders.objectfile.ObjectFileMaterials
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.loaders.objectfile.ObjectFileMaterials$ObjectFileMaterial

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.