我正在尝试使用适用于 Android 的 OpenGL ES 2.0 来显示具有法线和颜色的简单立方体。为此,我创建了一个着色器程序,附加我的顶点和片段着色器,并链接我的程序,如下所示:
// Create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
MyGLRenderer.checkGlError("glCreateProgram");
// Add the vertex shader to program
vertexShaderCode = Utilities.convertResourceToString(context, R.raw.vert_shader_hw3);
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
GLES20.glAttachShader(mProgram, vertexShader);
MyGLRenderer.checkGlError("glAttachShader");
// Add the fragment shader to program
fragmentShaderCode = Utilities.convertResourceToString(context, R.raw.frag_shader_hw3);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
GLES20.glAttachShader(mProgram, fragmentShader);
MyGLRenderer.checkGlError("glAttachShader");
// Bind attributes
GLES20.glBindAttribLocation(mProgram, 0, "aPosition");
MyGLRenderer.checkGlError("glBindAttribLocation");
GLES20.glBindAttribLocation(mProgram, 1, "aNormal");
MyGLRenderer.checkGlError("glBindAttribLocation");
GLES20.glBindAttribLocation(mProgram, 2, "aColor");
MyGLRenderer.checkGlError("glBindAttribLocation");
// Create OpenGL program executables
GLES20.glLinkProgram(mProgram);
MyGLRenderer.checkGlError("glLinkProgram");
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
MyGLRenderer.checkGlError("glUseProgram");
最后它崩溃了,打印到LogCat:glUseProgram: glError 1281
我检查了文档,glError 1281,即GL_INVALID_VALUE
如果程序既不是0也不是OpenGL生成的值时生成。
在谷歌搜索了大约 10 个小时之后,我决定向 StackOverflow 寻求帮助。知道我的顶点着色器代码有什么问题使 glUseProgram 以这种方式运行吗?
这是我的顶点着色器代码,我尝试实现 phong 照明:
uniform mat4 uMVPMatrix;
uniform mat4 uMVMatrix;
uniform vec3 uLightPosition;
uniform vec4 uAmbient;
uniform vec4 uDiffuse;
uniform vec4 uSpecular;
uniform vec4 uEmission;
uniform float uShininess;
attribute vec4 aPosition;
attribute vec3 aNormal;
attribute vec4 aColor;
varying vec4 vColor;
vec4 phong()
{
// P is the vertex coordinate on body
vec3 P = vec3(uMVMatrix * aPosition);
// N is the object normal at P
vec3 N = vec3(uMVMatrix * vec4(aNormal, 0.0));
// Light Position for light 0
vec3 LightPos = uLightPosition;
// L is the light vector
vec3 L = normalize(LightPos - P);
// R is the reflected light vector R = 2(L.N)N - L
vec3 R = reflect(-L, N);
// V is the view vector (eye at the origin)
vec3 V = normalize(-P);
// Diffuse light intensity is cosine of light and normal vectors
float Id = max(dot(L,N) , 0.0);
// Shininess intensity is cosine of light and reflection vectors to a power
float Is = (Id>0.0) ? pow(max(dot(R,V) , 0.0) , uShininess) : 0.0;
// Vertex color
return uEmission + uAmbient + Id*uDiffuse + Is*uSpecular;
}
void main()
{
gl_Position = uMVPMatrix * aPosition;
vColor = phong() * 0.5*(gl_Position/gl_Position.w + 1.0);
}