2017-10-06 125 views
-2

這是我質感一流的樣子:爲什麼glBindTexture()拋出GL_INVALID_OPERATION

Texture* Texture::load(std::string filename) { 
    static std::unordered_map<std::string, Texture*> loadedTextures; 

    //If we already have the texture loaded, don't load it again 
    if(loadedTextures.find(filename) != loadedTextures.end()) { 
     return loadedTextures[filename]; 
    } 
    Texture* newTex = new Texture(); 

    //Load our texture with ImageMagick 
    try { 
     newTex->m_Image.read(filename); 
     newTex->m_Image.write(&newTex->m_Blob, "RGBA"); 
    } catch(Magick::Error& err) { 
     std::cout << "Could not load texture \"" << filename <<"\": " << err.what() << std::endl; 
     return nullptr; 
    } 

    //set up the texture with OpenGL 
    glGenTextures(1, &newTex->m_textureObj); 
    glBindTexture(GL_TEXTURE_2D, newTex->m_textureObj); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, newTex->m_Image.columns(), newTex->m_Image.rows(), 0, GL_RGBA, GL_UNSIGNED_BYTE, newTex->m_Blob.data()); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glBindTexture(GL_TEXTURE_2D, 0); 

    //Now add it onto our list 
    loadedTextures[filename] = newTex; 

    return newTex; 
} 

void Texture::bind() { 
    glActiveTexture(GL_TEXTURE0); 

    glBindTexture(GL_TEXTURE_2D, m_textureObj); //ERROR HERE 

} 

什麼我的渲染功能是這樣的:

glEnableVertexAttribArray(0); 
glEnableVertexAttribArray(1); 
glEnableVertexAttribArray(2); 

//Now send vertices, uvs, and normals 
glBindBuffer(GL_ARRAY_BUFFER, VB); 
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); 
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, uv)); 
glVertexAttribPointer(2, 3, GL_FLOAT, GL_TRUE, sizeof(Vertex), (void *) offsetof(Vertex, normal)); 

//Send all our face information 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB); 

//If we have a texture, use it 
if(ctx.texture != nullptr) { 
    ctx.texture->bind(); 
} 

//Now draw everything 
glDrawElements(GL_TRIANGLES, ctx.model->_indices.size(), GL_UNSIGNED_INT, 0); 

glDisableVertexAttribArray(0); 
glDisableVertexAttribArray(1); 
glDisableVertexAttribArray(2); 

當我打電話提升口感>綁定() ,openGL給了我這個錯誤:

Error initializing OpenGL! 1282, GL_INVALID_OPERATION: The specified operation is not allowed in the current state. 

而我的片段着色器剛從紋理採樣器變黑。

我試圖按照this tutorial,但我無法弄清楚我要去哪裏錯了。任何人都可以告訴我爲什麼我得到這個錯誤?

+2

你確定在你有一個有效的GL上下文綁定時調用它嗎?另外,是什麼讓你確信這個特定電話的錯誤來自? – derhass

+0

這是什麼'm_textureObj'在'bind()'調用的值? – Ripi2

+0

我用glGetError()檢查了它,所以我知道錯誤來自該調用。另外,如果我只是註釋掉'ctx.texture-> bind()'的調用,那麼一切正常(無紋理)。 –

回答

0

我在創建OpenGL上下文之前調用glGenTextures(),所以它沒有給我一個有效的紋理對象。之後拋出錯誤,當我試圖在OpenGL運行後綁定無效的紋理對象時。

相關問題