2016-05-02 12 views
1

オブジェクトをy軸上で動かすことができないようですが、現在の位置に留まります。私は動作するようには思えないし、私は私が行方不明です何段階かわからないしかしOpenGL Androidオブジェクトをy軸上に移動2D

Matrix.translateM() 

を使用することを知っている私の現在の知識に

。どんな助けでも大歓迎です。次のように私のレンダラで

はOnDrawFrame方法は次のとおりです。

public class Circle{ 
public float[] mModelMatrix = new float[16]; 
public FloatBuffer mVertexBuffer; 
public float vertices[] = new float[364 * 3]; 
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f }; 
public float ofset = 0; 
private final String vertexShaderCode = 
     // This matrix member variable provides a hook to manipulate 
     // the coordinates of the objects that use this vertex shader 
     "uniform mat4 uMVPMatrix;" + 
       "attribute vec4 vPosition;" + 
       "void main() {" + 
       // the matrix must be included as a modifier of gl_Position 
       // Note that the uMVPMatrix factor *must be first* in order 
       // for the matrix multiplication product to be correct. 
       " gl_Position = uMVPMatrix * vPosition;" + 
       "}"; 

private final String fragmentShaderCode = 
     "precision mediump float;" + 
       "uniform vec4 vColor;" + 
       "void main() {" + 
       " gl_FragColor = vColor;" + 
       "}"; 

private final FloatBuffer vertexBuffer; 
private final int mProgram; 
private int mPositionHandle; 
private int mColorHandle; 
private int mMVPMatrixHandle; 

// number of coordinates per vertex in this array 
static final int COORDS_PER_VERTEX = 3; 
static float triangleCoords[] = { 
     // in counterclockwise order: 
     0.0f, 0.622008459f, 0.0f, // top 
     -0.5f, -0.311004243f, 0.0f, // bottom left 
     0.5f, -0.311004243f, 0.0f // bottom right 
}; 
private final int vertexCount = triangleCoords.length/COORDS_PER_VERTEX; 
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex 

    public Circle(){ 
     vertices[0] = 0; 
     vertices[1] = 0; 
     vertices[2] = 0; 

     for(int i =1; i < 364; i++){ 
      vertices[(i * 3)+ 0] = (float) (0.1 * Math.cos((3.14/180) * (float)i)+ofset); 

      vertices[(i * 3)+ 1] = (float) (0.059 * Math.sin((3.14/180) * (float)i)+ofset); 
      vertices[(i * 3)+ 2] = 0; 
     } 
     // initialize vertex byte buffer for shape coordinates 
     ByteBuffer bb = ByteBuffer.allocateDirect(
       // (number of coordinate values * 4 bytes per float) 
       vertices.length * 4); 
     // use the device hardware's native byte order 
     bb.order(ByteOrder.nativeOrder()); 

     // create a floating point buffer from the ByteBuffer 
     vertexBuffer = bb.asFloatBuffer(); 
     // add the coordinates to the FloatBuffer 
     vertexBuffer.put(vertices); 
     // set the buffer to read the first coordinate 
     vertexBuffer.position(0); 

     // prepare shaders and OpenGL program 
     int vertexShader = ImpulseRushRenderer.loadShader(
       GLES20.GL_VERTEX_SHADER, vertexShaderCode); 
     int fragmentShader = ImpulseRushRenderer.loadShader(
       GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); 

     mProgram = GLES20.glCreateProgram();    // create empty OpenGL Program 
     GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program 
     GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program 
     GLES20.glLinkProgram(mProgram);     // create OpenGL program executables 
    } 

public void draw(float[] mvpMatrix) { 
    // Add program to OpenGL environment 
    GLES20.glUseProgram(mProgram); 

    // get handle to vertex shader's vPosition member 
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 

    // 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); 

    // get handle to fragment shader's vColor member 
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); 

    // Set color for drawing the triangle 
    GLES20.glUniform4fv(mColorHandle, 1, color, 0); 

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


    // Apply the projection and view transformation 
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); 

    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_FAN, 0, 364); 

    // Disable vertex array 
    GLES20.glDisableVertexAttribArray(mPositionHandle); 

} 

}

私はステップバイステップで見つけることができないよう、次のように

@Override 
public void onDrawFrame(GL10 glUnused) { 
    // Clear the rendering surface. 
    glClear(GL_COLOR_BUFFER_BIT); 
    // Apply transformation, start with translation 

    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 
    // Calculate the projection and view transformation 
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); 
    // Create a rotation transformation for the triangle 
    long time = SystemClock.uptimeMillis() % 4000L; 
    float mAngle = 0.090f * ((int) time); 
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f); 



    Matrix.orthoM(mMatrix, 0, -1, 1, -1, 1, -1, 1); 

    // Combine Rotation and Translation matrices 
    mTempMatrix = mModelMatrix.clone(); 
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0); 

    // Combine the model matrix with the projection and camera view 
    mTempMatrix = mMVPMatrix.clone(); 
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0); 
    Matrix.orthoM(mMatrix, 0, -1, 1, -1, 1, -1, 1); 
    mCircle.draw(mMatrix); 

    Matrix.setIdentityM(mCircle.mModelMatrix, 0); 
    Matrix.translateM(mCircle.mModelMatrix, 0, 50.5f, 5.7f, 5.7f); 

    //mTriangle.draw(mMatrix); 

} 

マイサークルクラスがあります私が何か重要なことを逃していると仮定していると仮定しています。私はRotateを以前に働かせましたが、翻訳を稼働させようとする運がない。

+0

あなたは全く使用されていないマトリックスで翻訳を使用しています。または、オンdrawメソッドのmCircle.mModelMatrixとmModelMatrixの間に何らかの関係がありますか? –

+0

いいえ、私は両方で1つを作成しましたが、mModelMatrixを使用して翻訳するのは少し曇っています。 – L1ghtk3ira

+0

あなたが翻訳している行列が使われていない場合でも、それを翻訳するときに変更を見ることは期待できません。 –

答えて

1

あなたはあなたのマトリックスと一貫性があり、いつ使用するかを知る必要があります。実際に投稿したコードでは、使用されていない行列が実際に翻訳されたので、変更はありませんでした。

通常、モデル、ビュー、および投影である3つの行列の積を意味するMVP行列システムが使用されます。これらはそれぞれ、描画されたオブジェクトの変形の一部を表しており、最適化するまでは、アプリケーションとシェーダの両方で3つすべてを使用することをお勧めします。照明などの一部のシステムでは、とにかくシェーダのためにそれらを分離する必要があります。

したがって、モデル行列はシーンのモデルを表す行列です。アイデンティティを使用して、回転やスケールなしで中央に配置する必要があります。それを修正して、シーンのモデル位置を変換するなどの変更を行います。各モデルが独自のモデル・マトリックスを持っていることは意味があります(ただし、投影とビューはありません)。

ビューマトリックスは、シーン内のユーザーまたはカメラの位置を表します。ほとんどの場合、ルックアップ手順を使用して、位置、探している場所、アップベクトルを与えます。ほとんどの場合、これらのうちの1つしかありませんが、分割画面などの場合はさらに多くなることがあります。

投影行列は、シーンの座標系および/または投影を定義するために使用されます。ここで最も一般的なのはorthoまたはfrustumです。彼らは見ると非常に似ており、基本的に1つは2Dのために使用され、もう1つは3Dのために使用されます。この行列は、「表示」行列と密接に使用されます。

したがって、3つすべてを使用すると、かなり簡単になります。ビューが生成されたり、ビューフレームが変更されたら、投影行列を設定する必要があります。ビー・マトリックスは、シーンを見回したり移動したりするときに変更する必要があります。また、描画行列ごとにモデル行列を設定する必要があります。それらのすべてが少なくとも初めからアイデンティティに設定されていることを確認してください。そうしないと、操作のどれもそれらに対して機能しません。

これがいくつかクリアされることを願っています。

+0

私はまだ少し混乱しています。私は円クラスでmmatrixを使用しています。だから、私はmcircle.mModelMatrix isnを使用してそれを使用している?レンダラークラスのメソッドdraw私はそれらを使用してxとyの座標に移動するよう指示します。だから私はちょっと混乱していますが、その実装方法に何が間違っていますか?あなたの説明から、私は何とかオブジェクトがある場所の座標を持つ必要があることを知っています。 – L1ghtk3ira

+0

いいえ、この行: "GLES20.glUniformMatrix4fv(mMVPMatrixHandle、1、false、mvpMatrix、0);"は、行列を設定し、メソッドパラメータとして受け取ったmvpMatrixを使用し、円が保持する実際の行列によって変更されません。 –

+0

よろしくお願いします。そこで私はmCirvle.mModelMatrixを使用します。今はサークル用ですだから今それを動かすために翻訳を適用する前に円の現在の位置にmModelMatrixを設定する必要がありますか?もう一度おねがいします – L1ghtk3ira

関連する問題