Normand Briere
2019-11-21 ddb10cb84dddfeef1ef9946f2e13cef3c93e6cc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 * $RCSf ObjectFileMaterials.java,v $
 *
 * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are 
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 * $Revis 1.6 $
 * $D 2007/02/0920:10 $
 * $St Exp $
 */
 
//package com.sun.j3d.loaders.objectfile;
import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import java.io.FileNotFoundException;
import java.io.StringReader;
import java.io.Reader;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.HashMap;
//import com.sun.j3d.loaders.objectfile.DefaultMaterials;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.media.j3d.Texture2D;
import java.awt.image.ImageObserver;
import java.awt.image.PixelGrabber;
import java.awt.image.DataBufferInt;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.TexCoordGeneration;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.media.j3d.GeometryArray;
import com.sun.j3d.utils.image.TextureLoader;
import javax.media.j3d.TransparencyAttributes;
 
class ObjectFileMaterials implements ImageObserver
{
    // DEBUG
    // 1 = Name of materials
    // 16 = Tokens
    private static final int DEBUG = 17;
    private String curName = null;
    private ObjectFileMaterial cur = null;
    private HashMap materials; // key=String name of material
    // value=ObjectFileMaterial
    private String basePath;
    private boolean fromUrl;
 
    private class ObjectFileMaterial
    {
 
        public Color3f Ka;
        public Color3f Kd;
        public Color3f Ks;
        public int illum;
        public float Ns;
        public Texture2D t;
        public boolean transparent;
        public float transparencyLevel;
        public String map_kd;
        public String map_bump;
    }
 
    void assignMaterial(String matName, Shape3D shape)
    {
        ObjectFileMaterial p = null;
 
        if ((DEBUG & 1) != 0)
        {
            System.out.println("Color " + matName);
        }
 
        Material m = new Material();
        p = (ObjectFileMaterial) materials.get(matName);
        if (p != null)
            m.setUserData(p.map_kd);
        Appearance a = new Appearance();
 
        if (p != null)
        {
            // Set ambient & diffuse color
            if (p.Ka != null)
            {
                m.setAmbientColor(p.Ka);
            }
            if (p.Kd != null)
            {
                m.setDiffuseColor(p.Kd);
            }
 
            // Set specular color
            if ((p.Ks != null) && (p.illum != 1))
            {
                m.setSpecularColor(p.Ks);
            } else if (p.illum == 1)
            {
                m.setSpecularColor(0.0f, 0.0f, 0.0f);
            }
 
            if (p.illum >= 1)
            {
                m.setLightingEnable(true);
            } else if (p.illum == 0)
            {
                m.setLightingEnable(false);
            }
 
            if (p.Ns != -1.0f)
            {
                m.setShininess(p.Ns);
            }
 
            if (p.t != null)
            {
                a.setTexture(p.t);
                // Create Texture Coordinates if not already present
                if ((((GeometryArray) shape.getGeometry()).getVertexFormat() & GeometryArray.TEXTURE_COORDINATE_2) == 0)
                {
                    TexCoordGeneration tcg = new TexCoordGeneration();
                    a.setTexCoordGeneration(tcg);
                }
            }
 
            if (p.transparent)
            {
                a.setTransparencyAttributes(new TransparencyAttributes(
                        TransparencyAttributes.NICEST,
                        p.transparencyLevel));
            }
        }
        a.setMaterial(m);
        if (p != null)
            a.setUserData(p.map_bump);
        if ((DEBUG & 1) != 0)
        {
            System.out.println(m);
        }
        shape.setAppearance(a);
    } // End of assignMaterial
 
    private void readName(ObjectFileParser st)
            throws ParsingErrorException
    {
        st.getToken();
 
        if (st.ttype == ObjectFileParser.TT_WORD)
        {
 
            if (curName != null)
            {
                materials.put(curName, cur);
            }
            curName = new String(st.sval);
            cur = new ObjectFileMaterial();
        }
        st.skipToNextLine();
    } // End of readName
 
    private void readAmbient(ObjectFileParser st)
            throws ParsingErrorException
    {
        Color3f p = new Color3f();
 
        st.getNumber();
        p.x = (float) st.nval;
        st.getNumber();
        p.y = (float) st.nval;
        st.getNumber();
        p.z = (float) st.nval;
 
        cur.Ka = p;
 
        st.skipToNextLine();
    } // End of readAmbient
 
    private void readDiffuse(ObjectFileParser st)
            throws ParsingErrorException
    {
        Color3f p = new Color3f();
 
        st.getNumber();
        p.x = (float) st.nval;
        st.getNumber();
        p.y = (float) st.nval;
        st.getNumber();
        p.z = (float) st.nval;
 
        cur.Kd = p;
 
        st.skipToNextLine();
    } // End of readDiffuse
 
    private void readSpecular(ObjectFileParser st)
            throws ParsingErrorException
    {
        Color3f p = new Color3f();
 
        st.getNumber();
        p.x = (float) st.nval;
        st.getNumber();
        p.y = (float) st.nval;
        st.getNumber();
        p.z = (float) st.nval;
 
        cur.Ks = p;
 
        st.skipToNextLine();
    } // End of readSpecular
 
    private void readIllum(ObjectFileParser st)
            throws ParsingErrorException
    {
 
        st.getNumber();
        cur.illum = (int) st.nval;
 
        st.skipToNextLine();
    } // End of readSpecular
 
    private void readTransparency(ObjectFileParser st)
            throws ParsingErrorException
    {
 
        st.getNumber();
        cur.transparencyLevel = (float) st.nval;
        if (cur.transparencyLevel < 1.0f)
        {
            cur.transparent = true;
        }
        st.skipToNextLine();
    } // End of readTransparency
 
    private void readShininess(ObjectFileParser st)
            throws ParsingErrorException
    {
        float f;
 
        st.getNumber();
        cur.Ns = (float) st.nval;
        if (cur.Ns < 1.0f)
        {
            cur.Ns = 1.0f;
        } else if (cur.Ns > 128.0f)
        {
            cur.Ns = 128.0f;
        }
 
        st.skipToNextLine();
    } // End of readSpecular
 
    public void readMapBump(ObjectFileParser st)
    {
        // Filenames are case sensitive
        st.lowerCaseMode(false);
 
        // Get name of texture file (skip path)
        String tFile = null;
        do
        {
            st.getToken();
            if (st.ttype == ObjectFileParser.TT_WORD)
            {
                tFile = st.sval;
            }
        } while (st.ttype != ObjectFileParser.TT_EOL);
 
        st.lowerCaseMode(true);
 
        if ((DEBUG & 1) != 0)
        {
            System.out.println(tFile);
        }
        
        cur.map_bump = tFile;
    }
    
    public void readMapKd(ObjectFileParser st)
    {
        // Filenames are case sensitive
        st.lowerCaseMode(false);
 
        // Get name of texture file (skip path)
        String tFile = null;
        do
        {
            st.getToken();
            if (st.ttype == ObjectFileParser.TT_WORD)
            {
                tFile = st.sval;
            }
        } while (st.ttype != ObjectFileParser.TT_EOL);
 
        st.lowerCaseMode(true);
 
        if ((DEBUG & 1) != 0)
        {
            System.out.println(tFile);
        }
        
        cur.map_kd = tFile;
        
        if (false) // tFile != null)
        {
            // Check for filename with no extension
            if (tFile.lastIndexOf('.') != -1)
            {
                try
                {
                    // Convert filename to lower case for extension comparisons
                    String suffix = tFile.substring(
                            tFile.lastIndexOf('.') + 1).toLowerCase();
 
                    TextureLoader t = null;
 
                    if ((suffix.equals("int")) || (suffix.equals("inta")) || (suffix.equals("rgb")) || (suffix.equals("rgba")) || (suffix.equals("bw")) || (suffix.equals("sgi")))
                    {
                        RgbFile f;
                        if (fromUrl)
                        {
                            f = new RgbFile(new URL(basePath + tFile).openStream());
                        } else
                        {
                            f = new RgbFile(new FileInputStream(
                                    basePath + tFile));
                        }
                        BufferedImage bi = f.getImage();
 
                        boolean luminance = suffix.equals("int") || suffix.equals("inta");
                        boolean alpha = suffix.equals("inta") || suffix.equals("rgba");
                        cur.transparent = alpha;
 
                        String s = null;
                        if (luminance && alpha)
                        {
                            s = "LUM8_ALPHA8";
                        } else if (luminance)
                        {
                            s = "LUMINANCE";
                        } else if (alpha)
                        {
                            s = "RGBA";
                        } else
                        {
                            s = "RGB";
                        }
 
                        t = new TextureLoader(bi, s,
                                TextureLoader.GENERATE_MIPMAP);
                    } else
                    {
                        // For all other file types, use the TextureLoader
                        if (fromUrl)
                        {
                            t = new TextureLoader(new URL(basePath + tFile), "RGB",
                                    TextureLoader.GENERATE_MIPMAP, null);
                        } else
                        {
                            t = new TextureLoader(basePath + tFile,
                                    "RGB",
                                    TextureLoader.GENERATE_MIPMAP, null);
                        }
                    }
                    Texture2D texture = (Texture2D) t.getTexture();
                    if (texture != null)
                    {
                        cur.t = texture;
                    }
                } catch (FileNotFoundException e)
                {
                // Texture won't get loaded if file can't be found
                } catch (MalformedURLException e)
                {
                // Texture won't get loaded if file can't be found
                } catch (IOException e)
                {
                // Texture won't get loaded if file can't be found
                }
            }
        }
        st.skipToNextLine();
    } // End of readMapKd
 
    private void readFile(ObjectFileParser st)
            throws ParsingErrorException
    {
        int t;
        st.getToken();
        while (st.ttype != ObjectFileParser.TT_EOF)
        {
 
            // Print out one token for each line
            if ((DEBUG & 16) != 0)
            {
                System.out.print("Token ");
                if (st.ttype == ObjectFileParser.TT_EOL)
                {
                    System.out.println("EOL");
                } else if (st.ttype == ObjectFileParser.TT_WORD)
                {
                    System.out.println(st.sval);
                } else
                {
                    System.out.println((char) st.ttype);
                }
            }
 
            if (st.ttype == ObjectFileParser.TT_WORD)
            {
                if (st.sval.equals("newmtl"))
                {
                    readName(st);
                } else if (st.sval.equals("ka"))
                {
                    readAmbient(st);
                } else if (st.sval.equals("kd"))
                {
                    readDiffuse(st);
                } else if (st.sval.equals("ks"))
                {
                    readSpecular(st);
                } else if (st.sval.equals("illum"))
                {
                    readIllum(st);
                } else if (st.sval.equals("d"))
                {
                    readTransparency(st);
                } else if (st.sval.equals("ns"))
                {
                    readShininess(st);
                } else if (st.sval.equals("tf"))
                {
                    st.skipToNextLine();
                } else if (st.sval.equals("sharpness"))
                {
                    st.skipToNextLine();
                } else if (st.sval.equals("map_kd"))
                {
                    readMapKd(st);
                } else if (st.sval.equals("map_ka"))
                {
                    st.skipToNextLine();
                } else if (st.sval.equals("map_ks"))
                {
                    st.skipToNextLine();
                } else if (st.sval.equals("map_ns"))
                {
                    st.skipToNextLine();
                } else if (st.sval.equals("map_bump"))
                {
                    readMapBump(st);
                    //st.skipToNextLine();
                }
            }
 
            st.skipToNextLine();
 
            // Get next token
            st.getToken();
        }
        if (curName != null)
        {
            materials.put(curName, cur);
        }
    } // End of readFile
 
    void readMaterialFile(boolean fromUrl, String basePath,
            String fileName) throws ParsingErrorException
    {
 
        Reader reader;
 
        this.basePath = basePath;
        this.fromUrl = fromUrl;
 
        try
        {
            if (fromUrl)
            {
                reader = (Reader) (new InputStreamReader(
                        new BufferedInputStream((new URL(basePath + fileName).openStream()))));
            } else
            {
                reader = new BufferedReader(new FileReader(basePath + fileName));
            }
        } catch (IOException e)
        {
            // couldn't find it - ignore mtllib
            return;
        }
        if ((DEBUG & 1) != 0)
        {
            System.out.println("Material f " + basePath + fileName);
        }
 
        ObjectFileParser st = new ObjectFileParser(reader);
        readFile(st);
    } // End of readMaterialFile
 
    ObjectFileMaterials() throws ParsingErrorException
    {
        Reader reader = new StringReader(DefaultMaterials.materials);
 
        ObjectFileParser st = new ObjectFileParser(reader);
        materials = new HashMap(50);
        readFile(st);
    } // End of ObjectFileMaterials
 
    /**
     * Implement the ImageObserver interface.  Needed to load jpeg and gif
     * files using the Toolkit.
     */
    public boolean imageUpdate(Image img, int flags, int x, int y,
            int w, int h)
    {
 
        return (flags & (ALLBITS | ABORT)) == 0;
    } // End of imageUpdate
} // End of class ObjectFileMaterials
 
// End of file ObjectFileMaterials.java