2015-11-02 306 views
3

我正在绘制一个颜色条的OpenGL纹理。输入图像来自OpenCV,因此其格式为BGRA。输出格式是RGBA。OpenGL纹理奇怪的颜色

这是我画的:

// 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

出了什么问题?

编辑

也许这有事情做,只有绘制3D顶点与RGB颜色,与glVertex3fglEnableClientState(GL_COLOR_ARRAY)

回答

4

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

你可以改变模式由

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) 

Siden注仅使用纹理颜色:固定管线是reeeeeeealy老弃用,因此,如果没有一个很好的理由去使用它,一个应躲开它。