我的Open GL视图与我的代码不匹配

时间:2014-06-25 16:39:18

标签: java android opengl-es

我在设置相机视图和投影视图时遇到问题。我已经多次检查了所有内容,并且我已经完成了许多不同的教程,但我仍然无法弄清楚为什么显示器与我的代码不匹配。我将眼睛视图设置为(0,0,-5),我看向(0,0,0)。我的三角形坐标设置为直角三角形,但执行时,我的三角形非常歪斜。我不知道它是我的相机视图还是我的投影视图,但有些事情是非常错误的。另外,我跟踪并复制了制作立方体的另一个教程中的代码。我的代码与教程相匹配,但我的立方体根本不是一个立方体,而是一个非常奇怪的2D形状。但是,我发布的代码中没有绘制多维数据集,所以我现在并不是真的担心这个问题。

这是我的代码:

class MyGLSurfaceView extends GLSurfaceView{

    public MyGLSurfaceView(Context context){
        super(context);
        MyRenderer=new MyGlRenderer();
        setEGLContextClientVersion(2);
        setRenderer(MyRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}

Triangle mTriangle;
Cube mCube;

public class MyGlRenderer implements GLSurfaceView.Renderer{

    private float[] mViewMatrix = new float[16];
    private float[] mMVPMatrix = new float[16];
    private float[] mProjectionMatrix = new float[16];
    private float[] mRotationMatrix=new float[16];
    @Override
    public void onSurfaceCreated(GL10 unused, EGLConfig config){
        //GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glClearDepthf(1f);

        mTriangle= new Triangle();
        mCube=new Cube();

    }
    @Override
    public void onDrawFrame(GL10 unused){

        float random1=(float)Math.random();
        float random2=(float)Math.random();
        float random3=(float)Math.random();
        GLES20.glClearColor(random1, random2, random3, 1.0f);

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

        float[] scratch = new float[16];
        long time = SystemClock.uptimeMillis() % 4000L;
        float angle = 0.090f * ((int) time);
        Matrix.setRotateM(mRotationMatrix, 0, angle, 1, 0, -1.0f);

        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -5, 0f, 0f, 0f, 0f, 2.0f, 0.0f);
        //Matrix.multiplyMM(scratch, 0, mProjectionMatrix, 0, mRotationMatrix, 0);
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
        mTriangle.draw(mMVPMatrix);
        //mCube.draw(scratch);
    }
    @Override
    public void onSurfaceChanged(GL10 unused, int width, int height){
        GLES20.glViewport(0,0,width,height);
        float ratio=(float) width/height;

        Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1f, 1, 25);
    }

}

这里是三角形代码:

    public class Triangle{

    private FloatBuffer vertexBuffer;
    static final int COORDS_PER_VERTEX=3;
    float triangleCoords[]={
            -1.0f,  1.0f, 0.0f, // top
            -1.0f, -1.0f, 0.0f, // bottom left
             1.0f, -1.0f, 0.0f  // bottom right
    };

    int mProgram;
    float color[] = { 0.13671875f, 0.66953125f, 0.72265625f, 1.0f };

    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +"uniform mat4 uMVPMatrix;"+
            "void main() {" +
            "  gl_Position =vPosition*uMVPMatrix;" +
            "}";

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

    public Triangle(){
        ByteBuffer bb=ByteBuffer.allocateDirect(
                triangleCoords.length*4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer=bb.asFloatBuffer();
        vertexBuffer.put(triangleCoords);
        vertexBuffer.position(0);

        int vertexShader=loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader=loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram=GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);

    }

    public int loadShader(int type, String shaderCode){
        int shader=GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }

    static final int vertexStride = COORDS_PER_VERTEX * 3;
    static final int vertexCount = 3;


    public void draw(float[]mvpMatrix){
        GLES20.glUseProgram(mProgram);
        int mPositionHandle= GLES20.glGetAttribLocation(mProgram, "vPosition");
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                     GLES20.GL_FLOAT, false,
                                      vertexStride, vertexBuffer);
        int mColorHandle=GLES20.glGetUniformLocation(mProgram,"vColor");
        GLES20.glUniform4fv(mColorHandle, 1, color,0);
        int mMVPMatrixHandle=GLES20.glGetUniformLocation(mProgram,"uMVPMatrix");
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix,0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }
}

这里是Cube代码(虽然我没有在此代码中绘制我的立方体):

    public class Cube {

int mProgram;
float color[] = { 0.13671875f, 0.66953125f, 0.72265625f, 1.0f };

private final String vertexShaderCode =
        "attribute vec4 vPosition;" +"uniform mat4 uMVPMatrix;"+
        "void main() {" +
        "  gl_Position =vPosition*uMVPMatrix;" +
        "}";

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

private float vertices[]={

        0,0,0,// top left front
        0,-1,0,//bottom left front
        1,-1,0,//bottom right front
        1,0,0,//top right front

        0,0,-1,//top left back
        0,-1,-1,//bottom left back
        1,-1,-1,//bottom right back
        1,0,-1//top right back


};

private FloatBuffer vertBuff;

private short pIndex[]={
        1,0,5, 1,5,2, 1,2,0, 
        3,0,2, 3,2,7, 3,7,0, 
         4,7,5, 4,5,0, 4,0,7,
        6,7,2, 6,2,5, 6,5,7 
};

private ShortBuffer pBuff;

public Cube(){
    ByteBuffer bBuff= ByteBuffer.allocateDirect(vertices.length*4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff=bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);

    ByteBuffer pbBuff=ByteBuffer.allocateDirect(pIndex.length*2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff=pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);

    int vertexShader=loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader=loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram=GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);
}

public int loadShader(int type, String shaderCode){
    int shader=GLES20.glCreateShader(type);
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    return shader;
}

static final int vertexStride = COORDS_PER_VERTEX * 8;
static final int vertexCount = 8;

public void draw(float[]mvpMatrix){
    GLES20.glUseProgram(mProgram);
    GLES20.glFrontFace(GLES20.GL_CCW);
    int mPositionHandle= GLES20.glGetAttribLocation(mProgram, "vPosition");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                 GLES20.GL_FLOAT, false,
                                  vertexStride, vertBuff);
    int mColorHandle=GLES20.glGetUniformLocation(mProgram,"vColor");
    GLES20.glUniform4fv(mColorHandle, 1, color,0);
    int mMVPMatrixHandle=GLES20.glGetUniformLocation(mProgram,"uMVPMatrix");
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix,0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}
    }        

0 个答案:

没有答案