Search code examples
javaopenglrenderlwjglvao

Java lwjgl Modern OpenGL Access Violation Exception using VAOs


I am currently following ThinMatrix's OpenGL tutorial on rendering with VAOs and VBOS. I copy the code almost exactly (the only difference being I make a factory class static instead of just having it normally). 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 is 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's 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 having is that whenever I try to run the project it just crashes 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

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

I am using an AMD graphics card, but I still gave it a try to see if it fixed my problem, however it does nothing, as it still comes up with the same error message.

Putting some breakpoints in I have found that the problem lies in the createVAO() method, more specifically the call to glGenVertexArrays() fails for some reason. I have tried explicitly telling glfw to use OpenGL 3.0 but it still doesn't help.

At this point I am completely out of ideas. Any guidance on what I should do?


Solution

  • The problem is this:

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

    You need to flip it around:

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

    The problem with that is that when you call modelLoader.loadToVAO(vertices) it calls glGenVertexArrays() (as you've observed). At this point you however don't have a current context set. Which you do in initApp() with glfwMakeContextCurrent().

    You must have a current context set before calling any OpenGL functions.