# EXCEPTION\_ACCESS\_VIOLATION when trying to render

**URL:** <https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865>\
**Category:** support\
**Created:** [March 1, 2017, 12:35am UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865 "2017-03-01T00:35:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [March 1, 2017, 12:35am UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/1 "2017-03-01T00:35:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![elmindreda](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/elmindreda/32/26_2.png) [@elmindreda](https://discourse.glfw.org/u/elmindreda)\
**Post date:** [March 1, 2017, 2:22am UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/2 "2017-03-01T02:22:50Z")

</div>

```java
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.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 1, 2017, 8:40am UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/4 "2017-03-01T08:40:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [March 1, 2017, 2:12pm UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/5 "2017-03-01T14:12:29Z")

</div>

> 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!

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [March 1, 2017, 4:18pm UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/6 "2017-03-01T16:18:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [March 1, 2017, 6:57pm UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/7 "2017-03-01T18:57:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [March 1, 2017, 7:19pm UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/8 "2017-03-01T19:19:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ArtemisHD](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/artemishd/32/92_2.png) [@ArtemisHD](https://discourse.glfw.org/u/ArtemisHD)\
**Post date:** [March 1, 2017, 7:53pm UTC](https://discourse.glfw.org/t/exception-access-violation-when-trying-to-render/865/9 "2017-03-01T19:53:21Z")

</div>

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.
