我正在努力学习GLSL ES,这样我就可以将一堆动画从CPU卸载到GPU。我的问题是创建一个使用精灵表的简单动画着色器。着色器根本不使用灯光,我只是显示完整的纹理值以保持基本。我选择的OpenGL ES框架是Rajawali,所以我的一些值和参数都是从这些类中提取的。
当我将此着色器与isAnimated = false
一起使用时,纹理将按预期应用。但是,当isAnimated = true
模型全部为黑色时,就好像没有应用纹理一样。
我能够确认顶点和片段着色器正在将ifdef ANIMATED
评估为true
,所以它似乎是我的纹理坐标修改导致了问题。
" vTextureCoord.s = mod(uCurrentFrame, uNumTileRows) * uTileSize;" +
" vTextureCoord.t = uTileSize * floor(uCurrentFrame / uNumTileRows);\n" +
非常感谢任何帮助!
这是我的完整&非工作着色器类:
package rajawali.materials;
import android.opengl.GLES20;
public class SimpleAnimatedMaterial extends AMaterial {
protected static final String mVShader =
"uniform mat4 uMVPMatrix;\n" +
"#ifdef ANIMATED\n" +
"uniform float uCurrentFrame;\n" +
"uniform float uTileSize;\n" +
"uniform float uNumTileRows;\n" +
"#endif\n" +
"attribute vec4 aPosition;\n" +
"attribute vec2 aTextureCoord;\n" +
"attribute vec4 aColor;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec4 vColor;\n" +
"void main() {\n" +
" gl_Position = uMVPMatrix * aPosition;\n" +
" #ifdef ANIMATED\n" +
" vTextureCoord.s = mod(uCurrentFrame, uNumTileRows) * uTileSize;" +
" vTextureCoord.t = uTileSize * floor(uCurrentFrame / uNumTileRows);\n" +
" #else\n" +
" vTextureCoord = aTextureCoord;\n" +
" #endif\n" +
" vColor = aColor;\n" +
"}\n";
protected static final String mFShader =
"precision mediump float;\n" +
"uniform sampler2D uDiffuseTexture;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec4 vColor;\n" +
"#ifdef ANIMATED\n" +
" uniform float uTileSize;\n" +
" uniform float uNumTileRows;\n" +
"#endif\n" +
"void main() {\n" +
"#ifdef TEXTURED\n" +
" #ifdef ANIMATED\n" +
" vec2 realTexCoord = vTextureCoord + (vTextureCoord / uNumTileRows);" +
" gl_FragColor = texture2D(uDiffuseTexture, realTexCoord);\n" +
" #else\n" +
" gl_FragColor = texture2D(uDiffuseTexture, vTextureCoord);\n" +
" #endif\n" +
"#else\n" +
" gl_FragColor = vColor;\n" +
"#endif\n" +
"}\n";
protected int muCurrentFrameHandle;
protected int muTileSizeHandle;
protected int muNumTileRowsHandle;
protected int mCurrentFrame;
protected float mTileSize;
protected float mNumTileRows;
protected boolean mIsAnimated;
public SimpleAnimatedMaterial() {
this(false);
}
public SimpleAnimatedMaterial(boolean isAnimated) {
this(mVShader, mFShader, isAnimated);
}
public SimpleAnimatedMaterial(String vertexShader, String fragmentShader, boolean isAnimated) {
super(vertexShader, fragmentShader, NONE);
mIsAnimated = isAnimated;
if(mIsAnimated) {
mUntouchedVertexShader = "\n#define ANIMATED\n" + mUntouchedVertexShader;
mUntouchedFragmentShader = "\n#define ANIMATED\n" + mUntouchedFragmentShader;
}
setShaders(mUntouchedVertexShader, mUntouchedFragmentShader);
}
@Override
public void useProgram() {
super.useProgram();
}
@Override
public void setShaders(String vertexShader, String fragmentShader)
{
super.setShaders(vertexShader, fragmentShader);
muCurrentFrameHandle = getUniformLocation("uCurrentFrame");
muTileSizeHandle = getUniformLocation("uTileSize");
muNumTileRowsHandle = getUniformLocation("uNumTileRows");
}
public void setCurrentFrame(int currentFrame) {
mCurrentFrame = currentFrame;
GLES20.glUniform1f(muCurrentFrameHandle, mCurrentFrame);
}
public void setTileSize(float tileSize) {
mTileSize = tileSize;
GLES20.glUniform1f(muTileSizeHandle, mTileSize);
}
public void setNumTileRows(int numTileRows) {
mNumTileRows = numTileRows;
GLES20.glUniform1f(muNumTileRowsHandle, mNumTileRows);
}
}
答案 0 :(得分:0)
我注意到你错过了一些' \ n'几行换行符。虽然我不认为它只是这个,但如果我记得的话,它在过去给我带来了问题。
您将使用(realTexCoord)采样的纹理坐标的界限是什么。看起来它可能会大于1,对我来说通常意味着只会显示清晰的颜色。