尝试加载VOA时,没有当前上下文或当前上下文中不可用的函数

w8ntj3qf  于 2022-10-18  发布在  其他
关注(0)|答案(2)|浏览(263)

来源:

import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

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);
    }

    public void cleanUP() {
        for(int vao:vaos) {
            GL30.glDeleteVertexArrays(vao);
        }
        for(int vbo:vbos) {
            GL30.glDeleteVertexArrays(vbo);
        }
    }

    private int createVAO() {
        int vaoID = GL30.glGenVertexArrays();
        vaos.add(vaoID);
        GL30.glBindVertexArray(vaoID);
        return vaoID;
    }

    private void storeDataInAttributeList(int attributeNumber,float[] data) {
        int vaoID = GL15.glGenBuffers();
        vbos.add(vaoID);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vaoID);
        FloatBuffer buffer = storeDataImFloatBuffer(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);
    }

    private FloatBuffer storeDataImFloatBuffer(float[] data) {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
        buffer.put(data);
        buffer.flip();
        return buffer;
    }
}

错误:

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called.
 The JVM will abort execution.
 at org.lwjgl.opengl.GL30C.nglGenVertexArrays(Native Method) 
 at org.lwjgl.opengl.GL30C.glGenVertexArrays(GL30C.java:2420) 
 at org.lwjgl.opengl.GL30.glGenVertexArrays(GL30.java:2369) at Loader.createVAO(Loader.java:32) 
 at Loader.loadToVAO(Loader.java:16) at MainGameLoop.main(MainGameLoop.java:21)
b4lqfgs4

b4lqfgs41#

您尚未调用glfwMakeConextCurrent(Window)或GL.createCapables()来实际创建OpenGL上下文。在窗口创建之后添加这两行。

ghg1uchk

ghg1uchk2#

如果您使用的是MacOS,请在LWJGL 3中创建显示后执行此操作:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

相关问题