イントロダクション
前回、惨敗を喫してしまいました。Textureが結局判らずじまい。。。
悔しいので、2次元描画のプログラムをカスタムしてもう一度やってみます。
参照するドキュメント:LWJGL GitBookのChapterのChapter5
カスタムするコード:LWJGL GitBook Chapter5
ここで描画した四角形にテクスチャを貼ろうと思います。
<元の画像>
元々のコードにTextureクラスを追加して、MeshクラスにTextureを作成するコードを加えたものを起動してみました。
ちなみにGitに登録しているソースです。下のような感じです。
そして、実行してみた結果は以下です。
想定と全く違うものです。。。原因は一体なんだろう?
ちなみに一部ですが、ソースはこんな感じです。
<Meshクラス>
// 最後の「boolean」はコンストラクタのオーバーロードのためだけにつけたものです。エラー回避ようです。
public Mesh(float[] positions, int[] indices, float[] textCoords, boolean flg) {
FloatBuffer posBuffer = null;
IntBuffer indicesBuffer = null;
FloatBuffer textCoordBuffer = null;
try {
texture = new Texture("/textures/Mon.png");
} catch(Exception e) {
e.printStackTrace();
}
vboIdList = new ArrayList<>();
try {
vertexCount = indices.length;
vaoId = glGenVertexArrays();
vboIdList.add(vaoId);
glBindVertexArray(vaoId);
// Position VBO
int posVboId = glGenBuffers();
vboIdList.add(posVboId);
posBuffer = MemoryUtil.memAllocFloat(positions.length);
posBuffer.put(positions).flip();
glBindBuffer(GL_ARRAY_BUFFER, posVboId);
glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
// Texutre coordinates VBO:2018/11/07追記部分です。
int vboId = glGenBuffers();
vboIdList.add(vboId);
textCoordBuffer = MemoryUtil.memAllocFloat(textCoords.length);
textCoordBuffer.put(textCoords).flip();
glBindBuffer(GL_ARRAY_BUFFER, vaoId);
glBufferData(GL_ARRAY_BUFFER, textCoordBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
// Index VBO
idxVboId = glGenBuffers();
vboIdList.add(idxVboId);
indicesBuffer = MemoryUtil.memAllocInt(indices.length);
indicesBuffer.put(indices).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idxVboId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
} finally {
if (posBuffer != null) {
MemoryUtil.memFree(posBuffer);
}
if (indicesBuffer != null) {
MemoryUtil.memFree(indicesBuffer);
}
if (textCoordBuffer != null) {
MemoryUtil.memFree(textCoordBuffer);
}
}
}
これで、とりあえず動かしてみたのだけど、上記のキャプチャ通りの結果でした。ブログを書いていて気がついたけど「シェーダ・プログラム」を修正していなかった。。。
関連ページ一覧
<今回のやっていること>
<Java Basic>
- Java Basic for文 〜Step1_3_1〜
- Java Basic Level8 〜How to use for statement〜
- Java Basic Level 9〜Training of for statement〜
- Java 3DGame LWJGL GitBook chapter7-1〜Cube作成〜「動画あり」
<サイトマップ>