经过几个小时的麻烦拍摄后,我想我已经将问题缩小到我的片段和顶点着色器。我为glUseProgram调用得到了一个错误1281。我已经检查过,确保在此次通话之前没有其他错误。我试图制作一个球体,所以知道一些顶点坐标有一个' E'在它们中,这意味着它们有时候很小。这是我的片段和顶点着色器。
final String vertexShaderCode =
"uniform mat4 u_MVPMatrix;" + // A constant representing the combined model/view/projection matrix.
"uniform mat4 u_MVMatrix;" + // A constant representing the combined model/view matrix.
"attribute vec4 a_Position;" + // Per-vertex position information we will pass in.
"attribute vec3 a_Normal;" + // Per-vertex normal information we will pass in.
"varying vec3 v_Position;" + // This will be passed into the fragment shader.
"varying vec3 v_Normal;" + // This will be passed into the fragment shader.
"void main() {" +
"v_Position = vec3( u_MVMatrix * a_Position);" + // Transform the vertex into eye space.
"v_Normal = vec3( u_MVMatrix * vec4(a_Normal, 0.0));" + // Transform the normal's orientation into eye space.
// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
"gl_Position = u_MVPMatrix * a_Position;" +
"}";
final String fragmentShaderCode =
"precision mediump float;" +// Set the default precision to medium. We don't need as high of a precision in the fragment shader.
"uniform vec3 u_LightPos;" + // The position of the light in eye space.
"uniform sampler2D u_Texture;" + // The input texture.
"uniform vec4 v_Color;" +
"varying vec3 v_Position;" + // Interpolated position for this fragment.
"varying vec3 v_Normal;" + // Interpolated normal for this fragment.
"void main(){" +
"float distance = length(u_LightPos - v_Position);" + // Will be used for attenuation.
"vec3 lightVector = normalize(u_LightPos - v_Position);" + // Get a lighting direction vector from the light to the vertex.
// Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
"float diffuse = max(dot(v_Normal, lightVector), 0.0);" +
"diffuse = diffuse * (1.0 / (1.0 + (0.25 * distance)));" +// Add attenuation.
"diffuse = diffuse + 0.7;" +// Add ambient lighting
"gl_FragColor = v_Color + diffuse;" +
"}";
这是我的绘制方法
public void draw(float[] mvpMatrix, float[] mvMatrix, float[] mMatrix) {
// Translate the cube into the screen.
Matrix.setIdentityM(mvpMatrix, 0);
Matrix.translateM(mvpMatrix, 0, posX, posY, posZ);
Matrix.scaleM(mvpMatrix, 0, scale, scale, scale);
Matrix.rotateM(mvpMatrix, 0, rotX, 1, 0, 0);
Matrix.rotateM(mvpMatrix, 0, rotY, 0, 1, 0);
Matrix.rotateM(mvpMatrix, 0, rotZ, 0, 0, 1);
// Logging errors before the call to glUseProgram
int error;
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
Log.i("error", String.valueOf(error));
}
Log.i("mprogram", String.valueOf(GLES20.glIsProgram(mProgramHandle))); // true
GLES20.glUseProgram(mProgramHandle);
int error1;
while ((error1 = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
throw new RuntimeException("glUseProgram" + ": glError " + error1);
}
GLES20.glFrontFace(GLES20.GL_CCW); // Counter-clockwise winding.
GLES20.glEnable(GLES20.GL_CULL_FACE);// Enable face culling.
GLES20.glCullFace(GLES20.GL_BACK);// What faces to remove with the face culling.
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Specifies the location and data format of an array of vertex coordinates to use when rendering.
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
mNormalHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Normal");
GLES20.glEnableVertexAttribArray(mNormalHandle);
mColorHandle = GLES20.glGetUniformLocation(mProgramHandle, "v_Color");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
// Pass in the model view matrix.
GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mvMatrix, 0);
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
// Pass in the modal view projection matrix.
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, indices.length, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
//Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
编辑
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgramHandle = GLES20.glCreateProgram();// create empty OpenGL ES PROGRAM
GLES20.glAttachShader(mProgramHandle, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgramHandle, fragmentShader);// add the fragment shader to program
GLES20.glLinkProgram(mProgramHandle);// creates OpenGL ES PROGRAM executables
check1();
vertexCount = vertices.length / COORDS_PER_VERTEX;
vertexStride = COORDS_PER_VERTEX * 4;
}
int check1() {
int[] linkStatus = new int[1];
GLES20.glGetProgramiv(mProgramHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] != GLES20.GL_TRUE) {
Log.e("TAG", "Could not link program: ");
Log.e("TAG", GLES20.glGetProgramInfoLog(mProgramHandle));
GLES20.glDeleteProgram(mProgramHandle);
mProgramHandle = 0;
}
}
编辑2
public static int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
编辑3 Logcat
06-09 12:25:39.436 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.436 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.436 848: 859 V/WindowManager ]
Window{430341b0 u0 Keyguard}mOrientationRequetedFromKeyguard=false
06-09 12:25:39.446 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.446 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.456 1164: 1164 D/STATUSBAR-PhoneStatusBar ]
animateCollapsePanels
06-09 12:25:39.476 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.476 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.486 14020:15026 E/TAG ]
Could not link program:
06-09 12:25:39.486 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.496 14020:15026 E/TAG ]
Could not link program:
06-09 12:25:39.496 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.496 848: 848 V/WindowManager ]
Window{430341b0 u0 Keyguard}mOrientationRequetedFromKeyguard=false
06-09 12:25:39.526 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.526 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.556 285: 285 I/SurfaceFlinger ]
id=3619 Removed NainActivit (2/7)
06-09 12:25:39.566 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.566 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.566 14020:15026 E/TAG ]
Could not link program:
06-09 12:25:39.566 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.576 15014:15014 I/dalvikvm ]
Could not find method android.webkit.WebView.setWebContentsDebuggingEnabled, referenced from method com.google.apps.dots.android.newsstand.NSDepend.setupInternal
06-09 12:25:39.606 14020-15026/com.example.james.rollingsphere E/TAG﹕ Could not link program:
06-09 12:25:39.606 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.616 14020:15026 E/TAG ]
Could not link program:
06-09 12:25:39.616 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.636 14020:15026 E/TAG ]
Could not link program:
06-09 12:25:39.636 14020-15026/com.example.james.rollingsphere E/TAG﹕ [ 06-09 12:25:39.636 15014:15014 I/dalvikvm ]
Could not find method android.os.PowerManager.isInteractive, referenced from method com.google.apps.dots.android.newsstand.util.AndroidUtil.isDeviceInteractive