Købmanden Købmanden - 1 month ago 19
C++ Question

Loading texture using SOILs OGL function and OpenGL

I have a function to load a texture from a JPEG image using SOIL.

So far I have been loading the texture with the

SOIL_load_image()
function and then supplying the image to OpenGL using
glTexImage2D
(see code below). However! My textures is upside down, so I wanted to use the
SOIL_load_OGL_texture()
instead and supply the
SOIL_FLAG_INVERT_Y
in order to flip the images. My problem is though, that I get an unhandled exception at the
SOIL_load_OGL_texture()
function.

The code is almost a copy paste from the documentation, so I don’t understand why this error occurs?

(NOTE: I could invert the textures in my vertex shader, but I would like to use SOIL.)

The old way

int width;
int height;

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);

image = SOIL_load_image(filename, &width, &height, 0, SOIL_LOAD_RGB);

if (image == NULL) {
std::cout << "An error occurred while loading image." << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Loaded first texture image" << std::endl;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);


What I am trying now

GLuint image;

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);

image = SOIL_load_OGL_texture(
filename,
SOIL_LOAD_RGB,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);

if (image == 0)
cerr << "SOIL loading error: '" << SOIL_last_result() << "' (" << "res_texture.png" << ")" << endl;


And the error

Unhandled exception at 0x0F5427FF (msvcr110d.dll) in AnotherTutorial.exe: 0xC0000005: Access violation reading location 0x00000000.

Answer

Seems like there is no answer to using SOIL, so i'll post my solution:

In the vertex shader I do:

Texcoord = vec2(texcoord.x, 1.0-texcoord.y);
gl_Position = proj * view * model * vec4( position, 1.0 );

The 1.0-texcoord.y inverts the y-axis of the image. Not as clean a solution, but it works.

Comments