View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.irurueta.geometry.io;
17  
18  /**
19   * Structure containing parameters defining a material, such as color, texture,
20   * etc.
21   */
22  public class Material {
23  
24      /**
25       * Material ID.
26       * This value is generated automatically when loading a 3D file, and it is
27       * intended to uniquely identify a material.
28       */
29      private int id = 0;
30  
31      /**
32       * Ambient red color component value. If defined, must be between 0 and 255
33       * (both included).
34       */
35      private short ambientRedColor = -1;
36  
37      /**
38       * Ambient green color component value. If defined, must be between 0 and
39       * 255 (both included).
40       */
41      private short ambientGreenColor = -1;
42  
43      /**
44       * Ambient blue color component value. If defined, must be between 0 and 255
45       * (both included).
46       */
47      private short ambientBlueColor = -1;
48  
49      /**
50       * Diffuse red color component value. If defined, must be between 0 and 255
51       * (both included).
52       */
53      private short diffuseRedColor = -1;
54  
55      /**
56       * Diffuse green color component value. If defined, must be between 0 and
57       * 255 (both included).
58       */
59      private short diffuseGreenColor = -1;
60  
61      /**
62       * Diffuse blue color component value. If defined, must be between 0 and 255
63       * (both included).
64       */
65      private short diffuseBlueColor = -1;
66  
67      /**
68       * Specular red color component value. If defined, must be between 0 and 255
69       * (both included).
70       */
71      private short specularRedColor = -1;
72  
73      /**
74       * Specular green color component value. If defined, must be between 0 and
75       * 255 (both included).
76       */
77      private short specularGreenColor = -1;
78  
79      /**
80       * Specular blue color component value. If defined, must be between 0 and
81       * 255 (both included).
82       */
83      private short specularBlueColor = -1;
84  
85  
86      /**
87       * Specular coefficient that determines how much shininess the material has.
88       */
89      private float specularCoefficient = 0.0f;
90  
91      /**
92       * Indicates if specular coefficient is available or not.
93       */
94      private boolean specularCoefficientAvailable = false;
95  
96      /**
97       * Ambient texture.
98       */
99      private Texture ambientTextureMap;
100 
101     /**
102      * Diffuse texture.
103      */
104     private Texture diffuseTextureMap;
105 
106     /**
107      * Specular texture.
108      */
109     private Texture specularTextureMap;
110 
111     /**
112      * Alpha texture.
113      */
114     private Texture alphaTextureMap;
115 
116     /**
117      * Bump texture.
118      */
119     private Texture bumpTextureMap;
120 
121     /**
122      * Transparency value. If defined, must be a value between 0 and 100 (both
123      * included).
124      */
125     private short transparency = -1;
126 
127     /**
128      * Illumination of this material.
129      */
130     private Illumination illumination;
131 
132     /**
133      * Returns id of material.
134      * This value is generated automatically when loading a 3D file, and it is
135      * intended to uniquely identify a material
136      *
137      * @return id of material.
138      */
139     public int getId() {
140         return id;
141     }
142 
143     /**
144      * Sets id of material.
145      * This value must be generated when loading a 3D file, and it is intended
146      * to uniquely identify a material.
147      *
148      * @param id id of material to be set.
149      */
150     public void setId(final int id) {
151         this.id = id;
152     }
153 
154     /**
155      * Returns ambient red color component value. If defined, must be between 0
156      * and 255 (both included).
157      *
158      * @return ambient red color component value.
159      */
160     public short getAmbientRedColor() {
161         return ambientRedColor;
162     }
163 
164     /**
165      * Sets ambient red color component value. If defined, must be between 0 and
166      * 255 (both included).
167      *
168      * @param ambientRedColor ambient red color component value to be set.
169      */
170     public void setAmbientRedColor(final short ambientRedColor) {
171         this.ambientRedColor = ambientRedColor;
172     }
173 
174     /**
175      * Indicates if ambient red color component value is available.
176      * Returns true if ambient red color is not negative.
177      *
178      * @return true if ambient red color is not negative.
179      */
180     public boolean isAmbientRedColorAvailable() {
181         return ambientRedColor >= 0;
182     }
183 
184     /**
185      * Returns ambient green color component value. If defined, must be between
186      * 0 and 255 (both included).
187      *
188      * @return ambient green color component value.
189      */
190     public short getAmbientGreenColor() {
191         return ambientGreenColor;
192     }
193 
194     /**
195      * Sets ambient green color component value. If defined, must be between 0
196      * and 255 (both included).
197      *
198      * @param ambientGreenColor ambient green color component value to be set.
199      */
200     public void setAmbientGreenColor(final short ambientGreenColor) {
201         this.ambientGreenColor = ambientGreenColor;
202     }
203 
204     /**
205      * Indicates if ambient green color component value is available.
206      * Returns true if ambient green color is not negative.
207      *
208      * @return true if ambient green color is not negative.
209      */
210     public boolean isAmbientGreenColorAvailable() {
211         return ambientGreenColor >= 0;
212     }
213 
214     /**
215      * Returns ambient blue color component value. If defined, must be between
216      * 0 and 255 (both included).
217      *
218      * @return ambient blue color component value.
219      */
220     public short getAmbientBlueColor() {
221         return ambientBlueColor;
222     }
223 
224     /**
225      * Sets ambient blue color component value. If defined, must be between 0
226      * and 255 (both included).
227      *
228      * @param ambientBlueColor ambient blue color component value to be set.
229      */
230     public void setAmbientBlueColor(final short ambientBlueColor) {
231         this.ambientBlueColor = ambientBlueColor;
232     }
233 
234     /**
235      * Indicates if ambient blue color component value is available.
236      * Returns true if ambient blue color is not negative.
237      *
238      * @return true if ambient blue color is not negative.
239      */
240     public boolean isAmbientBlueColorAvailable() {
241         return ambientBlueColor >= 0;
242     }
243 
244     /**
245      * Sets all color components of ambient color.
246      * Ambient color usually refers to the color of the light that gives a
247      * certain tonality to all the objects of a scene.
248      *
249      * @param red   ambient red color component to be set. If defined, must be
250      *              between 0 and 255 (both included).
251      * @param green ambient green color component to be set. If defined, must be
252      *              between 0 and 255 (both included).
253      * @param blue  ambient blue color component to be set. If defined, must be
254      *              between 0 and 255 (both included).
255      */
256     public void setAmbientColor(final short red, final short green, final short blue) {
257         ambientRedColor = red;
258         ambientGreenColor = green;
259         ambientBlueColor = blue;
260     }
261 
262     /**
263      * Indicates if ambient color has been defined.
264      *
265      * @return true if all components of ambient color have been defined (are
266      * not negative).
267      */
268     public boolean isAmbientColorAvailable() {
269         return ambientRedColor >= 0 && ambientGreenColor >= 0 && ambientBlueColor >= 0;
270     }
271 
272     /**
273      * Returns diffuse red color component value. If defined, must be between 0
274      * and 255 (both included).
275      *
276      * @return diffuse red color component value.
277      */
278     public short getDiffuseRedColor() {
279         return diffuseRedColor;
280     }
281 
282     /**
283      * Sets diffuse red color component value. If defined, must be between 0 and
284      * 255 (both included).
285      *
286      * @param diffuseRedColor diffuse red color component value to be set.
287      */
288     public void setDiffuseRedColor(final short diffuseRedColor) {
289         this.diffuseRedColor = diffuseRedColor;
290     }
291 
292     /**
293      * Indicates if diffuse red color component value is available.
294      * Returns true if diffuse red color is not negative.
295      *
296      * @return true if diffuse red color is not negative.
297      */
298     public boolean isDiffuseRedColorAvailable() {
299         return diffuseRedColor >= 0;
300     }
301 
302     /**
303      * Returns diffuse green color component value. If defined, must be between
304      * 0 and 255 (both included).
305      *
306      * @return diffuse green color component value.
307      */
308     public short getDiffuseGreenColor() {
309         return diffuseGreenColor;
310     }
311 
312     /**
313      * Sets diffuse green color component value. If defined, must be between 0
314      * and 255 (both included).
315      *
316      * @param diffuseGreenColor diffuse green color component value to be set.
317      */
318     public void setDiffuseGreenColor(final short diffuseGreenColor) {
319         this.diffuseGreenColor = diffuseGreenColor;
320     }
321 
322     /**
323      * Indicates if diffuse green color component value is available.
324      * Returns true if diffuse green color is not negative.
325      *
326      * @return true if diffuse green color is not negative.
327      */
328     public boolean isDiffuseGreenColorAvailable() {
329         return diffuseGreenColor >= 0;
330     }
331 
332     /**
333      * Returns diffuse blue color component value. If defined, must be between
334      * 0 and 255 (both included).
335      *
336      * @return diffuse blue color component value.
337      */
338     public short getDiffuseBlueColor() {
339         return diffuseBlueColor;
340     }
341 
342     /**
343      * Sets diffuse blue color component value. If defined, must be between 0
344      * and 255 (both included).
345      *
346      * @param diffuseBlueColor diffuse blue color component value to be set.
347      */
348     public void setDiffuseBlueColor(final short diffuseBlueColor) {
349         this.diffuseBlueColor = diffuseBlueColor;
350     }
351 
352     /**
353      * Indicates if diffuse blue color component value is available.
354      * Returns true if diffuse blue color is not negative.
355      *
356      * @return true if diffuse blue color is not negative.
357      */
358     public boolean isDiffuseBlueColorAvailable() {
359         return diffuseBlueColor >= 0;
360     }
361 
362     /**
363      * Sets all color components of diffuse color.
364      * Diffuse color usually refers to the surface color of a given object
365      * without accounting for highlights or shininess.
366      *
367      * @param red   diffuse red color component to be set. If defined, must be
368      *              between 0 and 255 (both included).
369      * @param green diffuse green color component to be set. If defined, must be
370      *              between 0 and 255 (both included).
371      * @param blue  diffuse blue color component to be set. If defined, must be
372      *              between 0 and 255 (both included).
373      */
374     public void setDiffuseColor(final short red, final short green, final short blue) {
375         diffuseRedColor = red;
376         diffuseGreenColor = green;
377         diffuseBlueColor = blue;
378     }
379 
380     /**
381      * Indicates if diffuse color has been defined.
382      *
383      * @return true if all components of diffuse color have been defined (are
384      * not negative).
385      */
386     public boolean isDiffuseColorAvailable() {
387         return diffuseRedColor >= 0 && diffuseGreenColor >= 0 && diffuseBlueColor >= 0;
388     }
389 
390     /**
391      * Returns specular red color component value. If defined, must be between 0
392      * and 255 (both included).
393      *
394      * @return specular red color component value.
395      */
396     public short getSpecularRedColor() {
397         return specularRedColor;
398     }
399 
400     /**
401      * Sets specular red color component value. If defined, must be between 0
402      * and 255 (both included).
403      *
404      * @param specularRedColor specular red color component value to be set.
405      */
406     public void setSpecularRedColor(final short specularRedColor) {
407         this.specularRedColor = specularRedColor;
408     }
409 
410     /**
411      * Indicates if specular red color component value is available.
412      * Returns true if specular red color is not negative.
413      *
414      * @return true if specular red color is not negative.
415      */
416     public boolean isSpecularRedColorAvailable() {
417         return specularRedColor >= 0;
418     }
419 
420     /**
421      * Returns specular green color component value. If defined, must be between
422      * 0 and 255 (both included).
423      *
424      * @return specular green color component value.
425      */
426     public short getSpecularGreenColor() {
427         return specularGreenColor;
428     }
429 
430     /**
431      * Sets specular green color component value. If defined, must be between 0
432      * and 255 (both included).
433      *
434      * @param specularGreenColor specular green color component value to be set.
435      */
436     public void setSpecularGreenColor(final short specularGreenColor) {
437         this.specularGreenColor = specularGreenColor;
438     }
439 
440     /**
441      * Indicates if specular green color component value is available.
442      * Returns true if specular green color is not negative.
443      *
444      * @return true if specular green color is not negative.
445      */
446     public boolean isSpecularGreenColorAvailable() {
447         return specularGreenColor >= 0;
448     }
449 
450     /**
451      * Returns specular blue color component value. If defined, must be between
452      * 0 and 255 (both included).
453      *
454      * @return specular blue color component value.
455      */
456     public short getSpecularBlueColor() {
457         return specularBlueColor;
458     }
459 
460     /**
461      * Sets specular blue color component value. If defined, must be between 0
462      * and 255 (both included).
463      *
464      * @param specularBlueColor specular blue color component value to be set.
465      */
466     public void setSpecularBlueColor(final short specularBlueColor) {
467         this.specularBlueColor = specularBlueColor;
468     }
469 
470     /**
471      * Indicates if specular blue color component value is available.
472      * Returns true if specular blue color is not negative.
473      *
474      * @return true if specular blue color is not negative.
475      */
476     public boolean isSpecularBlueColorAvailable() {
477         return specularBlueColor >= 0;
478     }
479 
480     /**
481      * Sets all color components of specular color.
482      * Specular color usually refers to the color of the highlights on an object
483      * surface when it shines because of the light.
484      *
485      * @param red   specular red color component to be set. If defined, must be
486      *              between 0 and 255 (both included).
487      * @param green specular green color component to be set. If defined, must
488      *              be between 0 and 255 (both included).
489      * @param blue  specular blue color component to be set. If defined, must be
490      *              between 0 and 255 (both included).
491      */
492     public void setSpecularColor(final short red, final short green, final short blue) {
493         specularRedColor = red;
494         specularGreenColor = green;
495         specularBlueColor = blue;
496     }
497 
498     /**
499      * Indicates if specular color has been defined.
500      *
501      * @return true if all components of specular color have been defined (are
502      * not negative).
503      */
504     public boolean isSpecularColorAvailable() {
505         return specularRedColor >= 0 && specularGreenColor >= 0 && specularBlueColor >= 0;
506     }
507 
508     /**
509      * Returns specular coefficient. This indicates the amount of shininess of
510      * an object.
511      *
512      * @return specular coefficient.
513      */
514     public float getSpecularCoefficient() {
515         return specularCoefficient;
516     }
517 
518     /**
519      * Sets specular coefficient. This indicates the amount of shininess of an
520      * object.
521      *
522      * @param specularCoefficient specular coefficient to be set.
523      */
524     public void setSpecularCoefficient(final float specularCoefficient) {
525         this.specularCoefficient = specularCoefficient;
526         specularCoefficientAvailable = true;
527     }
528 
529     /**
530      * Indicates if specular coefficient has been defined for this material.
531      *
532      * @return true if specular coefficient has been defined and is available,
533      * false otherwise.
534      */
535     public boolean isSpecularCoefficientAvailable() {
536         return specularCoefficientAvailable;
537     }
538 
539     /**
540      * Returns ambient texture.
541      * Ambient texture usually refers to the texture used to give a certain
542      * tonality to an object, as if it was lit by some source of light.
543      *
544      * @return ambient texture.
545      */
546     public Texture getAmbientTextureMap() {
547         return ambientTextureMap;
548     }
549 
550     /**
551      * Sets ambient texture.
552      * Ambient texture usually refers to the texture used to give a certain
553      * tonality to anb object, as if it was lit by some source of light.
554      *
555      * @param ambientTextureMap ambient texture to be set.
556      */
557     public void setAmbientTextureMap(final Texture ambientTextureMap) {
558         this.ambientTextureMap = ambientTextureMap;
559     }
560 
561     /**
562      * Indicates if ambient texture has been defined and is available.
563      *
564      * @return true if ambient texture has been defined and is available, false
565      * otherwise.
566      */
567     public boolean isAmbientTextureMapAvailable() {
568         return ambientTextureMap != null;
569     }
570 
571     /**
572      * Returns diffuse texture.
573      * Diffuse texture usually refers to the texture applied on the surface of
574      * an object as its color.
575      *
576      * @return diffuse texture.
577      */
578     public Texture getDiffuseTextureMap() {
579         return diffuseTextureMap;
580     }
581 
582     /**
583      * Sets diffuse texture.
584      * Diffuse texture usually refers to the texture applied on the surface of
585      * an object as its color.
586      *
587      * @param diffuseTextureMap diffuse texture to be set.
588      */
589     public void setDiffuseTextureMap(final Texture diffuseTextureMap) {
590         this.diffuseTextureMap = diffuseTextureMap;
591     }
592 
593     /**
594      * Indicates if diffuse texture has been defined and is available.
595      *
596      * @return true if diffuse texture has been defined and is available, false
597      * otherwise.
598      */
599     public boolean isDiffuseTextureMapAvailable() {
600         return diffuseTextureMap != null;
601     }
602 
603     /**
604      * Returns specular texture.
605      * Specular texture usually refers to the highlights or sparkles applied
606      * on the surface of an object.
607      *
608      * @return specular texture.
609      */
610     public Texture getSpecularTextureMap() {
611         return specularTextureMap;
612     }
613 
614     /**
615      * Sets specular texture.
616      * Specular texture usually refers to the highlights or sparkles applied
617      * on the surface of an object.
618      *
619      * @param specularTextureMap specular texture to be set.
620      */
621     public void setSpecularTextureMap(final Texture specularTextureMap) {
622         this.specularTextureMap = specularTextureMap;
623     }
624 
625     /**
626      * Indicates if specular texture has been defined and is available
627      *
628      * @return true if specular texture has been defined and is available, false
629      * otherwise.
630      */
631     public boolean isSpecularTextureMapAvailable() {
632         return specularTextureMap != null;
633     }
634 
635     /**
636      * Returns alpha texture.
637      * Alpha texture usually contains information related to transparency.
638      *
639      * @return alpha texture.
640      */
641     public Texture getAlphaTextureMap() {
642         return alphaTextureMap;
643     }
644 
645     /**
646      * Sets alpha texture.
647      * Alpha texture usually contains information related to transparency.
648      *
649      * @param alphaTextureMap alpha texture to be set.
650      */
651     public void setAlphaTextureMap(final Texture alphaTextureMap) {
652         this.alphaTextureMap = alphaTextureMap;
653     }
654 
655     /**
656      * Indicates if alpha texture has been defined and is available.
657      *
658      * @return true if alpha texture has been defined and is available, false
659      * otherwise.
660      */
661     public boolean isAlphaTextureMapAvailable() {
662         return alphaTextureMap != null;
663     }
664 
665     /**
666      * Returns bump texture.
667      * Bump textures are used to give the appearance of bumpiness or rugosity to
668      * plain surfaces.
669      *
670      * @return bump texture.
671      */
672     public Texture getBumpTextureMap() {
673         return bumpTextureMap;
674     }
675 
676     /**
677      * Sets bump texture.
678      * Bump textures are used to give the appearance of bumpiness or rugosity to
679      * plain surfaces.
680      *
681      * @param bumpTextureMap bump texture.
682      */
683     public void setBumpTextureMap(final Texture bumpTextureMap) {
684         this.bumpTextureMap = bumpTextureMap;
685     }
686 
687     /**
688      * Indicates if bump texture has been defined and is available.
689      *
690      * @return true if bump texture has been defined and is available, false
691      * otherwise.
692      */
693     public boolean isBumpTextureMapAvailable() {
694         return bumpTextureMap != null;
695     }
696 
697     /**
698      * Returns transparency of this material. If defined, value must be between
699      * 0 (no transparency) and 100 (totally transparent).
700      *
701      * @return transparency of this material.
702      */
703     public short getTransparency() {
704         return transparency;
705     }
706 
707     /**
708      * Sets transparency of this material. If defined, value must be between
709      * 0 (no transparency) and 100 (totally transparent).
710      *
711      * @param transparency transparency of this material to be set.
712      */
713     public void setTransparency(final short transparency) {
714         this.transparency = transparency;
715     }
716 
717     /**
718      * Indicates if transparency has been defined and is available.
719      *
720      * @return true if transparency is not negative, false otherwise.
721      */
722     public boolean isTransparencyAvailable() {
723         return transparency >= 0;
724     }
725 
726     /**
727      * Returns illumination method to be used for this material.
728      *
729      * @return illumination.
730      */
731     public Illumination getIllumination() {
732         return illumination;
733     }
734 
735     /**
736      * Sets illumination method to be used for this material.
737      *
738      * @param illumination illumination method to be used.
739      */
740     public void setIllumination(final Illumination illumination) {
741         this.illumination = illumination;
742     }
743 
744     /**
745      * Indicates if an illumination has been defined and is available.
746      *
747      * @return true if illumination has been defined, false otherwise.
748      */
749     public boolean isIlluminationAvailable() {
750         return illumination != null;
751     }
752 }