イントロダクション
前回は、"LWJGL Hello Wold"を写経しました。そして起動しても動きませんでした。そんな時に何をするべきか記載いたします。※自分の場合になります。
Introduction
Last time, we did "写経(shakyo)". But it did not view anything. This time, list up to what we have to do?. ※ In my case.
やるべきこと
この時に考えるのは以下の点見直す、もしくは考えます。
- 写し間違えがないか確認する
- ビルドエラーがないか確認する
- 実装漏れがないか確認する
- エラーが出ている部分を見つける
What we have to do?
- check there is not miss in target code.
- check there is not build error in Eclipse.
- check wrote all code in target code.
- if you get Exception then find out Exception point in your code.
自分の場合 (In my case)
解決はしたので、結果から記載します。下は実行結果です。
I had get resolution after performing the above procedure.
package zenryokuservice.gui.jogl.prac; import org.lwjgl.*; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; import org.lwjgl.system.*; import java.nio.*; import static org.lwjgl.glfw.Callbacks.*; import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryStack.*; import static org.lwjgl.system.MemoryUtil.*; /** * LWJGL初めの練習用クラスです。 * This Class is Hello LWJGL * * @see https://github.com/SilverTiger/lwjgl3-tutorial/blob/master/src/silvertiger/tutorial/lwjgl/Introduction.java * * @author takunoji */ public class LwjglPracLv1 { /** ウィンドウハンドラ */ long window; /** * 初めに動くメソッドです。全てはここから始まる。。。<br/> * Java program start from main method...<br/> * <ul> * <li>LWJGLの起動には"-XstartOnFirstThread"のVM引数がいるようだ</li> * <li>Javaのバージョンは1.8以降を使用する</li> * </ul> * @param args Array of String */ public static void main(String[] args) { LwjglPracLv1 lv1 = new LwjglPracLv1(); // 2018/10/12 このメソッドを起動していないから動かなかった。。。 lv1.run(); } public void run() { System.out.println("Hello LWJGL " + Version.getVersion()); // 初期処理 init(); // ゲームループ gameLoop(); glfwFreeCallbacks(window); glfwDestroyWindow(window); System.out.println("終了処理"); glfwTerminate(); glfwSetErrorCallback(null).free(); } /** * 初期処理 */ private void init() { // エラー時のコールバックに標準エラー出力を設定する GLFWErrorCallback.createPrint(System.err).set(); // GLFWの初期化 if (!glfwInit()) { throw new IllegalStateException("Unalbe to Initialize GLFW"); } // GLFWの設定処理 glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); window = glfwCreateWindow(300, 300, "Hello LWJGL", NULL, NULL); if(window == NULL) { throw new RuntimeException("Failed to create GLFW Window"); } glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop }); // 2018/10/12 実装漏れ try (MemoryStack stack = stackPush()) { IntBuffer pWidth = stack.mallocInt(1); IntBuffer pHeight = stack.mallocInt(1); // ウィンドウサイズ指定 glfwGetWindowSize(window, pWidth, pHeight); // GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor()); // 画面の中央座標を設定する glfwSetWindowPos( window , (vidMode.width() - pWidth.get(0) / 2) , (vidMode.height() - pHeight.get(0)) / 2 ); // OpenGLコンテキストを作成する glfwMakeContextCurrent(window); // enable v-sync glfwSwapInterval(1); // 表示処理 glfwShowWindow(window); // 2018/10/12 実装漏れここまで } } // 写経元のコードでは「loop()」になっている private void gameLoop() { GL.createCapabilities(); glClearColor(1.0f, 0.0f, 0.0f, 0.0f); while ( !glfwWindowShouldClose(window) ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer glfwSwapBuffers(window); // swap the color buffers glfwPollEvents(); } } }
初めの起動時には以下のようなエラーが出ました。
First time, I had get Exception as bellow.
Exception in thread "main" java.lang.IllegalStateException: There is no OpenGL context current in the current thread. at org.lwjgl.opengl.GL.createCapabilities(GL.java:376) at org.lwjgl.opengl.GL.createCapabilities(GL.java:320) at zenryokuservice.gui.jogl.prac.LwjglPracLv1.gameLoop(LwjglPracLv1.java:108) at zenryokuservice.gui.jogl.prac.LwjglPracLv1.run(LwjglPracLv1.java:48) at zenryokuservice.gui.jogl.prac.LwjglPracLv1.main(LwjglPracLv1.java:40)
もし、英語がよめなかったら「google翻訳」などを使えば良いと思います。※自分はフル活用しています(笑)
「OpenGLのコンテキストがありませんよ」と怒られているので、調べました。そしたら、赤字で書いている行以降の実装をしていなかったので上記のようなエラー(例外)が出ました。その他にもメインメソッドのJavaDoc部分に記載したようなことがありました。JavaDoc部分は緑色にしてあります。
VM引数に関してはこちらのページを参照してください。ページ内を「VM引数」で検索すれば見つかります。
I found message that "There is no OpenGL context....", and thought how to get resolution. so I wrote resolution in javaDoc wrote in green text. and I missed implementation in red text part.
次回は、このコードをいじってみようと思います(笑)
Next time I will play with this code(笑)