OSDN Git Service

bdc5e5a46cca470b929a4c7f8d143ea55f187039
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / pmd / pmdloader / MaterialBuilder.java
1 /*
2  * building material information
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.pmd.pmdloader;
9
10 import java.awt.Color;
11 import java.util.Iterator;
12 import java.util.List;
13 import jp.sourceforge.mikutoga.corelib.ListUtil;
14 import jp.sourceforge.mikutoga.parser.ParseStage;
15 import jp.sourceforge.mikutoga.pmd.model.Material;
16 import jp.sourceforge.mikutoga.pmd.model.PmdModel;
17 import jp.sourceforge.mikutoga.pmd.model.ShadeInfo;
18 import jp.sourceforge.mikutoga.pmd.model.Surface;
19 import jp.sourceforge.mikutoga.pmd.model.ToonMap;
20 import jp.sourceforge.mikutoga.pmd.parser.PmdMaterialHandler;
21
22 /**
23  * マテリアル素材関連の通知をパーサから受け取る。
24  */
25 class MaterialBuilder implements PmdMaterialHandler {
26
27     private final List<Material> materialList;
28     private Iterator<Material> materialIt;
29     private Material currentMaterial = null;
30
31     private final List<Surface> surfacelList;
32     private Iterator<Surface> surfaceIt;
33
34     private final ToonMap toonMap;
35
36     /**
37      * コンストラクタ。
38      * @param model モデル
39      */
40     MaterialBuilder(PmdModel model){
41         super();
42
43         this.materialList = model.getMaterialList();
44         this.surfacelList = model.getSurfaceList();
45         this.toonMap = model.getToonMap();
46
47         return;
48     }
49
50     /**
51      * {@inheritDoc}
52      * @param stage {@inheritDoc}
53      * @param loops {@inheritDoc}
54      */
55     @Override
56     public void loopStart(ParseStage stage, int loops){
57         assert stage == PmdMaterialHandler.MATERIAL_LIST;
58
59         ListUtil.prepareDefConsList(this.materialList, Material.class, loops);
60
61         this.materialIt = this.materialList.iterator();
62         if(this.materialIt.hasNext()){
63             this.currentMaterial = this.materialIt.next();
64         }
65
66         this.surfaceIt = this.surfacelList.iterator();
67
68         return;
69     }
70
71     /**
72      * {@inheritDoc}
73      * @param stage {@inheritDoc}
74      */
75     @Override
76     public void loopNext(ParseStage stage){
77         assert stage == PmdMaterialHandler.MATERIAL_LIST;
78
79         if(this.materialIt.hasNext()){
80             this.currentMaterial = this.materialIt.next();
81         }
82
83         return;
84     }
85
86     /**
87      * {@inheritDoc}
88      * @param stage {@inheritDoc}
89      */
90     @Override
91     public void loopEnd(ParseStage stage){
92         assert stage == PmdMaterialHandler.MATERIAL_LIST;
93         return;
94     }
95
96     /**
97      * {@inheritDoc}
98      * @param red {@inheritDoc}
99      * @param green {@inheritDoc}
100      * @param blue {@inheritDoc}
101      * @param alpha {@inheritDoc}
102      */
103     @Override
104     public void pmdMaterialDiffuse(float red,
105                                    float green,
106                                    float blue,
107                                    float alpha ){
108         Color diffuse = new Color(red, green, blue, alpha);
109         this.currentMaterial.setDiffuseColor(diffuse);
110         return;
111     }
112
113     /**
114      * {@inheritDoc}
115      * @param red {@inheritDoc}
116      * @param green {@inheritDoc}
117      * @param blue {@inheritDoc}
118      */
119     @Override
120     public void pmdMaterialAmbient(float red,
121                                    float green,
122                                    float blue ){
123         Color ambient = new Color(red, green, blue);
124         this.currentMaterial.setAmbientColor(ambient);
125         return;
126     }
127
128     /**
129      * {@inheritDoc}
130      * @param red {@inheritDoc}
131      * @param green {@inheritDoc}
132      * @param blue {@inheritDoc}
133      * @param shininess {@inheritDoc}
134      */
135     @Override
136     public void pmdMaterialSpecular(float red,
137                                     float green,
138                                     float blue,
139                                     float shininess ){
140         Color specular = new Color(red, green, blue);
141         this.currentMaterial.setSpecularColor(specular);
142         this.currentMaterial.setShininess(shininess);
143         return;
144     }
145
146     /**
147      * {@inheritDoc}
148      * @param hasEdge {@inheritDoc}
149      * @param vertexNum {@inheritDoc}
150      */
151     @Override
152     public void pmdMaterialInfo(boolean hasEdge, int vertexNum){
153         this.currentMaterial.setEdgeAppearance(hasEdge);
154
155         List<Surface> list = this.currentMaterial.getSurfaceList();
156
157         int surfaceNum = vertexNum / 3;
158         for(int ct = 1; ct <= surfaceNum; ct++){
159             Surface surface = this.surfaceIt.next();
160             list.add(surface);
161         }
162
163         return;
164     }
165
166     /**
167      * {@inheritDoc}
168      * @param toonIdx {@inheritDoc}
169      * @param textureFile {@inheritDoc}
170      * @param sphereFile {@inheritDoc}
171      */
172     @Override
173     public void pmdMaterialShading(int toonIdx,
174                                    String textureFile,
175                                    String sphereFile ){
176         ShadeInfo info = this.currentMaterial.getShadeInfo();
177
178         ToonMap map = this.toonMap;
179
180         info.setToonMap(map);
181         info.setToonIndex(toonIdx);
182         info.setTextureFileName(textureFile);
183         info.setSpheremapFileName(sphereFile);
184
185         return;
186     }
187
188 }