我正在尝试使用OpenGL ES 2.0 for Android来显示带有法线和颜色的简单立方体。为此,我创建了一个着色器程序,附加我的顶点和片段着色器,并链接我的程序,如下所示:
// 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
并检查了文档,如果程序既不是0也不是由{0}生成的值,则会生成glError 1281,即GL_INVALID_VALUE
的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);
}
答案 0 :(得分:2)
感谢帮助人员。我终于弄明白了。问题是在我的顶点着色器中,我声明了#34;属性vec4 aColor;"然后在我的代码中,我尝试这样做:
mColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
GLES20.glEnableVertexAttribArray(mColorHandle);
GLES20.glVertexAttribPointer(mColorHandle, 4, GLES20.GL_FLOAT, false, 16, colorsBuffer);
然而,aColor实际上从未在顶点着色器代码中使用,并且已经在编译后的着色器中进行了优化,因此当我试图获得它的句柄并将其填满时,它会导致我的程序的其余部分失败。
答案 1 :(得分:0)
我发现您使用checkGLError存在潜在问题。在MyGLRenderer.loadShader
之后没有检查错误。你可以在那里添加一个检查,看看它是否真的已正确编译(在glAttachShader
之后的下一次错误检查之前)?请注意,loadShader
调用内部有2个GL调用 - 一个用于加载字符串,另一个用于编译着色器本身。
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&#34);