Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code successfully renders an image using an absolute path;
C++
unsigned int renderer_id;
std::string filepath;
unsigned char* local_buffer;
int width;
int height;
int bpp;

std::string filepath("filepath\\here.png");
stbi_set_flip_vertically_on_load(1);
local_buffer = stbi_load(filepath.c_str(), &width, &height, &bpp, 4);

glGenTextures(1, &renderer_id);
glBindTexture(GL_TEXTURE_2D, renderer_id);

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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, local_buffer);
glBindTexture(GL_TEXTURE_2D, 0);

if (local_buffer) {
    stbi_image_free(local_buffer);
}

However if I cut the stbi code out and try to load the image from resources, it renders completely black;
C++
HMODULE hMod = GetModuleHandleW(NULL);
const std::wstring str = utils::Conversions::string_to_wstring("PNG");
HRSRC hRes = FindResourceW(hMod, MAKEINTRESOURCEW(TEST_IMAGE), str.c_str());
std::cout << "1: " << hRes << std::endl; // outputs 0x7ff795773270
HGLOBAL hGlobal = LoadResource(hMod, hRes);
std::cout << "2: " << hGlobal << std::endl; // outputs 0x7ff795773378
local_buffer = LockResource(hGlobal);
DWORD pwPngDataBytes = SizeofResource(hMod, hRes);
std::cout << "3: " << pwPngDataBytes << std::endl; // outputs 161

TEST_IMAGE is #defined as 102

resources.rc contains:
TEST_IMAGE PNG "filepath\\here.png"

Here is the PNG metadata, in case any of it is relevant:
img[^]

As there are no errors and those debug values mean nothing, I'm not sure how to debug this.

What I have tried:

All details already in the question
Posted
Updated 11-Sep-22 19:07pm
v3

1 solution

You are missing some steps, at least in the code you have posted. Take a look at the call that maps the image into OpenGL :
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, local_buffer);
You need to make sure that you provide a width, a height, and a pointer to the pixel data which is local_buffer. It appears that you have local_buffer set but is it set to the pixel data or to the start of the image data which includes the header? If it is not set to the pixel data then you need to adjust the pointer so that it is because that is what glTexImage2D expects.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900