SpartanJ / SOIL2

SOIL2 is a tiny C library used primarily for uploading textures into OpenGL.
MIT No Attribution
370 stars 75 forks source link

Error loading DDS into OGL texture from memory #26

Closed SpartanJ closed 5 years ago

SpartanJ commented 5 years ago

Original report by Anonymous.


Hi!

I'm having problems loading a DDS file from memory. Loading it via "SOIL_load_OGL_texture" works perfectly while "SOIL_load_OGL_texture_from_memory" only partially loads the image with a corrupted palette.

Here's the code I'm using, maybe I'm missing something obvious.

#!c++
std::ifstream stream("test.dds", std::ios::ate);
size_t size = stream.tellg();
stream.seekg(0, std::ios::beg);

char* buffer = new char[size];
stream.read(buffer, size);

unsigned int id = SOIL_load_OGL_texture_from_memory
(
 reinterpret_cast<unsigned char*>(buffer), size,
 0, 0, SOIL_LOAD_AUTO
);

I've attached the DDS file I'm testing on and comparisons between outside and inside of the application.

Cheers!

SpartanJ commented 5 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


Erased my comment, your code is wrong:
size_t size = stream.tellg();
to get the size seek to the end of the file before tellg:
stream.seekg(0, std::ios::end);

SpartanJ commented 5 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


Not a bug.

SpartanJ commented 5 years ago

Original comment by Edvin Hansson (Bitbucket: [Edvin Hansson](https://bitbucket.org/Edvin Hansson), ).


Hi again!

I don’t feel like that’s the issue. The parameter I’m using for “std::ifstream” should start the cursor at the end of the file already and the size matches with the file when I check via debugging.

SpartanJ commented 5 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


Oh, sorry, you’re right about that. But I still don’t think this is a bug from the library. I took your code and made a minimal example and works. You can use the code to find your problem: https://puu.sh/DFXrM/3fba7c7b28.cpp

SpartanJ commented 5 years ago

Original comment by Edvin Hansson (Bitbucket: [Edvin Hansson](https://bitbucket.org/Edvin Hansson), ).


I’ve realized what the problem was…

I didn’t use std::ios::binary when loading the file…

std::ifstream stream("test.dds", std::ios::ate);
std::ifstream stream("test.dds", std::ios::binary | std::ios::ate);

I knew it would be something like this. I should have gone through each line thoroughly before creating an issue, sorry about that.

Thanks for trying to help me out though! Hopefully someone else with the same “problem” runs into this thread in the future.

Cheers!

SpartanJ commented 5 years ago

Original comment by Martín Lucas Golini (Bitbucket: SpartanJ, GitHub: SpartanJ).


Thanks you for taking the time to investigate this issue further! Now that I see it… it was pretty obvious haha. Well, I’m glad you solve it! Cheers!