c++ - Texture not appearing via stb_image -
i have been working on new project in c++ using glfw opengl wrapper, , using stb_image.h tool load images.
i load images using following snippet:
this->width = width; this->height = height; int bpp; unsigned char* image = stbi_load(("./res/textures/"+filename).c_str(), &width, &height, &bpp, stbi_rgb_alpha); if (image == nullptr) std::cerr << "unable load texture: " + filename << std::endl; glgentextures(1, &id); glbindtexture(gl_texture_2d, id); 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(bpp == 3) glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgb, gl_unsigned_byte, image); else if (bpp == 4) glteximage2d(gl_texture_2d, 0, gl_rgb, width, height, 0, gl_rgba, gl_unsigned_byte, image); stbi_image_free(image);
and render texture via:
glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glenable(gl_texture_2d); gltexenvf(gl_texture_env, gl_texture_env_mode, gl_modulate); glbindtexture(gl_texture_2d, texture->id); glbegin(gl_quads); gltexcoord2i(0, 0); glvertex3i(0, 0, -5); gltexcoord2i(0, 1); glvertex3i(0, 1, -5); gltexcoord2i(1, 1); glvertex3i(1, 1, -5); gltexcoord2i(1, 0); glvertex3i(1, 0, -5); glend();
i swapping buffers, polling events, , have set various color bits glfwwindowhint()
to little avail. when run, receive following result:
you have call glactivetexture(gl_texture0)
before glbindtexture(gl_texture_2d, texture->id)
. so, last code should like:
glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glenable(gl_texture_2d); gltexenvf(gl_texture_env, gl_texture_env_mode, gl_modulate); glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, texture->id); glbegin(gl_quads); gltexcoord2f(0, 0); glvertex3f(0, 0, -5); gltexcoord2f(0, 1); glvertex3f(0, 1, -5); gltexcoord2f(1, 1); glvertex3f(1, 1, -5); gltexcoord2f(1, 0); glvertex3f(1, 0, -5); glend();
also, glactivetexture(...)
, not have glfw. part of opengl.
Comments
Post a Comment