我是opengl的新手,我正在尝试将纹理应用于3D图形,但我得到了glGetUniformLocation:glError 1282错误,请帮我解决这个问题,我在网上搜索但无法纠正它。将是如果有人解释这个问题也很高兴。
我的着色器:
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"attribute vec2 texCoord;" +
"varying vec2 texCoordOut;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
" texCoordOut = texCoord;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 texColor"+
"varying vec2 texCoordOut;" +
"uniform sampler2D Texture;" +
"void main() {" +
" texColor = texture2D(Texture, texCoordOut);" +
" gl_FragColor = texColor;" +
"}";
绘制方法:
public void draw1(float[] mvpMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
MyGLRenderer.checkGlError("glGetUniformLocation");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
MyGLRenderer.checkGlError("glUniformMatrix4fv");
vsTextureCoord = GLES20.glGetAttribLocation(mProgram, "texCoord");
fsTexture = GLES20.glGetUniformLocation(mProgram, "Texture");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(
mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
GLES20.glEnableVertexAttribArray(vsTextureCoord);
GLES20.glVertexAttribPointer(vsTextureCoord, COORDS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
TextureStride, texBuffer);
// get handle to shape's transformation matrix
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
GLES20.glUniform1i(fsTexture, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, tablelamp21NumVerts);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(vsTextureCoord);
}
LoadTexture:
public void loadGLTexture(Context context) {
GLES20.glGenTextures(4, textures, 0);
Bitmap bitmap = null;
for (int i=0;i<4;i++)
{
// Create a bitmap
bitmap = BitmapFactory.decodeResource(context.getResources(), resourceIds[i]);
//...and bind it to our array
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[i]);
//Create Nearest Filtered Texture
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
//Clean up
bitmap = null;
}
}
答案 0 :(得分:0)
您甚至不应该首先编译着色器。
在片段着色器中,您已声明 Sampler2D为变量纹理,但您使用 u_texture 对其进行采样。 在两个地方使用相同的变量名称,也应与
相同private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"attribute vec2 texCoord;" +
"varying vec2 texCoordOut;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
" texCoordOut = texCoord;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 texcColor"+
"varying vec2 texCoordOut;" +
"uniform sampler2D u_texture;" +
"void main() {" +
" texColor = texture2D(u_texture, texCoordOut);" +
" gl_FragColor = texColor;" +
"}";
在GLES代码中,
fsTexture = GLES20.glGetUniformLocation(mProgram, "u_texture");