2013-08-28 70 views
-1

我使用https://github.com/d3kod/Texample2中的代碼在OpenGL 2.0中呈現文本。該項目的解釋可以在http://primalpond.wordpress.com/2013/02/26/rendering-text-in-opengl-2-0-es-on-android/glEnableVertexAttribArray上的OpenGL 2.0 ES 1281錯誤

該代碼在我運行android 4.1.2的三星Galaxy S3上效果很好。然而,它不適用於運行android 2.3.4的我的Droid X,而其他人在Android 4.0.4 Onepad 940平板電腦上遇到了這個問題(請參閱http://primalpond.wordpress.com/2013/02/26/rendering-text-in-opengl-2-0-es-on-android/#comment-89)。

我收到了1281錯誤就行了: GLES20.glEnableVertexAttribArray(mColorHandle)

我已經確定,這是因爲在我的Droid X的GL_MAX_VERTEX_ATTRIBS只有8,當我上運行我的應用程序mColorHandle設置爲26。我的三星GS3,GL_MAX_VERTEX_ATTRIBS是16,但mColorHandle設置爲0.

因此,由於26> 8,1281錯誤被拋出。我不明白爲什麼在Droid得到的26句柄值和GS3得到0

mProgram程序設置在此行中的對象的構造函數:

mColorHandle = GLES20.glGetUniformLocation(mProgram.getHandle(), "u_Color"); 

這是該行創建錯誤:

void initDraw(float red, float green, float blue, float alpha) { 
GLES20.glUseProgram(mProgram.getHandle()); // specify the program to use 

// set color TODO: only alpha component works, text is always black #BUG 
float[] color = {red, green, blue, alpha}; 
GLES20.glUniform4fv(mColorHandle, 1, color , 0); 
GLES20.glEnableVertexAttribArray(mColorHandle); 

GLES20.glActiveTexture(GLES20.GL_TEXTURE0); // Set the active texture unit to texture unit 0 

GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); // Bind the texture to this unit 

// Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0 
GLES20.glUniform1i(mTextureUniformHandle, 0); 
//Log.i("error", "glerror3: " + GLES20.glGetError()); 
} 

編輯

這是設置程序的類:

public class BatchTextProgram extends Program { 

private static final AttribVariable[] programVariables = { 
    AttribVariable.A_Position, AttribVariable.A_TexCoordinate, AttribVariable.A_MVPMatrixIndex 
}; 

private static final String vertexShaderCode = 
     "uniform mat4 u_MVPMatrix[24];  \n"  // An array representing the combined 
                // model/view/projection matrices for each sprite 

     + "attribute float a_MVPMatrixIndex; \n" // The index of the MVPMatrix of the particular sprite 
     + "attribute vec4 a_Position;  \n"  // Per-vertex position information we will pass in. 
     + "attribute vec2 a_TexCoordinate;\n"  // Per-vertex texture coordinate information we will pass in 
     + "varying vec2 v_TexCoordinate; \n" // This will be passed into the fragment shader. 
     + "void main()     \n"  // The entry point for our vertex shader. 
     + "{        \n" 
     + " int mvpMatrixIndex = int(a_MVPMatrixIndex); \n" 
     + " v_TexCoordinate = a_TexCoordinate; \n" 
     + " gl_Position = u_MVPMatrix[mvpMatrixIndex] \n"  // gl_Position is a special variable used to store the final position. 
     + "    * a_Position; \n"  // Multiply the vertex by the matrix to get the final point in 
               // normalized screen coordinates. 
     + "}        \n";  


private static final String fragmentShaderCode = 
     "uniform sampler2D u_Texture;  \n" // The input texture. 
     + "precision mediump float;  \n"  // Set the default precision to medium. We don't need as high of a 
     // precision in the fragment shader. 
     + "uniform vec4 u_Color;   \n" 
     + "varying vec2 v_TexCoordinate; \n" // Interpolated texture coordinate per fragment. 

     + "void main()     \n"  // The entry point for our fragment shader. 
     + "{        \n" 
     + " gl_FragColor = texture2D(u_Texture, v_TexCoordinate).w * u_Color;\n" // texture is grayscale so take only grayscale value from 
                        // it when computing color output (otherwise font is always black) 
     + "}        \n"; 

@Override 
public void init() { 
    super.init(vertexShaderCode, fragmentShaderCode, programVariables); 
} 

} 
+0

我被困在相同的問題,你有沒有找到解決辦法?我使用的是Texample2代碼,但看起來這是代碼中的一個錯誤。 – neur0tic

回答

1

您的邏輯被破壞。如果u_Color是統一的,則它不能是頂點屬性。修復。

+0

我在上面添加了着色器代碼。你是不是要說把''uniform vec4 u_Color''換成別的東西,比如'attribute vec4 u_Color'。那麼我將如何更改'mColorHandle'賦值? (我嘗試了幾件沒有運氣的東西。) – eonor

+0

我不是說改變幾行。您似乎並不知道OpenGL中的頂點屬性和統一之間的區別。對它們進行一些研究,你會很快理解爲什麼我說「你的邏輯被破壞了」。 –