OpenGL textures trigger error 1281 and strange background behavior

Posted by user3714670 on Stack Overflow See other posts from Stack Overflow or by user3714670
Published on 2014-08-22T12:41:59Z Indexed on 2014/08/25 10:20 UTC
Read the original article Hit count: 295

Filed under:
|
|
|

I am using SOIL to apply textures to VBOs, without textures i could change the background and display black (default color) vbos easily, but now with textures, openGL is giving an error 1281, the background is black and some textures are not applied. There must be something i didn't understand about applying/loading the textures. BUt the texture IS applied (nothing else is working though), the error is applied when i try to use the shader program however i checked the compilation of these and no problems were written.

Here is the code i use to load textures, once loaded it is kept in memory, it mostly comes from the example of 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;
    }
}

and how i apply it when drawing :

        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);

I am not sure that SOIL is adapted to my program as i want something as simple as possible (i used sfml's texture object which was the best but i can't anymore), but if i can get it to work it would be great.

EDIT :

After narrowing the code implied by the error, here is the code that provokes it, it is called between texture loading and bos drawing :

    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();

© Stack Overflow or respective owner

Related posts about c++

Related posts about opengl