Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

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

Thread: strange multitexture problems

  1. #1
    Intern Newbie
    Join Date
    Mar 2005
    Posts
    38

    strange multitexture problems

    Hello.

    I discovered some strange probs with multitexturing.

    my setup:
    -NO vertex shader (fixed function default)
    -simple texture mixing in fragment shader
    -only one tex-coord for both textures
    -rendering unlit to fullscreen-quad
    -all textures generated BEFORE any mutli-texture commands (eg all in stage GL_TEXTURE0)
    -one texture is rendered as FrameBuffer before in a complex pass (which works completely on his own).

    for debug, i split the two textures into different rgb channels, coloring one image yellow, the other blue:

    Code :
    uniform sampler2D Image;
    uniform sampler2D Image2;			
    varying vec4 gl_TexCoord[];			
    void main(void){
    	vec2 tc=vec2(gl_TexCoord[0]);
       gl_FragColor =  vec4(texture2D(Image,vec2(gl_TexCoord[0])).xy,texture2D(Image2,vec2(gl_TexCoord[0])).x,1.0);
    }
    i allways fill in both sampler2D uniforms from GL constant by Image=0, Image2=1.

    now following happens:

    -if i do nothing multitexure commands in GL, only a yellow "Image" renders. correct.

    -if i bind a second texture, preceded by glActiveTexture(GL_TEXTURE0+1), i only see it, "Image2", tinted blue. NOT correct. Both textures should appear mixed.

    -if i now exchange the sample-bindings; eg. Image=1; Image2=0 then only the new texture is rendered, now yellow. simple. so switched back.

    -now for the very strange, hurray..
    if i switch to glActiveTexture(GL_TEXTURE0+1) while my whole mainloop which renders to the FrameBuffer and therefore "Image", all my texures on objects are gone (correct)but now the mixing works! i see a yellow AND blue "Image", "Image2" result, both textures mixed, like i wanted, with one exception: all parts rendered with the texture bound to "Image2" inside the "Image"-creating FramBuffer draw pass now vanish.
    cool, eh?

    smoking on this for two days now.

    sorry for no c-code and no use of cool gl debuggers, its coded in java.

    any ideas?
    (maybe "no fixed function with fragment shaders AND multitexture, please!!" or some strange limitations known?)

    many thanks & greets
    Paul

  2. #2
    Intern Newbie
    Join Date
    Mar 2005
    Posts
    38

    Re: strange multitexture problems

    Arf..

    Bothering you all with this crap i just go very experimental to salve my conscience...

    with results!

    i got it working finally with a "great" difference:
    -Image2=1 is bound first introducing "multitexturing".
    -FrameBuffer Image=0 is now bound as second texture but on stage GL_TEXTURE0

    against my presumptions the actual active stage while creation of texture seems not relevant

    maybe some fault concerning multitexturing like gl "as long i dont know its multi-textured, i made some things easier. if it is switched on, i get aware of." while just forget some small relations to fragment progs.

    thx much for anyone thought about it
    &greetings
    Paul

  3. #3
    Senior Member OpenGL Pro sqrt[-1]'s Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    1,006

    Re: strange multitexture problems

    FYI: you can use GLIntercept (http://glintercept.nutty.org) to debug such problems with java. Just put the openGL32.dll in the javaw.exe directory and take a XML frame grab.

    From here you can see what textures are bound where on each render call.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Posts
    249

    Re: strange multitexture problems

    After your framebuffer is rendered you need to do glActiveTexture(GL_TEXTURE0); then you have to bind your texture you want to copy to, and then you call glCopyTexSubImage2D().
    After that you call glActiveTexture(GL_TEXTURE1) and bind your second texture.

    This works for me for multitexturing with 7 textures and ARB_fragment program, no vertex program. This shouldn't be different with GLSL.

    Indeed, the texture stage is just an "index" where the bound texture is referenced to. (non-technically speaking)

    I hope I'm not repeating just what you are already doing...

  5. #5
    Intern Newbie
    Join Date
    Mar 2005
    Posts
    38

    Re: strange multitexture problems

    no, you don't. but it doesn't apply either because i'm using a real FBO FrameBufferObject which is capable of rendering directly to the texture, no need for CopySubPixel.

    and, as i stated before, the problem is solved for now. but some invesitgation is allways good, as irrational behaveiour is not what i like :-)

    i really hope i made some stupid bug and eliminated it without noticing so.

    thanks you all
    Paul

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2005
    Posts
    26

    Re: strange multitexture problems

    Same problem for me but no solution yet. Perhaps somebody can tell me why to following doesn't work, i.e. the samplers always access the texture on GL_TEXTURE0.

    Vertex shader:
    Code :
    void main()
    {
    	gl_TexCoord[0] = gl_MultiTexCoord0;
    	gl_Position    = ftransform();
    }
    Fragment shader:
    Code :
    uniform sampler2D tex;
    uniform sampler2D old;
     
    void main()
    {
    	vec4 color   = texture2D(tex, gl_TexCoord[0].st);
    	vec4 base    = texture2D(old, gl_TexCoord[0].st);
    	gl_FragColor = base + color;
    }
    Setup:
    Code :
    glGenTextures(3, texFloat);
     
    		glBindTexture(GL_TEXTURE_2D, texFloat[0]);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 4, 4, 0,
    					 GL_RED, GL_FLOAT, matrix);
     
    		glBindTexture(GL_TEXTURE_2D, texFloat[1]);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 4, 4, 0,
    					 GL_RED, GL_FLOAT, matrix2);
     
    		glBindTexture(GL_TEXTURE_2D, texFloat[2]);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 4, 4, 0,
    					 GL_RED, GL_FLOAT, out);
     
    		glGenFramebuffers(1, &gp_framebuffer);
    		glBindFramebuffer(GL_FRAMEBUFFER_EXT, gp_framebuffer);
     
    		glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
    							   GL_TEXTURE_2D, texFloat[2], 0);
     
    		glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
     
    		glClampColor(GL_CLAMP_VERTEX_COLOR_ARB, GL_FALSE);
    		glClampColor(GL_CLAMP_FRAGMENT_COLOR_ARB, GL_FALSE);
    		glClampColor(GL_CLAMP_READ_COLOR_ARB, GL_FALSE);
     
    		vert_sh = glCreateShader(GL_VERTEX_SHADER);
    		frag_sh = glCreateShader(GL_FRAGMENT_SHADER);
     
    		glShaderSource(vert_sh, 1, &vert_src, NULL);
    		glShaderSource(frag_sh, 1, &frag_src, NULL);
     
    		glCompileShader(vert_sh);
    		glCompileShader(frag_sh);
     
    		prog = glCreateProgram();
     
    		glAttachShader(prog, vert_sh);
    		glAttachShader(prog, frag_sh);
     
    		glLinkProgram(prog);
     
    		location[0] = glGetUniformLocation(prog, "tex");
    		location[1] = glGetUniformLocation(prog, "old");
     
    		glUniform1i(location[0], GL_TEXTURE0);
    		glUniform1i(location[1], GL_TEXTURE1);
     
    		glUseProgram(prog);
    Inside display callback:
    Code :
    glActiveTexture(GL_TEXTURE0);
    		glEnable(GL_TEXTURE_2D);
    		glBindTexture(GL_TEXTURE_2D, texFloat[0]);
     
    		glActiveTexture(GL_TEXTURE1);
    		glEnable(GL_TEXTURE_2D);
    		glBindTexture(GL_TEXTURE_2D, texFloat[1]);
     
    		glBegin(GL_QUADS);
     
    			glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
    						glVertex2f(0, 0);
     
    			glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
    						glVertex2f(4, 0);
     
    			glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
    						glVertex2f(4, 4);
     
    			glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
    						glVertex2f(0, 4);
     
    		glEnd();
    Thanks for your help!

  7. #7
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421

    Re: strange multitexture problems

    This is wrong.


    glUniform1i(location[0], GL_TEXTURE0);
    glUniform1i(location[1], GL_TEXTURE1);
    This is right.

    glUniform1i(location[0], 0);
    glUniform1i(location[1], 1);

    Are you using calling glGetError at all?
    You can solve most problems that way.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  8. #8
    Junior Member Newbie
    Join Date
    Aug 2005
    Posts
    26

    Re: strange multitexture problems

    Thanks for your help! I did not include any debug code but there is plenty of them in the original code.

    Replacing GL_TEXTUREi with i wherever it occurs has the follwong effect: now only the texture bound to unit 1 works, texture bound to unit 0 does not work.

    Replacing them only in the call to glUniform has no effect, i.e. same as if I use the symbolic constants.

    So can anyone explain the meaning of the constants GL_TEXTUREi. According to the spec and the programming guide they refer to texture image unit i.

    Thanks

  9. #9
    Junior Member Regular Contributor Zulfiqar Malik's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    247

    Re: strange multitexture problems

    GL_TEXTUREi refers to the i-th texture stage. Its quite simple really ... bind a texture to a particular stage using a combination of glActiveTexture(...) and glBindTexture(...) (Note: that by default i-th stage is not enabled so you must enable it using glEnable(...)). Furthermore, note that you must pass the stage index to glUniform1i(...) and not the logical GL_TEXTUREi constant(s).

    uniform sampler2D tex;
    uniform sampler2D old;

    void main()
    {
    vec4 color = texture2D(tex, gl_TexCoord[0].st);
    vec4 base = texture2D(old, gl_TexCoord[0].st);
    gl_FragColor = base + color;
    }
    If you are trying to replicate the default modulating behavior then do
    gl_FragColor = base * color;

    Hope this helps.
    Zulfiqar Inayat Malik.
    Senior Developer, The Foundry.

  10. #10
    Junior Member Regular Contributor Zulfiqar Malik's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    247

    Re: strange multitexture problems

    @dronus - An optimization note. Swizzles are free (i.e. gl_TexCoord[0].xy) whereas casting is not! A compiler might optimize it, but one is better off doing something oneself, rather than depending on a compiler. Try to use swizzles whenever possible and they do get used quite a lot.
    Zulfiqar Inayat Malik.
    Senior Developer, The Foundry.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •