简体   繁体   English

LWJGL OpenGL创建着色器时找不到上下文

[英]LWJGL OpenGL no context found when creating a shader

When i call glCreateShader() I get No OpenGL context found in the current thread. 当我调用glCreateShader()时,在当前线程中找不到OpenGL上下文。 runtime exception and I've initialized the display before doing it here's my main func : 运行时异常,在执行此操作之前,我已经初始化了显示,这是我的主要功能:

public static void main(String[] args) {
    MainDisplay md = new MainDisplay();
    md.create();
    md.init();
    md.run();
}

here's the create func : 这是创建函数:

public void create() {
    try {
        Display.setDisplayMode(new DisplayMode(1920, 1080));
        Display.setTitle("Dryad Engine 1.0.0");
        Display.setFullscreen(false);
        Display.setResizable(true);
    } catch (LWJGLException ex) {
        Logger.getLogger(MainDisplay.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(-1);
    }
}

here's the init func : 这是init函数:

public void init() {
    //glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // commented because I get the same error ..
    try {
        shaderProgramID = ShaderFactory.createShaderProgram("vertexShader", "fragmentShader");
        glUseProgram(shaderProgramID);
        bunny = OBJLoader.parseOBJ(new File("src/com/dryadengine/assets/bunny.obj"));
        FloatBuffer vbo = BufferUtils.createFloatBuffer(bunny.getVertices().length);
        vbo.put(bunny.getVertices());
        vbo.flip();
        vboID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, vbo, GL_STATIC_DRAW);
        vPositionID = glGetAttribLocation(shaderProgramID, "vPosition");
        glEnableVertexAttribArray(vPositionID);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainDisplay.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainDisplay.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I get the exception from this call in the init func : 我在init func中从此调用中获得了异常:

shaderProgramID = ShaderFactory.createShaderProgram("vertexShader", "fragmentShader");

here's the createShaderProgram func : 这是createShaderProgram函数:

public static int createShaderProgram(String vertexShaderName, String fragmentShaderName) throws FileNotFoundException, IOException {
    ArrayList<Integer> shaders = new ArrayList();
    shaders.add(ShaderFactory.compileShader(GL_VERTEX_SHADER, getShaderFileCode(COMMON_SHADERS_PATH + vertexShaderName + SHADER_EXTENSION)));
    shaders.add(ShaderFactory.compileShader(GL_FRAGMENT_SHADER, getShaderFileCode(COMMON_SHADERS_PATH + fragmentShaderName + SHADER_EXTENSION)));
    return ShaderFactory.linkProgram(shaders);
}

now the exception is coming from the compileShader func, here is the func : 现在,异常来自compileShader函数,这是func:

public static int compileShader(int shaderType, String shaderCode) {
    int shaderID = glCreateShader(shaderType);
    glShaderSource(shaderID, shaderCode);
    glCompileShader(shaderID);
    int status = glGetShaderi(shaderID, GL_COMPILE_STATUS);
    if (status == GL_FALSE) {
        glDeleteShader(shaderID);
        throw new RuntimeException(glGetShaderInfoLog(shaderID, glGetShaderi(shaderID, GL_INFO_LOG_LENGTH)));
    }
    return shaderID;
}

now to be more precise the exception is coming from this call in the compileShader func : 现在更准确地说,异常来自于compileShader func中的此调用:

int shaderID = glCreateShader(shaderType);

seems like openGL is unable to generate me a new shader program, why is that ? 似乎openGL无法生成我新的着色器程序,这是为什么? I initialized the Display before calling it so what could it be ? 我在调用Display之前对其进行了初始化,那会是什么?

You need to call Display.create() to create the OpengGL context. 您需要调用Display.create()来创建OpengGL上下文。 You could add this to the end of the try block in your create() method. 您可以将其添加到create()方法中try块的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM