I have a very simple program that displays a dummy red texture in a square.
Here is the definition of texture in C ++:
struct DummyRGB8Texture2d
{
uint8_t data[3*4];
int width;
int height;
};
DummyRGB8Texture2d myTexture
{
{
255,0,0,
255,0,0,
255,0,0,
255,0,0
},
2u,
2u
};
This is how I adjust the texture:
void SetupTexture()
{
GL_CHECK(glCreateTextures(GL_TEXTURE_2D, 1, &m_texture));
GL_CHECK(glTextureStorage2D(m_texture, 1, GL_RGB8, myTexture.width, myTexture.height));
GL_CHECK(glTextureParameteri(m_texture, GL_TEXTURE_WRAP_S, GL_REPEAT));
GL_CHECK(glTextureParameteri(m_texture, GL_TEXTURE_WRAP_T, GL_REPEAT));
GL_CHECK(glTextureParameteri(m_texture, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GL_CHECK(glTextureParameteri(m_texture, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GL_CHECK(glProgramUniform1i(m_program->Id(), 3, 0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_texture));
GL_CHECK(glGenerateMipmap(GL_TEXTURE_2D));
}
This is how I draw the texture squared:
void Draw()
{
m_target->ClearTargetBuffers();
m_program->MakeCurrent();
GL_CHECK(glTextureSubImage2D(m_texture, 0, 0, 0, myTexture.width, myTexture.height,
GL_RGB, GL_UNSIGNED_BYTE, myTexture.data));
GL_CHECK(glBindVertexArray(m_vao));
GL_CHECK(glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(VideoQuadElementArray.size()), GL_UNSIGNED_INT, 0));
m_target->SwapTargetBuffers();
}
Result:

I can’t understand why this texture will not be red. Also, if I change the internal texture format to RGB A / RGB A 8 and an array of texture data to have a different element on each line, I get a beautiful red texture.
In case this is necessary, here are my vertex attributes and my (very simple) shaders:
struct VideoQuadVertex
{
glm::vec3 vertex;
glm::vec2 uv;
};
std::array<VideoQuadVertex, 4> VideoQuadInterleavedArray
{
VideoQuadVertex{ glm::vec3{ -0.25f, -0.25f, 0.5f }, glm::vec2{ 0.0f, 0.0f } },
VideoQuadVertex{ glm::vec3{ 0.25f, -0.25f, 0.5f }, glm::vec2{ 1.0f, 0.0f } },
VideoQuadVertex{ glm::vec3{ 0.25f, 0.25f, 0.5f }, glm::vec2{ 1.0f, 1.0f } },
VideoQuadVertex{ glm::vec3{ -0.25f, 0.25f, 0.5f }, glm::vec2{ 0.0f, 1.0f } }
};
vertex setting:
void SetupVertexData()
{
GL_CHECK(glCreateVertexArrays(1, &m_vao));
GL_CHECK(glCreateBuffers(static_cast<GLsizei>(m_vbo.size()), m_vbo.data()));
GL_CHECK(glNamedBufferData(m_vbo[EVbo::Data], VideoQuadInterleavedArray.size() * sizeof(VideoQuadVertex), VideoQuadInterle
GL_CHECK(glVertexArrayAttribBinding(m_vao, 0, 0));
GL_CHECK(glVertexArrayVertexBuffer(m_vao, 0, m_vbo[EVbo::Data], 0, sizeof(VideoQuadVertex)));
GL_CHECK(glNamedBufferData(m_vbo[EVbo::Element], VideoQuadElementArray.size() * sizeof(GLuint), VideoQuadElementArray.data
GL_CHECK(glVertexArrayElementBuffer(m_vao, m_vbo[EVbo::Element]));
GL_CHECK(glEnableVertexArrayAttrib(m_vao, 0 ));
GL_CHECK(glVertexArrayAttribFormat(
m_vao,
0,
3,
GL_FLOAT,
GL_FALSE,
offsetof(VideoQuadVertex, vertex)
));
GL_CHECK(glEnableVertexArrayAttrib(m_vao, 1 ));
GL_CHECK(glVertexAttribFormat(
1,
2,
GL_FLOAT,
GL_FALSE,
offsetof(VideoQuadVertex, uv)
));
GL_CHECK(glVertexArrayAttribBinding(m_vao, 1, 0));
}
vertex shader:
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texture_coordinate;
out FragmentData
{
vec2 uv;
} toFragment;
void main(void)
{
toFragment.uv = texture_coordinate;
gl_Position = vec4 (position, 1.0f);
}
shader fragment:
in FragmentData
{
vec2 uv;
} data;
out vec4 color;
layout (location = 3) uniform sampler2D tex_object;
void main()
{
color = texture(tex_object, data.uv);
}