Yanthir
Yanthir

Reputation: 31

Crash on calling glGenTextures

I've been trying to create a program that would turn on a window and be able to display images (I want to make a game). As I've worked with OGL before in C++, this time I decided to learn some Java and got myself LWJGL. I set up the window with help of the official tutorial and everything was fine. But as soon as I tried loading textures I came up across crashes. The program keeps crashing on calling glGenTextures. I've seen many similar questions and most answers were that the thread was lacking a context, but I set that up as well. Here is the code I use to load the image:

public Image(String path) {
    this();
    try {
        InputStream in = new FileInputStream(path);
        try {
            PNGDecoder decoder = new PNGDecoder(in);
            ByteBuffer buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight());

            decoder.decode(buf, decoder.getWidth()*4, Format.RGBA);
            buf.flip();

            ByteBuffer TextureNameGen = ByteBuffer.allocate(8);

            glGenTextures(1, TextureNameGen); //<-- This is where the program crashes

            TextureID = TextureNameGen.getInt();

            ByteBuffer boundTexture = ByteBuffer.allocate(8);
            glGetIntegerv(GL_TEXTURE_BINDING_2D, boundTexture);

            glBindTexture(GL_TEXTURE_2D, TextureID);

            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);

            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

            glBindTexture(GL_TEXTURE_2D, boundTexture.getInt());

            Width   = decoder.getWidth();
            Height  = decoder.getHeight();

        } finally {
           in.close();
        }

        list.add(this);
    } catch (Exception e) {
        //Do nothing
    }
}

And here is how I set up the window and the context:

private void init() {
    //Setup an error callback
    glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));

    //Initialize GLFW
    if ( glfwInit() != GLFW_TRUE )
        throw new IllegalStateException("Unable to initialize GLFW");

    //Create the window
    window = glfwCreateWindow(WIDTH, HEIGHT, "RopeProject", NULL, NULL);
    if ( window == NULL )
        throw new RuntimeException("Failed to create the GLFW window");

    //Setup a key callback
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            Controls.instance.pushCommand(key, action);
        }
    }
    );

    //Get the resolution of the primary monitor
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    //Center our window
    glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2);

    //Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable v-sync
    glfwSwapInterval(1);

    //Essential
    GL.createCapabilities();

    //GL Attributes
    glViewport(0, 0, WIDTH, HEIGHT);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();

    new Image("./gfx/Test.png"); //<-- Here is where I try to load up the texture.

    PhysicsThread.instance.start();
}

Am I missing something obvious? Am I doing something wrong? I've been at it for the past 6 hours and I can't seem to find the answer. I can provide a crash log if it were to help. They are all saying this: EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff9e87ebf60, pid=15984, tid=17180

Upvotes: 0

Views: 1121

Answers (1)

Yanthir
Yanthir

Reputation: 31

Of course as soon as I post a question I find the answer. As it turns out the ByteBuffers need to be direct, so I had to use the ByteBuffer.allocateDirect(n) method. As soon as I changed all of my ByteBuffers to direct everything ran smoothly.

Upvotes: 3

Related Questions