为什么它不是立方体?

时间:2012-08-01 07:44:26

标签: android matrix rotation opengl-es-2.0 cube

我正在开发一个简单的项目,它可以在OpenGLES 2.0 android 4.1中打开一个立方体并旋转它。但绘制的形状不应该t cube. and it is rotating a axis that i wasn。我想它在相机空间中旋转X,Y轴。谢谢你的建议。

    public float mAngleX;
public float mAngleY;
private Quadrat quad;
private final float[] MVPMatrix = new float[16]; 
private final float[] projectionMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] rotationMatrix = new float[16];

public MyRenderer(){
    mAngleX = 0;
    mAngleY = 0;
}

//  @Override
public void onSurfaceCreated(GL10 unused, EGLConfig config){
    // Set the background frame color
    GLES20.glClearColor(0.5f, 0.2f, 0.1f, 1.0f);

    Matrix.setLookAtM(mVMatrix, 0, 0.0f, 0.0f, -5.0f, 0f, 0f, 1f, 0f, 1.0f, 0.0f);

    quad = new Quadrat();     
}           

//  @Override
public void onSurfaceChanged(GL10 unused, int width, int height){
    GLES20.glViewport(0, 0, width, height);        
    float ratio = (float) width / height;
    Matrix.perspectiveM(projectionMatrix, 0, 45.0f, ratio, 1, 9);
}

//@Override 
public void onDrawFrame(GL10 unused) {        
    // Draw background color
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);


    Matrix.setRotateM(rotationMatrix, 0, mAngleX, 0.0f, 1.0f, 0.0f);
    Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0);
    Matrix.setRotateM(rotationMatrix, 0, mAngleY, 1.0f, 0.0f, 0.0f);
    Matrix.multiplyMM(MVPMatrix, 0, rotationMatrix, 0, MVPMatrix, 0);

    Matrix.multiplyMM(MVPMatrix, 0, projectionMatrix, 0, mVMatrix, 0);

    quad.draw(MVPMatrix, mVMatrix);     
}

public static int loadShader(int type, String shaderCode){
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}
}

并且quadrat类在下面。

class Quadrat {
private final String vertexShaderCode =
        "uniform mat4 uMVPMatrix;\n" +
        "uniform mat4 uMVMatrix;\n" +
        "uniform vec3 uLightPos;\n" +

        "attribute vec4 aPosition;\n" +
        "attribute vec4 aColor;\n" +
        "attribute vec4 aNormal;\n" +

        "varying vec4 vColor;\n" +

        "void main() {\n" +
            "gl_Position = uMVPMatrix * aPosition;\n" +
        "}\n";

private final String fragmentShaderCode =
        "precision mediump float;\n" +

        "varying vec4 vColor;\n" +

        "void main() {\n" +
            "gl_FragColor = vec4(1.0, 0.9, 0.4, 1.0);\n" +
        "}\n";

private final FloatBuffer vertexBuffer;
private final int mProgram;
private int mPositionHandle;
private int muMVPMatrixHandle;
private int muLightPosHandle;
private int muMVMatrixHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float quadrateCoords[] = { // in counterclockwise order:
    // front
    -0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, 0.5f,
    // back
    -0.5f, -0.5f, -0.5f,
     0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, -0.5f,
     0.5f, 0.5f, -0.5f,
    // left
    -0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, -0.5f,
    // right  
     0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, -0.5f,
     0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, -0.5f,  
    // up  
    -0.5f, 0.5f, 0.5f,
     0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, -0.5f,
     0.5f, 0.5f, -0.5f,
    // bottom  
    -0.5f, -0.5f, 0.5f,
     0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, -0.5f,
     0.5f, -0.5f, -0.5f
};


private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

public Quadrat() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(quadrateCoords.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(quadrateCoords);
    vertexBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = MyRenderer.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, float[] mVMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

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

    // Enable a handle to the triangle vertices
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(mPositionHandle);

 GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,     vertexStride, vertexBuffer);

vertexStride,vertexBuffer2);

vertexStride,vertexBuffer3);

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mvpMatrix, 0);        

    muMVMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVMatrix");
    GLES20.glUniformMatrix4fv(muMVMatrixHandle, 1, false, mVMatrix, 0);        

    muLightPosHandle = GLES20.glGetUniformLocation(mProgram, "uLightPos");
    GLES20.glUniform3f(muLightPosHandle, 10.0f, 10.0f, 15.0f);

    // Front
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

    // Back
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 4, 4);

    // Left
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 8, 4);

    // Right
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 12, 4);

    // Top
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 16, 4);

    // Right
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 20, 4);

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

1 个答案:

答案 0 :(得分:0)

安装软件包Api demos并查看

中的示例Touch Rotate
Api demos > Graphics > OpenGL ES > Touch Rotate

您可以通过

访问源代码
android-sdk-linux/samples/android-version/ApiDemos/