LWJGL OpenGL glGenVertexArrays()错误:此函数不可用(LWJGL OpenGL glGenVertexArrays() Error: This Function is n

编程入门 行业动态 更新时间:2024-10-26 18:24:31
LWJGL OpenGL glGenVertexArrays()错误:此函数不可用(LWJGL OpenGL glGenVertexArrays() Error: This Function is not available)

全部,glGenVertexArrays()有困难。 我收到以下错误:

线程“main”中的异常java.lang.IllegalStateException:此功能不可用。 org.lwjgl.system.Checks.checkFunctionality(Checks.java:57)at org.lwjgl.opengl.GL30.getInstance(GL30.java:667)at org.lwjgl.opengl.GL30.getInstance(GL30.java:662) )atg.lwjgl.opengl.GL30.nglGenVertexArrays(GL30.java:2789)atg.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:2816)at renderEngine.Loader.createVAO(Loader.java:26)at renderEngine .Loader.loadToVAO(Loader.java:19)at renderEngine.MainGameLoop.main(MainGameLoop.java:27)

这是我的主游戏循环(我从createDisplay()方法调用GL.createCapabilities()。)

package renderEngine; import static org.lwjgl.glfw.GLFW.*; public class MainGameLoop { public static DisplayManager dm = new DisplayManager(); public static void main(String[] args) { //Create Display dm.createDisplay(); Loader loader = new Loader(); Renderer renderer = new Renderer(); float[] vertices = { -0.5f,0.5f,0f, -0.5f,-0.5f,0f, 0.5f,-0.5f,0f, 0.5f,-0.5f,0f, 0.5f,0.5f,0f, -0.5f,0.5f,0f }; RawModel model = loader.loadToVAO(vertices); //Game Loop while (glfwWindowShouldClose(dm.getWindowID()) != GLFW_TRUE) { renderer.prepare(); renderer.render(model); dm.updateDisplay(); } //Destroy Display dm.destroyWindow(); loader.cleanUp(); } }

如上所述,这是Display Manager类:

package renderEngine; import static org.lwjgl.glfw.GLFW.*; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.opengl.GL; public class DisplayManager { private GLFWErrorCallback errorCallback; private GLFWKeyCallback keyCallback; private long windowID; //Constructor public DisplayManager(){ init(); } private void init(){ if(glfwInit() != GLFW_TRUE){ throw new IllegalStateException("Unable to initiate GLFW"); } } public long createDisplay(){ windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); setErrorCallback(); setKeyCallback(); glfwMakeContextCurrent(windowID); GL.createCapabilities(); return windowID; } public long getWindowID(){ return this.windowID; } private void setErrorCallback(){ errorCallback = GLFWErrorCallback.createPrint(System.err); glfwSetErrorCallback(errorCallback); } private void setKeyCallback(){ keyCallback = new GLFWKeyCallback(){ @Override public void invoke(long window, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ glfwSetWindowShouldClose(window, GLFW_TRUE); } } }; glfwSetKeyCallback(windowID, keyCallback); } public void updateDisplay(){ GLFW.glfwSwapInterval(1); glfwSwapBuffers(windowID); glfwPollEvents(); } public void destroyWindow(){ glfwDestroyWindow(windowID); keyCallback.release(); glfwTerminate(); errorCallback.release(); } }

如果看起来上面提到的错误与loader类有关:

package renderEngine; import java.util.List; import java.nio.FloatBuffer; import java.util.ArrayList; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; public class Loader { private List<Integer> vaos = new ArrayList<Integer>(); private List<Integer> vbos = new ArrayList<Integer>(); public RawModel loadToVAO(float[] positions){ int vaoID = createVAO(); storeDataInAttributeList(0,positions); unbindVAO(); return new RawModel(vaoID,positions.length/3); } private int createVAO() { int vaoID = GL30.glGenVertexArrays(); vaos.add(vaoID); GL30.glBindVertexArray(vaoID); return vaoID; } private void storeDataInAttributeList(int attributeNumber, float[] data) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); FloatBuffer buffer = storeDataInFloatBuffer(data); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } private void unbindVAO() { GL30.glBindVertexArray(0); //0 un-binds currently bound VAO } public void cleanUp(){ for(int vao:vaos){ GL30.glDeleteVertexArrays(vao); } for(int vbo:vbos){ GL15.glDeleteBuffers(vbo); } } private FloatBuffer storeDataInFloatBuffer(float[] data){ FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); buffer.put(data); buffer.flip(); return buffer; } }

特别是这行代码:

GL30.glBindVertexArray(vaoID);

据我所知,这是正确的方法,并且已经从主线程设置/调用GL功能/上下文(主游戏循环主方法,利用显示管理器)

为了完整性,这里是RawModel和Renderer类,不要怀疑这些问题:

package renderEngine; public class RawModel { private int vaoID; private int vertexCount; public RawModel(int vaoID, int vertexCount){ this.vaoID = vaoID; this.vertexCount = vertexCount; } public int getVaoID() { return vaoID; } public int getVertexCount() { return vertexCount; } }

这是Renderer类:

package renderEngine; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; public class Renderer { public void prepare(){ GL11.glClearColor(1, 0, 0, 1); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); } public void render(RawModel model){ GL30.glBindVertexArray(model.getVaoID()); GL20.glEnableVertexAttribArray(0); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount()); GL20.glDisableVertexAttribArray(0); GL30.glBindVertexArray(0); } }

对于我对'glGenVertexArrays'的实现,你能给我的任何帮助都将非常感激。

我现在添加了以下内容

GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);

但是,我现在收到以下错误:

线程“main”中的异常java.lang.RuntimeException:无法在renderEngine.MainGameLoop.main(MainGameLoop.java:12)的renderEngine.DisplayManager.createDisplay(DisplayManager.java:37)创建GLFW窗口

我的createDisplay()的具体实现如下:

public long createDisplay(){ GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); setErrorCallback(); setKeyCallback(); glfwMakeContextCurrent(windowID); GL.createCapabilities(); return windowID; }

UPDATE

如果已删除错误回调,则认为它们可能导致问题。 产生的错误是:

线程“main”中的异常java.lang.RuntimeException:无法在renderEngine.MainGameLoop.main(MainGameLoop.java:12)的renderEngine.DisplayManager.createDisplay(DisplayManager.java:40)上创建GLFW窗口

似乎抛出RuntimeException,并且如果无法创建glfw窗口,则显示我的自定义错误消息。 这可能与我运行此命令的顺序一致吗? 我已经玩过使用noluck放置'init'方法。 必须有一些我可以忽略的东西?...谢谢。

更新了显示管理器的代码:

package renderEngine;

import static org.lwjgl.glfw.GLFW。*;

import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.opengl.GL; public class DisplayManager { private GLFWErrorCallback errorCallback; private GLFWKeyCallback keyCallback; private long windowID; //Constructor public DisplayManager(){ init(); } private void init(){ if(glfwInit() != GLFW_TRUE){ throw new IllegalStateException("Unable to initiate GLFW"); } } public long createDisplay(){ GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); System.out.println(windowID); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } glfwMakeContextCurrent(windowID); GL.createCapabilities(); GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); //setErrorCallback(); //setKeyCallback(); return windowID; } public long getWindowID(){ return this.windowID; } private void setErrorCallback(){ errorCallback = GLFWErrorCallback.createPrint(System.err); glfwSetErrorCallback(errorCallback); } private void setKeyCallback(){ keyCallback = new GLFWKeyCallback(){ @Override public void invoke(long window, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ glfwSetWindowShouldClose(window, GLFW_TRUE); } } }; glfwSetKeyCallback(windowID, keyCallback); } public void updateDisplay(){ GLFW.glfwSwapInterval(1); glfwSwapBuffers(windowID); glfwPollEvents(); } public void destroyWindow(){ glfwDestroyWindow(windowID); keyCallback.release(); glfwTerminate(); errorCallback.release(); } }

非常感谢和亲切的问候,杰克

All, Having difficulty with glGenVertexArrays(). I get the following error:

Exception in thread "main" java.lang.IllegalStateException: This functionality is not available. at org.lwjgl.system.Checks.checkFunctionality(Checks.java:57) at org.lwjgl.opengl.GL30.getInstance(GL30.java:667) at org.lwjgl.opengl.GL30.getInstance(GL30.java:662) at org.lwjgl.opengl.GL30.nglGenVertexArrays(GL30.java:2789) at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:2816) at renderEngine.Loader.createVAO(Loader.java:26) at renderEngine.Loader.loadToVAO(Loader.java:19) at renderEngine.MainGameLoop.main(MainGameLoop.java:27)

Here is my Main Game Loop (I call GL.createCapabilities() from the createDisplay() method.)

package renderEngine; import static org.lwjgl.glfw.GLFW.*; public class MainGameLoop { public static DisplayManager dm = new DisplayManager(); public static void main(String[] args) { //Create Display dm.createDisplay(); Loader loader = new Loader(); Renderer renderer = new Renderer(); float[] vertices = { -0.5f,0.5f,0f, -0.5f,-0.5f,0f, 0.5f,-0.5f,0f, 0.5f,-0.5f,0f, 0.5f,0.5f,0f, -0.5f,0.5f,0f }; RawModel model = loader.loadToVAO(vertices); //Game Loop while (glfwWindowShouldClose(dm.getWindowID()) != GLFW_TRUE) { renderer.prepare(); renderer.render(model); dm.updateDisplay(); } //Destroy Display dm.destroyWindow(); loader.cleanUp(); } }

As referenced above, here is the Display Manager class:

package renderEngine; import static org.lwjgl.glfw.GLFW.*; import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.opengl.GL; public class DisplayManager { private GLFWErrorCallback errorCallback; private GLFWKeyCallback keyCallback; private long windowID; //Constructor public DisplayManager(){ init(); } private void init(){ if(glfwInit() != GLFW_TRUE){ throw new IllegalStateException("Unable to initiate GLFW"); } } public long createDisplay(){ windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); setErrorCallback(); setKeyCallback(); glfwMakeContextCurrent(windowID); GL.createCapabilities(); return windowID; } public long getWindowID(){ return this.windowID; } private void setErrorCallback(){ errorCallback = GLFWErrorCallback.createPrint(System.err); glfwSetErrorCallback(errorCallback); } private void setKeyCallback(){ keyCallback = new GLFWKeyCallback(){ @Override public void invoke(long window, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ glfwSetWindowShouldClose(window, GLFW_TRUE); } } }; glfwSetKeyCallback(windowID, keyCallback); } public void updateDisplay(){ GLFW.glfwSwapInterval(1); glfwSwapBuffers(windowID); glfwPollEvents(); } public void destroyWindow(){ glfwDestroyWindow(windowID); keyCallback.release(); glfwTerminate(); errorCallback.release(); } }

If would seem the above mentioned error is with the loader class:

package renderEngine; import java.util.List; import java.nio.FloatBuffer; import java.util.ArrayList; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL15; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; public class Loader { private List<Integer> vaos = new ArrayList<Integer>(); private List<Integer> vbos = new ArrayList<Integer>(); public RawModel loadToVAO(float[] positions){ int vaoID = createVAO(); storeDataInAttributeList(0,positions); unbindVAO(); return new RawModel(vaoID,positions.length/3); } private int createVAO() { int vaoID = GL30.glGenVertexArrays(); vaos.add(vaoID); GL30.glBindVertexArray(vaoID); return vaoID; } private void storeDataInAttributeList(int attributeNumber, float[] data) { int vboID = GL15.glGenBuffers(); vbos.add(vboID); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID); FloatBuffer buffer = storeDataInFloatBuffer(data); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); GL20.glVertexAttribPointer(attributeNumber, 3, GL11.GL_FLOAT, false, 0, 0); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); } private void unbindVAO() { GL30.glBindVertexArray(0); //0 un-binds currently bound VAO } public void cleanUp(){ for(int vao:vaos){ GL30.glDeleteVertexArrays(vao); } for(int vbo:vbos){ GL15.glDeleteBuffers(vbo); } } private FloatBuffer storeDataInFloatBuffer(float[] data){ FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); buffer.put(data); buffer.flip(); return buffer; } }

In Particular this line of code:

GL30.glBindVertexArray(vaoID);

As far as I can see this is the correct method to do it, and the GL capabilities/context have been set/called from the main thread (main game loops main method, utilising the display manager)

For completeness, here are the RawModel and Renderer Classes, do not suspect an issue these these:

package renderEngine; public class RawModel { private int vaoID; private int vertexCount; public RawModel(int vaoID, int vertexCount){ this.vaoID = vaoID; this.vertexCount = vertexCount; } public int getVaoID() { return vaoID; } public int getVertexCount() { return vertexCount; } }

And here is the Renderer Class:

package renderEngine; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL30; public class Renderer { public void prepare(){ GL11.glClearColor(1, 0, 0, 1); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); } public void render(RawModel model){ GL30.glBindVertexArray(model.getVaoID()); GL20.glEnableVertexAttribArray(0); GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, model.getVertexCount()); GL20.glDisableVertexAttribArray(0); GL30.glBindVertexArray(0); } }

Any assistance you can give me regarding my implementation of 'glGenVertexArrays' would be much appreciated.

I have now added the following

GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);

However, am now getting the following error:

Exception in thread "main" java.lang.RuntimeException: Failed to create the GLFW window at renderEngine.DisplayManager.createDisplay(DisplayManager.java:37) at renderEngine.MainGameLoop.main(MainGameLoop.java:12)

The specific implementation of my createDisplay() is as follows:

public long createDisplay(){ GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); setErrorCallback(); setKeyCallback(); glfwMakeContextCurrent(windowID); GL.createCapabilities(); return windowID; }

UPDATE

If have removed the error callbacks, as thought they might be causing an issue. The resulting error is:

Exception in thread "main" java.lang.RuntimeException: Failed to create the GLFW window at renderEngine.DisplayManager.createDisplay(DisplayManager.java:40) at renderEngine.MainGameLoop.main(MainGameLoop.java:12)

It seems the RuntimeException is being thrown, and is displaying my custom error message if the glfw window cannot be created. Could this be todo with the order in which I run this? I have played around with the placement of the 'init' method with noluck. There has got to be something I am overlooking?... Thanks.

Updated code for the Display Manager:

package renderEngine;

import static org.lwjgl.glfw.GLFW.*;

import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.opengl.GL; public class DisplayManager { private GLFWErrorCallback errorCallback; private GLFWKeyCallback keyCallback; private long windowID; //Constructor public DisplayManager(){ init(); } private void init(){ if(glfwInit() != GLFW_TRUE){ throw new IllegalStateException("Unable to initiate GLFW"); } } public long createDisplay(){ GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); windowID = glfwCreateWindow(640,480,"Hello World!", 0, 0); System.out.println(windowID); if(windowID == 0){ glfwTerminate(); throw new RuntimeException("Failed to create the GLFW window"); } glfwMakeContextCurrent(windowID); GL.createCapabilities(); GLFW.glfwSetWindowTitle(windowID, "GLFW Window"); //setErrorCallback(); //setKeyCallback(); return windowID; } public long getWindowID(){ return this.windowID; } private void setErrorCallback(){ errorCallback = GLFWErrorCallback.createPrint(System.err); glfwSetErrorCallback(errorCallback); } private void setKeyCallback(){ keyCallback = new GLFWKeyCallback(){ @Override public void invoke(long window, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ glfwSetWindowShouldClose(window, GLFW_TRUE); } } }; glfwSetKeyCallback(windowID, keyCallback); } public void updateDisplay(){ GLFW.glfwSwapInterval(1); glfwSwapBuffers(windowID); glfwPollEvents(); } public void destroyWindow(){ glfwDestroyWindow(windowID); keyCallback.release(); glfwTerminate(); errorCallback.release(); } }

Many Thanks and Kind Regards, Jake

最满意答案

glGenVertexArrays函数需要OpenGL 3.0或更高版本。 你必须明确地告诉GLFW。 在调用glfwCreateWindow之前:

GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.CONTEXT_VERSION_MINOR, 0);

您可以通过调用GL11.glGetString(GL11.GL_VERSION)或查看GL.getCapabilities().OpenGL30标志来检查您是否拥有OpenGL 3.0或更高版本。

The glGenVertexArrays function requires OpenGL 3.0 or higher. You have to explicitly tell GLFW this. Before calling glfwCreateWindow:

GLFW.glfwDefaultWindowHints(); GLFW.glfwWindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3); GLFW.glfwWindowHint(GLFW.CONTEXT_VERSION_MINOR, 0);

You can check that you have OpenGL 3.0 or higher by calling GL11.glGetString(GL11.GL_VERSION) or by looking at the GL.getCapabilities().OpenGL30 flag.

更多推荐

本文发布于:2023-07-29 14:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316731.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不可用   函数   错误   OpenGL   LWJGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!