简体   繁体   中英

Cocos2dx OpenGl Shader two textures problem

My fragment shader for a mobile game in cocos2dx works on iOS, but does not work as expected on Android and I'm out of ideas.

The idea: u_texture is the input texture of a sprite whose appearance I want to modify. u_texture1 is a "mask" texture that has some transparency and depending on its alpha channel, I want to render the sprite's appearance with some changes or without.

The very minimal example of the problem I'm experiencing:

  1. This is working "as expected", namely, colours the sprite red where the mask is transparent and yellow where the mask is opaque.
    uniform sampler2D u_texture;
    uniform sampler2D u_texture1;
    
    varying vec2 v_texCoord;
    
    varying vec2 v_resolution;
    
    void main()
    {
        vec4 maskCol=texture2D(u_texture1, v_texCoord);
        vec4 currentCol = texture2D(u_texture,v_texCoord);
        vec4 newCol = currentCol;
    
        if(maskCol.a == 0.0){
            newCol.r = 1.0;
            newCol.g = 0.0;
            newCol.b = 0.0;
            newCol.a = 1.0;
        } else {
            newCol.r = 1.0;
            newCol.g = 1.0;
            newCol.b = 0.0;
            newCol.a = 1.0;
        }
        gl_FragColor = newCol;
    }
  1. This code I would expect to colour the sprite red where the mask is transparent and leave the rest alone. However, it does not do that. Instead, it colours the sprite red where the INPUT TEXTURE is transparent, and leaves the rest alone.
    uniform sampler2D u_texture;
    uniform sampler2D u_texture1;

    varying vec2 v_texCoord;

    varying vec2 v_resolution;

    void main()
    {
      vec4 maskCol=texture2D(u_texture1, v_texCoord);
      vec4 currentCol = texture2D(u_texture,v_texCoord);
      vec4 newCol = currentCol;

      if(maskCol.a == 0.0){
          newCol.r = 1.0;
          newCol.g = 0.0;
          newCol.b = 0.0;
          newCol.a = 1.0;
      } 
     gl_FragColor = newCol;
 }

Can anyone see anything wrong with this code? Apart from equality comparing the alpha value - which is not the problem here, I have checked, but I'm leaving it for simplicity.

Does v_resolution do anything?

glsl is fine.

Two ways to check:

1.Make sure that u_texture1 is a mask texture. Is the texture passing reversed?

2.The first glsl is correct because each newcol has a color. But the second glsl is not, so you can try to output newcol directly and see if the result is expected?

2022-12-29

I copied this glsl.

right is:

unsigned int texture1, texture2;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

shader is:

  vec4 maskCol=texture2D(texture2, TexCoord);
  vec4 currentCol = texture2D(texture1,TexCoord);
  vec4 newCol = currentCol;

  if(maskCol.a == 0.0){
      newCol.r = 1.0;
      newCol.g = 0.0;
      newCol.b = 0.0;
      newCol.a = 1.0;
  } 
 FragColor = newCol;

texture1 is:

在此处输入图像描述

texture2 is

在此处输入图像描述

在此处输入图像描述

error is:

glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

maybe try to change texture glTexParameteri or other Settings for textures!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM