hao haochao
hao haochao

Reputation: 91

what's wrong with my shader using texture by through glUniform1i

in my texturing with GLSL program,just one texture bind to texture unit 0.such as following:

.......................
        glActiveTexture(GL_TEXTURE0);
        GLuint tid;
        glGenTextures(1, &tid);
        glBindTexture(GL_TEXTURE_2D, tid);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0,
                     img.format, GL_UNSIGNED_BYTE, img.data);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

and then inform to frag shader through a uniform variable(as setUniform does),here's name is "texture0" corresponding to my frag shader:

void CShader::setUniform( const char *name, int val )
    {
        int loc = getUniformLocation(name);
        glAssert();  //note!,my own assertion wrapping around glGetError.
              if( loc >= 0 )
        {
            glUniform1i(loc, val);

        } else {
            printf("Uniform: %s not found.\n",name);
        }
              glAssert();   //note!,my own assertion wrapping around glGetError.

    }

it is out of my expectation, it assert failed at last glAssert() line,and giving me GL_INVALID_OPERATION .what was wrong,doesn't my display card is supported for this operation?,it can't be,my OpenGL version is 3.x. after few hours struggle, did a try commented the last glAssert(),but another assertion failed at my gldrawelements call. obviously, i absolutely think that cause gldrawelements failed is glUniform1i failed. can someone give me suggestion.

EDIT:

from datenwolf suggestion,here 's code for glAssert, it's very simple,just call glGetError once.

void _glAssert(const char * info, int line){
 char const* sz_GL_INVALID_ENUM="GL_INVALID_ENUM";
 char const* sz_GL_INVALID_VALUE="GL_INVALID_VALUE";
 char const* sz_GL_INVALID_OPERATION="GL_INVALID_OPERATION";
 char const* sz_GL_OUT_OF_MEMORY ="GL_OUT_OF_MEMORY";
GLenum result;
    if((result=glGetError()) != GL_NO_ERROR )
    {
        const char *  sz_result=NULL;
        switch(result)
        {
        case GL_INVALID_ENUM:
            sz_result = sz_GL_INVALID_ENUM;
        break;
        case GL_INVALID_VALUE:
            sz_result = sz_GL_INVALID_VALUE;
        break;
        case GL_INVALID_OPERATION:
            sz_result = sz_GL_INVALID_OPERATION;
        break;
        case GL_OUT_OF_MEMORY:
            sz_result = sz_GL_OUT_OF_MEMORY;
        break;
        }

        _assert(sz_result,info,line);
    }
}

#define glAssert() \
    _glAssert(__FILE__, __LINE__);

BTW,before i use glUniform1i call,everything works fine with my shader

attribute vec3 VertexPosition;
varying vec3 Color;
void main()
{
Color = vec3(0.5,0,0);
gl_TexCoord[0]  = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * vec4( VertexPosition, 1.0 );
}


uniform sampler2D texture0;

varying vec3 Color;
void main() {

 vec4 texColor = texture2D( texture0, gl_TexCoord[0].st);
gl_FragColor= vec4(Color, 1.0)*texColor;
}

i have stripped out many code in shader to simplify the problem .

i'm Sorry that I don't describe this in detail. actually,my wrapper around opengl is very simple,no complex things with it, no multiple threads.

this is constructor used for loading texture and shader script into GPU,memory separately, then call "compileandlink" method to compile the shader script , and call setUniform immediately.

IEntity(const char * szvs,const char * szfs): m_shader(new CShader(szvs,szfs)){
         ...........................
}

CCube::CCube():IEntity("v_simple.glsl","f_simple.glsl") {
    ...................

        IMAGE img;
        img.Load("texture.bmp");
        img.ExpandPalette();

        glActiveTexture(GL_TEXTURE0);
        GLuint tid;
        glGenTextures(1, &tid);
        glBindTexture(GL_TEXTURE_2D, tid);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0,
                     img.format, GL_UNSIGNED_BYTE, img.data);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        m_shader->compileandlink();
        m_shader->setUniform("texture0", 0);
}

CShader::compileandlink method

bool CShader::compileandlink(){
            if(m_vsscript == NULL || m_fsscript == NULL)
                return false;

            if(strlen(m_vsscript) <= 0  || strlen(m_fsscript) <= 0)
                return false;

            glShaderSource(m_vs, 1, (const GLchar **) &m_vsscript, NULL);
            glCompileShader(m_vs);

            glShaderSource(m_fs, 1, (const GLchar **) &m_fsscript, NULL);
            glCompileShader(m_fs);

            glAttachShader(m_program, m_vs);
            glAttachShader(m_program, m_fs);
            //glBindFragDataLocation(m_program, 0, "FragColor");
            glLinkProgram(m_program);
            glAssert();


            GLint status;
            glGetProgramiv( m_program, GL_LINK_STATUS, &status );
            if( GL_FALSE == status ) {
            fprintf( stderr, "Failed to link shader program!\n" );
            GLint logLen;
            glGetProgramiv(m_program, GL_INFO_LOG_LENGTH,
            &logLen);

            if( logLen > 0 )
                {
                char * log = (char *)malloc(logLen);
                GLsizei written;
                glGetProgramInfoLog(m_program, logLen,
                    &written, log);
                printf("Program log: \n%s", log);
                free(log);
                }
            }


            return true;

    }

CShader::setUniform method, as i say before,it have GL_INVALID_OPERATION result of "glGetError" after glGetUniformiv returned inside this method.

void CShader::setUniform( const char *name, int val )
    {
        int loc = getUniformLocation(name);
        glAssert();
        if( loc >= 0 )
        {
            GLint outval=-1;

            glUniform1i(loc, val);
            /*int tmploc =glGetUniformLocation(m_program, name);
            glGetUniformiv(m_program,
                            tmploc,
                           &outval);
            printf("Value is %d\n", outval);*/

        } else {
            printf("Uniform: %s not found.\n",name);
        }
        glAssert();
    }

Upvotes: 1

Views: 4774

Answers (1)

Brett Hale
Brett Hale

Reputation: 22358

m_shader->compileandlink();
m_shader->setUniform("texture0", 0);

This will not work, because you can only set uniforms when there's an active program object. i.e., you need glUseProgram(m_program) before trying to set the uniform value. That's why there's no program parameter for the glUniform functions.

I don't understand: int loc = getUniformLocation(name), since it does require the name of a linked program. The commented: glGetUniformLocation(m_program, name) is actually correct.

Upvotes: 7

Related Questions