Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Fragment Shader and Textures

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    14

    Fragment Shader and Textures

    What im trying to do:
    I have 2 textures, which i want to blend together on a plane (GL_Quad) using my own fragment shader. Later on i want to implement bump mapping.
    At the moment im using the same image for BOTH textures to test my code.

    The Problem:
    Both Versions of my fragment-shader should have the same result, since the textures are equal (same image). But only Version2 seems correct...
    It seems that the secound texture is missing!

    Code :
    //VERSION1
    uniform sampler2D texture;
    uniform sampler2D bumpmap;
     
    void main() {
            vec4 colTexture = texture2D(texture,gl_TexCoord[0].st);
            vec4 colBumpmap = texture2D(bumpmap,gl_TexCoord[1].st);
            gl_FragColor =    0.1* colBumpmap + colTexture;
     
    }
    -------------------------------------------------------------------
    //VERSION2
    uniform sampler2D texture;
    uniform sampler2D bumpmap;
     
    void main() {
            vec4 colTexture = texture2D(texture,gl_TexCoord[0].st);
            vec4 colBumpmap = texture2D(bumpmap,gl_TexCoord[1].st);
            gl_FragColor =    colBumpmap + 0.1 * colTexture;
     
    }


    I think that the error lies in the c++ code. Here are the 3 methods that are relevant:
    • initializeShader();
    • initializeTextures();
    • drawPlane();


    Code :
    void OpenglWindow::initializeTextures(){
           //1 texture
           glActiveTexture(GL_TEXTURE0);
           glEnable(GL_TEXTURE_2D);
           glGenTextures(1,&textureNr1);
           glBindTexture(GL_TEXTURE_2D,textureNr1);
     
           QImage image = QGLWidget::convertToGLFormat(QImage("droplets.jpg"));
           glTexImage2D(GL_TEXTURE_2D,0, GL_RGB ,image.width(),image.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,image.bits());
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     
           //2 texture (same code, same image, only for GL_TEXTURE1)
           glActiveTexture(GL_TEXTURE1);
           glEnable(GL_TEXTURE_2D);
           glGenTextures(1,&textureNr2);
           glBindTexture(GL_TEXTURE_2D,textureNr2);
     
           QImage image2 = QGLWidget::convertToGLFormat(QImage("droplets.jpg"));       
           glTexImage2D(GL_TEXTURE_2D,0, GL_RGB ,image2.width(),image2.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,image2.bits());
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    }

    Code :
    void OpenglWindow::initializeShader(){
        //1. create new fragmentshader
        GLuint fragmentHandle = glCreateShader(GL_FRAGMENT_SHADER);
        //2. read shader program from file
        QFile fragmentFile("shader.frag");
        if (!fragmentFile.open(QIODevice::ReadOnly | QIODevice::Text))
          exit(1);
        QTextStream in(&fragmentFile);
        QString fragSourceCode = in.readAll();
        fragmentFile.close();    
        QByteArray ba = fragSourceCode.toLocal8Bit();
        printf("size %d \n",ba.size());
        const char* src = ba.data();
        glShaderSource(fragmentHandle, 1,&src ,NULL);
        //3. compile fragmentshader
        glCompileShader(fragmentHandle);
        int compiled;
        glGetShaderiv(fragmentHandle, GL_COMPILE_STATUS, &compiled);
        if (!compiled){
            printf("compile error \n");
        }    
        //4. create program and attach the fragmentshader to it
        GLuint prog = glCreateProgram();
        glAttachShader(prog,fragmentHandle);
        //5. link programm
        glLinkProgram(prog);
        int linked;
        glGetProgramiv(prog, GL_LINK_STATUS, &linked);
        if (!linked){
            printf("linker error \n");
        }        
        //6. init shader uniforms
        int textureLoc = glGetUniformLocation(prog, "texture");
        glUniform1i(textureLoc, GL_TEXTURE0);
        int bumpmapLoc = glGetUniformLocation(prog, "bumpmap");
        glUniform1i(bumpmapLoc, GL_TEXTURE1);
        //use the prog
        glUseProgram(prog);
    }

    Code :
    void OpenglWindow::drawPlane(){    
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
            glBegin(GL_QUADS);
                glMultiTexCoord2f(GL_TEXTURE0,0,1); glMultiTexCoord2f(GL_TEXTURE1,0,1); glVertex3f(-1,1,0);
                glMultiTexCoord2f(GL_TEXTURE0,0,0); glMultiTexCoord2f(GL_TEXTURE1,0,0); glVertex3f(-1,-1,0);
                glMultiTexCoord2f(GL_TEXTURE0,1,0); glMultiTexCoord2f(GL_TEXTURE1,1,0); glVertex3f(1,-1,0);
                glMultiTexCoord2f(GL_TEXTURE0,1,1); glMultiTexCoord2f(GL_TEXTURE1,1,1); glVertex3f(1,1,0);
            glEnd();    
    }
    Thx for your help!
    Hurgh

  2. #2
    Senior Member Frequent Contributor scratt's Avatar
    Join Date
    May 2008
    Location
    Thailand
    Posts
    555

    Re: Fragment Shader and Textures

    I don't really understand your explanation.. and how / what are you testing when you use two identical textures? A bit confusing...

    But at a quick glance.. the maths after glFragColor in the two shaders (Version 1 and Version 2) are very different from each other..

    The order of multiplication and addition will be different, hence a different result.


  3. #3
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Fragment Shader and Textures

    It should be:

    Code :
     //6. init shader uniforms
        int textureLoc = glGetUniformLocation(prog, "texture");
        glUniform1i(textureLoc, 0);
        int bumpmapLoc = glGetUniformLocation(prog, "bumpmap");
        glUniform1i(bumpmapLoc, 1);

    See the wiki

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    14

    Re: Fragment Shader and Textures

    Sorry for my bad englisch i will try to clear things up.

    Both Shader-Versions should give the same result since the textures are the same. But the results are diffrent.

    if colTexture == colBumpmap => same result:
    Code :
     gl_FragColor =    0.1* colTexture + colBumpmap;
     gl_FragColor =    0.1* colBumpmap + colTexture;

    But -as i said- the results are diffrent.

    Thanks for your time,
    Hurgh

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    14

    Re: Fragment Shader and Textures

    Quote Originally Posted by dletozeun
    It should be:

    Code :
     //6. init shader uniforms
        int textureLoc = glGetUniformLocation(prog, "texture");
        glUniform1i(textureLoc, 0);
        int bumpmapLoc = glGetUniformLocation(prog, "bumpmap");
        glUniform1i(bumpmapLoc, 1);

    See the wiki
    didnt made a diffrence -.-

  6. #6
    Senior Member Regular Contributor
    Join Date
    Feb 2004
    Posts
    248

    Re: Fragment Shader and Textures

    You need to use a vertex shader to fill the gl_TexCoord[] varyings correctly ( with gl_MultiTexCoord0 etc.). Otherwise their content is undefined in the fragment shader.

  7. #7
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Fragment Shader and Textures

    Try to use a simpler shader with just one sampler and see at first if you can render both textures, first binding textureNr1 to TEXTURE0 then binding textureNr2 to TEXTURE0;
    This way, you will be sure that both textures are correctly loaded in memory.

    You can then debug your shader getting the number of active uniforms with glGetProgramiv( program, GL_ACTIVE_UNIFORMS, &activeUniformNb ) and get more information about these uniforms with glGetActiveUniform.

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    14

    Re: Fragment Shader and Textures

    Is this also the explenation if i use this code instead?

    Code :
    //Version1 (seems right, i see a bright image(texture))
    gl_FragColor = 0.1*texture2D(bumpmap,gl_TexCoord[0].st) + texture2D(texture,gl_TexCoord[0].st);
     
    //Version2 (seems wrong, i see a very dark image(texture))
    gl_FragColor = texture2D(bumpmap,gl_TexCoord[0].st) + 0.1*texture2D(texture,gl_TexCoord[0].st);

    Because in this way both textures are accessed using the same texture coordinates (gl_TexCoord[0]).

    ...and it is not working...

    Hurgh

    Quote Originally Posted by def
    You need to use a vertex shader to fill the gl_TexCoord[] varyings correctly ( with gl_MultiTexCoord0 etc.). Otherwise their content is undefined in the fragment shader.

  9. #9
    Senior Member Regular Contributor
    Join Date
    Feb 2004
    Posts
    248

    Re: Fragment Shader and Textures

    From the GLSL Specs (1.2):

    The following built-in varying variables are available to write to in a vertex shader. A particular one
    should be written to if any functionality in a corresponding fragment shader or fixed pipeline uses it or
    state derived from it. Otherwise, behavior is undefined.
    varying vec4 gl_FrontColor;
    varying vec4 gl_BackColor;
    varying vec4 gl_FrontSecondaryColor;
    varying vec4 gl_BackSecondaryColor;
    varying vec4 gl_TexCoord[];
    "undefined" means exactly that, you don't know what you are getting.

    You could use for testing:

    vec2( gl_FragCoord.x/WIDTH, gl_FragCoord.y/HEIGHT )

    as texture coordinates (exchange WIDTH and HEIGHT with your viewport dimensions)

  10. #10
    Junior Member Newbie
    Join Date
    Jun 2009
    Posts
    14

    Re: Fragment Shader and Textures

    @dletozeun
    I did as you said. Both textures are loaded in memory.
    I also used glGetProgramiv => the number of uniforms is 2 (if i use my shader with two samples). Seems ok though. I didnt used glGetActiveUniform because i do not know which of the accessible information could be useful to debug.

    @def
    I attached a vertex shader to the program and changed the fragment shader a little.

    Code :
     
    //VERTEX shader
    void main(){
            gl_TexCoord[0]  = gl_MultiTexCoord0;
            gl_TexCoord[1]  = gl_MultiTexCoord1;
            gl_Position     = ftransform();
    }
     
    //FRAGMENT shader
    uniform sampler2D texture;
    uniform sampler2D bumpmap;
     
    void main() {
            gl_FragColor = 0.5 * texture2D(texture,gl_TexCoord[0].st) + 0.5 * texture2D(bumpmap,gl_TexCoord[1].st);
    }
    My (fragment) shader is still not working as i expect.
    Here is an image showing my results.
    I used two diffrent textures, as you can see.

    any ideas?

    Thanks,
    Hurgh

Page 1 of 2 12 LastLast

Similar Threads

  1. How to load textures into the fragment shader?
    By danielr in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 07-05-2017, 11:58 PM
  2. sending textures to trhe fragment shader
    By juanito in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 02-22-2014, 09:09 PM
  3. Fragment shader for unsigned integer textures
    By debinair in forum OpenGL: GLSL
    Replies: 3
    Last Post: 02-22-2013, 10:22 AM
  4. How to use multiple textures in fragment shader?
    By RobertPub in forum OpenGL: GLSL
    Replies: 10
    Last Post: 09-26-2006, 05:39 AM
  5. how to use cg with textures in vertex and fragment shader?
    By nitschke in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 04-10-2006, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean