Opengl es 纹理未设置动画
显然,我知道我做错了什么。无论如何,我已经写了一些代码来尝试让我的纹理动画化,但不确定哪里出了问题,或者我做错了什么 以下是加载到“我的纹理”中的代码:Opengl es 纹理未设置动画,opengl-es,opengl-es-2.0,Opengl Es,Opengl Es 2.0,显然,我知道我做错了什么。无论如何,我已经写了一些代码来尝试让我的纹理动画化,但不确定哪里出了问题,或者我做错了什么 以下是加载到“我的纹理”中的代码: if(PVRTTextureLoadFromPVR(c_szTextureFile, &m_uiTexture[0]) != PVR_SUCCESS) { PVRShellSet(prefExitMessage, "ERROR: Cannot load the texture\n"); retu
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);
这是我的定时器函数,它试图更新纹理坐标
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
}
这是我的着色器,我正在将数据传递给它
uniform sampler2D sTexture;
均匀取样器2
可变mediump vec2 TexCoord;
可变介质矢量2 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;
}
texture2D的第二个参数从0->1剪裁,它不是绝对值或像素值。你有一个TexCoord,而我假设它也在(0->1,0->1)范围内。然后通过updateTexture++“添加”一个浮点值,该值在C代码中递增。它也没有被初始化 尝试类似以下内容:
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
}
然后在着色器中执行比向vec2添加浮点更明确的操作:
...
mediump vec4 tex1 = texture2D(sTexture, vec2(TexCoord.x + Time, TexCoord.y + Time));
...
你好mobob谢谢你的帮助,我已经按照你说的进行了,但是它似乎没有移动我的纹理,似乎只是在改变颜色?如果需要,我可以提供更多的代码。你为什么要这样做<代码>gl_FragColor=tex2*tex1代码>为什么不是这个<代码>gl_FragColor=0.5*tex2+0.5*tex1代码>抱歉,丹,这是因为我对ES2.0很陌生,只是有点了解,但我会实现你的建议。不,你没有错。只是vec4*vec不同于0.5*vec4+0.5*vec4。第一个用于遮罩颜色,第二个用于将两个纹理混合在一起。什么是正确的取决于你想完成什么。:)顺便说一句,如果你想通过切换纹理来使用动画,那么使用
uniformintu\u模式不是更容易吗代码>然后<代码>如果(u_模式==0)gl_FragColor=tex1;else gl_FragColor=tex2代码>?可能我没有正确定义我要实现的目标,我希望两种纹理都能够独立滚动?回顾我的代码,我认为我采取了错误的方法