GLES 2.0 Matrix.rotateM不适用于小于180度的角度值

时间:2014-06-20 06:51:47

标签: opengl-es opengl-es-2.0

我正在努力实现多次轮换。当我执行180度旋转时,代码工作正常,但是当角度值小于该值时,它执行平移或缩放,但不执行旋转。 请在此处提出错误或其他任何替代方法。

public class myRenderer implements Renderer {
    private int TexProgram;
    private int TexAPosLoc;
    private int TexAColorLoc;
    private int TexACoordLoc;
    private int UMVPLocation;
    private int USamplerLocation;
    private FloatBuffer VertexFB;
    private FloatBuffer ColorFB;
    private FloatBuffer TexFB;

    private float[] TexMatrix           =   new float[16];
    private float[] TexViewMatrix       =   new float[16];
    private float[] TexProjectionMatrix =   new float[16];
    private float[] TexMVPMatrix        =   new float[16];
    private int textureId;

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1);

    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        initPlane();

        float zNear = 0.1f;
        float zFar = 10;

        Matrix.setLookAtM(TexViewMatrix, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0);
    //  Matrix.setLookAtM(rm, rmOffset, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)
        Matrix.frustumM(TexProjectionMatrix, 0, -0.1f, 0.1f, -0.1f, 0.1f, zNear, zFar);
        TexProgram = loadProgram(_planeVertexShaderCode, _planeFragmentShaderCode);

        TexAPosLoc = GLES20.glGetAttribLocation(TexProgram, "aPosition");
        TexAColorLoc = GLES20.glGetAttribLocation(TexProgram, "aColor");
        TexACoordLoc = GLES20.glGetAttribLocation(TexProgram, "aCoord");
        UMVPLocation = GLES20.glGetUniformLocation(TexProgram, "uMVP");
        USamplerLocation = GLES20.glGetUniformLocation(TexProgram, "uSampler");

        int[] textures = new int[1];
        GLES20.glGenTextures(1, textures, 0);
        textureId = textures[0];

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); // GL_LINEAR
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    }

    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        Matrix.setIdentityM(TexMatrix, 0);
        for(int i =0; i<2;i++)
        {

                Matrix.multiplyMM(TexMVPMatrix, 0, TexViewMatrix, 0, TexMatrix, 0);
                Matrix.multiplyMM(TexMVPMatrix, 0, TexProjectionMatrix, 0, TexMVPMatrix, 0);
                GLES20.glUseProgram(TexProgram);
                GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
                GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
                GLES20.glUniform1i(USamplerLocation, 0);

                GLES20.glUniformMatrix4fv(UMVPLocation, 1, false, TexMVPMatrix, 0);
                GLES20.glVertexAttribPointer(TexAPosLoc, 3, GLES20.GL_FLOAT, false, 12, VertexFB);
                GLES20.glEnableVertexAttribArray(TexAPosLoc);
                GLES20.glVertexAttribPointer(TexAColorLoc, 4, GLES20.GL_FLOAT, false, 16, ColorFB);
                GLES20.glEnableVertexAttribArray(TexAColorLoc);
                GLES20.glVertexAttribPointer(TexACoordLoc, 2, GLES20.GL_FLOAT, false, 8, TexFB);
                GLES20.glEnableVertexAttribArray(TexACoordLoc);
                GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3);

                Matrix.setIdentityM(TexMatrix, 0);  
        //      Matrix.translateM(TexMatrix,0,0,1,0);
                Matrix.rotateM(TexMatrix,0,180.0f,1,0,0);
           //           Matrix.rotateM(TexMatrix,0,90.0f,1,0,0);

        }
    //  System.gc();

    }

    private void initPlane() {
        float[] planeVFA = {
                0.0f, 0.866f, 0.0f, // top
                -0.5f, 0f, 0.0f, // bottom left
                0.5f, 0f, 0.0f // bottom right
        };

        float[] planeTFA = {
                1,1, 0,1, 1,0, 0,0
        };

        float[] planeCFA = {
                1,0,1,1, 
                1,1,0,1, 
                0,1,1,1
        };

        ByteBuffer planeVBB = ByteBuffer.allocateDirect(planeVFA.length * 4);
        planeVBB.order(ByteOrder.nativeOrder());
        VertexFB = planeVBB.asFloatBuffer();
        VertexFB.put(planeVFA);
        VertexFB.position(0);

        ByteBuffer planeTBB = ByteBuffer.allocateDirect(planeTFA.length * 4);
        planeTBB.order(ByteOrder.nativeOrder());
        TexFB = planeTBB.asFloatBuffer();
        TexFB.put(planeTFA);
        TexFB.position(0);

        ByteBuffer planeCBB = ByteBuffer.allocateDirect(planeCFA.length * 4);
        planeCBB.order(ByteOrder.nativeOrder());
        ColorFB= planeCBB.asFloatBuffer();
        ColorFB.put(planeCFA);
        ColorFB.position(0);
    }   //  initPlane() ends

    private int loadShader(int type, String source)  {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, source);
        GLES20.glCompileShader(shader);
        return shader;
    }   //  loadShader() ends

    private int loadProgram(String vertexShaderCode, String fragmentShaderCode) {
        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        int program = GLES20.glCreateProgram();
        GLES20.glAttachShader(program, vertexShader);
        GLES20.glAttachShader(program, fragmentShader);
        GLES20.glLinkProgram(program);
        return program;
    }   //  loadProgram() ends

    private final String _planeVertexShaderCode = 
            "attribute vec4 aPosition;          \n"
        +   "attribute vec2 aCoord;             \n"
        +   "attribute vec4 aColor;             \n"
        +   "varying vec2 vCoord;               \n"
        +   "varying vec4 vColor;               \n"
        +   "uniform mat4 uMVP;                 \n"
        +   "void main() {                      \n"
        +   " gl_Position = uMVP * aPosition;   \n"
        +   " vCoord = aCoord;                  \n"
        +   " vColor = aColor;                  \n"
        +   "}                                  \n";

    private final String _planeFragmentShaderCode = 
            "#ifdef GL_FRAGMENT_PRECISION_HIGH              \n"
        +   "precision highp float;                         \n"
        +   "#else                                          \n"
        +   "precision mediump float;                       \n"
        +   "#endif                                         \n"
        +   "varying vec2 vCoord;                           \n"
        +   "varying vec4 vColor;                           \n"
        +   "uniform sampler2D uSampler;                    \n"
        +   "void main() {                                  \n"
        +   " vec4 textureColor;                            \n"
        +   " textureColor = texture2D(uSampler,vCoord);    \n"
        +   " gl_FragColor = vColor + textureColor;         \n"
        +   "}                                              \n";

}   // myRenderer() ends

此外,请建议我如何改进或优化代码。 纹理和采样器的使用是有意的。

0 个答案:

没有答案