イントロダクション
ここのところ負け越しの1引分けでしたが、ついにやっつけました。
<戦闘履歴>
- Eclipse アプリ作成 Lv5〜惨敗:CubeにTextureを貼る〜「惨敗」
- Java 3DGame LWJGL Retry Lv3 Texture〜動かして理解する3〜「負け越し」
- Java 3DGame LWJGL Retry Lv4 デバック〜動かして理解する4〜「黒星」
- Java 3DGame LWJGL Retry Lv5 遊んでみる〜動かして理解する5〜「引分け」
- Java 3DGame LWJGL Retry Lv6 遊んでみる2〜動かして理解する6〜「白星」
最終的に以下の様なコードができました。主にいじったのは「DummyGame」クラスです。ポイントは以下になります。
- メッシュを作成するときはテクスチャを使用する様に修正
- メッシュを作成するときの各頂点(Vertex)の定義を見直す
- 同様に頂点を結びつける順番(indices)を見直す
<DummyGame#init()>
@Override public void init(Window window) throws Exception { renderer.init(window); // Cubeの高さ ArrayList<float[]> floats = new ArrayList<>(); floats.add(new float[] {0.0001f, 0.12f, 0.3f, 0.001f, 0.1f, 0.25f, 0.1f}); floats.add(new float[] {0.15f, 0.19f, 0.23f, 0.2f, 0.08f, 0.13f, 0.12f}); floats.add(new float[] {0.1f, 0.2f, 0.4f, 0.001f, 0.2f, 0.05f, 0.15f}); floats.add(new float[] {0.11f, 0.12f, 0.3f, 0.001f, 0.1f, 0.25f, 0.1f}); floats.add(new float[] {0.12f, 0.13f, 0.14f, 0.015f, 0.16f, 0.17f, 0.18f}); // Cubeの底面のサイズ(正方形) final float cubeSize = 0.1f; // x軸の初期値 final float xInit = -0.5f; // y軸の初期値 final float yInit = -0.8f; // z軸の初期値 final float zInit = -2; // x軸の増減幅 final float xWidth = 0.185f; // y軸の増減幅 final float yWidth = 0.033f; // z軸の増減幅 final float zWidth = 0.1f; ArrayList arr = new ArrayList(); // 初めの1回目だけ曜日のテキストプレートを作成する boolean isCreateTexture = false; // 1ヶ月分(5週間分のマスを作る) for(int j = 1; j <= 5; j++) { // 開始点より一列文ずらす // X軸の開始点 float xStart = xInit - (0.1f * j); // Y軸の開始点 float yStart = yInit + (0.06f * j) ; // Z軸の開始点 float zStart = zInit - (0.16f * j); // 1週間分 float[] weekArr = floats.get(j-1); // 1回目だけテクスチャを作成する isCreateTexture = j == 1 ? true: false; for(int i = 1; i <= 7; i++) { float xAdd = xWidth * i; float yAdd = yWidth * i; float zAdd = zWidth * i; if (isCreateTexture) { arr.add(putOnTexturePlate(xStart + xAdd + 0.01f , yStart + yAdd - 0.03f, zStart - zAdd + 0.03f, i-1)); } float val = weekArr[i - 1]; arr.add(createCube(val, cubeSize, xStart + xAdd, yStart + yAdd, zStart - zAdd)); } } // 配列の要素数を指定する System.out.println("GmaeItems: " + arr.size()); GameItem[] items = new GameItem[arr.size()]; // 配列の取り出し gameItems = arr.toArray(items); // DEBUG //debug(); }
<DummyGame#putOnTexturePlate()>
/** * Texture作成メソッド * @param xPos * @param yPos * @param zPos * @param num 曜日の番号0:月〜7:日 * @return GameItem */ private GameItem putOnTexturePlate(float xPos, float yPos, float zPos, int num) { float size = 0.08f; float[] positions = new float[] { -1 * size, size, size, // V0 -1 * size, -1 * size, size, //V1 size, -1 * size, size, // V2 size, size, size, // V3 -1 * size, size, -1 * size, // V4 size, -1 * size, size, //V5 -1 * size, -1 * size, -1 * size, // V6 size, -1 * size, -1 * size, // V7 }; float[] textCoords = new float[]{ 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, }; int[] indices = new int[] { 0, 1, 3, 3, 1, 2 }; Texture texture = null; try { texture = new Texture("/textures/" + weekTexture[num] + ".png"); } catch(Exception e) { e.printStackTrace(); } TexturedMesh mesh = new TexturedMesh(positions, textCoords, indices, texture); GameItem gameItem = new GameItem(mesh); gameItem.setPosition(xPos, yPos, zPos); gameItem.setRotation(20, 30, 0); return gameItem; }
<DummyGame#createCube()>
private GameItem createCube(float height, float cubeSize, float posX, float posY, float posZ) { // Create the Mesh float[] positions = new float[]{ // VO -1 * cubeSize, height, cubeSize, // V1 -1 * cubeSize, -1 * cubeSize, cubeSize, // V2 cubeSize, -1 * cubeSize, cubeSize, // V3 cubeSize, height, cubeSize, // V4 -1 * cubeSize, height, -1 * cubeSize, // V5 cubeSize, height, -1 * cubeSize, // V6 -1 * cubeSize, -1 * cubeSize, -1 * cubeSize, // V7 cubeSize, -1 * cubeSize, -1 * cubeSize, }; float[] colours = new float[]{ 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, }; int[] indices = new int[]{ // Front face 0, 1, 3, 3, 1, 2, // Top Face 4, 0, 3, 5, 4, 3, // Right face 3, 2, 7, 5, 3, 7, // Left face 6, 1, 0, 6, 0, 4, // Bottom face 2, 1, 6, 2, 6, 7, // Back face 7, 6, 4, 7, 4, 5, }; Texture texture = null; try { texture = new Texture("/textures/wood1.png"); } catch(Exception e) { e.printStackTrace(); } TexturedMesh mesh = new TexturedMesh(positions, colours, indices, texture); GameItem gameItem = new GameItem(mesh); gameItem.setPosition(posX, posY, posZ); gameItem.setRotation(20, 30, 0); return gameItem; }
上記のポイントを抑えサイドコードを直したところうまくいきました。
関連ページ一覧
<Java Basic>