2016-03-07 94 views
0

我是新的OpenGL的,我想申请纹理的3D图形,但我正在逐渐glGetUniformLocation:glError 1282错误,请帮我这个,我在网上搜索,但不能纠正它如果有人解释这个问题,也会很高兴。OpenGlES2.0纹理问题

MY着色器:

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

你甚至不应该能够编译着色器摆在首位。

在片段着色器,你声明 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"); 
+0

谢谢指点出来,固定的,但仍然得到同样的错误。 –

+0

texcColor ??希望你已经修复了着色器中的所有拼写错误。 –

+0

遗憾,那就是当我在写这个问题打字错误,它是完美的,我code.after挖我发现我的glCreateProgram()将返回0,即它不能创建一个程序,我打电话glCreateProgram()在三角形类的构造,三角形类对象的初始化在onSurfaceCreated完成()。 –