Java lwjgl Modern OpenGL Access Violation Exception using VAO

I am currently following the ThinMatrix OpenGL tutorial on rendering with VAO and VBOS. I am copying the code almost exactly (with the only difference that I am making a static factory class instead of just normal). The only technical difference I can see between my version of the program and his is that I am using lwjgl 3 instead of lwjgl 2.

Here's my source code:

/*************
    MAIN
*************/
import Render.ModelLoader;
import Render.Render;
import Render.RawModel;
import org.lwjgl.Version;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;

import static  org.lwjgl.opengl.GL11.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.system.MemoryUtil.NULL;

public class Main {

    private static long window;
    private static ModelLoader modelLoader;
    private static Render renderer;


    private static final int WIDTH = 1280;
    private static final int HEIGHT = 720;

    public static void main(String[] args) {

        float[] vertices = {
                //Bottom left triangle
                -.5f,.5f,0f,
                -.5f,-.5f,0f,
                .5f,-.5f,0f,
                //Top right triangle
                .5f,-.5f,0f,
                .5f,.5f,0f,
                -.5f,.5f,0f
        };

        RawModel model = modelLoader.loadToVAO(vertices);

        initApp();

        //Main Loop
        while (!glfwWindowShouldClose(window)){
            glfwPollEvents();
            renderer.prepare();
            renderer.render(model);
            glfwSwapBuffers(window);
        }

        cleanUp();
    }

    public static void initApp(){
        System.out.println("LWJGL " + Version.getVersion());
        //Set the error callback routine to use System.err
        GLFWErrorCallback.createPrint(System.err).set();

        //Init GLFW
        if (!glfwInit()){
            throw new IllegalStateException("Could not initialise GLFW.");
        }

        //Create the window
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_RESIZABLE,GLFW_FALSE);
        window = glfwCreateWindow(WIDTH, HEIGHT, "My Display", NULL, NULL);
        if (window == NULL){
            throw new IllegalStateException("Could not create window");
        }

        //Center the window
        GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (vidMode.width()-WIDTH)/2, (vidMode.height()-HEIGHT)/2);

        //Link window context to current thread
        glfwMakeContextCurrent(window);
        //VSync
        glfwSwapInterval(1);
        glfwShowWindow(window);

        GL.createCapabilities();
        System.out.println("OpenGL " + glGetString(GL_VERSION));
    }

    public static void cleanUp(){
        modelLoader.cleanUp();
        Callbacks.glfwFreeCallbacks(window); //Release callbacks
        glfwDestroyWindow(window); //Destroy the window
        glfwTerminate(); //Terminate GLFW
        glfwSetErrorCallback(null).set();
    }

}
/*************
   RAWMODEL
*************/
package Render;

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;
    }
}
/*************
  MODELLOADER
*************/
package Render;

import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class ModelLoader {

    private static List<Integer> vaos = new ArrayList<>();
    private static List<Integer> vbos = new ArrayList<>();

    public static RawModel loadToVAO(float[] positions){
        int vaoID = createVAO();
        vaos.add(vaoID);
        storeDataInAttribList(0,positions);
        unbindVAO();
        return new RawModel(vaoID, positions.length/3);
    }

    private static int createVAO(){
        int vaoID = glGenVertexArrays();
        glBindVertexArray(vaoID);
        return vaoID;
    }

    private static void storeDataInAttribList(int attribNum, float[] data){
        int vboID = glGenBuffers();
        vbos.add(vboID);
        glBindBuffer(GL_ARRAY_BUFFER,vboID);
        FloatBuffer buffer = storeDataInFB(data);
        glBufferData(GL_ARRAY_BUFFER,buffer,GL_STATIC_DRAW);
        glVertexAttribPointer(attribNum,3,GL_FLOAT,false,0,0);
        glBindBuffer(GL_ARRAY_BUFFER,0);
    }

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

    private static void unbindVAO(){
        glBindVertexArray(0);
    }

    public static void cleanUp(){
        for (int vao:vaos) {
            glDeleteVertexArrays(vao);
        }
        for (int vbo:vbos) {
            glDeleteBuffers(vbo);
        }
    }
}
/*************
    RENDER
*************/
package Render;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class Render {

    public static void prepare(){
        glClearColor(0.0f,0.4f,0.6f,1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
    }

    public static void render(RawModel model){
        glEnableClientState(GL_VERTEX_ARRAY);
        glBindVertexArray(model.getVaoID());
        glEnableVertexAttribArray(0);
        glDrawArrays(GL_TRIANGLES,0,model.getVertexCount());
        glDisableVertexAttribArray(0);
        glBindVertexArray(0);
        glDisableClientState(GL_VERTEX_ARRAY);
    }
}

      

The problem I'm running into is that whenever I try to run a project, it just fails with this error message:

Connected to the target VM, address: '127.0.0.1:49390', transport: 'socket'

 A fatal error has been detected by the Java Runtime Environment:

  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffffa09862d, pid=12556, tid=0x00000000000020ac

 JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
 Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode windows-amd64 compressed oops)
 Problematic frame:
 C  [lwjgl_opengl.dll+0x862d]

 Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

 An error report file with more information is saved as:
 C:\Users\TokenGuard\IdeaProjects\ModernOpenGLTutorial\hs_err_pid12556.log

 If you would like to submit a bug report, please visit:
   http://bugreport.java.com/bugreport/crash.jsp
 The crash happened outside the Java Virtual Machine in native code.
 See problematic frame for where to report the bug.

Disconnected from the target VM, address: '127.0.0.1:49390', transport: 'socket'

Process finished with exit code 1

      

Looking around I found this stackoverflow post: Java OpenGL EXCEPTION_ACCESS_VIOLATION on glDrawArrays on NVIDIA only , which seems to have the same problem and the OP of this question actually posted a solution.

I am using an AMD graphics card, but I still tried to find out if it fixed my problem, however it does nothing as it still appears with the same error message.

Putting some breakpoints on, I found that the problem is with the method createVAO()

, more specifically the call by mistake glGenVertexArrays()

for some reason is failing. I tried to explicitly tell glfw to use OpenGL 3.0, but it still doesn't help.

At this point, I completely rule out ideas. Any guidance on what should I do?

+3


source to share


1 answer


The problem is this:

RawModel model = modelLoader.loadToVAO(vertices);
initApp();

      

You need to flip it:



initApp();
RawModel model = modelLoader.loadToVAO(vertices)

      

The problem is that when called, modelLoader.loadToVAO(vertices)

it calls glGenVertexArrays()

(as you noticed). At the moment, however, you do not have a current context set. What are you doing initApp()

with glfwMakeContextCurrent()

.

The current context must be set before calling any OpenGL functions.

+3


source







All Articles