2 * building shape information
\r
4 * License : The MIT License
\r
5 * Copyright(c) 2010 MikuToga Partners
\r
8 package jp.sourceforge.mikutoga.pmd.pmdloader;
\r
10 import jp.sourceforge.mikutoga.corelib.ListUtil;
\r
11 import java.util.Iterator;
\r
12 import java.util.List;
\r
13 import java.util.RandomAccess;
\r
14 import jp.sourceforge.mikutoga.parser.ParseStage;
\r
15 import jp.sourceforge.mikutoga.parser.pmd.PmdShapeHandler;
\r
16 import jp.sourceforge.mikutoga.pmd.BoneInfo;
\r
17 import jp.sourceforge.mikutoga.pmd.PmdModel;
\r
18 import jp.sourceforge.mikutoga.pmd.Pos2d;
\r
19 import jp.sourceforge.mikutoga.pmd.Pos3d;
\r
20 import jp.sourceforge.mikutoga.pmd.Surface;
\r
21 import jp.sourceforge.mikutoga.pmd.Vec3d;
\r
22 import jp.sourceforge.mikutoga.pmd.Vertex;
\r
25 * モデル形状に関する通知をパーサから受け取る。
\r
27 class ShapeBuilder implements PmdShapeHandler {
\r
29 private final List<Vertex> vertexList;
\r
30 private final List<BoneInfo> boneList;
\r
31 private final List<Surface> surfaceList;
\r
33 private Iterator<Vertex> vertexIt;
\r
34 private Vertex currentVertex = null;
\r
36 private Iterator<Surface> surfaceIt;
\r
37 private Surface currentSurface = null;
\r
43 ShapeBuilder(PmdModel model){
\r
46 this.vertexList = model.getVertexList();
\r
47 this.boneList = model.getBoneList();
\r
48 this.surfaceList = model.getSurfaceList();
\r
50 assert this.vertexList instanceof RandomAccess;
\r
51 assert this.surfaceList instanceof RandomAccess;
\r
52 assert this.boneList instanceof RandomAccess;
\r
59 * すでに指定位置にボーンがあればなにもしない。
\r
60 * @param id 0から始まるボーン番号
\r
63 private BoneInfo prepareBone(int id){
\r
64 ListUtil.extendCollection(this.boneList, id + 1);
\r
65 BoneInfo bone = this.boneList.get(id);
\r
67 bone = new BoneInfo();
\r
68 bone.setSerialNumber(id);
\r
69 this.boneList.set(id, bone);
\r
76 * @param stage {@inheritDoc}
\r
77 * @param loops {@inheritDoc}
\r
80 public void loopStart(ParseStage stage, int loops){
\r
81 if(stage == PmdShapeHandler.VERTEX_LIST){
\r
82 ListUtil.prepareDefConsList(this.vertexList, Vertex.class, loops);
\r
83 ListUtil.assignIndexedSerial(this.vertexList);
\r
85 this.vertexIt = this.vertexList.iterator();
\r
86 if(this.vertexIt.hasNext()){
\r
87 this.currentVertex = this.vertexIt.next();
\r
89 }else if(stage == PmdShapeHandler.SURFACE_LIST){
\r
90 ListUtil.prepareDefConsList(this.surfaceList,
\r
91 Surface.class, loops );
\r
92 ListUtil.assignIndexedSerial(this.surfaceList);
\r
94 this.surfaceIt = this.surfaceList.iterator();
\r
95 if(this.surfaceIt.hasNext()){
\r
96 this.currentSurface = this.surfaceIt.next();
\r
100 throw new AssertionError();
\r
107 * @param stage {@inheritDoc}
\r
110 public void loopNext(ParseStage stage){
\r
111 if(stage == PmdShapeHandler.VERTEX_LIST){
\r
112 if(this.vertexIt.hasNext()){
\r
113 this.currentVertex = this.vertexIt.next();
\r
115 }else if(stage == PmdShapeHandler.SURFACE_LIST){
\r
116 if(this.surfaceIt.hasNext()){
\r
117 this.currentSurface = this.surfaceIt.next();
\r
121 throw new AssertionError();
\r
128 * @param stage {@inheritDoc}
\r
131 public void loopEnd(ParseStage stage){
\r
137 * @param xPos {@inheritDoc}
\r
138 * @param yPos {@inheritDoc}
\r
139 * @param zPos {@inheritDoc}
\r
142 public void pmdVertexPosition(float xPos, float yPos, float zPos){
\r
143 Pos3d position = this.currentVertex.getPosition();
\r
144 position.setXPos(xPos);
\r
145 position.setYPos(yPos);
\r
146 position.setZPos(zPos);
\r
152 * @param xVec {@inheritDoc}
\r
153 * @param yVec {@inheritDoc}
\r
154 * @param zVec {@inheritDoc}
\r
157 public void pmdVertexNormal(float xVec, float yVec, float zVec){
\r
158 Vec3d normal = this.currentVertex.getNormal();
\r
159 normal.setXVal(xVec);
\r
160 normal.setYVal(yVec);
\r
161 normal.setZVal(zVec);
\r
167 * @param uVal {@inheritDoc}
\r
168 * @param vVal {@inheritDoc}
\r
171 public void pmdVertexUV(float uVal, float vVal){
\r
172 Pos2d uv = this.currentVertex.getUVPosition();
\r
180 * @param boneId1 {@inheritDoc}
\r
181 * @param boneId2 {@inheritDoc}
\r
182 * @param weightForB1 {@inheritDoc}
\r
185 public void pmdVertexWeight(int boneId1, int boneId2, int weightForB1){
\r
186 BoneInfo bone1 = prepareBone(boneId1);
\r
187 BoneInfo bone2 = prepareBone(boneId2);
\r
189 this.currentVertex.setBonePair(bone1, bone2);
\r
190 this.currentVertex.setWeightA(weightForB1);
\r
197 * @param hideEdge {@inheritDoc}
\r
200 public void pmdVertexEdge(boolean hideEdge){
\r
201 this.currentVertex.setEdgeAppearance( ! hideEdge );
\r
207 * @param vertexId1 {@inheritDoc}
\r
208 * @param vertexId2 {@inheritDoc}
\r
209 * @param vertexId3 {@inheritDoc}
\r
212 public void pmdSurfaceTriangle(int vertexId1,
\r
215 Vertex vtx1 = this.vertexList.get(vertexId1);
\r
216 Vertex vtx2 = this.vertexList.get(vertexId2);
\r
217 Vertex vtx3 = this.vertexList.get(vertexId3);
\r
219 this.currentSurface.setTriangle(vtx1, vtx2, vtx3);
\r