在学习OpenGL ES的工作原理时,我试图在屏幕上显示纹理方块。这是广场的相关代码。该代码基于谷歌示例:
public class Shape2Square {
private static final String TAG = "Shape2Square";
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"attribute vec2 a_TexCoordinate;"+
"varying vec2 v_TexCoordinate;"+
"void main() {" +
"v_TexCoordinate = a_TexCoordinate;"+
"gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform sampler2D u_Texture"+
"varying vec2 v_TexCoordinate;"+
"void main() {" +
"gl_FragColor = texture2D(u_Texture, v_TexCoordinate);"+
"}";
private final FloatBuffer vertexBuffer;
private final FloatBuffer textureBuffer;
private final ShortBuffer drawListBuffer;
private final int mProgram;
private int mPositionHandle;
private int mMVPMatrixHandle;
private int mtexture;
private int mtexCoordHandler;
private int mtextureHandler;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float squareCoords[] = { -0.3f, -0.3f, 0.0f, // top left
-0.3f, 0.3f, 0.0f, // bottom left
0.3f, 0.3f, 0.0f, // bottom right
0.3f, -0.3f, 0.0f }; // top right
// u,v
static float texturedata[] = {0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f};
private final short drawOrder[] = { 0, 1, 2, 0, 2, 3 };
private final int vertexStride = COORDS_PER_VERTEX * 4;
public Shape2Square() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
ByteBuffer bbtexture = ByteBuffer.allocateDirect(texturedata.length*4);
bbtexture.order(ByteOrder.nativeOrder());
textureBuffer = bbtexture.asFloatBuffer();
textureBuffer.put(texturedata);
textureBuffer.position(0);
ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
int vertexShader = CommonMethods.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = CommonMethods.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
mProgram = GLES20.glCreateProgram();
Shape2Square.checkGlError("glCreateProgram");
GLES20.glAttachShader(mProgram, vertexShader);
Shape2Square.checkGlError("glAttachShader");
GLES20.glAttachShader(mProgram, fragmentShader);
Shape2Square.checkGlError("glAttachShader");
GLES20.glLinkProgram(mProgram);
Shape2Square.checkGlError("glLinkProgram");
}
我从logcat获得的麻烦指向这个方块的“draw”方法。请注意,CommonMethods.loadTexture
只是将位图代码加载到OPEN GL:
EDIT1 :(在整个代码中添加了checkGlErrors)
public void draw(float[] mvpMatrix, int textureid) {
GLES20.glUseProgram(mProgram);
Shape2Square.checkGlError("glUseProgram");
vertexBuffer.position(0);
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
Shape2Square.checkGlError("glVertexAttribPointer");
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
Shape2Square.checkGlError("glVertexAttribPointer");
GLES20.glEnableVertexAttribArray(mPositionHandle);
Shape2Square.checkGlError("glEnableVertexAttribArray");
textureBuffer.position(0);
mtexCoordHandler = GLES20.glGetAttribLocation(mProgram,"a_TexCoordinate");
Shape2Square.checkGlError("glGetAttribLocation");
GLES20.glEnableVertexAttribArray(mtexCoordHandler);
Shape2Square.checkGlError("glEnableVertexAttribArray");
GLES20.glVertexAttribPointer(mtexCoordHandler, 2, GLES20.GL_FLOAT, false, 8, textureBuffer);
Shape2Square.checkGlError("glVertexAttribPointer");
mtextureHandler = GLES20.glGetUniformLocation(mProgram, "u_Texture");
Shape2Square.checkGlError("mtextureHandler");
mtexture = CommonMethods.loadTexture(textureid);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
Shape2Square.checkGlError("glActiveTexture");
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mtexture);
Shape2Square.checkGlError("glBindTexture");
GLES20.glUniform1i(mtextureHandler, 0);
Shape2Square.checkGlError("glUniform1i");
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
Shape2Square.checkGlError("glGetUniformLocation");
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
Shape2Square.checkGlError("glUniformMatrix4fv");
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
Shape2Square.checkGlError("glDrawElements");
GLES20.glDisableVertexAttribArray(mPositionHandle);
Shape2Square.checkGlError("glDisableVertexAttribArray");
}
public static void checkGlError(String glOperation) {
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.e(TAG, glOperation + ": glError " + error);
throw new RuntimeException(glOperation + ": glError " + error);
}
}
执行此操作会导致错误并强制关闭应用程序。在搞乱代码之后,logcat导致glGetUniformLocation
最可能成为mMVPMatrixHandle
的罪魁祸首,这很奇怪,因为它之前有效。为了证明我的观点,如果我删除所有纹理代码并添加一个vColor(在实际代码和fragmentShadercode中)都具有所有通常的相关性,那么我将获得彩色方块。我不明白错误如何与glGetUniformLocation
或如何解决它,所以任何帮助都是值得赞赏的。
EDIT1:于是,我就空出了“glgetuniformlocation”的checkglerror但现在的错误出现在“glUniformMatrix4fv”的形式,这涉及到一个令人失望。有些事情是非常错误的......
EDIT2:决定在整个代码中使用checkglerror,到目前为止,似乎整个绘图方法(包括glUseProgram)都有错误。在绘制方法之前出现的gl位(包括附加着色器和链接)没有错误。
答案 0 :(得分:0)
好的,我发现了我做错了什么(我是一个菜鸟......)。 u_Texture之后的fragmentShadercode应该有“;”在它背后。不敢相信让我回来2天:(。无论如何感谢ClayMontgomery的帮助。虽然我已经把形状颠倒了,但我很确定我能解决这个问题。