gpt4 book ai didi

c++ - OpenGL纹理奇怪的颜色

转载 作者:太空狗 更新时间:2023-10-29 20:56:31 24 4
gpt4 key购买 nike

我正在为颜色条绘制 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);

但是,我得到了这个奇怪的输出,在它应该是白色的地方带有蓝色调:

colorbar

有什么问题吗?

编辑

也许这与仅使用 RGB 颜色绘制 3d 顶点有关,使用 glVertex3fglEnableClientState(GL_COLOR_ARRAY)

最佳答案

这里的问题似乎是纹理的颜色是用顶点颜色调制的。默认情况下,glTexEnv 设置为 GL_MODULATE,这基本上意味着来自纹理的颜色与顶点颜色相乘。

您可以更改模式以仅使用纹理颜色

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)

旁注:固定功能管道非常陈旧且已弃用,因此如果没有充分的理由使用它,应该避免使用它。

关于c++ - OpenGL纹理奇怪的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com