I have a problem with OpenGL and glGetTexImage()
.
I want to get the pixels of a texture that I've created before with code like this:
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width,
height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
someRandomPixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
I'm using SDL2.
After I loaded that texture I tried to get the pixels of this texture in a GLuint* pixels pointer which I declared as follows:
GLuint* pixels = new GLuint[256*256*4];
the size of this image was 256x256 pixels.
I got the pixel data by using
GLuint* Textur::getPixelsOfTextur(GLuint textur, int width, int height, int numChannels)
{
GLuint* pixels=new GLuint[width*height*numChannels];
glBindTexture(GL_TEXTURE_2D, textur);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_INT, pixels);
return pixels;
}
Where "numChannels" is 4 for RGBA and 3 for RGB
Now my problem is that I really have no clue how to address Pixel p(x,y) correctly since the size of my array is 4 times bigger than the pixel count and every time I use a smaller size visual studios throws an error:
Exception thrown at 0x5ED71FCE (opengl32.dll) in sdl_opengl_Frameworktest.exe: 0xC0000005: Access violation writing location 0x09C33000.
Also, is it possible to get those data in a 2D array?