I'm new to bgfx and have been trying to render a textured cube to the screen for quite a while how. But instead of having the texture I specified it's covered in red and black lines.
Here is my fragment shader code, I don't see anything wrong with it:
$input v_texcoord0
#include "../common.sh"
SAMPLER2D(s_tex, 0);
void main()
{
gl_FragColor = vec4(texture2D(s_tex, v_texcoord0).rgb, 1.0);
}
And this is how I'm setting the texture:
inline void SetTexture(const std::string &name, uint8_t stage, const Texture& texture)
{
bgfx::setTexture(stage, m_uniforms.at(name).handle, texture.m_handle);
}
And this is how I'm creating the texture:
Texture::Texture(const std::string& path)
: valid(true)
{
static const std::string basePath = "res/textures/";
std::string fullPath = basePath + path;
uint8_t *data = stbi_load("./res/textures/atlas.png", &size.x, &size.y, &channels, STBI_rgb_alpha);
channels = 4;
if (stbi_failure_reason()) std::cout << stbi_failure_reason();
auto res =
bgfx::createTexture2D(
size.x, size.y,
false, 1,
bgfx::TextureFormat::RGBA8,
BGFX_SAMPLER_U_CLAMP
| BGFX_SAMPLER_V_CLAMP
| BGFX_SAMPLER_MIN_POINT
| BGFX_SAMPLER_MAG_POINT,
bgfx::copy(data, size.x * size.y * channels));
if (!bgfx::isValid(res)) {
std::cout << ("Error loading texture " + path) << std::endl;
valid = false;
}
stbi_image_free(data);
std::cout << "created texture\n";
}
The strange thing is that when I pass an empty shader handle to bgfx::setTexture
by passing in bgfx::TextureHandle()
as the third argument, I get the same result. So what I'm assuming is that the texture isn't getting passed to the shader properly. But I have no idea what could be causing this.