Monthly Archives: March 2013

TexImage2D Access Violation Exception

Annoyed and confused?

Is OpenTK throwing a System.AccessViolationException in GL.TexImage2D?

  1. Check that the input has the correct size = width * height * bpp.
    BPP is bytes per pixel and depends on PixelFormat and PixelType. For PixelFormat.Bgra PixelType.UnsignedByte, there are 4 channels of 1 byte input, so bpp = 4. This BPP doesn’t have any relation to PixelInternalFormat, that’s what OpenGL converts your input to and stores it as.
  2. Make sure you tell OpenGL that you’ve packed the pixels as tight as they go (no skipping some at the end of each row)

...
GL.ActiveTexture(unit);
GL.BindTexture(target, texture_location);
...
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
GL.PixelStore(PixelStoreParameter.UnpackRowLength, width);
...
GL.TexImage2D(...);
...