OSDN Git Service

9115c870e56964c490ef01ada06aacdda480ac0a
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / vmd / parser / VmdLightingParser.java
1 /*
2  * VMD lighting data parser
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.vmd.parser;
9
10 import java.io.IOException;
11 import jp.sourceforge.mikutoga.parser.CommonParser;
12 import jp.sourceforge.mikutoga.parser.MmdFormatException;
13 import jp.sourceforge.mikutoga.parser.MmdInputStream;
14 import jp.sourceforge.mikutoga.vmd.VmdConst;
15
16 /**
17  * VMDモーションファイルのライティング情報パーサ。
18  * 照明光源演出データと影演出データを含む。
19  * <p>古い版のVMDファイルには影演出データが記述されていない場合がある。
20  */
21 class VmdLightingParser extends CommonParser {
22
23     private VmdLightingHandler handler = null;
24
25
26     /**
27      * コンストラクタ。
28      * @param source 入力ソース
29      */
30     VmdLightingParser(MmdInputStream source){
31         super(source);
32         return;
33     }
34
35
36     /**
37      * ライティング情報通知用ハンドラを登録する。
38      * @param lightingHandler ハンドラ
39      */
40     void setLightingHandler(VmdLightingHandler lightingHandler){
41         this.handler = lightingHandler;
42         return;
43     }
44
45     /**
46      * ライティングデータのパースと通知。
47      * <p>影演出データが無ければ読みに行かない。
48      * @throws IOException IOエラー
49      * @throws MmdFormatException フォーマットエラー
50      */
51     void parse() throws IOException, MmdFormatException {
52         parseVmdLighting();
53         if(hasMore()){
54             parseVmdShadow();
55         }
56         return;
57     }
58
59     /**
60      * 光源モーションデータのパースと通知。
61      * @throws IOException IOエラー
62      * @throws MmdFormatException フォーマットエラー
63      */
64     private void parseVmdLighting() throws IOException, MmdFormatException{
65         int lightMotionNo = parseLeInt();
66
67         if(this.handler == null){
68             skip(VmdConst.LUMINOUS_DATA_SZ * lightMotionNo);
69             return;
70         }
71
72         this.handler.loopStart(VmdLightingHandler.LUMINOUS_LIST,
73                 lightMotionNo);
74
75         for(int ct = 0; ct < lightMotionNo; ct++){
76             int keyFrameNo = parseLeInt();
77             this.handler.vmdLuminousMotion(keyFrameNo);
78
79             float rVal = parseLeFloat();
80             float gVal = parseLeFloat();
81             float bVal = parseLeFloat();
82             this.handler.vmdLuminousColor(rVal, gVal, bVal);
83
84             float xVec = parseLeFloat();
85             float yVec = parseLeFloat();
86             float zVec = parseLeFloat();
87             this.handler.vmdLuminousDirection(xVec, yVec, zVec);
88
89             this.handler.loopNext(VmdLightingHandler.LUMINOUS_LIST);
90         }
91
92         this.handler.loopEnd(VmdLightingHandler.LUMINOUS_LIST);
93
94         return;
95     }
96
97     /**
98      * 影演出データのパースと通知。
99      * @throws IOException IOエラー
100      * @throws MmdFormatException フォーマットエラー
101      */
102     private void parseVmdShadow() throws IOException, MmdFormatException{
103         int shadowMotionNo = parseLeInt();
104
105         if(this.handler == null){
106             skip(VmdConst.SHADOW_DATA_SZ * shadowMotionNo);
107             return;
108         }
109
110         this.handler.loopStart(VmdLightingHandler.SHADOW_LIST,
111                 shadowMotionNo);
112
113         for(int ct = 0; ct < shadowMotionNo; ct++){
114             int keyFrameNo = parseLeInt();
115             this.handler.vmdShadowMotion(keyFrameNo);
116
117             byte shadowMode = parseByte();
118             this.handler.vmdShadowMode(shadowMode);
119
120             float shadowScope = parseLeFloat();
121             this.handler.vmdShadowScopeRaw(shadowScope);
122
123             this.handler.loopNext(VmdLightingHandler.SHADOW_LIST);
124         }
125
126         this.handler.loopEnd(VmdLightingHandler.SHADOW_LIST);
127
128         return;
129     }
130
131 }