Load Texture From File problem Android NDK

Started by
1 comment, last by _WeirdCat_ 3 years, 11 months ago

I am trying to load an image through OpenGL and stbi_load on android ndk. The problem is that it is generating an invalid texture (equal to zero).

LoadTextureFromFile("/storage/emulated/0/Download/BPV/mic_close.jpg", &my_image_texture, &my_image_width, &my_image_height);
    
bool CGUI::LoadTextureFromFile(const char* filename, GLuint* out_texture, int* out_width, int* out_height)
{
	// Load from file
	int image_width = 0;
	int image_height = 0;
	unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
	if (image_data == NULL)
		return false;
	
	// Create a OpenGL texture identifier
	GLuint image_texture;
	glGenTextures(1, &image_texture);
	glBindTexture(GL_TEXTURE_2D, image_texture);

	// Setup filtering parameters for display
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	// Upload pixels into texture
	glPixelStorei(0x0CF2, 0);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
	stbi_image_free(image_data);

	*out_texture = image_texture; // Receive zero
	*out_width = image_width;
	*out_height = image_height;
	return true;
}

I use libGLESv2

Advertisement

Most common mistake is to call loadtexture code before opengl initialization,

However aside the fact of this weird line

glGenTextures(1, &imagetexture); where there should not be any &amps amps anywhere just image_texture

You try to return a local variable GLuint image_texture;

Which is being destroyed after function end so you need to create a new pointer

GLuint* image_texture = new GLuint();

glGenTextures(1, image_texture); glBindTexture(GL_TEXTURE_2D, (*image_texture));

-----

out_texture = image_texture

You've just made a c++ mistake

But now i see how you want to manage this:

Ao you have lets call a.static var where you want to put a tex

Try changing GLuint* out_texture

To GLuint*& out_texture

This topic is closed to new replies.

Advertisement