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