2012-08-04 28 views
0

我正在嘗試爲使用GLES20的android應用程序編寫自己的着色器類和矩陣處理類。但是,現在我只能獲取背景顏色。編寫自定義OpenGL ES 2.0着色器/ MatrixHandler,變得空白屏幕

我一直拉着我的頭髮這幾天。我想比較我的類和GL10之間的矩陣操作,以確保它們吐出相同的結果,但是我不能在我的生活中使用api示例MatrixGrabber和MatrixTrackingGL來吐出除了單位矩陣之外的任何東西(儘管渲染正確)。

所以我打算髮布一些代碼,請讓我知道你是否看到任何可能成爲這兩個類的問題!

這裏是我MatrixHandler類:

import java.util.Stack; 

import android.opengl.Matrix; 

public class MatrixHandler 
{ 
    public static float[] mMVPMatrix = new float[16]; 
    public static float[] mProjMatrix = new float[16]; 
    public static float[] mViewMatrix = new float[16]; 
    public static float[] mRotationMatrix = new float[16]; 

    public static Stack<float[]> mvpStack = new Stack<float[]>(); 
    public static Stack<float[]> projStack = new Stack<float[]>(); 
    public static Stack<float[]> viewStack = new Stack<float[]>(); 
    public static Stack<float[]> rotationStack = new Stack<float[]>(); 

    public static void setViewIdentity() 
    { 
     Matrix.setIdentityM(mViewMatrix, 0); 
    } 

    public static void setProjIdentity() 
    { 
     Matrix.setIdentityM(mProjMatrix, 0); 
    } 

    public static void setViewport(float left, float right, float bottom, float top, float near, float far) 
    { 
     Matrix.frustumM(mProjMatrix, 0, 
       left, right, 
       bottom, top, 
       near, far 
      ); 
    } 

    public static void setLookAt(
      float eyeX, float eyeY, float eyeZ, 
      float posX, float posY, float posZ, 
      float upX, float upY, float upZ 
     ) 
    { 
     Matrix.setLookAtM(
       mViewMatrix, 0, 

       eyeX, eyeY, eyeZ, 
       posX, posY, posZ, 
       upX, upY, upZ 
      ); 

     //Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mViewMatrix, 0); 
    } 

    public static void translate(float x, float y, float z) 
    { 
     Matrix.translateM(mViewMatrix, 0, 
       x, y, z 
      ); 
     Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mMVPMatrix, 0); 
    } 

    public static void rotate(float a, float x, float y, float z) 
    { 
     Matrix.rotateM(mRotationMatrix, 0, 
       a, x, y, z 
      ); 
     Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0); 
    } 

    public static void pushMatrix() 
    { 
     mvpStack.push(mMVPMatrix); 
     projStack.push(mProjMatrix); 
     viewStack.push(mViewMatrix); 
     rotationStack.push(mRotationMatrix); 
    } 

    public static void popMatrix() 
    { 
     mMVPMatrix = mvpStack.pop(); 
     mProjMatrix = projStack.pop(); 
     mViewMatrix = viewStack.pop(); 
     mRotationMatrix = rotationStack.pop(); 
    } 

    public static void printMatrix(String label, float[] m) 
    { 
     System.err.print(label + " : {"); 
     for(float i : m) 
     { 
      System.err.print(i + ", "); 
     } 
     System.err.println("}"); 
    } 
} 

這裏是我的Shader類:

import java.nio.FloatBuffer; 

import com.bradsproject.appName.MatrixHandler; 

import android.opengl.GLES20; 

public class Shader 
{ 
    private static int mProgram; 

    static int maPositionHandle; 
    static int maColorHandle; 
    static int maTextureHandle; 

    static int muMVPMatrixHandle; 

    static int maTexture; 

    private final static String mVertexShader = 
     "uniform mat4 uMVPMatrix;" + 
     "attribute vec3 aPosition;" + 
     "attribute vec2 aTextureCoord;" + 
     "attribute vec4 aColor;" + 
     "varying vec4 vColor;" + 
     "varying vec2 vTextureCoord;" + 
     "void main() {" + 
     " gl_Position = uMVPMatrix * vec4(aPosition, 1.0);" + 
     " vTextureCoord = aTextureCoord;" + 
     " vColor = aColor;" + 
     "}"; 

    private final static String mFragmentShader = 
      "precision mediump float;" + 
      "varying vec2 vTextureCoord;" + 
      "uniform sampler2D sTexture;" + 
      "varying vec4 vColor;" + 
      "void main() {" + 
      " gl_FragColor = texture2D(sTexture, vTextureCoord);" + 
      "}"; 

    public static void init() 
    { 
     mProgram = createProgram(mVertexShader, mFragmentShader); 
     if (mProgram == 0) 
      return; 

     maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); 
     maColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor"); 
     maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord"); 

     maTexture = GLES20.glGetUniformLocation(mProgram, "sTexture"); 

     muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 
    } 

    public static void drawArrays(FloatBuffer mPosition, FloatBuffer mColor, FloatBuffer mTexture, int textureId, int mode) 
    { 
     GLES20.glUseProgram(mProgram); 

     mPosition.position(0); 
     GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mPosition); 
     GLES20.glEnableVertexAttribArray(maPositionHandle); 

     mColor.position(0); 
     GLES20.glVertexAttribPointer(maColorHandle, 4, GLES20.GL_FLOAT, false, 4 * 4, mColor); 
     GLES20.glEnableVertexAttribArray(maColorHandle); 

     mTexture.position(0); 
     GLES20.glVertexAttribPointer(maTextureHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexture); 
     GLES20.glEnableVertexAttribArray(maTextureHandle); 

     GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId); 
     GLES20.glUniform1i(maTexture, 0); 

     GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixHandler.mMVPMatrix, 0); 

     GLES20.glDrawArrays(mode, 0, mPosition.capacity()/3); 
    } 

    private static int createProgram(String vertexSource, String fragmentSource) 
    { 
     int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); 
     if (vertexShader == 0) 
     { 
      System.err.println("Failed to load vertex shader."); 
      return 0; 
     } 

     int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); 
     if (pixelShader == 0) 
     { 
      System.err.println("Failed to load fragment shader."); 
      return 0; 
     } 

     int program = GLES20.glCreateProgram(); 
     if (program != 0) 
     { 
      GLES20.glAttachShader(program, vertexShader); 
      checkGlError("glAttachShader"); 
      GLES20.glAttachShader(program, pixelShader); 
      checkGlError("glAttachShader"); 
      GLES20.glLinkProgram(program); 
      int[] linkStatus = new int[1]; 
      GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); 
      if (linkStatus[0] != GLES20.GL_TRUE) 
      { 
       GLES20.glDeleteProgram(program); 
       program = 0; 
      } 
     } 
     return program; 
    } 

    private static int loadShader(int shaderType, String source) 
    { 
     int shader = GLES20.glCreateShader(shaderType); 
     if (shader != 0) 
     { 
      GLES20.glShaderSource(shader, source); 
      GLES20.glCompileShader(shader); 
      int[] compiled = new int[1]; 
      GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); 
      if (compiled[0] == 0) 
      { 
       GLES20.glDeleteShader(shader); 
       shader = 0; 
      } 
     } 
     return shader; 
    } 

    private static void checkGlError(String op) 
    { 
     int error; 
     while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) 
     { 
      throw new RuntimeException(op + ": glError " + error); 
     } 
    } 
} 
+0

我居然發現了一些錯誤。我會稍後嘗試發佈新的源代碼。 – Brad 2012-08-07 05:46:14

回答

0

你缺少 「\ n」 末

避免這些矩陣堆棧,當你的代碼變得大於1000行代碼時,很快你會遇到那些沉重的東西它是openglgame或openglapp

對於速度使用常量,在着色器代碼局部變量和執行片段着色器

這裏面更多的計算是一種着色器設置

private static final String vertexShaderCodeLight = 
     "uniform vec4 uVPosition;     \n" 
    + "uniform mat4 uP;       \n" 
    + "void main(){        \n" 
    + " gl_PointSize = 15.0;      \n" 
    + " gl_Position = uP * uVPosition;   \n" 
    + "}           \n"; 
private static final String fragmentShaderCodeLight = 
     "#ifdef GL_FRAGMENT_PRECISION_HIGH   \n" 
    + "precision highp float;      \n" 
    + "#else          \n" 
    + "precision mediump float;     \n" 
    + "#endif          \n" 
    + "void main(){        \n" 
    + " gl_FragColor = vec4(1.0,1.0,1.0,1.0);  \n" 
    + "}           \n"; 
相關問題