2015-11-02 143 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老棄用,因此,如果沒有一個很好的理由去使用它,一個應躲開它。