OSDN Git Service

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