EXCEPTION_ACCESS_VIOLATION when trying to render

I don’t really know if this is the right place to post it but maybe you know something. I’m getting the above error when trying to render a simple texture. This work is licensed under MIT.

I’m splitting a sprite_sheet like this:

private static final resource_parser_01 tex_resources_01 = resource_parser_01.loads_images("res\\sprite01.png");

public static int parse_texture(int row, int col) {

    ByteBuffer sprite_sheet = tex_resources_01.get_image();
    int texture_ID = glGenTextures();
    int width = tex_resources_01.get_width();
    int heigh = tex_resources_01.get_heigh();

    int xoffset = (col - 1) * 17, yoffset = (row - 1) * 17;

    glBindTexture(GL_TEXTURE_2D, texture_ID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

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

    glTexStorage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, heigh);
    glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, sprite_sheet);

    return texture_ID;
}

And trying to render the texture like this:

private void render() {
    glBindTexture(GL_TEXTURE_2D, resource_parser_02.parse_texture(1, 1));
    glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glEnd();
    glfwSwapBuffers(window);
}

Maybe it’s not the best way to code it as I’m still learning. Any help would be appreciated!

glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glEnd();

I don’t know if this was intended to be shorthand, but you need to specify four vertices to draw a quad.

Your render function is calling parse_texture every frame, generating a new texture each frame which is identical.

This seems like it’s probably not intended.

Rather than call:

glBindTexture(GL_TEXTURE_2D, resource_parser_02.parse_texture(1, 1));

I would expect that you to call resource_parser_02.parse_texture(1, 1) once in your initialization phase and then in the render bind with the same id you used in glGenTextures() and the bind call in your parse_texture() function.:

glBindTexture(GL_TEXTURE_2D, texture_ID);

Which means you need to store texture_ID in a static variable or something as a class member so render() can access it.

I don’t know if this was intended to be shorthand, but you need to specify four vertices to draw a quad.

I see, my bad there, I thought I only needed the top-left vertex and that it gets the width/height to calculate the rest. Will code and post it when it’s ready. Thanks!

Solved, just add GL.createCapabilities(); in init();. Also improved the code as you recommended. Thanks again!

This work is licensed under MIT. I now have it initialized with:

GL.createCapabilities();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glOrtho(0, wind_width, wind_height, 0, 1, -1);

texture_ID = resource_parser_02.parse_texture(1, 1);

And trying to render it with:

private void render() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glBindTexture(GL_TEXTURE_2D, texture_ID);
    glTexCoord2f(0, 1); glVertex2f(16, 33);
    glTexCoord2f(1, 1); glVertex2f(33, 33);
    glTexCoord2f(1, 0); glVertex2f(33, 16);
    glTexCoord2f(0, 0); glVertex2f(16, 16);
    glEnd();

    glfwSwapBuffers(window);
}

I get a nice quad where I want it to be, but the texture won’t show up.

Without seeing the full code it’s always difficult to help, but the following may be of use:

  1. Ensure you have set: glEnable(GL_TEXTURE_2D);
  2. Texture coordinates are in the 0.0f to 1.0f, so you should change the 16 to 1.0f for glTexCoord2f unless you want 16 repeated textures.

Good luck!

I did those, nothing changed, is there anything wrong with the way I’m cutting a sprite_sheet into respective textures? I posted all of the relevant code here already, there isn’t much left honestly. I tried using the glEnable(GL_TEXTURE_2D); in both init(); and render();. Nothing happened. I might be missing something or doing the calculations wrong, still I should have ended up with a cut-off texture instead.