2016-08-24 45 views
0

我試圖讓紋理顯示在由三角形風扇製成的正方形上,紋理由Canvas製成。 主要顏色只是黃色,其內部繪製了一個較小的框,但最終的紋理只是純黃色。Android:紋理顯示爲純色

黃色方沒有紋理(圖像)

enter image description here

片段shadder:

public static final String fragmentShaderCode_TEXTURED = 
       "precision mediump float;" + 
       "varying vec2 v_texCoord;" + 
       "uniform sampler2D s_texture;" + 
       "void main() {" + 
       //"gl_FragColor = vColor;"+ 
       " gl_FragColor = texture2D(s_texture, v_texCoord);" + 
       "}"; 

紋理生成:

public static int loadGLTexture(String s){ 
    Rect r = new Rect(); 
    ThreadDat.get().paint.getTextBounds(s, 0, 1, r); //get string dimensions, yeilds 8x9 pxls 
    Bitmap bitmap = Bitmap.createBitmap(bestSize(r.width()),bestSize(r.height()), Bitmap.Config.ARGB_8888); 
    //example size is 16x16pxls 
    Log.i("TextureSize", r.width() + " " + r.height()); 
    Canvas c = new Canvas(bitmap); 

    //some temporary test code setting the background yellow 
    //Paint colors are stored per thread, only one right now 
    ThreadDat.get().paint.setARGB(255, 255, 255, 0); 
    c.drawRect(0, 0, c.getWidth(), c.getHeight(), ThreadDat.get().paint); 
    //type the letter, in this case "A" in blue 
    ThreadDat.get().paint.setARGB(255, 0, 0, 255); 
    ThreadDat.get().paint.setTypeface(Typeface.create("Consolas", Typeface.NORMAL)); 
    c.drawText(s.charAt(0) + "", 0, 0, ThreadDat.get().paint); 
    //draw another square that is half width and height, should be Blue 
    c.drawRect(0, 0, c.getWidth()/2, c.getHeight()/2, ThreadDat.get().paint); 
    return loadTexture(bitmap); 
} 

繪製代碼:

@Override 
public void draw() { 
    //clearing any error to check if program has an error 
    GLES20.glGetError(); 
    //get the compiled shader for textured shapes 
    int prgm = MyGLRenderer.getSTRD_TXTR_SHDR(); 
    GLES20.glUseProgram(prgm); 
    //check for new errors and log to logcat (nothing) 
    MyGLRenderer.logError(); 

    //setup projection view matrix 
    float[] scratch = new float[16]; 
    Matrix.setIdentityM(scratch, 0); 
    Matrix.multiplyMM(scratch, 0, MyGLRenderer.getmMVPMatrix(), 0, scratch, 0); 
    //apply translations to matrix 
    Matrix.translateM(scratch, 0, xOffset, yOffset, zOffset); 
    Matrix.setRotateEulerM(scratch, 0, yaw, pitch, roll); 


    //get vPosition variable handle from chosen shader 
    mPosHandle = GLES20.glGetAttribLocation(prgm, "vPosition"); 
    GLES20.glEnableVertexAttribArray(mPosHandle); 

    GLES20.glVertexAttribPointer(mPosHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, 
      false, VERTEX_STRIDE, vertexBuffer); 

    ////pass color data (set to white) 
    //mColorHandle = GLES20.glGetUniformLocation(prgm, "vColor"); 
    //GLES20.glUniform4fv(mColorHandle, 1, color, 0); 


    //use texture0 
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
    //use texture from -> int textureID = MyGLRenderer.loadGLTexture("A"); 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID); 
    //get handel for "uniform sampler2D s_texture;" to set value 
    int txtureHandle = GLES20.glGetUniformLocation(prgm, "s_texture"); 
    GLES20.glUniform1i(txtureHandle, 0); //set s_texture to use binded texture 0 


    //pass in texture coords (u,v/s,t) 
    int textureCoordHndl = GLES20.glGetAttribLocation(prgm, "a_texCoord"); 
    GLES20.glVertexAttribPointer(textureCoordHndl, 2/*size, 2 points per vector*/, 
      GLES20.GL_FLOAT, false, 0, textureBuffer); 

    //pass in the model view projection matrix 
    mMVPMatrixHandle = GLES20.glGetUniformLocation(prgm, "uMVPMatrix"); 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, scratch, 0); 



    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertex_count); 

    GLES20.glDisableVertexAttribArray(mPosHandle); 
    MyGLRenderer.logError(); 
} 

我使用相同的座標設定爲這個example:

頂點使用方嘗試:

{   0, 0, 0, //bottom left 
      0, height, 0, //topLeft 
      width, 0, 0, // bottom Right 
      width, height, 0)}; //topRight 

紋理座標:

 0.0f, 1.0f,  // top left  (V2) 
     0.0f, 0.0f,  // bottom left (V1) 
     1.0f, 1.0f,  // top right (V4) 
     1.0f, 0.0f  // bottom right (V3) 

Similar Issue

+0

像其他答案一樣,這很可能是您紋理UV的問題。也請將您的頂點着色器至少只發送發送UV的部分變化。 – codetiger

+0

[頂點着色器](http://pastebin.com/SybkZXXX) – TheIncgi

回答

1

這確實聲音就像紋理c有問題一樣oordinates。由於整個事情是黃色的,我會懷疑片段着色器中的v_texCoord總是(0,0),所以第一個紋理像素正在重複。

由於顏色被繪製,紋理本身似乎是確定的。沒有紋理,你很可能會看到一個黑色的矩形。

無論如何,爲了解決這些問題,您需要在調試,測試中有點創新。爲了測試座標,使用gl_FragColor = vec4(v_texCoord.x, v_texCoord.y, .0, 1.0);。這應該輸出一個梯度矩形,其中左上角是黑色,右上角是紅色,左下角是綠色。如果你沒有看到這個結果,那麼你的紋理座標是不正確的。在這種情況下,首先檢查varying是否從頂點着色器正確連接。您可以在頂點着色器中使用v_texCoord = vec2(1.0, 0.0),並且結果應該是紅色的矩形(假設您仍然在片段着色器中具有先前的測試)。如果矩形是紅色的,那麼問題很可能出現在您的手柄中,而不是着色器中(否則變量設置不正確,可能命名不匹配)。檢查手柄textureCoordHndl的價值是多少。如果這是一個負值,那麼手柄沒有連接。這很可能是由於命名不匹配。

進一步檢查:

你缺少的屬性紋理啓用座標GLES20.glEnableVertexAttribArray(textureCoordHndl);。請記住,在使用它們之前,必須啓用每個屬性。

+0

替換片段着色器框中的行爲黑色。 替換頂點着色器框中的線條就像預測的那樣是紅色的。 textureCoordHndl在登錄時爲1。 – TheIncgi

+0

OMG,你是否只錯過了屬性的啓用: glEnableVertexAttribArray(txtureHandle)? –

+0

textureCoordHndl和是! 工作! 謝謝! :D (字母不想出現,但小藍盒現在在那裏) – TheIncgi