Unhandled exception in for loop

-1

I'm trying to create two textures for my OpenGL project. Initially i created the textures seperately and this worked perfectly fine. Not to repeat myself, I decided to attempt to make them as an array and use a for loop to perform the steps twice.

unsigned int textures[2];
int width, height, nrChannels;
unsigned char *data = stbi_load("C:\\Users\\A\\Desktop\\wall.jpg", &width, &height, &nrChannels, 0);
glGenTextures(2, textures);
for (int i = 0; i < 2; i++)
{
    glBindTexture(GL_TEXTURE_2D, (i == 0) ? textures[0] : textures[1]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    if (data)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, (i == 0) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
        if (i == 1)
        data = stbi_load("C:\\Users\\A\\Desktop\\awesomeface.png", &width, &height, &nrChannels, 0);
    }
    else
    {
        std::cout << "Failed to load texture!" << std::endl;
    }
}

On line 13 (glTexImage2D), I recive the following unhandled exception:

Unhandled exception at 0x03EE8893 (ig9icd32.dll) in LearnOpenGL.exe: 0xC0000005: Access violation reading location 0x00942000.

Basically I have 3 questions, what is causing this error? How can i fix it? And is this a bad way to go about doing this anyway?

EDIT:

This was my old code, which works perfectly fine:

unsigned int texture1, texture2;
// texture 1
// ---------
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char *data = stbi_load("C:\\Users\\A\\Desktop\\wall.jpg", &width, &height, &nrChannels, 0);
if (data)
{
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
    std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
// texture 2
// ---------
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
data = stbi_load("C:\\Users\\A\\Desktop\\awesomeface.png", &width, &height, &nrChannels, 0);
if (data)
{
    // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
    std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
c++
opengl
textures
asked on Stack Overflow Sep 22, 2018 by Behar • edited Oct 25, 2018 by Rabbid76

1 Answer

2

Please read the manuals of the libraries you're using.

Loading images with stb wrote:

The fifth parameter of stbi_load() specifies the number of 8-bit components per pixel to use. It can be set to one of following four options:

STBI_grey = 1

STBI_grey_alpha = 2

STBI_rgb = 3

STBI_rgb_alpha = 4

If you pass 0 as the fifth parameter stbi_load() will use your nrChannels which is uninitialized in your code and thuss causes undefined behaviour.

answered on Stack Overflow Sep 22, 2018 by Swordfish

User contributions licensed under CC BY-SA 3.0