OpenGL纹理触发错误1281和奇怪的背景行为

时间:2014-08-22 12:42:00

标签: c++ opengl textures shader

我正在使用SOIL将纹理应用于VBO,没有纹理我可以更改背景并轻松显示黑色(默认颜色)vbos,但现在使用纹理,openGL给出错误1281,背景是黑色,一些纹理是没有申请。必须有一些我不了解的关于应用/加载纹理的东西。 BUt纹理IS应用(虽然没有其他工作),当我尝试使用着色器程序时应用错误但是我检查了这些编译并没有写出任何问题。

这是我用来加载纹理的代码,一旦加载它保存在内存中,它主要来自SOIL的例子:

    texture = SOIL_load_OGL_single_cubemap(
    filename,
    SOIL_DDS_CUBEMAP_FACE_ORDER,
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_POWER_OF_TWO
    | SOIL_FLAG_MIPMAPS
    | SOIL_FLAG_DDS_LOAD_DIRECT
    );
if( texture > 0 )
{
    glEnable( GL_TEXTURE_CUBE_MAP );
    glEnable( GL_TEXTURE_GEN_S );
    glEnable( GL_TEXTURE_GEN_T );
    glEnable( GL_TEXTURE_GEN_R );
    glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
    glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
    glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
    glBindTexture( GL_TEXTURE_CUBE_MAP, texture );
    std::cout << "the loaded single cube map ID was " << texture << std::endl;
} else {
    std::cout << "Attempting to load as a HDR texture" << std::endl;
    texture = SOIL_load_OGL_HDR_texture(
        filename,
        SOIL_HDR_RGBdivA2,
        0,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_POWER_OF_TWO
        | SOIL_FLAG_MIPMAPS
        );
    if( texture < 1 )
    {
        std::cout << "Attempting to load as a simple 2D texture" << std::endl;
        texture = SOIL_load_OGL_texture(
            filename,
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_POWER_OF_TWO
            | SOIL_FLAG_MIPMAPS
            | SOIL_FLAG_DDS_LOAD_DIRECT
            );
    }
    if( texture > 0 ) {
        //  enable texturing
        glEnable( GL_TEXTURE_2D );
        //  bind an OpenGL texture ID
        glBindTexture( GL_TEXTURE_2D, texture );
        std::cout << "the loaded texture ID was " << texture << std::endl;
    } else {
        glDisable( GL_TEXTURE_2D );
        std::cout << "Texture loading failed: '" << SOIL_last_result() << "'" << std::endl;
    }
}

以及我在绘图时如何应用它:

        GLuint TextureID  = glGetUniformLocation(shaderProgram, "myTextureSampler");
    if(!TextureID)
        cout << "TextureID not found ..." << endl;
    // glEnableVertexAttribArray(TextureID);
    glActiveTexture(GL_TEXTURE0);
    if(SFML)
        sf::Texture::bind(sfml_texture);
    else {
        glBindTexture (GL_TEXTURE_2D, texture);
        // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 768, 0, GL_RGB, GL_UNSIGNED_BYTE, &texture);
    }

    glUniform1i(TextureID, 0);

我不确定SOIL是否适合我的程序,因为我想要尽可能简单的东西(我使用sfml的纹理对象,这是最好的,但我不能再),但如果我能让它工作,它会太棒了。

编辑:

在缩小错误隐含的代码之后,这里是引发它的代码,它在纹理加载和bos绘制之间调用:

    glEnableClientState(GL_VERTEX_ARRAY);
//this gives the error :
glUseProgram(this->shaderProgram);

if (!shaderLoaded)
{
    std::cout << "Loading default shaders" << std::endl;
    if(textured)
        loadShaderProgramm(texture_vertexSource, texture_fragmentSource);
    else
        loadShaderProgramm(default_vertexSource,default_fragmentSource);
}

glm::mat4 Projection = camera->getPerspective();

glm::mat4 View = camera->getView();
glm::mat4 Model = glm::mat4(1.0f);
Model[0][0] *= scale_x;
Model[1][1] *= scale_y;
Model[2][2] *= scale_z;

glm::vec3 translate_vec(this->x,this->y,this->z);
glm::mat4 object_transform = glm::translate(glm::mat4(1.0f),translate_vec);
glm::quat rotation = QAccumulative.getQuat(); 
glm::mat4 matrix_rotation = glm::mat4_cast(rotation);
object_transform *= matrix_rotation;
Model *= object_transform;

glm::mat4 MVP = Projection * View * Model;  

GLuint ModelID = glGetUniformLocation(this->shaderProgram, "M");
if(ModelID ==-1)
    cout << "ModelID not found ..." << endl;
GLuint MatrixID = glGetUniformLocation(this->shaderProgram, "MVP");
if(MatrixID ==-1)
    cout << "MatrixID not found ..." << endl;
GLuint ViewID = glGetUniformLocation(this->shaderProgram, "V");
if(ViewID ==-1)
    cout << "ViewID not found ..." << endl;

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelID, 1, GL_FALSE, &Model[0][0]);
glUniformMatrix4fv(ViewID, 1, GL_FALSE, &View[0][0]);

drawObject();

1 个答案:

答案 0 :(得分:1)

我发现了问题,我曾经在绘图后停用了着色器,在那个方法中我接到了电话:

        sf::Texture::bind(NULL);

也不知何故弄乱了着色器,我只是评论了这一点,现在一切正常