0
votes

I know I have done something wrong, obviously. Anyway I have wrote some code to try and get my textures to animate and not really sure what's gone wrong or what I have done wrong.

here is the code that loads in my textures:

 if(PVRTTextureLoadFromPVR(c_szTextureFile, &m_uiTexture[0]) != PVR_SUCCESS)
    {
        PVRShellSet(prefExitMessage, "ERROR: Cannot load the texture\n");
        return false;
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    //This loads in the second texture
    if(PVRTTextureLoadFromPVR(c_szTextureFile2, &m_uiTexture[1]) != PVR_SUCCESS)
    {
        PVRShellSet(prefExitMessage, "ERROR: Cannot load the texture2\n");
        return false;
    }
    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);

and here is my timer function that attempts to update the texture coordinates

int time_Loc = glGetUniformLocation(m_ShaderProgram.uiId, "Time"); //This stores the location of the uniform
float updateTexture;
float timeElapsed = difftime(time(NULL), Timer) * 1000;
if(timeElapsed > 16.0f)
{
    glUniform1f(time_Loc, updateTexture++); // passes the updated value to shader
}

and here is my shader that I am passing the data to

uniform sampler2D  sTexture;

uniform sampler2D sTexture2;

varying mediump vec2 TexCoord; varying mediump vec2 TexCoord2;

//This gets updated within the main code
uniform highp float Time;

void main()
{
    mediump vec4 tex1 = texture2D(sTexture, TexCoord + Time);
    mediump vec4 tex2 = texture2D(sTexture2, TexCoord2 + Time); 

    gl_FragColor = tex2 * tex1;
}
1
why would you do this? gl_FragColor = tex2 * tex1; and why not this? gl_FragColor = 0.5*tex2 + 0.5*tex1;DanP
Sorry Dan it's because I'm quite new to ES 2.0 and only just kind of grasped it, but I will implement what you have advised.DorkMonstuh
No, you're not wrong. It's just that vec4*vec is different from 0.5*vec4+0.5*vec4. The first one is useful for masking colors, the second one is for blending 2 textures together. What is correct depends on what you want to accomplish. :) Btw, if you want to use animation by switching textures, isn't it easier to use uniform int u_mode; then if(u_mode==0) gl_FragColor = tex1; else gl_FragColor = tex2; ?DanP
Maybe I haven't defined properly what I am trying to achieve, I want both textures to be able to scroll independently? I think I have taken a wrong approach looking back at my codeDorkMonstuh

1 Answers

0
votes

The second paramater of texture2D is clipped from 0->1, it is not in absolutes or pixels. You have a TexCoord, while I'll assume is also in the (0->1, 0->1) range. You're then "adding" a float value, that is getting incremented in your C code, via updateTexture++. Its also not getting initialized!

Try something more like this:

  float updateTexture = 0.0f;
  ...
  if(timeElapsed > 16.0f)
  {
    updateTexture += 0.01; // arbitrary increment
    if(updateTexture > 1.0) updateTexture = 0.0f;  // wrap, if you want
    glUniform1f(time_Loc, updateTexture); // passes the updated value to shader
  }

Then in your shader do something more explicit than adding a float to a vec2:

  ...
  mediump vec4 tex1 = texture2D(sTexture, vec2(TexCoord.x + Time, TexCoord.y + Time));
  ...