如果你有足够的内存,我肯定会选择 b),它在处理器上更容易。此外,您只会更改制服的价值。但是,由于预处理,打开应用程序可能需要一些时间。
获取统一变量,在其中编译着色器,animationPos 应该是全局的。
Gluint animationPos = glGetUniformLocation(shaderProgram, "nameoftheuniform");
您的主循环应该将 animationPos 值传递给着色器:
Gluniform1i ( animationPos, curentAnimationIndex);
添加您的片段着色器变量:
uniform int animationPos;
片段着色器主:
float texCoordY = texCoord.y; //texture coordinates should be passed from vertex shader
float texCoordX = texCoord.x/20.0f; //we are dividing it with 20 since it is the amount of textures that we have and if we use it directly it would try to use all the texture. Whereas the texture stores at 20 different textures.
float textureIndex = 1.0f*animationPos/20.0f; //Pointer to the start of the animation texture.
gl_fragColor = texture2D ( yourTexture, vec2( textureIndex + texCoordX, texCoordY));
上面的代码假设你在x方向扩展了你的纹理,你也可以尝试像矩阵一样扩展它,那么你需要改变texCoord计算部分。此外,我们使用了 20 种纹理。
选项 a) 对处理器的负担更大,并且您每次都将更改纹理,因此它会更多地使用 pci,但更容易占用内存。这个问题更像是一个设计决策,但我想可以处理 20 张图片,所以请选择选项 b)。
编辑:添加代码。