我正在为彩条绘制OpenGL纹理。输入图像来自OpenCV,因此其格式为BGRA。输出格式为RGBA。
这是我绘制它的方式:
// This drawer is for a texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_colorbarTextureID);
glBegin(GL_QUADS);
int width = m_colorbar.cols;
int height = m_colorbar.rows;
static const float inverse_scale = 2; // inverse scale value to resize the colormap image
float x = static_cast<float>(width) / (2 * inverse_scale);
float y = static_cast<float>(height) / (2 * inverse_scale);
static const float border = 10;
// Send the texture to minz in order to guarantee it will show up behind all the other elements
glTexCoord2f(0.0f, 0.0f); glVertex3f(m_lastCx - x - border, border, static_cast<GLfloat>(m_min2DDepth));
glTexCoord2f(0.0f, 1.0f); glVertex3f(m_lastCx - x - border, border + y, static_cast<GLfloat>(m_min2DDepth));
glTexCoord2f(1.0f, 1.0f); glVertex3f(m_lastCx - border, border + y, static_cast<GLfloat>(m_min2DDepth));
glTexCoord2f(1.0f, 0.0f); glVertex3f(m_lastCx - border, border, static_cast<GLfloat>(m_min2DDepth));
glEnd();
glDisable(GL_TEXTURE_2D);
这是我创建它的方式:
glEnable(GL_TEXTURE_2D);
// Resize image
cv::Mat colorbar_flip;
cv::resize(m_colorbar, colorbar_flip, cv::Size(), m_colorbarScale, m_colorbarScale);
// Flip y axis due to openGL convention (0 on bottom, positive pointing up)
cv::flip(colorbar_flip, colorbar_flip, 0);
glGenTextures(1, &m_colorbarTextureID);
if (m_colorbarTextureID == 0) {
LoggerPublic::debug("Error creating colorbar texture");
return;
}
glBindTexture(GL_TEXTURE_2D, m_colorbarTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//use fast 4-byte alignment (default anyway) if possible
//glPixelStorei(GL_UNPACK_ALIGNMENT, (colorbar_flip.step & 3) ? 1 : 4);
//set length of one complete row in data (doesn't need to equal image.cols)
//glPixelStorei(GL_UNPACK_ROW_LENGTH, static_cast<GLint>(colorbar_flip.step) / static_cast<GLint>(colorbar_flip.elemSize()));
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGBA, // Internal colour format to convert to
colorbar_flip.cols, // Image width
colorbar_flip.rows, // Image height
0, // Border width in pixels (can either be 1 or 0)
GL_BGRA, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
colorbar_flip.ptr()); // The actual image data itself
glDisable(GL_TEXTURE_2D);
wglMakeCurrent(nullptr, nullptr);
然而,我得到了这个奇怪的输出,带有蓝色调,应该是白色的:
可能出现什么问题?
修改
这可能与仅使用RGB颜色绘制三维顶点有关,glVertex3f
和glEnableClientState(GL_COLOR_ARRAY)
答案 0 :(得分:4)
这里的问题似乎是纹理的颜色是用顶点颜色调制的。默认情况下,glTexEnv
设置为GL_MODULATE
,这基本上意味着来自纹理的颜色与顶点颜色相乘。
您可以将模式更改为仅使用纹理颜色
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
Siden注意:固定功能管道是旧的并且已弃用,所以如果没有充分的理由使用它,就应该避免使用它。