【发布时间】:2021-12-26 18:22:47
【问题描述】:
在某些情况下,我以一维二进制数据和指定格式的形式从相机获取实时图像数据。我想将此格式转换为 RGBA 或 BGRA 并使用它来纹理屏幕对齐的四边形。但是,我似乎误解了一些关于如何在 OpenGL 中生成和加载纹理的核心概念,因为我无法让以下示例正常工作:
void OpenGLRenderer::renderScreenAlignedQuad(const XrCompositionLayerProjectionView& view)
{
CHECK_GL_ERROR(glBindVertexArray(m_screenAlignedQuad.vao));
CHECK_GL_ERROR(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_screenAlignedQuad.indexBuffer));
// Update texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, m_screenAlignedQuad.texture);
#define BUFF_HEIGHT 1152
#define BUFF_WIDTH 1152
unsigned char *buffer = new unsigned char[BUFF_HEIGHT * BUFF_WIDTH * 4];
for (int32_t y = 0; y < BUFF_HEIGHT; y++) {
for (int32_t x = 0; x < BUFF_WIDTH; x++) {
int32_t ind = y * BUFF_WIDTH + x * 4;
buffer[ind] = 255; // R
buffer[ind + 1] = 0; // G
buffer[ind + 2] = 0; // B
buffer[ind + 3] = 255; // A
}
}
{// =! Critical section !=
// The mutex will be unlocked when this object goes out of scope;
// Note that it blocks other threads from writing, but allows reading
std::shared_lock<std::shared_mutex> sl(m_videoStreamContext.g_currentFrameDataMutex);
CHECK_GL_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BUFF_HEIGHT, BUFF_WIDTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer));
}// =! Critical section !=
CHECK_GL_ERROR(glDrawElements(GL_TRIANGLES, m_screenAlignedQuad.indexCount, GL_UNSIGNED_SHORT, 0));
}
我想在这里实现的是将整个屏幕变成红色的纹理。相反,我得到了这个:
对于更多调试信息,我添加了更多颜色:
void OpenGLRenderer::renderScreenAlignedQuad(const XrCompositionLayerProjectionView& view)
{
CHECK_GL_ERROR(glBindVertexArray(m_screenAlignedQuad.vao));
CHECK_GL_ERROR(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_screenAlignedQuad.indexBuffer));
// Update texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, m_screenAlignedQuad.texture);
unsigned char *buffer = new unsigned char[BUFF_HEIGHT * BUFF_WIDTH * 4];
for (int32_t y = 0; y < BUFF_HEIGHT; y++) {
for (int32_t x = 0; x < BUFF_WIDTH; x=x+4) {
int32_t ind = y * BUFF_WIDTH + x;
if (y < BUFF_HEIGHT / 2) {
buffer[ind] = 255; // R
buffer[ind + 1] = 0; // G
buffer[ind + 2] = 0; // B
buffer[ind + 3] = 255; // A
} else if (x < BUFF_WIDTH / 2) {
buffer[ind] = 0; // R
buffer[ind + 1] = 0; // G
buffer[ind + 2] = 255; // B
buffer[ind + 3] = 255; // A
} else {
buffer[ind] = 0; // R
buffer[ind + 1] = 255; // G
buffer[ind + 2] = 0; // B
buffer[ind + 3] = 255; // A
}
}
}
{// =! Critical section !=
// The mutex will be unlocked when this object goes out of scope;
// Note that it blocks other threads from writing, but allows reading
std::shared_lock<std::shared_mutex> sl(m_videoStreamContext.g_currentFrameDataMutex);
CHECK_GL_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, BUFF_HEIGHT, BUFF_WIDTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer));
}// =! Critical section !=
CHECK_GL_ERROR(glDrawElements(GL_TRIANGLES, m_screenAlignedQuad.indexCount, GL_UNSIGNED_SHORT, 0));
delete buffer;
}
所以看起来纹理在两个方向上都被渲染得太小了。纹理上的环绕设置设置为钳位,因此不应像以前那样重复。我在这里做错了什么?
编辑:只要不影响程序的正确性,请忽略任何明显的低效率或丑陋的代码结构。我正在尝试让最简单的版本暂时可用。
【问题讨论】:
标签: opengl textures texture-mapping texture2d