2013-04-05 161 views
1

編輯代碼加入,請參見下面正確的頂點着色器代碼? OpenGL ES 2.0的

編輯2個 - 截圖來自包括在底部裝置與解釋和說明

編輯3 - 新的代碼添加

我有2個課程,一個渲染和一個自定義'四'級。

我的這些聲明在一流水平在我的渲染器類:

final float[] mMVPMatrix = new float[16]; 
final float[] mProjMatrix = new float[16]; 
final float[] mVMatrix = new float[16]; 

在我onSurfaceChanged方法我有:

@覆蓋 公共無效onSurfaceChanged(GL10 GL,詮釋寬度,高度INT ){

GLES20.glViewport(0, 0, width, height); 

    float ratio = (float) width/height; 
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); 

} 

和....

public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    // TODO Auto-generated method stub 

    myBitmap = BitmapFactory.decodeResource(curView.getResources(), R.drawable.box); 

     //Create new Dot objects 

     dot1 = new Quad(); 
     dot1.setTexture(curView, myBitmap); 
     dot1.setSize(300,187); //These numbers are the size but are redundant/not used at the moment. 

     myBitmap.recycle(); 


      //Set colour to black 
      GLES20.glClearColor(0, 0, 0, 1); 

} 

最後,來自這個班,onDrawFrame:

@Override 
public void onDrawFrame(GL10 gl) { 
    // TODO Auto-generated method stub 

    //Paint the screen the colour defined in onSurfaceCreated 
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); 

    // Set the camera position (View matrix) so looking from the front 
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 

    // Combine 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); 

    dot1.rotateQuad(0,0,45, mMVPMatrix); //x,y,angle and matrix passed in 

} 

然後,在我的四等級:

該聲明在類級別:

 private float[] mRotationMatrix = new float[16];   
    private final float[] mMVPMatrix = new float[16]; 
    private final float[] mProjMatrix = new float[16]; 
    private final float[] mVMatrix = new float[16]; 
    private int mMVPMatrixHandle; 
    private int mPositionHandle; 
    private int mRotationHandle; 
//Create our vertex shader 
    String strVShader = 
       "uniform mat4 uMVPMatrix;" + 
       "uniform mat4 uRotate;" + 
       "attribute vec4 a_position;\n"+ 
       "attribute vec2 a_texCoords;" + 
       "varying vec2 v_texCoords;" + 
       "void main()\n" + 
       "{\n" + 
     //  "gl_Position = a_position * uRotate;\n"+ 
     //   "gl_Position = uRotate * a_position;\n"+ 
       "gl_Position = a_position * uMVPMatrix;\n"+ 
     //  "gl_Position = uMVPMatrix * a_position;\n"+ 
        "v_texCoords = a_texCoords;" + 
       "}"; 

    //Fragment shader 

    String strFShader = 
     "precision mediump float;" + 
     "varying vec2 v_texCoords;" + 
     "uniform sampler2D u_baseMap;" + 
     "void main()" + 
     "{" + 
     "gl_FragColor = texture2D(u_baseMap, v_texCoords);" + 
     "}"; 

然後設置紋理的方法(唐不過認爲這與這個問題有關!!)

public void setTexture(GLSurfaceView view, Bitmap imgTexture){ 
     this.imgTexture=imgTexture; 

     iProgId = Utils.LoadProgram(strVShader, strFShader); 
     iBaseMap = GLES20.glGetUniformLocation(iProgId, "u_baseMap"); 
       iPosition = GLES20.glGetAttribLocation(iProgId, "a_position"); 
     iTexCoords = GLES20.glGetAttribLocation(iProgId, "a_texCoords"); 
     texID = Utils.LoadTexture(view, imgTexture); 

    } 

最後,我的'rotateQuad'方法(目前應該繪製並旋轉四邊形)。

public void rotateQuad(float x, float y, int angle, float[] mvpMatrix){ 

Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, 0.1f); 

// Matrix.translateM(mRotationMatrix, 0, 0, 0, 0); //Removed temporarily 

// Combine the rotation matrix with the projection and camera view 
    Matrix.multiplyMM(mvpMatrix, 0, mRotationMatrix, 0, mvpMatrix, 0); 

float[] vertices = { 

     -.5f,.5f,0, 0,0, 
     .5f,.5f,0, 1,0, 
     -.5f,-.5f,0, 0,1, 
     .5f,-.5f,0, 1,1 

     }; 

vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); 
vertexBuf.put(vertices).position(0); 

//Bind the correct texture 
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texID);  
//Use program 
GLES20.glUseProgram(iProgId); 
// get handle to shape's transformation matrix 
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix"); 
    // Apply the projection and view transformation 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); 
    // get handle to shape's rotation matrix 
mRotationHandle = GLES20.glGetUniformLocation(iProgId, "uRotate"); 
    // Apply the projection and view transformation 
    GLES20.glUniformMatrix4fv(mRotationHandle, 1, false, mRotationMatrix, 0); 

//Set starting position for vertices 
vertexBuf.position(0); 
//Specify attributes for vertex 
GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf); 
//Enable attribute for position 
GLES20.glEnableVertexAttribArray(iPosition); 
//Set starting position for texture 
vertexBuf.position(3); 
//Specify attributes for vertex 
GLES20.glVertexAttribPointer(iTexCoords, 2, GLES20.GL_FLOAT, false, 5 * 4, vertexBuf); 
//Enable attribute for texture 
GLES20.glEnableVertexAttribArray(iTexCoords); 
//Draw it 
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 
} 

編輯2.

這是我四是在屏幕的中心繪製。沒有旋轉。

enter image description here

這是相同的四在45度與代碼旋轉 「GL_POSITION = a_position * uMVPMatrix;」 +在我的頂點着色器(它現在來自不同的項目,所以着色器變量是a_position而不是vPosition),它看起來是正確的!

enter image description here

但是,這是相同的四在+45度的2個着色器變量旋轉切換(所以他們讀「GL_POSITION = uMVPMatrix * a_position;」 - 你可以看到,這是不完全正確。

還只是一個側面說明,你不能在這裏看到它作爲quare是對稱的,但每種方法也沿相反方向等....

enter image description here

的y幫助表示讚賞。

回答

1

這是不可能的,因爲我們不知道你傳遞給這兩個變量。

OpenGL是列主要格式,所以如果vPosition實際上是一個向量,uMVPMatrix是一個矩陣,那麼第一個選項是正確的,如果這是在你的着色器中。

如果這不是在你的着色器中,而是在你的程序代碼中,那麼沒有足夠的信息。

如果您使用的是第一個選項,但得到了意想不到的結果,那麼您可能無法正確計算矩陣或未傳遞正確的頂點。

+0

嗨@SebbyJohanns,我又已經編輯我更多的碼Q -is這還不夠的時候是發生在訴說?謝謝。 – Zippy 2013-04-05 15:52:08

+0

需要頂點shader代碼也是如此 – 2013-04-05 16:04:06

+0

如有需要,請多加謝謝 – Zippy 2013-04-05 16:08:50

0

通常在頂點着色器,你應該多在MVP的位置,也就是

GL_POSITION = uMVPMatrix * vPosition;

當你改變訂單這應該工作...

+0

你有什麼想法嗎?當我這樣做時,當我旋轉時,我的四邊形'扭曲'/'傾斜' – Zippy 2013-04-05 16:45:30

+0

當你以某種方式做事時?以及如何應用旋轉變換? – paulczak 2013-04-05 16:57:31

+0

也許可以你上傳的失真圖片?這通常只是一個以錯誤的順序應用模型轉換的問題... – paulczak 2013-04-05 17:03:00

0

感謝所有的幫助。

我設法追查問題(大部分)。我會展示我所做的。

這是以下行:

Matrix.multiplyMM(mvpMatrix, 0, mvpMatrix, 0, mRotationMatrix, 0); 

正如你看到的我是相乘的矩陣並將其存儲回一個,我是用乘法。

因此,我創建了一個名爲mvpMatrix2的新矩陣,並在其中存儲結果。然後將其傳遞給我的頂點着色器。

//Multiply matrices 
Matrix.multiplyMM(mvpMatrix2, 0, mvpMatrix, 0, mRotationMatrix, 0); 

//get handle to shape's transformation matrix 
mMVPMatrixHandle = GLES20.glGetUniformLocation(iProgId, "uMVPMatrix"); 

//Give to vertex shader variable 
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix2, 0); 

應用此之後,沒有失真(也,關於我在這裏的其他問題Using Matrix. Rotate in OpenGL ES 2.0我能翻譯四的中心)。我說'大部分',因爲然而,當我旋轉它時,它會向後旋轉(所以如果我說旋轉+45度(順時針),它實際上將四方形旋轉-45度(逆時針方向)

但希望,這將幫助任何人誰在未來有類似的問題。